From 00cf7111fdd1d72f8ff06f3c3259cd0c92ece784 Mon Sep 17 00:00:00 2001
From: 01zulfi <85733202+01zulfi@users.noreply.github.com>
Date: Mon, 30 Dec 2024 10:32:31 +0500
Subject: [PATCH] core: fix vault unlock event publish && web: add vault status
tests Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
---
apps/web/__e2e__/vault.test.ts | 88 ++++++++++++++++++++
apps/web/src/components/status-bar/index.tsx | 1 +
packages/core/src/api/vault.ts | 2 +-
3 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 apps/web/__e2e__/vault.test.ts
diff --git a/apps/web/__e2e__/vault.test.ts b/apps/web/__e2e__/vault.test.ts
new file mode 100644
index 0000000000..c113b406d6
--- /dev/null
+++ b/apps/web/__e2e__/vault.test.ts
@@ -0,0 +1,88 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see .
+*/
+
+import { test, expect } from "@playwright/test";
+import { AppModel } from "./models/app.model";
+import { getTestId, NOTE, PASSWORD } from "./utils";
+
+test("locking a note should show vault unlocked status", async ({ page }) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote(NOTE);
+ const vaultUnlockedStatus = page.locator(getTestId("vault-unlocked"));
+
+ await note?.contextMenu.lock(PASSWORD);
+
+ await expect(vaultUnlockedStatus).toBeVisible();
+});
+
+test("clicking on vault unlocked status should lock the vault", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote(NOTE);
+ const vaultUnlockedStatus = page.locator(getTestId("vault-unlocked"));
+
+ await note?.contextMenu.lock(PASSWORD);
+ await note?.openLockedNote(PASSWORD);
+ await vaultUnlockedStatus.waitFor({ state: "visible" });
+ await vaultUnlockedStatus.click();
+
+ await expect(vaultUnlockedStatus).toBeHidden();
+ expect(await note?.contextMenu.isLocked()).toBe(true);
+});
+
+test("opening a locked note should show vault unlocked status", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote(NOTE);
+ const vaultUnlockedStatus = page.locator(getTestId("vault-unlocked"));
+
+ await note?.contextMenu.lock(PASSWORD);
+ await vaultUnlockedStatus.waitFor({ state: "visible" });
+ await vaultUnlockedStatus.click();
+ await vaultUnlockedStatus.waitFor({ state: "hidden" });
+ await note?.openLockedNote(PASSWORD);
+
+ await expect(vaultUnlockedStatus).toBeVisible();
+});
+
+test("unlocking a note permanently should not show vault unlocked status", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote(NOTE);
+ const vaultUnlockedStatus = page.locator(getTestId("vault-unlocked"));
+
+ await note?.contextMenu.lock(PASSWORD);
+ await vaultUnlockedStatus.waitFor({ state: "visible" });
+ await vaultUnlockedStatus.click();
+ await vaultUnlockedStatus.waitFor({ state: "hidden" });
+ await note?.contextMenu.unlock(PASSWORD);
+
+ await expect(vaultUnlockedStatus).toBeHidden();
+});
diff --git a/apps/web/src/components/status-bar/index.tsx b/apps/web/src/components/status-bar/index.tsx
index 7af2e18831..a487d4a945 100644
--- a/apps/web/src/components/status-bar/index.tsx
+++ b/apps/web/src/components/status-bar/index.tsx
@@ -174,6 +174,7 @@ function StatusBar() {
justifyContent: "center",
display: "flex"
}}
+ data-test-id="vault-unlocked"
>
diff --git a/packages/core/src/api/vault.ts b/packages/core/src/api/vault.ts
index cfb3b76bfe..7cb4c84dfe 100644
--- a/packages/core/src/api/vault.ts
+++ b/packages/core/src/api/vault.ts
@@ -48,6 +48,7 @@ export default class Vault {
}
private startEraser() {
+ EV.publish(EVENTS.vaultUnlocked);
clearTimeout(this.erasureTimeout);
this.erasureTimeout = setTimeout(() => {
this.lock();
@@ -95,7 +96,6 @@ export default class Vault {
throw new Error(VAULT_ERRORS.wrongPassword);
}
this.password = password;
- EV.publish(EVENTS.vaultUnlocked);
return true;
}