Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 19 | import * as angular from 'angular'; |
| 20 | import 'angular-mocks'; |
| 21 | import 'angular-resource'; |
| 22 | import 'angular-cookies'; |
| 23 | import {xosDataSources} from '../index'; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 24 | import {IXosAuthService} from './auth.rest'; |
| 25 | |
| 26 | let service: IXosAuthService; |
| 27 | let httpBackend: ng.IHttpBackendService; |
| 28 | let $scope; |
| 29 | let $cookies; |
| 30 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 31 | const MockAppCfg = { |
| 32 | apiEndpoint: 'http://xos-test:3000/api', |
| 33 | websocketClient: 'http://xos-test:3000' |
| 34 | }; |
| 35 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 36 | describe('The AuthService service', () => { |
| 37 | |
| 38 | beforeEach(angular.mock.module(xosDataSources)); |
| 39 | |
| 40 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 41 | |
| 42 | angular.module(xosDataSources) |
| 43 | .constant('AppConfig', MockAppCfg); |
| 44 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 45 | angular.mock.module(xosDataSources); |
| 46 | }); |
| 47 | |
| 48 | |
| 49 | beforeEach(angular.mock.inject(( |
| 50 | AuthService: IXosAuthService, |
| 51 | $httpBackend: ng.IHttpBackendService, |
| 52 | _$rootScope_: ng.IRootScopeService, |
| 53 | _$cookies_: ng.cookies.ICookiesService |
| 54 | ) => { |
| 55 | service = AuthService; |
| 56 | httpBackend = $httpBackend; |
| 57 | $scope = _$rootScope_; |
| 58 | $cookies = _$cookies_; |
| 59 | })); |
| 60 | |
| 61 | describe('when logging in', () => { |
| 62 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 63 | httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/login/`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 64 | .respond({ |
| 65 | user: JSON.stringify({usernane: 'test@xos.org'}), |
| 66 | xoscsrftoken: 'token', |
| 67 | xossessionid: 'session' |
| 68 | }); |
| 69 | }); |
| 70 | it('should store user auth in cookies', (done) => { |
| 71 | service.login({username: 'test', password: 'xos'}) |
| 72 | .then((res) => { |
| 73 | expect($cookies.get('xoscsrftoken')).toEqual('token'); |
| 74 | expect($cookies.get('xossessionid')).toEqual('session'); |
| 75 | expect($cookies.get('xosuser')).toEqual(JSON.stringify({usernane: 'test@xos.org'})); |
| 76 | done(); |
| 77 | }) |
| 78 | .catch(e => { |
| 79 | done(e); |
| 80 | }); |
| 81 | $scope.$apply(); |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 82 | // httpBackend.flush(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 83 | }); |
| 84 | }); |
| 85 | |
| 86 | describe('when logging out', () => { |
| 87 | beforeEach(() => { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 88 | httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/logout/`) |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 89 | .respond({ |
| 90 | user: JSON.stringify({usernane: 'test@xos.org'}), |
| 91 | xoscsrftoken: 'token', |
| 92 | xossessionid: 'session' |
| 93 | }); |
| 94 | }); |
| 95 | it('should remove user auth from cookies', (done) => { |
| 96 | service.logout() |
| 97 | .then((res) => { |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 98 | expect($cookies.get('sessionid')).toEqual(undefined); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 99 | done(); |
| 100 | }) |
| 101 | .catch(e => { |
| 102 | done(e); |
| 103 | }); |
| 104 | $scope.$apply(); |
Matteo Scandolo | 710dc15 | 2017-04-11 13:54:23 -0700 | [diff] [blame] | 105 | // httpBackend.flush(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 106 | }); |
| 107 | }); |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 108 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 109 | describe('the isAuthError method', () => { |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 110 | |
| 111 | beforeEach(() => { |
| 112 | spyOn(service, 'clearUser'); |
| 113 | }); |
| 114 | |
| 115 | it('should logout the user and redirect to login', () => { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 116 | let res = service.isAuthError({ |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 117 | error: 'XOSPermissionDenied', |
| 118 | fields: {}, |
| 119 | specific_error: 'test' |
| 120 | }); |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 121 | expect(res).toBeTruthy(); |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 122 | }); |
| 123 | |
| 124 | it('should catch errors from strings', () => { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 125 | let res = service.isAuthError('{"fields": {}, "specific_error": "failed to authenticate token g09et150o2s25kdzg8t2n9wotvds9jyl", "error": "XOSPermissionDenied"}'); |
| 126 | expect(res).toBeTruthy(); |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 127 | }); |
| 128 | |
| 129 | it('should not catch other errors', () => { |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 130 | let res = service.isAuthError({ |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 131 | error: 'XOSProgrammingError', |
| 132 | fields: {}, |
| 133 | specific_error: 'test' |
| 134 | }); |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 135 | expect(res).toBeFalsy(); |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 136 | |
Matteo Scandolo | 29edc0f | 2018-04-26 17:19:10 +0200 | [diff] [blame] | 137 | res = service.isAuthError('some error'); |
| 138 | expect(res).toBeFalsy(); |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 139 | }); |
| 140 | }); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 141 | }); |