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();
+ });
+ });
+});
diff --git a/src/app/datasources/rest/auth.rest.ts b/src/app/datasources/rest/auth.rest.ts
index a94599a..f71d295 100644
--- a/src/app/datasources/rest/auth.rest.ts
+++ b/src/app/datasources/rest/auth.rest.ts
@@ -12,6 +12,11 @@
xossessionid: string;
};
}
+
+export interface IXosAuthService {
+ login(data: IAuthRequestData): Promise<any>;
+ logout(): Promise<any>;
+}
export class AuthService {
@@ -38,4 +43,22 @@
});
return d.promise;
}
+
+ public logout(): Promise<any> {
+ const d = this.$q.defer();
+ this.$http.post(`${AppConfig.apiEndpoint}/utility/login/`, {
+ xoscsrftoken: this.$cookies.get('xoscsrftoken'),
+ xossessionid: this.$cookies.get('xossessionid')
+ })
+ .then(() => {
+ this.$cookies.remove('xoscsrftoken');
+ this.$cookies.remove('xossessionid');
+ this.$cookies.remove('xosuser');
+ d.resolve();
+ })
+ .catch(e => {
+ d.reject(e);
+ });
+ return d.promise;
+ }
}
diff --git a/src/app/datasources/rest/core.rest.ts b/src/app/datasources/rest/core.rest.ts
deleted file mode 100644
index 2c10e10..0000000
--- a/src/app/datasources/rest/core.rest.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import {AppConfig} from '../../config/app.config';
-export class CoreRest {
-
- /** @ngInject */
- constructor(
- private $http: angular.IHttpService,
- private $q: angular.IQService
- ) {
- }
-
- public query(): Promise<any> {
- const d = this.$q.defer();
- this.$http.get(`${AppConfig.apiEndpoint}/core/`)
- .then(res => d.resolve(res.data))
- .catch(d.reject);
- return d.promise;
- }
-}
diff --git a/src/app/datasources/rest/model.rest.spec.ts b/src/app/datasources/rest/model.rest.spec.ts
new file mode 100644
index 0000000..f57c0c8
--- /dev/null
+++ b/src/app/datasources/rest/model.rest.spec.ts
@@ -0,0 +1,76 @@
+import * as angular from 'angular';
+import 'angular-mocks';
+import 'angular-resource';
+import 'angular-cookies';
+import {IXosResourceService} from './model.rest';
+import {xosDataSources} from '../index';
+import {AppConfig} from '../../config/app.config';
+
+let service: IXosResourceService;
+let resource: ng.resource.IResourceClass<any>;
+let httpBackend: ng.IHttpBackendService;
+let $resource;
+let $scope;
+
+describe('The ModelRest service', () => {
+
+ beforeEach(angular.mock.module(xosDataSources));
+
+ beforeEach(() => {
+ angular.mock.module(xosDataSources);
+ });
+
+
+ beforeEach(angular.mock.inject((
+ ModelRest: IXosResourceService,
+ $httpBackend: ng.IHttpBackendService,
+ _$resource_: ng.resource.IResourceService,
+ _$rootScope_: ng.IRootScopeService
+ ) => {
+ service = ModelRest;
+ httpBackend = $httpBackend;
+ $resource = _$resource_;
+ $scope = _$rootScope_;
+ }));
+
+ it('should return a resource based on the URL', () => {
+ resource = service.getResource('/core/test');
+ expect(resource.constructor).toEqual($resource.constructor);
+ });
+
+ it('should have a query method', (done) => {
+ httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/test`)
+ .respond([
+ {status: 'ok'}
+ ]);
+ resource = service.getResource('/core/test');
+ resource.query().$promise
+ .then((res) => {
+ expect(res[0].status).toEqual('ok');
+ done();
+ })
+ .catch(e => {
+ done(e);
+ });
+ $scope.$apply();
+ httpBackend.flush();
+ });
+
+ it('should have a get method', (done) => {
+ httpBackend.expectGET(`${AppConfig.apiEndpoint}/core/test/1`)
+ .respond([
+ {status: 'ok'}
+ ]);
+ resource = service.getResource('/core/test');
+ resource.get({id: 1}).$promise
+ .then((res) => {
+ expect(res[0].status).toEqual('ok');
+ done();
+ })
+ .catch(e => {
+ done(e);
+ });
+ $scope.$apply();
+ httpBackend.flush();
+ });
+});
diff --git a/src/app/datasources/rest/model.rest.ts b/src/app/datasources/rest/model.rest.ts
index 45431d4..908ca0f 100644
--- a/src/app/datasources/rest/model.rest.ts
+++ b/src/app/datasources/rest/model.rest.ts
@@ -16,6 +16,6 @@
}
public getResource(url: string): ng.resource.IResourceClass<ng.resource.IResource<any>> {
- return this.resource = this.$resource(`${AppConfig.apiEndpoint}${url}`);
+ return this.resource = this.$resource(`${AppConfig.apiEndpoint}${url}/:id`);
}
}
diff --git a/src/app/datasources/rest/modeldefs.rest.spec.ts b/src/app/datasources/rest/modeldefs.rest.spec.ts
new file mode 100644
index 0000000..6469cca
--- /dev/null
+++ b/src/app/datasources/rest/modeldefs.rest.spec.ts
@@ -0,0 +1,49 @@
+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 {IModeldefsService} from './modeldefs.rest';
+
+let service: IModeldefsService;
+let httpBackend: ng.IHttpBackendService;
+let $scope;
+
+describe('The ModelDefs service', () => {
+
+ beforeEach(angular.mock.module(xosDataSources));
+
+ beforeEach(() => {
+ angular.mock.module(xosDataSources);
+ });
+
+
+ beforeEach(angular.mock.inject((
+ ModelDefs: IModeldefsService,
+ $httpBackend: ng.IHttpBackendService,
+ _$resource_: ng.resource.IResourceService,
+ _$rootScope_: ng.IRootScopeService
+ ) => {
+ service = ModelDefs;
+ httpBackend = $httpBackend;
+ $scope = _$rootScope_;
+ }));
+
+ it('should have a get method', (done) => {
+ httpBackend.expectGET(`${AppConfig.apiEndpoint}/utility/modeldefs/`)
+ .respond([
+ {name: 'ok'}
+ ]);
+ service.get()
+ .then((res) => {
+ expect(res[0].name).toEqual('ok');
+ done();
+ })
+ .catch(e => {
+ done(e);
+ });
+ $scope.$apply();
+ httpBackend.flush();
+ });
+});