blob: 044cf882ce320a61dfd82a2e92615d5768c30f67 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandoloa4a47112016-12-16 10:06:13 -080019import * as angular from 'angular';
20import 'angular-mocks';
21import 'angular-resource';
22import 'angular-cookies';
23import {xosDataSources} from '../index';
Matteo Scandoloa4a47112016-12-16 10:06:13 -080024import {IXosAuthService} from './auth.rest';
25
26let service: IXosAuthService;
27let httpBackend: ng.IHttpBackendService;
28let $scope;
29let $cookies;
30
Matteo Scandolo828d1e82017-01-17 14:49:38 -080031const MockAppCfg = {
32 apiEndpoint: 'http://xos-test:3000/api',
33 websocketClient: 'http://xos-test:3000'
34};
35
Matteo Scandoloa4a47112016-12-16 10:06:13 -080036describe('The AuthService service', () => {
37
38 beforeEach(angular.mock.module(xosDataSources));
39
40 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080041
42 angular.module(xosDataSources)
43 .constant('AppConfig', MockAppCfg);
44
Matteo Scandoloa4a47112016-12-16 10:06:13 -080045 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 Scandolo828d1e82017-01-17 14:49:38 -080063 httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/login/`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080064 .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 Scandolo710dc152017-04-11 13:54:23 -070082 // httpBackend.flush();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080083 });
84 });
85
86 describe('when logging out', () => {
87 beforeEach(() => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080088 httpBackend.expectPOST(`${MockAppCfg.apiEndpoint}/utility/logout/`)
Matteo Scandoloa4a47112016-12-16 10:06:13 -080089 .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 Scandolo710dc152017-04-11 13:54:23 -070098 expect($cookies.get('sessionid')).toEqual(undefined);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080099 done();
100 })
101 .catch(e => {
102 done(e);
103 });
104 $scope.$apply();
Matteo Scandolo710dc152017-04-11 13:54:23 -0700105 // httpBackend.flush();
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800106 });
107 });
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700108
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200109 describe('the isAuthError method', () => {
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700110
111 beforeEach(() => {
112 spyOn(service, 'clearUser');
113 });
114
115 it('should logout the user and redirect to login', () => {
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200116 let res = service.isAuthError({
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700117 error: 'XOSPermissionDenied',
118 fields: {},
119 specific_error: 'test'
120 });
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200121 expect(res).toBeTruthy();
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700122 });
123
124 it('should catch errors from strings', () => {
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200125 let res = service.isAuthError('{"fields": {}, "specific_error": "failed to authenticate token g09et150o2s25kdzg8t2n9wotvds9jyl", "error": "XOSPermissionDenied"}');
126 expect(res).toBeTruthy();
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700127 });
128
129 it('should not catch other errors', () => {
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200130 let res = service.isAuthError({
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700131 error: 'XOSProgrammingError',
132 fields: {},
133 specific_error: 'test'
134 });
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200135 expect(res).toBeFalsy();
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700136
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200137 res = service.isAuthError('some error');
138 expect(res).toBeFalsy();
Matteo Scandolo0f3692e2017-07-10 14:06:41 -0700139 });
140 });
Matteo Scandoloa4a47112016-12-16 10:06:13 -0800141});