blob: 7a406dbcb1a1b428ae182c33f7157b8acb2f4d53 [file] [log] [blame]
Matteo Scandoloa4a47112016-12-16 10:06:13 -08001import * as angular from 'angular';
2import 'angular-mocks';
3import 'angular-resource';
4import 'angular-cookies';
5import {xosDataSources} from '../index';
Matteo Scandoloa4a47112016-12-16 10:06:13 -08006import {IXosAuthService} from './auth.rest';
7
8let service: IXosAuthService;
9let httpBackend: ng.IHttpBackendService;
10let $scope;
11let $cookies;
12
Matteo Scandolo828d1e82017-01-17 14:49:38 -080013const MockAppCfg = {
14 apiEndpoint: 'http://xos-test:3000/api',
15 websocketClient: 'http://xos-test:3000'
16};
17
Matteo Scandoloa4a47112016-12-16 10:06:13 -080018describe('The AuthService service', () => {
19
20 beforeEach(angular.mock.module(xosDataSources));
21
22 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080023
24 angular.module(xosDataSources)
25 .constant('AppConfig', MockAppCfg);
26
Matteo Scandoloa4a47112016-12-16 10:06:13 -080027 angular.mock.module(xosDataSources);
28 });
29
30
31 beforeEach(angular.mock.inject((
32 AuthService: IXosAuthService,
33 $httpBackend: ng.IHttpBackendService,
34 _$rootScope_: ng.IRootScopeService,
35 _$cookies_: ng.cookies.ICookiesService
36 ) => {
37 service = AuthService;
38 httpBackend = $httpBackend;
39 $scope = _$rootScope_;
40 $cookies = _$cookies_;
41 }));
42
43 describe('when logging in', () => {
44 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080045 httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/login/`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080046 .respond({
47 user: JSON.stringify({usernane: 'test@xos.org'}),
48 xoscsrftoken: 'token',
49 xossessionid: 'session'
50 });
51 });
52 it('should store user auth in cookies', (done) => {
53 service.login({username: 'test', password: 'xos'})
54 .then((res) => {
55 expect($cookies.get('xoscsrftoken')).toEqual('token');
56 expect($cookies.get('xossessionid')).toEqual('session');
57 expect($cookies.get('xosuser')).toEqual(JSON.stringify({usernane: 'test@xos.org'}));
58 done();
59 })
60 .catch(e => {
61 done(e);
62 });
63 $scope.$apply();
Matteo Scandolo710dc152017-04-11 13:54:23 -070064 // httpBackend.flush();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080065 });
66 });
67
68 describe('when logging out', () => {
69 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080070 httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/logout/`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080071 .respond({
72 user: JSON.stringify({usernane: 'test@xos.org'}),
73 xoscsrftoken: 'token',
74 xossessionid: 'session'
75 });
76 });
77 it('should remove user auth from cookies', (done) => {
78 service.logout()
79 .then((res) => {
Matteo Scandolo710dc152017-04-11 13:54:23 -070080 expect($cookies.get('sessionid')).toEqual(undefined);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080081 done();
82 })
83 .catch(e => {
84 done(e);
85 });
86 $scope.$apply();
Matteo Scandolo710dc152017-04-11 13:54:23 -070087 // httpBackend.flush();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080088 });
89 });
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070090
91 describe('the handleUnauthenticatedRequest method', () => {
92
93 beforeEach(() => {
94 spyOn(service, 'clearUser');
95 });
96
97 it('should logout the user and redirect to login', () => {
98 service.handleUnauthenticatedRequest({
99 error: 'XOSPermissionDenied',
100 fields: {},
101 specific_error: 'test'
102 });
103 expect(service.clearUser).toHaveBeenCalled();
104 });
105
106 it('should catch errors from strings', () => {
107 service.handleUnauthenticatedRequest('{"fields": {}, "specific_error": "failed to authenticate token g09et150o2s25kdzg8t2n9wotvds9jyl", "error": "XOSPermissionDenied"}');
108 expect(service.clearUser).toHaveBeenCalled();
109 });
110
111 it('should not catch other errors', () => {
112 service.handleUnauthenticatedRequest({
113 error: 'XOSProgrammingError',
114 fields: {},
115 specific_error: 'test'
116 });
117 expect(service.clearUser).not.toHaveBeenCalled();
118
119 service.handleUnauthenticatedRequest('some error');
120 expect(service.clearUser).not.toHaveBeenCalled();
121 });
122 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800123});