Added tests
Change-Id: I493675212f4b1548b32a6d92ce3664d184bc0e04
diff --git a/src/app/datasources/rest/auth.rest.spec.ts b/src/app/datasources/rest/auth.rest.spec.ts
new file mode 100644
index 0000000..85aefb4
--- /dev/null
+++ b/src/app/datasources/rest/auth.rest.spec.ts
@@ -0,0 +1,84 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import 'angular-resource';
+import 'angular-cookies';
+import {xosDataSources} from '../index';
+import {AppConfig} from '../../config/app.config';
+import {IXosAuthService} from './auth.rest';
+
+let service: IXosAuthService;
+let httpBackend: ng.IHttpBackendService;
+let $scope;
+let $cookies;
+
+describe('The AuthService service', () => {
+
+ beforeEach(angular.mock.module(xosDataSources));
+
+ beforeEach(() => {
+ angular.mock.module(xosDataSources);
+ });
+
+
+ beforeEach(angular.mock.inject((
+ AuthService: IXosAuthService,
+ $httpBackend: ng.IHttpBackendService,
+ _$rootScope_: ng.IRootScopeService,
+ _$cookies_: ng.cookies.ICookiesService
+ ) => {
+ service = AuthService;
+ httpBackend = $httpBackend;
+ $scope = _$rootScope_;
+ $cookies = _$cookies_;
+ }));
+
+ describe('when logging in', () => {
+ beforeEach(() => {
+ httpBackend.expectPOST(`${AppConfig.apiEndpoint}/utility/login/`)
+ .respond({
+ user: JSON.stringify({usernane: 'test@xos.org'}),
+ xoscsrftoken: 'token',
+ xossessionid: 'session'
+ });
+ });
+ it('should store user auth in cookies', (done) => {
+ service.login({username: 'test', password: 'xos'})
+ .then((res) => {
+ expect($cookies.get('xoscsrftoken')).toEqual('token');
+ expect($cookies.get('xossessionid')).toEqual('session');
+ expect($cookies.get('xosuser')).toEqual(JSON.stringify({usernane: 'test@xos.org'}));
+ done();
+ })
+ .catch(e => {
+ done(e);
+ });
+ $scope.$apply();
+ httpBackend.flush();
+ });
+ });
+
+ describe('when logging out', () => {
+ beforeEach(() => {
+ httpBackend.expectPOST(`${AppConfig.apiEndpoint}/utility/logout/`)
+ .respond({
+ user: JSON.stringify({usernane: 'test@xos.org'}),
+ xoscsrftoken: 'token',
+ xossessionid: 'session'
+ });
+ });
+ it('should remove user auth from cookies', (done) => {
+ service.logout()
+ .then((res) => {
+ expect($cookies.get('xoscsrftoken')).toEqual(null);
+ expect($cookies.get('xossessionid')).toEqual(null);
+ expect($cookies.get('xosuser')).toEqual(null);
+ done();
+ })
+ .catch(e => {
+ done(e);
+ });
+ $scope.$apply();
+ httpBackend.flush();
+ });
+ });
+});