2019-08-09 09:38:02 +02:00
|
|
|
import Session from 'common/session';
|
2019-08-09 10:12:48 +02:00
|
|
|
import User from 'model/user';
|
2019-08-12 10:26:26 +02:00
|
|
|
import MockAdapter from "axios-mock-adapter";
|
|
|
|
import Api from "common/api";
|
|
|
|
|
2019-08-13 08:10:51 +02:00
|
|
|
let chai = require('../../../node_modules/chai/chai');
|
|
|
|
let assert = chai.assert;
|
|
|
|
|
2019-08-12 13:00:41 +02:00
|
|
|
describe('common/session', () => {
|
2019-08-12 10:26:26 +02:00
|
|
|
|
2019-08-12 13:00:41 +02:00
|
|
|
const mock = new MockAdapter(Api);
|
2019-08-09 09:38:02 +02:00
|
|
|
|
2019-08-12 10:26:26 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
window.onbeforeunload = () => 'Oh no!';
|
|
|
|
});
|
|
|
|
|
2019-08-09 09:38:02 +02:00
|
|
|
it('should construct session', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
|
|
|
assert.equal(session.session_token, null);
|
|
|
|
});
|
|
|
|
|
2019-08-09 10:12:48 +02:00
|
|
|
it('should set, get and delete token', () => {
|
2019-08-09 09:38:02 +02:00
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
|
|
|
assert.equal(session.session_token, null);
|
|
|
|
session.setToken(123421);
|
|
|
|
assert.equal(session.session_token, 123421);
|
|
|
|
const result = session.getToken();
|
|
|
|
assert.equal(result, 123421);
|
2019-08-09 10:12:48 +02:00
|
|
|
session.deleteToken();
|
|
|
|
assert.equal(session.session_token, null);
|
2019-08-09 09:38:02 +02:00
|
|
|
});
|
|
|
|
|
2019-08-09 10:12:48 +02:00
|
|
|
it('should set, get and delete user', () => {
|
2019-08-09 09:38:02 +02:00
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-08-09 10:12:48 +02:00
|
|
|
assert.equal(session.user.ID, undefined);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
2019-11-08 06:53:40 +01:00
|
|
|
assert.equal(session.user.FirstName, "Max");
|
|
|
|
assert.equal(session.user.Role, "admin");
|
2019-08-09 10:12:48 +02:00
|
|
|
const result = session.getUser();
|
|
|
|
assert.equal(result.ID, 5);
|
2019-11-08 06:53:40 +01:00
|
|
|
assert.equal(result.Email, "test@test.com");
|
2019-08-09 10:12:48 +02:00
|
|
|
session.deleteUser();
|
|
|
|
assert.equal(session.user, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get user email', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
|
|
|
const result = session.getEmail();
|
|
|
|
assert.equal(result, "test@test.com");
|
2019-11-08 06:53:40 +01:00
|
|
|
const values2 = { FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user2 = new User(values2);
|
|
|
|
session.setUser(user2);
|
|
|
|
const result2 = session.getEmail();
|
|
|
|
assert.equal(result2, "");
|
|
|
|
session.deleteUser();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get user firstname', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
|
|
|
const result = session.getFirstName();
|
|
|
|
assert.equal(result, "Max");
|
2019-11-08 06:53:40 +01:00
|
|
|
const values2 = { FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user2 = new User(values2);
|
|
|
|
session.setUser(user2);
|
|
|
|
const result2 = session.getFirstName();
|
|
|
|
assert.equal(result2, "");
|
|
|
|
session.deleteUser();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get user full name', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
|
|
|
const result = session.getFullName();
|
|
|
|
assert.equal(result, "Max Last");
|
2019-11-08 06:53:40 +01:00
|
|
|
const values2 = { FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user2 = new User(values2);
|
|
|
|
session.setUser(user2);
|
|
|
|
const result2 = session.getFullName();
|
|
|
|
assert.equal(result2, "");
|
|
|
|
session.deleteUser();
|
2019-08-09 09:38:02 +02:00
|
|
|
});
|
2019-08-09 10:12:48 +02:00
|
|
|
|
|
|
|
it('should test whether user is set', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
|
|
|
const result = session.isUser();
|
|
|
|
assert.equal(result, true);
|
|
|
|
session.deleteUser();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should test whether user is admin', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
|
|
|
const result = session.isAdmin();
|
|
|
|
assert.equal(result, true);
|
|
|
|
session.deleteUser();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should test whether user is anonymous', () => {
|
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
2019-11-08 06:53:40 +01:00
|
|
|
const values = {ID: 5, FirstName: "Max", LastName: "Last", Email: "test@test.com", Role: "admin"};
|
2019-08-09 10:12:48 +02:00
|
|
|
const user = new User(values);
|
|
|
|
session.setUser(user);
|
|
|
|
const result = session.isAnonymous();
|
|
|
|
assert.equal(result, false);
|
|
|
|
session.deleteUser();
|
|
|
|
});
|
|
|
|
|
2019-08-12 10:26:26 +02:00
|
|
|
it('should test login and logout', async() => {
|
2019-08-12 13:00:41 +02:00
|
|
|
mock
|
2019-11-08 06:53:40 +01:00
|
|
|
.onPost("session").reply(200, {token: "8877", user: {ID: 1, Email: "test@test.com"}})
|
2019-08-12 13:00:41 +02:00
|
|
|
.onDelete("session/8877").reply(200);
|
2019-08-12 10:26:26 +02:00
|
|
|
const storage = window.localStorage;
|
|
|
|
const session = new Session(storage);
|
|
|
|
assert.equal(session.session_token, null);
|
|
|
|
assert.equal(session.storage.user, undefined);
|
|
|
|
await session.login("test@test.com", "passwd");
|
|
|
|
assert.equal(session.session_token, 8877);
|
2019-11-08 06:53:40 +01:00
|
|
|
assert.equal(session.storage.user, '{"ID":1,"Email":"test@test.com"}');
|
2019-08-12 10:26:26 +02:00
|
|
|
await session.logout();
|
|
|
|
assert.equal(session.session_token, null);
|
2019-08-12 13:00:41 +02:00
|
|
|
mock.reset();
|
2019-08-12 10:26:26 +02:00
|
|
|
});
|
2019-08-09 10:12:48 +02:00
|
|
|
|
2019-11-08 06:53:40 +01:00
|
|
|
});
|