From e53c1c59ac3045cd68336d49bafe28ecc53be5aa Mon Sep 17 00:00:00 2001 From: vaibhav garg Date: Mon, 5 Dec 2022 21:56:25 +0530 Subject: [PATCH 01/16] refactor: form el for ConfirmorCancel #1617 --- src/app/screens/ConfirmKeysend/index.tsx | 68 +++++----- src/app/screens/ConfirmPayment/index.tsx | 80 +++++------ .../ConfirmRequestPermission/index.tsx | 84 ++++++------ src/app/screens/Keysend/index.tsx | 54 ++++---- src/app/screens/MakeInvoice/index.tsx | 124 +++++++++--------- src/app/screens/Nostr/Confirm.tsx | 106 ++++++++------- src/app/screens/Nostr/ConfirmGetPublicKey.tsx | 70 +++++----- src/app/screens/Nostr/ConfirmSignMessage.tsx | 68 +++++----- 8 files changed, 351 insertions(+), 303 deletions(-) diff --git a/src/app/screens/ConfirmKeysend/index.tsx b/src/app/screens/ConfirmKeysend/index.tsx index e982f2ad09..72a4b745cb 100644 --- a/src/app/screens/ConfirmKeysend/index.tsx +++ b/src/app/screens/ConfirmKeysend/index.tsx @@ -123,45 +123,51 @@ function ConfirmKeysend() { }); } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
{!successMessage ? ( -
- -
-
- +
+ +
+
+ +
+ + { + setRememberMe(event.target.checked); + }} + budget={budget} + onBudgetChange={(event) => setBudget(event.target.value)} />
- - { - setRememberMe(event.target.checked); - }} - budget={budget} - onBudgetChange={(event) => setBudget(event.target.value)} - />
-
- + + ) : ( diff --git a/src/app/screens/ConfirmPayment/index.tsx b/src/app/screens/ConfirmPayment/index.tsx index 0cd321552c..2e99c2f612 100644 --- a/src/app/screens/ConfirmPayment/index.tsx +++ b/src/app/screens/ConfirmPayment/index.tsx @@ -133,52 +133,58 @@ function ConfirmPayment() { }); } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
{!successMessage ? ( -
- {navState.origin && ( - - )} -
-
- -
+
+
{navState.origin && ( - { - setRememberMe(event.target.checked); - }} - budget={budget} - onBudgetChange={(event) => setBudget(event.target.value)} + )} +
+
+ +
+ {navState.origin && ( + { + setRememberMe(event.target.checked); + }} + budget={budget} + onBudgetChange={(event) => setBudget(event.target.value)} + /> + )} +
-
-
- -

- {tComponents("only_trusted")} -

-
+
+ +

+ {tComponents("only_trusted")} +

+
+ ) : ( diff --git a/src/app/screens/ConfirmRequestPermission/index.tsx b/src/app/screens/ConfirmRequestPermission/index.tsx index c632497d79..eb24475350 100644 --- a/src/app/screens/ConfirmRequestPermission/index.tsx +++ b/src/app/screens/ConfirmRequestPermission/index.tsx @@ -35,52 +35,58 @@ const ConfirmRequestPermission: FC = () => { msg.error(USER_REJECTED_ERROR); }; + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + enable(); + } + return (
-
- +
+
+ -
-

{t("allow", { host: origin.host })}

-
- -

- {t("enable_method", { method: requestMethod })} -

-
-
- setAlwaysAllow((prev) => !prev)} - /> - +
+

{t("allow", { host: origin.host })}

+
+ +

+ {t("enable_method", { method: requestMethod })} +

