blob: 85aefb43ca6718d9de9c6dfd262530ab5ec137aa [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) => {
72 expect($cookies.get('xoscsrftoken')).toEqual(null);
73 expect($cookies.get('xossessionid')).toEqual(null);
74 expect($cookies.get('xosuser')).toEqual(null);
75 done();
76 })
77 .catch(e => {
78 done(e);
79 });
80 $scope.$apply();
81 httpBackend.flush();
82 });
83 });
84});