Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: add lock app button in status bar (#7333)

01zulfi4428ccd3a0d8
diff --git a/apps/web/__e2e__/app-lock.test.ts b/apps/web/__e2e__/app-lock.test.ts
new file mode 100644
index 000000000..d66cddc4e
--- /dev/null
+++ b/apps/web/__e2e__/app-lock.test.ts
@@ -0,0 +1,91 @@
+/*
+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 <http://www.gnu.org/licenses/>.
+*/
+
+import { test, expect } from "@playwright/test";
+import { AppModel } from "./models/app.model";
+import { APP_LOCK_PASSWORD, USER } from "./utils";
+
+test("don't show status bar lock app button to unauthenticated user", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+
+ expect(await app.lockAppButton()).toBeHidden();
+});
+
+test("don't show status bar lock app button to authenticated user", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.auth.goto();
+
+ await app.auth.login(USER.CURRENT);
+
+ expect(await app.lockAppButton()).toBeHidden();
+});
+
+test("show status bar lock app button to authenticated user if app lock is enabled", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.auth.goto();
+ await app.auth.login(USER.CURRENT);
+
+ const settings = await app.goToSettings();
+ await settings.enableAppLock(USER.CURRENT.password!, APP_LOCK_PASSWORD);
+ await settings.close();
+
+ expect(await app.lockAppButton()).toBeVisible();
+});
+
+test("clicking on status bar lock app button should lock app", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.auth.goto();
+ await app.auth.login(USER.CURRENT);
+
+ const settings = await app.goToSettings();
+ await settings.enableAppLock(USER.CURRENT.password!, APP_LOCK_PASSWORD);
+ await settings.close();
+ const lockAppButton = await app.lockAppButton();
+ await lockAppButton.waitFor({ state: "visible" });
+ await lockAppButton.click();
+
+ expect(page.getByText("Unlock your notes")).toBeVisible();
+});
+
+test("disabling app lock setting should remove status bar lock app button", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.auth.goto();
+ await app.auth.login(USER.CURRENT);
+
+ let settings = await app.goToSettings();
+ await settings.enableAppLock(USER.CURRENT.password!, APP_LOCK_PASSWORD);
+ await settings.close();
+ (await app.lockAppButton()).waitFor({ state: "visible" });
+ settings = await app.goToSettings();
+ await settings.disableAppLock(APP_LOCK_PASSWORD);
+ await settings.close();
+
+ expect(await app.lockAppButton()).toBeHidden();
+});
diff --git a/apps/web/__e2e__/models/app.model.ts b/apps/web/__e2e__/models/app.model.ts
index fcb706b56..0bdfa63f8 100644
--- a/apps/web/__e2e__/models/app.model.ts
+++ b/apps/web/__e2e__/models/app.model.ts
@@ -129,6 +129,10 @@ export class AppModel {
.waitFor({ state: "visible" });
}
+ async lockAppButton() {
+ return this.page.locator(getTestId("lock-app"));
+ }
+
async search(query: string, type: string) {
const searchinput = this.page.locator(getTestId("search-input"));
const searchButton = this.page.locator(getTestId("search-button"));
diff --git a/apps/web/__e2e__/models/settings-view.model.ts b/apps/web/__e2e__/models/settings-view.model.ts
index 486858f0e..80867af5b 100644
--- a/apps/web/__e2e__/models/settings-view.model.ts
+++ b/apps/web/__e2e__/models/settings-view.model.ts
@@ -21,6 +21,7 @@ import { Page } from "@playwright/test";
import { downloadAndReadFile, getTestId, uploadFile } from "../utils";
import {
confirmDialog,
+ fillConfirmPasswordDialog,
fillPasswordDialog,
waitForDialog,
waitToHaveText
@@ -150,4 +151,32 @@ export class SettingsViewModel {
await imageCompressionDropdown.selectOption(option);
}
+
+ async enableAppLock(userPassword: string, appLockPassword: string) {
+ const item = await this.navigation.findItem("App lock");
+ await item?.click();
+
+ const appLockSwitch = this.page
+ .locator(getTestId("setting-enable-app-lock"))
+ .locator("label");
+
+ await appLockSwitch.click();
+ await fillPasswordDialog(this.page, userPassword);
+ await this.page.waitForTimeout(100);
+ await fillConfirmPasswordDialog(this.page, appLockPassword);
+ await this.page.waitForTimeout(100);
+ }
+
+ async disableAppLock(appLockPassword: string) {
+ const item = await this.navigation.findItem("App lock");
+ await item?.click();
+
+ const appLockSwitch = this.page
+ .locator(getTestId("setting-enable-app-lock"))
+ .locator("label");
+
+ await appLockSwitch.click();
+ await fillPasswordDialog(this.page, appLockPassword);
+ await this.page.waitForTimeout(100);
+ }
}
diff --git a/apps/web/__e2e__/models/utils.ts b/apps/web/__e2e__/models/utils.ts
index 837da8a5a..e07c46995 100644
--- a/apps/web/__e2e__/models/utils.ts
+++ b/apps/web/__e2e__/models/utils.ts
@@ -123,6 +123,13 @@ export async function fillPasswordDialog(page: Page, password: string) {
await confirmDialog(dialog);
}
+export async function fillConfirmPasswordDialog(page: Page, password: string) {
+ const dialog = page.locator(getTestId("password-dialog"));
+ await dialog.locator(getTestId("password")).fill(password);
+ await dialog.locator(getTestId("confirmPassword")).fill(password);
+ await confirmDialog(dialog);
+}
+
export async function confirmDialog(dialog: Locator) {
const dialogConfirm = dialog.locator(getTestId("dialog-yes"));
await dialogConfirm.click();
diff --git a/apps/web/__e2e__/utils/index.ts b/apps/web/__e2e__/utils/index.ts
index 8d7c5fa9f..ca6ec78c2 100644
--- a/apps/web/__e2e__/utils/index.ts
+++ b/apps/web/__e2e__/utils/index.ts
@@ -72,6 +72,8 @@ const TITLE_ONLY_NOTE: Note = {
const PASSWORD = "123abc123abc";
+const APP_LOCK_PASSWORD = "lockapporelse🔪";
+
function getTestId<TId extends string>(id: TId): `[data-test-id="${TId}"]` {
return `[data-test-id="${id}"]`;
}
@@ -165,5 +167,6 @@ export {
isTestAll,
orderByOptions,
sortByOptions,
- groupByOptions
+ groupByOptions,
+ APP_LOCK_PASSWORD
};
diff --git a/apps/web/src/components/status-bar/index.tsx b/apps/web/src/components/status-bar/index.tsx
index f43f1222e..148a7b7e4 100644
--- a/apps/web/src/components/status-bar/index.tsx
+++ b/apps/web/src/components/status-bar/index.tsx
@@ -28,8 +28,8 @@ import {
Alert,
SyncOff,
Icon,
- Lock,
- Unlock
+ Unlock,
+ CellphoneLock
} from "../icons";
import { useStore as useUserStore } from "../../stores/user-store";
import { useStore as useAppStore } from "../../stores/app-store";
@@ -43,6 +43,7 @@ import { User } from "@notesnook/core";
import { showUpdateAvailableNotice } from "../../dialogs/confirm";
import { strings } from "@notesnook/intl";
import { useVault } from "../../hooks/use-vault";
+import { useKeyStore } from "../../interfaces/key-store";
function StatusBar() {
const user = useUserStore((state) => state.user);
@@ -51,6 +52,7 @@ function StatusBar() {
const updateStatus = useAutoUpdater();
const isFocusMode = useAppStore((state) => state.isFocusMode);
const { isVaultLocked, lockVault } = useVault();
+ const { activeCredentials, relock } = useKeyStore();
return (
<ScopedThemeProvider
@@ -115,6 +117,23 @@ function StatusBar() {
</Text>
</Button>
) : null}
+ {activeCredentials().length > 0 && (
+ <Button
+ variant="statusitem"
+ onClick={relock}
+ sx={{
+ alignItems: "center",
+ justifyContent: "center",
+ display: "flex",
+ color: "paragraph",
+ height: "100%"
+ }}
+ title={"Lock app"}
+ data-test-id="lock-app"
+ >
+ <CellphoneLock size={12} />
+ </Button>
+ )}
{statuses?.map((status) => {
const { key, icon: Icon } = status;
return (