Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error with deprecated db name #1328

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions src/app/core/session/session-service/local-session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { TEST_PASSWORD, TEST_USER } from "../../../utils/mocked-testing.module";
import { PouchDatabase } from "../../database/pouch-database";

describe("LocalSessionService", () => {
let userDBName;
let deprecatedDBName;
let localSession: LocalSession;
let testUser: DatabaseUser;
let database: jasmine.SpyObj<PouchDatabase>;
Expand All @@ -38,6 +40,8 @@ describe("LocalSessionService", () => {
remote_url: "https://demo.aam-digital.com/db/",
},
};
userDBName = `${TEST_USER}-${AppConfig.settings.database.name}`;
deprecatedDBName = AppConfig.settings.database.name;
database = jasmine.createSpyObj([
"initInMemoryDB",
"initIndexedDB",
Expand All @@ -54,9 +58,12 @@ describe("LocalSessionService", () => {
localSession.saveUser(testUser, TEST_PASSWORD);
});

afterEach(() => {
afterEach(async () => {
localSession.removeUser(TEST_USER);
window.localStorage.removeItem(LocalSession.DEPRECATED_DB_KEY);
const tmpDB = new PouchDatabase(undefined);
await tmpDB.initInMemoryDB(userDBName).destroy();
await tmpDB.initInMemoryDB(deprecatedDBName).destroy();
});

it("should be created", () => {
Expand Down Expand Up @@ -145,60 +152,56 @@ describe("LocalSessionService", () => {
});

it("should use current user db if database has content", async () => {
defineExistingDatabases(true, false);
await defineExistingDatabases(true, false);

await localSession.login(TEST_USER, TEST_PASSWORD);

const dbName = database.initInMemoryDB.calls.mostRecent().args[0];
expect(dbName).toBe(`${TEST_USER}-${AppConfig.settings.database.name}`);
expect(database.initInMemoryDB).toHaveBeenCalledOnceWith(userDBName);
});

it("should use and reserve a deprecated db if it exists and current db has no content", async () => {
defineExistingDatabases(false, true);
await defineExistingDatabases(false, true);

await localSession.login(TEST_USER, TEST_PASSWORD);

const dbName = database.initInMemoryDB.calls.mostRecent().args[0];
expect(dbName).toBe(AppConfig.settings.database.name);
expect(database.initInMemoryDB).toHaveBeenCalledOnceWith(deprecatedDBName);
const dbReservation = window.localStorage.getItem(
LocalSession.DEPRECATED_DB_KEY
);
expect(dbReservation).toBe(TEST_USER);
});

it("should open a new database if deprecated db is already in use", async () => {
defineExistingDatabases(false, true, "other-user");
await defineExistingDatabases(false, true, "other-user");

await localSession.login(TEST_USER, TEST_PASSWORD);

const dbName = database.initInMemoryDB.calls.mostRecent().args[0];
expect(dbName).toBe(`${TEST_USER}-${AppConfig.settings.database.name}`);
expect(database.initInMemoryDB).toHaveBeenCalledOnceWith(userDBName);
});

it("should use the deprecated database if it is reserved by the current user", async () => {
defineExistingDatabases(false, true, TEST_USER);
await defineExistingDatabases(false, true, TEST_USER);

await localSession.login(TEST_USER, TEST_PASSWORD);

const dbName = database.initInMemoryDB.calls.mostRecent().args[0];
expect(dbName).toBe(AppConfig.settings.database.name);
expect(database.initInMemoryDB).toHaveBeenCalledOnceWith(deprecatedDBName);
});

function defineExistingDatabases(userDB, deprecatedDB, reserved?: string) {
async function defineExistingDatabases(
initUserDB: boolean,
initDeprecatedDB: boolean,
reserved?: string
) {
if (reserved) {
window.localStorage.setItem(LocalSession.DEPRECATED_DB_KEY, reserved);
}
database.isEmpty.and.callFake(() => {
const dbName = database.initInMemoryDB.calls.mostRecent().args[0];
if (dbName === AppConfig.settings.database.name) {
return Promise.resolve(!deprecatedDB);
}
if (dbName === `${TEST_USER}-${AppConfig.settings.database.name}`) {
return Promise.resolve(!userDB);
} else {
return Promise.reject("unexpected database name");
}
});
const tmpDB = new PouchDatabase(undefined);
if (initUserDB) {
await tmpDB.initInMemoryDB(userDBName).put({ _id: "someDoc" });
}
if (initDeprecatedDB) {
await tmpDB.initInMemoryDB(deprecatedDBName).put({ _id: "someDoc" });
}
}

testSessionServiceImplementation(() => Promise.resolve(localSession));
Expand Down
18 changes: 11 additions & 7 deletions src/app/core/session/session-service/local-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,39 @@ export class LocalSession extends SessionService {

private async initializeDatabaseForCurrentUser() {
const userDBName = `${this.currentDBUser.name}-${AppConfig.settings.database.name}`;
this.initDatabase(userDBName);
if (!(await this.database.isEmpty())) {
// Work on a temporary database before initializing the real one
const tmpDB = new PouchDatabase(undefined);
this.initDatabase(userDBName, tmpDB);
if (!(await tmpDB.isEmpty())) {
// Current user has own database, we are done here
this.initDatabase(userDBName);
return;
}

this.initDatabase(AppConfig.settings.database.name);
this.initDatabase(AppConfig.settings.database.name, tmpDB);
const dbFallback = window.localStorage.getItem(
LocalSession.DEPRECATED_DB_KEY
);
const dbAvailable = !dbFallback || dbFallback === this.currentDBUser.name;
if (dbAvailable && !(await this.database.isEmpty())) {
if (dbAvailable && !(await tmpDB.isEmpty())) {
// Old database is available and can be used by the current user
window.localStorage.setItem(
LocalSession.DEPRECATED_DB_KEY,
this.currentDBUser.name
);
this.initDatabase(AppConfig.settings.database.name);
return;
}

// Create a new database for the current user
this.initDatabase(userDBName);
}

private initDatabase(dbName: string) {
private initDatabase(dbName: string, db = this.database) {
if (AppConfig.settings.session_type === SessionType.mock) {
this.database.initInMemoryDB(dbName);
db.initInMemoryDB(dbName);
} else {
this.database.initIndexedDB(dbName);
db.initIndexedDB(dbName);
}
}

Expand Down