Commit fb4c453c authored by Sandro Lutz's avatar Sandro Lutz
Browse files

Add auth module test

parent ab678f56
......@@ -4,4 +4,5 @@ dist
.vscode
npm-debug.log
DOCUMENTATION.html
DOCUMENTATION.md
\ No newline at end of file
DOCUMENTATION.md
.reify-cache
......@@ -2,6 +2,7 @@ image: node:latest
stages:
- lint
- test
- build
before_script:
......@@ -13,6 +14,12 @@ eslint:
script:
- npm run lint
test:
stage: test
when: always
script:
- npm run test
build:
stage: build
when: always
......
......@@ -4,7 +4,7 @@
"description": "AMIV website",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "ospec",
"docs": "doxdox 'src/**/*.js' --layout bootstrap --output DOCUMENTATION.html",
"docs-md": "doxdox 'src/**/*.js' --layout markdown --output DOCUMENTATION.md",
"start": "webpack -d --watch",
......@@ -52,6 +52,8 @@
"url-loader": "^1.0.1",
"markdown-loader": "^2.0.2",
"style-loader": "^0.20.3",
"string-replace-loader": "^2.1.1"
"string-replace-loader": "^2.1.1",
"reify": "^0.14.2",
"dom-storage": "^2.0.2"
}
}
......@@ -41,11 +41,11 @@ export function getToken() {
function reloadLocalStorage() {
if (localStorage.getItem('token') !== null) {
APISession.token = localStorage.token;
APISession.id = localStorage.id;
APISession.username = localStorage.username;
APISession.userId = localStorage.userId;
APISession.etag = localStorage.etag;
APISession.token = localStorage.getItem('token');
APISession.id = localStorage.getItem('id');
APISession.username = localStorage.getItem('username');
APISession.userId = localStorage.getItem('userId');
APISession.etag = localStorage.getItem('etag');
APISession.lastChecked = 0;
APISession.authenticated = true;
}
......@@ -76,6 +76,7 @@ export function login(username, password) {
APISession.authenticated = true;
APISession.username = username;
APISession.userId = result.user;
log(APISession.username);
localStorage.setItem('token', result.token);
localStorage.setItem('username', username);
localStorage.setItem('userId', result.user);
......@@ -92,7 +93,6 @@ export function login(username, password) {
*/
export function logout() {
reloadLocalStorage();
APISession.authenticated = false;
return m
.request({
method: 'DELETE',
......@@ -164,5 +164,6 @@ export function checkLogin() {
* @return {Boolean} `true` if authentication data is available
*/
export function isLoggedIn() {
log(`UserId: ${APISession.userId}`);
return APISession.authenticated;
}
// Polyfill DOM env for mithril
global.window = require('mithril/test-utils/browserMock.js')();
global.document = window.document;
require('reify');
const o = require('mithril/ospec/ospec');
const Storage = require('dom-storage');
const auth = require('../../src/models/auth');
o.spec('auth', () => {
o.beforeEach(() => {
global.localStorage = new Storage(null, { strict: false });
});
o.spec('login', () => {
o('login-success', () => {
global.window.$defineRoutes({
'POST /sessions': () => ({
status: 201,
responseText: JSON.stringify({
_updated: '2018-03-17T11:08:25Z',
_created: '2018-03-17T11:08:25Z',
user: '59e5f715edcr41000ae5ecd4',
token: 'iRg3Y6F4CJBOR_uZydsswMjlflq0m34IK4GCSUjaU_rI',
_etag: '9556ba93c498f3et3467bc5e1d0a3f3bdf529e38',
_id: '5aacf7291a89ew00014c5cba',
_links: {
self: {
title: 'Session',
href: 'sessions/5aacf7291a8j6900014c5cba',
methods: ['GET', 'OPTIONS', 'HEAD'],
},
},
_status: 'OK',
}),
}),
});
return auth.login('validuser', 'validpassword').then(() => {
o(auth.isLoggedIn()).equals(true);
auth.logout().then(() => {
o(auth.isLoggedIn()).equals(false);
});
});
});
o('login-failure', () => {
global.window.$defineRoutes({
'POST /sessions': () => ({
status: 401,
responseText: JSON.stringify({
_status: 'ERR',
_error: {
code: 401,
message:
"The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.",
},
}),
}),
});
return auth.login('invaliduser', 'validpassword').catch(() => {
o(auth.isLoggedIn()).equals(false);
});
});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment