Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 1 | import * as angular from 'angular'; |
| 2 | import 'angular-mocks'; |
| 3 | import 'angular-resource'; |
| 4 | import 'angular-cookies'; |
| 5 | import {xosDataSources} from '../index'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 6 | import {IXosAuthService} from './auth.rest'; |
| 7 | |
| 8 | let service: IXosAuthService; |
| 9 | let httpBackend: ng.IHttpBackendService; |
| 10 | let $scope; |
| 11 | let $cookies; |
| 12 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 13 | const MockAppCfg = { |
| 14 | apiEndpoint: 'http://xos-test:3000/api', |
| 15 | websocketClient: 'http://xos-test:3000' |
| 16 | }; |
| 17 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 18 | describe('The AuthService service', () => { |
| 19 | |
| 20 | beforeEach(angular.mock.module(xosDataSources)); |
| 21 | |
| 22 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 23 | |
| 24 | angular.module(xosDataSources) |
| 25 | .constant('AppConfig', MockAppCfg); |
| 26 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 27 | 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 Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 45 | httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/login/`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 46 | .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 Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 70 | httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/logout/`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 71 | .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 Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 80 | expect($cookies.get('xoscsrftoken')).toEqual(undefined); |
| 81 | expect($cookies.get('xossessionid')).toEqual(undefined); |
| 82 | expect($cookies.get('xosuser')).toEqual(undefined); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 83 | done(); |
| 84 | }) |
| 85 | .catch(e => { |
| 86 | done(e); |
| 87 | }); |
| 88 | $scope.$apply(); |
| 89 | httpBackend.flush(); |
| 90 | }); |
| 91 | }); |
| 92 | }); |