+
+
+ setAlwaysAllow((prev) => !prev)} + /> + +
-
-
- -
+
+ +
+
); diff --git a/src/app/screens/Keysend/index.tsx b/src/app/screens/Keysend/index.tsx index ce29446fad..057fb4c5da 100644 --- a/src/app/screens/Keysend/index.tsx +++ b/src/app/screens/Keysend/index.tsx @@ -89,6 +89,11 @@ function Keysend() { navigate(-1); } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
-
- {destination && } - -
- setAmountSat(e.target.value)} - value={amountSat} - fiatValue={fiatAmount} +
+
+ {destination && } + - +
+ setAmountSat(e.target.value)} + value={amountSat} + fiatValue={fiatAmount} + /> + +
-
- + + ) : ( diff --git a/src/app/screens/MakeInvoice/index.tsx b/src/app/screens/MakeInvoice/index.tsx index db3758ffd3..9a33cfb072 100644 --- a/src/app/screens/MakeInvoice/index.tsx +++ b/src/app/screens/MakeInvoice/index.tsx @@ -97,74 +97,80 @@ function MakeInvoice() { msg.error(USER_REJECTED_ERROR); } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
-
- - -
-
- {amountEditable ? ( -
- handleValueChange(e.target.value)} - fiatValue={fiatValue} +
+
+ + +
+
+ {amountEditable ? ( +
+ handleValueChange(e.target.value)} + fiatValue={fiatValue} + /> + +
+ ) : ( +
+
{t("amount.label")}
+
{invoiceAttributes.amount}
+
+ )} + + {error &&

{error}

} +
+ +
+ {memoEditable ? ( + - -
- ) : ( -
-
{t("amount.label")}
-
{invoiceAttributes.amount}
-
- )} - - {error &&

{error}

} + ) : ( +
+
{t("memo.label")}
+
{invoiceAttributes.memo}
+
+ )} +
+
-
- {memoEditable ? ( - - ) : ( -
-
{t("memo.label")}
-
{invoiceAttributes.memo}
-
- )} -
+
+ + +

+ {tComponents("confirm_or_cancel.only_trusted")} +

-
- -
- - -

- {tComponents("confirm_or_cancel.only_trusted")} -

-
+
); diff --git a/src/app/screens/Nostr/Confirm.tsx b/src/app/screens/Nostr/Confirm.tsx index 746a9ea4fe..51f8f55131 100644 --- a/src/app/screens/Nostr/Confirm.tsx +++ b/src/app/screens/Nostr/Confirm.tsx @@ -47,64 +47,70 @@ function NostrConfirm() { msg.error(USER_REJECTED_ERROR); } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
-
- -
-

{t("allow", { host: origin.host })}

-

- - {description} - {details && ( - <> -
- {details} - - )} -

-
+
+
+ +
+

{t("allow", { host: origin.host })}

+

+ + {description} + {details && ( + <> +
+ {details} + + )} +

