blob: 16c0f8dbfbd890cbff339b4cbbdfd2b52cadaeb1 [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';
6import {AppConfig} from '../../config/app.config';
7import {IXosAuthService} from './auth.rest';
8
9let service: IXosAuthService;
10let httpBackend: ng.IHttpBackendService;
11let $scope;
12let $cookies;
13
14describe('The AuthService service', () => {
15
16 beforeEach(angular.mock.module(xosDataSources));
17
18 beforeEach(() => {
19 angular.mock.module(xosDataSources);
20 });
21
22
23 beforeEach(angular.mock.inject((
24 AuthService: IXosAuthService,
25 $httpBackend: ng.IHttpBackendService,
26 _$rootScope_: ng.IRootScopeService,
27 _$cookies_: ng.cookies.ICookiesService
28 ) => {
29 service = AuthService;
30 httpBackend = $httpBackend;
31 $scope = _$rootScope_;
32 $cookies = _$cookies_;
33 }));
34
35 describe('when logging in', () => {
36 beforeEach(() => {
37 httpBackend.expectPOST(`${AppConfig.apiEndpoint}/utility/login/`)
38 .respond({
39 user: JSON.stringify({usernane: 'test@xos.org'}),
40 xoscsrftoken: 'token',
41 xossessionid: 'session'
42 });
43 });
44 it('should store user auth in cookies', (done) => {
45 service.login({username: 'test', password: 'xos'})
46 .then((res) => {
47 expect($cookies.get('xoscsrftoken')).toEqual('token');
48 expect($cookies.get('xossessionid')).toEqual('session');
49 expect($cookies.get('xosuser')).toEqual(JSON.stringify({usernane: 'test@xos.org'}));
50 done();
51 })
52 .catch(e => {
53 done(e);
54 });
55 $scope.$apply();
56 httpBackend.flush();
57 });
58 });
59
60 describe('when logging out', () => {
61 beforeEach(() => {
62 httpBackend.expectPOST(`${AppConfig.apiEndpoint}/utility/logout/`)
63 .respond({
64 user: JSON.stringify({usernane: 'test@xos.org'}),
65 xoscsrftoken: 'token',
66 xossessionid: 'session'
67 });
68 });
69 it('should remove user auth from cookies', (done) => {
70 service.logout()
71 .then((res) => {
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080072 expect($cookies.get('xoscsrftoken')).toEqual(undefined);
73 expect($cookies.get('xossessionid')).toEqual(undefined);
74 expect($cookies.get('xosuser')).toEqual(undefined);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080075 done();
76 })
77 .catch(e => {
78 done(e);
79 });
80 $scope.$apply();
81 httpBackend.flush();
82 });
83 });
84});