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(); |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 64 | // httpBackend.flush(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 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 | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 80 | expect($cookies.get('sessionid')).toEqual(undefined); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 81 | done(); |
| 82 | }) |
| 83 | .catch(e => { |
| 84 | done(e); |
| 85 | }); |
| 86 | $scope.$apply(); |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 87 | // httpBackend.flush(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 88 | }); |
| 89 | }); |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 90 | |
| 91 | describe('the handleUnauthenticatedRequest method', () => { |
| 92 | |
| 93 | beforeEach(() => { |
| 94 | spyOn(service, 'clearUser'); |
| 95 | }); |
| 96 | |
| 97 | it('should logout the user and redirect to login', () => { |
| 98 | service.handleUnauthenticatedRequest({ |
| 99 | error: 'XOSPermissionDenied', |
| 100 | fields: {}, |
| 101 | specific_error: 'test' |
| 102 | }); |
| 103 | expect(service.clearUser).toHaveBeenCalled(); |
| 104 | }); |
| 105 | |
| 106 | it('should catch errors from strings', () => { |
| 107 | service.handleUnauthenticatedRequest('{"fields": {}, "specific_error": "failed to authenticate token g09et150o2s25kdzg8t2n9wotvds9jyl", "error": "XOSPermissionDenied"}'); |
| 108 | expect(service.clearUser).toHaveBeenCalled(); |
| 109 | }); |
| 110 | |
| 111 | it('should not catch other errors', () => { |
| 112 | service.handleUnauthenticatedRequest({ |
| 113 | error: 'XOSProgrammingError', |
| 114 | fields: {}, |
| 115 | specific_error: 'test' |
| 116 | }); |
| 117 | expect(service.clearUser).not.toHaveBeenCalled(); |
| 118 | |
| 119 | service.handleUnauthenticatedRequest('some error'); |
| 120 | expect(service.clearUser).not.toHaveBeenCalled(); |
| 121 | }); |
| 122 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 123 | }); |