diff --git a/ng2-components/ng2-alfresco-login/package.json b/ng2-components/ng2-alfresco-login/package.json index 4539ee4d8d..1f604180df 100644 --- a/ng2-components/ng2-alfresco-login/package.json +++ b/ng2-components/ng2-alfresco-login/package.json @@ -5,7 +5,8 @@ "build": "typings install && tsc", "tsc": "tsc", "tsc:w": "tsc -w", - "typings": "typings" + "typings": "typings", + "test": "live-server --open=tests-runner.html" }, "license": "ISC", "dependencies": { @@ -18,6 +19,8 @@ }, "devDependencies": { "typescript": "^1.8.10", - "typings":"^0.7.12" + "typings":"^0.7.12", + "jasmine-core": "2.4.1", + "live-server": "^0.9.2" } } diff --git a/ng2-components/ng2-alfresco-login/src/authentication.service.spec.d.ts b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.d.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/ng2-components/ng2-alfresco-login/src/authentication.service.spec.js b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.js new file mode 100644 index 0000000000..e3a0016143 --- /dev/null +++ b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.js @@ -0,0 +1,91 @@ +System.register(['angular2/testing', 'angular2/core', 'angular2/http', 'angular2/http/testing', './authentication.service'], function(exports_1, context_1) { + "use strict"; + var __moduleName = context_1 && context_1.id; + var testing_1, core_1, http_1, testing_2, authentication_service_1; + return { + setters:[ + function (testing_1_1) { + testing_1 = testing_1_1; + }, + function (core_1_1) { + core_1 = core_1_1; + }, + function (http_1_1) { + http_1 = http_1_1; + }, + function (testing_2_1) { + testing_2 = testing_2_1; + }, + function (authentication_service_1_1) { + authentication_service_1 = authentication_service_1_1; + }], + execute: function() { + testing_1.describe('Authentication', function () { + var injector, backend, mockBackend, httpService, service; + beforeEach(function () { + injector = core_1.Injector.resolveAndCreate([ + http_1.HTTP_PROVIDERS, + testing_2.MockBackend, + core_1.provide(http_1.XHRBackend, { useClass: testing_2.MockBackend }), + authentication_service_1.Authentication + ]); + var store = {}; + spyOn(localStorage, 'getItem').and.callFake(function (key) { + return store[key]; + }); + spyOn(localStorage, 'setItem').and.callFake(function (key, value) { + return store[key] = value + ''; + }); + spyOn(localStorage, 'clear').and.callFake(function () { + store = {}; + }); + spyOn(localStorage, 'removeItem').and.callFake(function (key) { + delete store[key]; + }); + spyOn(localStorage, 'key').and.callFake(function (i) { + var keys = Object.keys(store); + return keys[i] || null; + }); + mockBackend = injector.get(testing_2.MockBackend); + backend = injector.get(http_1.XHRBackend); + httpService = injector.get(http_1.Http); + service = injector.get(authentication_service_1.Authentication); + }); + testing_1.it('should return true and token if the user is logged in', function () { + service.saveJwt('fake-local-token'); + expect(service.isLoggedIn()).toBe(true); + expect(localStorage.getItem('token')).toBeDefined(); + expect(localStorage.getItem('token')).toEqual('fake-local-token'); + }); + testing_1.it('should return false and token undefined if the user is not logged in', function () { + expect(service.isLoggedIn()).toEqual(false); + expect(localStorage.getItem('token')).not.toBeDefined(); + }); + testing_1.it('should return true and token on sign in', function () { + backend.connections.subscribe(function (connection) { + connection.mockRespond(new http_1.Response(new http_1.ResponseOptions({ body: { data: { ticket: 'fake-post-token' } } }))); + }); + service.token = ''; + service.login('POST', 'fakeUser', 'fakePassword') + .subscribe(function () { + expect(service.isLoggedIn()).toBe(true); + expect(service.token).toEqual('fake-post-token'); + expect(localStorage.getItem('token')).toBeDefined(); + expect(localStorage.getItem('token')).toEqual('fake-post-token'); + }); + }); + testing_1.it('should return false and token undefined on log out', function () { + service.token = 'fake-token'; + localStorage.setItem('token', 'fake-token'); + service.logout() + .subscribe(function () { + expect(service.isLoggedIn()).toBe(false); + expect(service.token).not.toBeDefined(); + expect(localStorage.getItem('token')).not.toBeDefined(); + }); + }); + }); + } + } +}); +//# sourceMappingURL=authentication.service.spec.js.map \ No newline at end of file diff --git a/ng2-components/ng2-alfresco-login/src/authentication.service.spec.js.map b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.js.map new file mode 100644 index 0000000000..832aefde92 --- /dev/null +++ b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.js.map @@ -0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AC,EAAC,EAAC,CAAC,CAAC,CAAC,CAAC;oBAC7G,CAAC,CAAC,CAAC;oBACH,OAAO,CAAC,KAAK,GAAG,EAAE,CAAC;oBACnB,OAAO,CAAC,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,cAAc,CAAC;yBAC5C,SAAS,CAAE;wBACR,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;wBACxC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;wBACjD,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;wBACpD,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;oBACrE,CAAC,CACJ,CAAC;gBACN,CAAC,CAAC,CAAC;gBAEH,YAAE,CAAC,oDAAoD,EAAE;oBACrD,OAAO,CAAC,KAAK,GAAG,YAAY,CAAC;oBAC7B,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;oBAC5C,OAAO,CAAC,MAAM,EAAE;yBACX,SAAS,CAAE;wBACR,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;wBACzC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;wBACxC,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC;oBAC5D,CAAC,CACJ,CAAC;gBACN,CAAC,CAAC,CAAC;YAEP,CAAC,CAAC,CAAC"} \ No newline at end of file diff --git a/ng2-components/ng2-alfresco-login/src/authentication.service.spec.ts b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.ts new file mode 100644 index 0000000000..d851967821 --- /dev/null +++ b/ng2-components/ng2-alfresco-login/src/authentication.service.spec.ts @@ -0,0 +1,88 @@ +import {it, describe} from 'angular2/testing'; +import {provide, Injector} from 'angular2/core'; +import {Http, HTTP_PROVIDERS, XHRBackend, Response, ResponseOptions} from 'angular2/http'; +import {MockBackend} from 'angular2/http/testing'; +import {Authentication} from './authentication.service'; + +describe('Authentication', () => { + let injector, + backend, + mockBackend, + httpService, + service; + + beforeEach(() => { + injector = Injector.resolveAndCreate([ + HTTP_PROVIDERS, + MockBackend, + provide(XHRBackend, {useClass: MockBackend}), + Authentication + ]); + + let store = {}; + + spyOn(localStorage, 'getItem').and.callFake(function (key) { + return store[key]; + }); + spyOn(localStorage, 'setItem').and.callFake(function (key, value) { + return store[key] = value + ''; + }); + spyOn(localStorage, 'clear').and.callFake(function () { + store = {}; + }); + spyOn(localStorage, 'removeItem').and.callFake(function (key) { + delete store[key]; + }); + spyOn(localStorage, 'key').and.callFake(function (i) { + let keys = Object.keys(store); + return keys[i] || null; + }); + + mockBackend = injector.get(MockBackend); + backend = injector.get(XHRBackend); + httpService = injector.get(Http); + service = injector.get(Authentication); + }); + + it('should return true and token if the user is logged in', () => { + service.saveJwt('fake-local-token'); + expect(service.isLoggedIn()).toBe(true); + expect(localStorage.getItem('token')).toBeDefined(); + expect(localStorage.getItem('token')).toEqual('fake-local-token'); + }); + + it('should return false and token undefined if the user is not logged in', () => { + expect(service.isLoggedIn()).toEqual(false); + expect(localStorage.getItem('token')).not.toBeDefined(); + + }); + + it('should return true and token on sign in', () => { + backend.connections.subscribe(connection => { + connection.mockRespond(new Response(new ResponseOptions({body: { data: { ticket: 'fake-post-token'}}}))); + }); + service.token = ''; + service.login('POST', 'fakeUser', 'fakePassword') + .subscribe( () => { + expect(service.isLoggedIn()).toBe(true); + expect(service.token).toEqual('fake-post-token'); + expect(localStorage.getItem('token')).toBeDefined(); + expect(localStorage.getItem('token')).toEqual('fake-post-token'); + } + ); + }); + + it('should return false and token undefined on log out', () => { + service.token = 'fake-token'; + localStorage.setItem('token', 'fake-token'); + service.logout() + .subscribe( () => { + expect(service.isLoggedIn()).toBe(false); + expect(service.token).not.toBeDefined(); + expect(localStorage.getItem('token')).not.toBeDefined(); + } + ); + }); + +}); + diff --git a/ng2-components/ng2-alfresco-login/tests-runner.html b/ng2-components/ng2-alfresco-login/tests-runner.html new file mode 100644 index 0000000000..18aa6b965b --- /dev/null +++ b/ng2-components/ng2-alfresco-login/tests-runner.html @@ -0,0 +1,51 @@ + + +
+ +