#17 Added Authentication Unit Test

This commit is contained in:
mauriziovitale84 2016-04-22 12:14:07 +01:00
parent 0d4f30d31f
commit ed7171ceb3
6 changed files with 236 additions and 2 deletions

View File

@ -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"
}
}

View File

@ -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

View File

@ -0,0 +1 @@
{"version":3,"file":"authentication.service.spec.js","sourceRoot":"","sources":["authentication.service.spec.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;YAMA,kBAAQ,CAAC,gBAAgB,EAAE;gBACvB,IAAI,QAAQ,EACR,OAAO,EACP,WAAW,EACX,WAAW,EACX,OAAO,CAAC;gBAEZ,UAAU,CAAC;oBACP,QAAQ,GAAG,eAAQ,CAAC,gBAAgB,CAAC;wBACjC,qBAAc;wBACd,qBAAW;wBACX,cAAO,CAAC,iBAAU,EAAE,EAAC,QAAQ,EAAE,qBAAW,EAAC,CAAC;wBAC5C,uCAAc;qBACjB,CAAC,CAAC;oBAEH,IAAI,KAAK,GAAG,EAAE,CAAC;oBAEf,KAAK,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC,GAAG,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AAC,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"}

View File

@ -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();
}
);
});
});

View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Alfresco Login component Tests</title>
<link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
<script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
<!-- #1. add the system.js library -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="node_modules/angular2/bundles/testing.dev.js"></script>
<script>
// #2. Configure systemjs to use the .js extension
// for imports from the app folder
System.config({
packages: {
'src': {defaultExtension: 'js'}
}
});
// #3. Import the spec file explicitly
System.import('src/authentication.service.spec')
// #4. wait for all imports to load ...
// then re-execute `window.onload` which
// triggers the Jasmine test-runner start
// or explain what went wrong.
.then(window.onload)
.catch(console.error.bind(console));
</script>
</head>
<body>
<!-- Unit Testing Chapter #1: Proof of life. -->
<script>
</script>
</body>
</html>