+
-
- { - setRememberPermission(event.target.checked); - }} +
+ { + setRememberPermission(event.target.checked); + }} + /> + +
+
+
+ - + {t("block_and_ignore", { host: origin.host })} +
-
- +
); diff --git a/src/app/screens/Nostr/ConfirmGetPublicKey.tsx b/src/app/screens/Nostr/ConfirmGetPublicKey.tsx index f5691f2a6c..1af7b8a85c 100644 --- a/src/app/screens/Nostr/ConfirmGetPublicKey.tsx +++ b/src/app/screens/Nostr/ConfirmGetPublicKey.tsx @@ -45,6 +45,11 @@ function NostrConfirmGetPublicKey() { msg.error(USER_REJECTED_ERROR); } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
@@ -66,39 +71,40 @@ function NostrConfirmGetPublicKey() {
-
- { - setRememberPermission(event.target.checked); - }} - /> - -
+
+
+ { + setRememberPermission(event.target.checked); + }} + /> + +
- + +
); diff --git a/src/app/screens/Nostr/ConfirmSignMessage.tsx b/src/app/screens/Nostr/ConfirmSignMessage.tsx index 5b30feacb3..20cec6c4ae 100644 --- a/src/app/screens/Nostr/ConfirmSignMessage.tsx +++ b/src/app/screens/Nostr/ConfirmSignMessage.tsx @@ -60,44 +60,50 @@ function ConfirmSignMessage() { } } + function handleSubmit(event: React.FormEvent) { + event.preventDefault(); + confirm(); + } + return (
{!successMessage ? ( -
- - -
- { - setRememberPermission(event.target.checked); - }} +
+
+ - + +
+ { + setRememberPermission(event.target.checked); + }} + /> + +
-
- + + ) : ( From 83ad14c574ddf6578587e4df01e532881796b768 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Tue, 29 Nov 2022 16:36:05 -0600 Subject: [PATCH 02/16] feat: bring back permission actions --- .../actions/permissions/__tests__/add.test.ts | 70 +++++++++++++++++++ .../permissions/__tests__/delete.test.ts | 63 +++++++++++++++++ .../actions/permissions/add.ts | 31 ++++++++ .../actions/permissions/delete.ts | 19 +++++ .../actions/permissions/index.ts | 4 ++ src/types.ts | 18 +++++ 6 files changed, 205 insertions(+) create mode 100644 src/extension/background-script/actions/permissions/__tests__/add.test.ts create mode 100644 src/extension/background-script/actions/permissions/__tests__/delete.test.ts create mode 100644 src/extension/background-script/actions/permissions/add.ts create mode 100644 src/extension/background-script/actions/permissions/delete.ts create mode 100644 src/extension/background-script/actions/permissions/index.ts diff --git a/src/extension/background-script/actions/permissions/__tests__/add.test.ts b/src/extension/background-script/actions/permissions/__tests__/add.test.ts new file mode 100644 index 0000000000..a0d28478b7 --- /dev/null +++ b/src/extension/background-script/actions/permissions/__tests__/add.test.ts @@ -0,0 +1,70 @@ +import db from "~/extension/background-script/db"; +import type { DbAllowance, MessagePermissionAdd } from "~/types"; + +import addPermission from "../add"; + +Date.now = jest.fn(() => 1487076708000); + +const stackerNews = { + host: "stacker.news", + imageURL: "https://stacker.news/apple-touch-icon.png", + name: "Stacker News", +}; + +const mockAllowances: DbAllowance[] = [ + { + ...stackerNews, + enabled: true, + id: 3, + lastPaymentAt: 1667291232423, + lnurlAuth: true, + remainingBudget: 500, + totalBudget: 500, + createdAt: "1667291216372", + tag: "", + }, +]; + +beforeEach(async () => { + // fill the DB first + await db.allowances.bulkAdd(mockAllowances); +}); + +afterEach(() => { + jest.clearAllMocks(); +}); + +describe("add permission", () => { + test("saves permissions", async () => { + const message: MessagePermissionAdd = { + application: "LBE", + prompt: true, + action: "addPermission", + origin: { + internal: true, + }, + args: { + host: stackerNews.host, + method: "listinvoices", + enabled: true, + blocked: false, + }, + }; + + await addPermission(message); + + const dbPermissions = await db.permissions.toArray(); + + expect(dbPermissions).toStrictEqual([ + { + id: 1, + allowanceId: 3, + createdAt: "1487076708000", + host: "stacker.news", + method: "listinvoices", + blocked: false, + enabled: true, + }, + ]); + }); +}); diff --git a/src/extension/background-script/actions/permissions/__tests__/delete.test.ts b/src/extension/background-script/actions/permissions/__tests__/delete.test.ts new file mode 100644 index 0000000000..316e7c5acf --- /dev/null +++ b/src/extension/background-script/actions/permissions/__tests__/delete.test.ts @@ -0,0 +1,63 @@ +import db from "~/extension/background-script/db"; +import type { MessagePermissionDelete } from "~/types"; + +import deletePermission from "../delete"; + +const mockNow = 1487076708000; +Date.now = jest.fn(() => mockNow); + +const stackerNews = { + host: "stacker.news", +}; +const mockPermissions = [ + { + id: 1, + allowanceId: 3, + createdAt: "1667291216372", + host: stackerNews.host, + method: "the-request-method-1", + blocked: false, + enabled: true, + }, + { + id: 2, + allowanceId: 3, + createdAt: "1667291216372", + host: stackerNews.host, + method: "the-request-method-2", + blocked: false, + enabled: true, + }, +]; + +beforeEach(async () => { + // fill the DB first + await db.permissions.bulkAdd(mockPermissions); +}); + +afterEach(() => { + jest.clearAllMocks(); +}); + +describe("delete permission", () => { + test("removes permission method from allowance and deletes permission", async () => { + const message: MessagePermissionDelete = { + application: "LBE", + prompt: true, + action: "deletePermission", + origin: { + internal: true, + }, + args: { + host: stackerNews.host, + method: "the-request-method-2", + }, + }; + + await deletePermission(message); + + const dbPermissions = await db.permissions.toArray(); + + expect(dbPermissions).toStrictEqual([mockPermissions[0]]); + }); +}); diff --git a/src/extension/background-script/actions/permissions/add.ts b/src/extension/background-script/actions/permissions/add.ts new file mode 100644 index 0000000000..9103307b89 --- /dev/null +++ b/src/extension/background-script/actions/permissions/add.ts @@ -0,0 +1,31 @@ +import type { MessagePermissionAdd } from "~/types"; + +import db from "../../db"; + +const addPermission = async (message: MessagePermissionAdd) => { + const { host, method, enabled, blocked } = message.args; + + const matchingAllowance = await db.allowances + .where("host") + .equalsIgnoreCase(host) + .first(); + + if (!matchingAllowance?.id) { + return { error: "No Allowance set for this host" }; + } + + const added = await db.permissions.add({ + createdAt: Date.now().toString(), + allowanceId: matchingAllowance.id, + host, + method, + enabled, + blocked, + }); + + await db.saveToStorage(); + + return { data: added }; +}; + +export default addPermission; diff --git a/src/extension/background-script/actions/permissions/delete.ts b/src/extension/background-script/actions/permissions/delete.ts new file mode 100644 index 0000000000..db639d6cd1 --- /dev/null +++ b/src/extension/background-script/actions/permissions/delete.ts @@ -0,0 +1,19 @@ +import type { MessagePermissionDelete } from "~/types"; + +import db from "../../db"; + +const deletePermission = async (message: MessagePermissionDelete) => { + const { host, method } = message.args; + + const deleteCount = await db.permissions + .where("host") + .equalsIgnoreCase(host) + .and((p) => p.method === method) + .delete(); + + await db.saveToStorage(); + + return { data: !!deleteCount }; +}; + +export default deletePermission; diff --git a/src/extension/background-script/actions/permissions/index.ts b/src/extension/background-script/actions/permissions/index.ts new file mode 100644 index 0000000000..885aeadec4 --- /dev/null +++ b/src/extension/background-script/actions/permissions/index.ts @@ -0,0 +1,4 @@ +import add from "./add"; +import deletePermission from "./delete"; + +export { add, deletePermission }; diff --git a/src/types.ts b/src/types.ts index 6b7cdb24f6..a9d6ba1e14 100644 --- a/src/types.ts +++ b/src/types.ts @@ -198,6 +198,24 @@ export interface MessageAccountAll extends MessageDefault { action: "getAccounts"; } +export interface MessagePermissionAdd extends MessageDefault { + args: { + host: string; + method: string; + enabled: boolean; + blocked: boolean; + }; + action: "addPermission"; +} + +export interface MessagePermissionDelete extends MessageDefault { + args: { + host: string; + method: string; + }; + action: "deletePermission"; +} + export interface MessageBlocklistAdd extends MessageDefault { args: { host: string; From af4e8c1019f6f346a278dcbc20e0a81c4856bbb8 Mon Sep 17 00:00:00 2001 From: Adithya Vardhan Date: Tue, 6 Dec 2022 12:05:14 +0530 Subject: [PATCH 03/16] feat: add permission actions - deleteByIds and list --- .../actions/permissions/deleteByIds.ts | 13 ++++++++ .../actions/permissions/index.ts | 4 ++- .../actions/permissions/list.ts | 32 +++++++++++++++++++ src/types.ts | 30 +++++++++++++---- 4 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/extension/background-script/actions/permissions/deleteByIds.ts create mode 100644 src/extension/background-script/actions/permissions/list.ts diff --git a/src/extension/background-script/actions/permissions/deleteByIds.ts b/src/extension/background-script/actions/permissions/deleteByIds.ts new file mode 100644 index 0000000000..c2260d49a5 --- /dev/null +++ b/src/extension/background-script/actions/permissions/deleteByIds.ts @@ -0,0 +1,13 @@ +import db from "~/extension/background-script/db"; +import type { MessagePermissionsDelete } from "~/types"; + +const deleteByIds = async (message: MessagePermissionsDelete) => { + const { ids } = message.args; + + await db.permissions.bulkDelete(ids); + await db.saveToStorage(); + + return { data: true }; +}; + +export default deleteByIds; diff --git a/src/extension/background-script/actions/permissions/index.ts b/src/extension/background-script/actions/permissions/index.ts index 885aeadec4..e0e3c9b663 100644 --- a/src/extension/background-script/actions/permissions/index.ts +++ b/src/extension/background-script/actions/permissions/index.ts @@ -1,4 +1,6 @@ import add from "./add"; import deletePermission from "./delete"; +import deleteByIds from "./deleteByIds"; +import listByAllowance from "./list"; -export { add, deletePermission }; +export { add, deletePermission, deleteByIds, listByAllowance }; diff --git a/src/extension/background-script/actions/permissions/list.ts b/src/extension/background-script/actions/permissions/list.ts new file mode 100644 index 0000000000..4d8336b347 --- /dev/null +++ b/src/extension/background-script/actions/permissions/list.ts @@ -0,0 +1,32 @@ +import db from "~/extension/background-script/db"; +import type { MessagePermissionsList, Permission } from "~/types"; + +const listByAllowance = async (message: MessagePermissionsList) => { + const { id } = message.args; + const dbPermissions = await db.permissions + .toCollection() + .filter((permission) => permission.allowanceId === id) + .toArray(); + + const permissions: Permission[] = []; + + for (const dbPermission of dbPermissions) { + if (dbPermission.id) { + const { id } = dbPermission; + const tmpPermission: Permission = { + ...dbPermission, + id, + }; + + permissions.push(tmpPermission); + } + } + + return { + data: { + permissions, + }, + }; +}; + +export default listByAllowance; diff --git a/src/types.ts b/src/types.ts index a9d6ba1e14..ff1601370a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -200,22 +200,36 @@ export interface MessageAccountAll extends MessageDefault { export interface MessagePermissionAdd extends MessageDefault { args: { - host: string; - method: string; - enabled: boolean; - blocked: boolean; + host: Permission["host"]; + method: Permission["method"]; + enabled: Permission["enabled"]; + blocked: Permission["blocked"]; }; action: "addPermission"; } export interface MessagePermissionDelete extends MessageDefault { args: { - host: string; - method: string; + host: Permission["host"]; + method: Permission["method"]; }; action: "deletePermission"; } +export interface MessagePermissionsList extends MessageDefault { + args: { + id: Allowance["id"]; + }; + action: "listPermissions"; +} + +export interface MessagePermissionsDelete extends MessageDefault { + args: { + ids: Permission["id"][]; + }; + action: "deletePermissions"; +} + export interface MessageBlocklistAdd extends MessageDefault { args: { host: string; @@ -534,6 +548,10 @@ export interface DbPermission { blocked: boolean; } +export interface Permission extends Omit { + id: number; +} + export interface PaymentResponse extends Pick { route: { From eba1e0a911a081bba283a50b41e3b70ef277ebf9 Mon Sep 17 00:00:00 2001 From: Adithya Vardhan Date: Tue, 6 Dec 2022 12:06:05 +0530 Subject: [PATCH 04/16] chore: add tests for permissions --- .../actions/permissions/__tests__/add.test.ts | 38 +++----------- .../permissions/__tests__/delete.test.ts | 35 ++++--------- .../permissions/__tests__/deleteByIds.test.ts | 41 +++++++++++++++ .../permissions/__tests__/list.test.ts | 52 +++++++++++++++++++ src/extension/background-script/router.ts | 5 ++ src/fixtures/permissions.ts | 31 +++++++++++ 6 files changed, 144 insertions(+), 58 deletions(-) create mode 100644 src/extension/background-script/actions/permissions/__tests__/deleteByIds.test.ts create mode 100644 src/extension/background-script/actions/permissions/__tests__/list.test.ts create mode 100644 src/fixtures/permissions.ts diff --git a/src/extension/background-script/actions/permissions/__tests__/add.test.ts b/src/extension/background-script/actions/permissions/__tests__/add.test.ts index a0d28478b7..cb3aeaf810 100644 --- a/src/extension/background-script/actions/permissions/__tests__/add.test.ts +++ b/src/extension/background-script/actions/permissions/__tests__/add.test.ts @@ -1,29 +1,13 @@ import db from "~/extension/background-script/db"; +import { allowanceFixture } from "~/fixtures/allowances"; +import { permissionsFixture } from "~/fixtures/permissions"; import type { DbAllowance, MessagePermissionAdd } from "~/types"; import addPermission from "../add"; Date.now = jest.fn(() => 1487076708000); -const stackerNews = { - host: "stacker.news", - imageURL: "https://stacker.news/apple-touch-icon.png", - name: "Stacker News", -}; - -const mockAllowances: DbAllowance[] = [ - { - ...stackerNews, - enabled: true, - id: 3, - lastPaymentAt: 1667291232423, - lnurlAuth: true, - remainingBudget: 500, - totalBudget: 500, - createdAt: "1667291216372", - tag: "", - }, -]; +const mockAllowances: DbAllowance[] = [allowanceFixture[0]]; beforeEach(async () => { // fill the DB first @@ -44,8 +28,8 @@ describe("add permission", () => { internal: true, }, args: { - host: stackerNews.host, - method: "listinvoices", + host: mockAllowances[0].host, + method: "the-request-method-1", enabled: true, blocked: false, }, @@ -55,16 +39,6 @@ describe("add permission", () => { const dbPermissions = await db.permissions.toArray(); - expect(dbPermissions).toStrictEqual([ - { - id: 1, - allowanceId: 3, - createdAt: "1487076708000", - host: "stacker.news", - method: "listinvoices", - blocked: false, - enabled: true, - }, - ]); + expect(dbPermissions).toStrictEqual([permissionsFixture[0]]); }); }); diff --git a/src/extension/background-script/actions/permissions/__tests__/delete.test.ts b/src/extension/background-script/actions/permissions/__tests__/delete.test.ts index 316e7c5acf..080ca6d7bf 100644 --- a/src/extension/background-script/actions/permissions/__tests__/delete.test.ts +++ b/src/extension/background-script/actions/permissions/__tests__/delete.test.ts @@ -1,34 +1,17 @@ import db from "~/extension/background-script/db"; -import type { MessagePermissionDelete } from "~/types"; +import { permissionsFixture } from "~/fixtures/permissions"; +import type { DbPermission, MessagePermissionDelete } from "~/types"; import deletePermission from "../delete"; const mockNow = 1487076708000; Date.now = jest.fn(() => mockNow); -const stackerNews = { - host: "stacker.news", -}; -const mockPermissions = [ - { - id: 1, - allowanceId: 3, - createdAt: "1667291216372", - host: stackerNews.host, - method: "the-request-method-1", - blocked: false, - enabled: true, - }, - { - id: 2, - allowanceId: 3, - createdAt: "1667291216372", - host: stackerNews.host, - method: "the-request-method-2", - blocked: false, - enabled: true, - }, -]; +const mockPermissions: DbPermission[] = permissionsFixture; + +const resultPermissions: DbPermission[] = permissionsFixture.filter( + (permission) => permission.id !== 2 +); beforeEach(async () => { // fill the DB first @@ -49,7 +32,7 @@ describe("delete permission", () => { internal: true, }, args: { - host: stackerNews.host, + host: mockPermissions[1].host, method: "the-request-method-2", }, }; @@ -58,6 +41,6 @@ describe("delete permission", () => { const dbPermissions = await db.permissions.toArray(); - expect(dbPermissions).toStrictEqual([mockPermissions[0]]); + expect(dbPermissions).toStrictEqual(resultPermissions); }); }); diff --git a/src/extension/background-script/actions/permissions/__tests__/deleteByIds.test.ts b/src/extension/background-script/actions/permissions/__tests__/deleteByIds.test.ts new file mode 100644 index 0000000000..0e58d37d6e --- /dev/null +++ b/src/extension/background-script/actions/permissions/__tests__/deleteByIds.test.ts @@ -0,0 +1,41 @@ +import db from "~/extension/background-script/db"; +import { permissionsFixture } from "~/fixtures/permissions"; +import type { DbPermission, MessagePermissionsDelete } from "~/types"; + +import deleteByIds from "../deleteByIds"; + +const mockNow = 1487076708000; +Date.now = jest.fn(() => mockNow); + +const mockPermissions: DbPermission[] = permissionsFixture; + +beforeEach(async () => { + // fill the DB first + await db.permissions.bulkAdd(mockPermissions); +}); + +afterEach(() => { + jest.clearAllMocks(); +}); + +describe("delete permissions by id", () => { + test("bulk deletes permissions using keys", async () => { + const message: MessagePermissionsDelete = { + application: "LBE", + prompt: true, + action: "deletePermissions", + origin: { + internal: true, + }, + args: { + ids: [2, 3], + }, + }; + + await deleteByIds(message); + + const dbPermissions = await db.permissions.toArray(); + + expect(dbPermissions).toStrictEqual([mockPermissions[0]]); + }); +}); diff --git a/src/extension/background-script/actions/permissions/__tests__/list.test.ts b/src/extension/background-script/actions/permissions/__tests__/list.test.ts new file mode 100644 index 0000000000..65e5090a54 --- /dev/null +++ b/src/extension/background-script/actions/permissions/__tests__/list.test.ts @@ -0,0 +1,52 @@ +import db from "~/extension/background-script/db"; +import { permissionsFixture } from "~/fixtures/permissions"; +import type { DbPermission, Permission, MessagePermissionsList } from "~/types"; + +import listByAllowance from "../list"; + +const mockNow = 1487076708000; +Date.now = jest.fn(() => mockNow); + +const mockPermissions: DbPermission[] = permissionsFixture; + +const resultPermissions: Permission[] = [ + { + ...mockPermissions[0], + id: 1, + }, + { + ...mockPermissions[1], + id: 2, + }, +]; + +beforeEach(async () => { + // fill the DB first + await db.permissions.bulkAdd(mockPermissions); +}); + +afterEach(() => { + jest.clearAllMocks(); +}); + +describe("delete permissions by id", () => { + test("bulk deletes permissions using keys", async () => { + const message: MessagePermissionsList = { + application: "LBE", + prompt: true, + action: "listPermissions", + origin: { + internal: true, + }, + args: { + id: 1, + }, + }; + + expect(await listByAllowance(message)).toStrictEqual({ + data: { + permissions: resultPermissions, + }, + }); + }); +}); diff --git a/src/extension/background-script/router.ts b/src/extension/background-script/router.ts index 13b93bc5a7..5dba8ab46f 100644 --- a/src/extension/background-script/router.ts +++ b/src/extension/background-script/router.ts @@ -6,6 +6,7 @@ import * as ln from "./actions/ln"; import lnurl, { auth } from "./actions/lnurl"; import * as nostr from "./actions/nostr"; import * as payments from "./actions/payments"; +import * as permissions from "./actions/permissions"; import * as settings from "./actions/settings"; import * as setup from "./actions/setup"; import * as webln from "./actions/webln"; @@ -17,6 +18,10 @@ const routes = { listAllowances: allowances.list, deleteAllowance: allowances.deleteAllowance, updateAllowance: allowances.updateAllowance, + addPermission: permissions.add, + deletePermission: permissions.deletePermission, + deletePermissionsById: permissions.deleteByIds, + listPermissions: permissions.listByAllowance, lock: accounts.lock, unlock: accounts.unlock, getInfo: ln.getInfo, diff --git a/src/fixtures/permissions.ts b/src/fixtures/permissions.ts new file mode 100644 index 0000000000..416dd77c46 --- /dev/null +++ b/src/fixtures/permissions.ts @@ -0,0 +1,31 @@ +import type { DbPermission } from "~/types"; + +export const permissionsFixture: DbPermission[] = [ + { + id: 1, + allowanceId: 1, + createdAt: "1487076708000", + host: "pro.kollider.xyz", + method: "the-request-method-1", + blocked: false, + enabled: true, + }, + { + id: 2, + allowanceId: 1, + createdAt: "1487076708000", + host: "pro.kollider.xyz", + method: "the-request-method-2", + blocked: false, + enabled: true, + }, + { + id: 3, + allowanceId: 2, + createdAt: "1487076708000", + host: "lnmarkets.com", + method: "the-request-method-3", + blocked: false, + enabled: true, + }, +]; From a0c3852fcd0efb1cef83516f0998dc53449f2582 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 7 Dec 2022 18:39:26 +0100 Subject: [PATCH 05/16] chore: add 25k option for the default amounts --- src/app/components/SatButtons/index.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/app/components/SatButtons/index.tsx b/src/app/components/SatButtons/index.tsx index d7eab832dd..d5cd282987 100644 --- a/src/app/components/SatButtons/index.tsx +++ b/src/app/components/SatButtons/index.tsx @@ -10,13 +10,6 @@ type Props = { function SatButtons({ onClick, disabled }: Props) { return (
-
); } From 4529d15385ae0d5a48f9b0ecba3659bf837ea17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=B6gel?= Date: Wed, 7 Dec 2022 22:19:33 +0100 Subject: [PATCH 06/16] refactor: getOS: don't use deprecated appVersion. From MDN: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/appVersion > Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. ... Be aware that this feature may cease to work at any time. There's no such warning for userAgent and here's a source on how to use it instead: https://www.educative.io/answers/how-to-detect-the-operating-system-of-the-client-machine-using-js --- src/app/components/CompanionDownloadInfo.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/components/CompanionDownloadInfo.tsx b/src/app/components/CompanionDownloadInfo.tsx index dcd54c8878..abc918a578 100644 --- a/src/app/components/CompanionDownloadInfo.tsx +++ b/src/app/components/CompanionDownloadInfo.tsx @@ -13,10 +13,11 @@ function CompanionDownloadInfo({ hasTorCallback }: Props) { }); function getOS() { - if (navigator.appVersion.indexOf("Win") != -1) return "Windows"; - if (navigator.appVersion.indexOf("Mac") != -1) return "MacOS"; - if (navigator.appVersion.indexOf("X11") != -1) return "UNIX"; - if (navigator.appVersion.indexOf("Linux") != -1) return "Linux"; + const userAgent = navigator.userAgent; + if (userAgent.indexOf("Win") !== -1) return "Windows"; + if (userAgent.indexOf("Mac") !== -1) return "MacOS"; + if (userAgent.indexOf("X11") !== -1) return "UNIX"; + if (userAgent.indexOf("Linux") !== -1) return "Linux" } // TODO: check if the companion app is already installed From 1505b42592744c4ba81402a0d9842b01fcf0652f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=B6gel?= Date: Wed, 7 Dec 2022 22:34:24 +0100 Subject: [PATCH 07/16] prettier --- src/app/components/CompanionDownloadInfo.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/components/CompanionDownloadInfo.tsx b/src/app/components/CompanionDownloadInfo.tsx index abc918a578..eb027bf723 100644 --- a/src/app/components/CompanionDownloadInfo.tsx +++ b/src/app/components/CompanionDownloadInfo.tsx @@ -17,7 +17,7 @@ function CompanionDownloadInfo({ hasTorCallback }: Props) { if (userAgent.indexOf("Win") !== -1) return "Windows"; if (userAgent.indexOf("Mac") !== -1) return "MacOS"; if (userAgent.indexOf("X11") !== -1) return "UNIX"; - if (userAgent.indexOf("Linux") !== -1) return "Linux" + if (userAgent.indexOf("Linux") !== -1) return "Linux"; } // TODO: check if the companion app is already installed From 94390e7a16bec0c0bacd819b6e7b2a5eab73659e Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Thu, 8 Dec 2022 13:06:31 +0100 Subject: [PATCH 08/16] chore: lowecase as it should be --- src/app/components/SatButtons/index.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/components/SatButtons/index.tsx b/src/app/components/SatButtons/index.tsx index d5cd282987..4805244ee4 100644 --- a/src/app/components/SatButtons/index.tsx +++ b/src/app/components/SatButtons/index.tsx @@ -12,28 +12,28 @@ function SatButtons({ onClick, disabled }: Props) {