blob: 483bcfc80689d2909f3d106b83a78b6ebb75773a [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();
64 httpBackend.flush();
65 });
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 Scandoloa8a6fbb2016-12-21 16:59:08 -080080 expect($cookies.get('xoscsrftoken')).toEqual(undefined);
81 expect($cookies.get('xossessionid')).toEqual(undefined);
82 expect($cookies.get('xosuser')).toEqual(undefined);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080083 done();
84 })
85 .catch(e => {
86 done(e);
87 });
88 $scope.$apply();
89 httpBackend.flush();
90 });
91 });
92});