photoprism/frontend/tests/unit/common/session_test.js

226 lines
6.1 KiB
JavaScript
Raw Normal View History

import "../fixtures";
2021-01-09 04:41:33 +01:00
import { config } from "session";
import Session from "common/session";
import StorageShim from "node-storage-shim";
2020-07-02 10:03:00 +02:00
let chai = require("chai/chai");
2019-08-13 08:10:51 +02:00
let assert = chai.assert;
2021-01-09 04:41:33 +01:00
describe("common/session", () => {
beforeEach(() => {
window.onbeforeunload = () => "Oh no!";
});
2019-08-09 09:38:02 +02:00
2021-01-09 04:41:33 +01:00
it("should construct session", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
assert.equal(session.session_id, null);
});
2019-08-12 10:26:26 +02:00
2021-01-09 04:41:33 +01:00
it("should set, get and delete token", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
assert.equal(session.hasToken("2lbh9x09"), false);
assert.equal(session.session_id, null);
session.setId(123421);
assert.equal(session.session_id, 123421);
const result = session.getId();
assert.equal(result, 123421);
session.deleteId();
assert.equal(session.session_id, null);
});
2019-08-09 09:38:02 +02:00
2021-01-09 04:41:33 +01:00
it("should set, get and delete user", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
assert.isFalse(session.user.hasId());
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData();
assert.equal(session.user.FullName, "");
session.setData(values);
assert.equal(session.user.FullName, "Max Last");
assert.equal(session.user.RoleAdmin, true);
const result = session.getUser();
assert.equal(result.ID, 5);
assert.equal(result.PrimaryEmail, "test@test.com");
session.deleteData();
assert.isFalse(session.user.hasId());
});
2019-08-09 09:38:02 +02:00
2021-01-09 04:41:33 +01:00
it("should get user email", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values);
const result = session.getEmail();
assert.equal(result, "test@test.com");
const values2 = {
user: {
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values2);
const result2 = session.getEmail();
assert.equal(result2, "");
session.deleteData();
});
it("should get user nick name", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values);
const result = session.getNickName();
assert.equal(result, "Foo");
const values2 = {
user: {
NickName: "Bar",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values2);
const result2 = session.getNickName();
assert.equal(result2, "");
session.deleteData();
});
it("should get user full name", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values);
const result = session.getFullName();
assert.equal(result, "Max Last");
const values2 = {
user: {
NickName: "Bar",
FullName: "Max New",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values2);
const result2 = session.getFullName();
assert.equal(result2, "");
session.deleteData();
});
it("should test whether user is set", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values);
const result = session.isUser();
assert.equal(result, true);
session.deleteData();
});
it("should test whether user is admin", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values);
const result = session.isAdmin();
assert.equal(result, true);
session.deleteData();
});
2019-08-09 10:12:48 +02:00
2021-01-09 04:41:33 +01:00
it("should test whether user is anonymous", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
const values = {
user: {
ID: 5,
NickName: "Foo",
FullName: "Max Last",
PrimaryEmail: "test@test.com",
RoleAdmin: true,
},
};
session.setData(values);
const result = session.isAnonymous();
assert.equal(result, false);
session.deleteData();
});
it("should use session storage", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
assert.equal(storage.getItem("session_storage"), null);
session.useSessionStorage();
assert.equal(storage.getItem("session_storage"), "true");
session.deleteData();
});
2020-06-29 11:16:11 +02:00
2021-01-09 04:41:33 +01:00
it("should use local storage", () => {
const storage = new StorageShim();
const session = new Session(storage, config);
assert.equal(storage.getItem("session_storage"), null);
session.useLocalStorage();
assert.equal(storage.getItem("session_storage"), "false");
session.deleteData();
});
it("should test redeem token", async () => {
const storage = new StorageShim();
const session = new Session(storage, config);
assert.equal(session.data, null);
await session.redeemToken("token123");
assert.equal(session.data.token, "123token");
session.deleteData();
});
});