-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1819 from getAlby/feature/permission-actions
feat: bring back permission actions
- Loading branch information
Showing
12 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/extension/background-script/actions/permissions/__tests__/add.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 mockAllowances: DbAllowance[] = [allowanceFixture[0]]; | ||
|
||
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: mockAllowances[0].host, | ||
method: "the-request-method-1", | ||
enabled: true, | ||
blocked: false, | ||
}, | ||
}; | ||
|
||
await addPermission(message); | ||
|
||
const dbPermissions = await db.permissions.toArray(); | ||
|
||
expect(dbPermissions).toStrictEqual([permissionsFixture[0]]); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
src/extension/background-script/actions/permissions/__tests__/delete.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import db from "~/extension/background-script/db"; | ||
import { permissionsFixture } from "~/fixtures/permissions"; | ||
import type { DbPermission, MessagePermissionDelete } from "~/types"; | ||
|
||
import deletePermission from "../delete"; | ||
|
||
const mockNow = 1487076708000; | ||
Date.now = jest.fn(() => mockNow); | ||
|
||
const mockPermissions: DbPermission[] = permissionsFixture; | ||
|
||
const resultPermissions: DbPermission[] = permissionsFixture.filter( | ||
(permission) => permission.id !== 2 | ||
); | ||
|
||
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: mockPermissions[1].host, | ||
method: "the-request-method-2", | ||
}, | ||
}; | ||
|
||
await deletePermission(message); | ||
|
||
const dbPermissions = await db.permissions.toArray(); | ||
|
||
expect(dbPermissions).toStrictEqual(resultPermissions); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
src/extension/background-script/actions/permissions/__tests__/deleteByIds.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]]); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
src/extension/background-script/actions/permissions/__tests__/list.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/extension/background-script/actions/permissions/add.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
19 changes: 19 additions & 0 deletions
19
src/extension/background-script/actions/permissions/delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
13 changes: 13 additions & 0 deletions
13
src/extension/background-script/actions/permissions/deleteByIds.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import add from "./add"; | ||
import deletePermission from "./delete"; | ||
import deleteByIds from "./deleteByIds"; | ||
import listByAllowance from "./list"; | ||
|
||
export { add, deletePermission, deleteByIds, listByAllowance }; |
32 changes: 32 additions & 0 deletions
32
src/extension/background-script/actions/permissions/list.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters