blob: 35732a3bb24dd2b853c0d4339aee8a15eba965df [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 });
90});