@teamkeel/functions-runtime 0.321.0 → 0.321.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,11 @@
1
- import {
1
+ const {
2
+ permissionsApiInstance,
2
3
  Permissions,
3
4
  PERMISSION_STATE,
4
5
  PermissionError,
5
6
  checkBuiltInPermissions,
6
- } from "./permissions";
7
+ } = require("./permissions");
8
+
7
9
  import { getDatabase } from "./database";
8
10
 
9
11
  import { beforeEach, describe, expect, test } from "vitest";
@@ -14,48 +16,43 @@ process.env.KEEL_DB_CONN = `postgresql://postgres:postgres@localhost:5432/functi
14
16
  let permissions;
15
17
  let ctx = {};
16
18
  let db = getDatabase();
17
- let permissionState = {
18
- status: "unknown",
19
- };
20
19
 
21
20
  describe("explicit", () => {
22
21
  beforeEach(() => {
23
- permissions = new Permissions(permissionState);
22
+ permissions = new Permissions();
24
23
  });
25
24
 
26
25
  test("explicitly allowing execution", () => {
27
- expect(permissions.getState()).toEqual(PERMISSION_STATE.UNKNOWN);
26
+ wrapWithAsyncLocalStorage({ permitted: null }, () => {
27
+ expect(permissions.getState()).toEqual(PERMISSION_STATE.UNKNOWN);
28
28
 
29
- permissions.allow();
29
+ permissions.allow();
30
30
 
31
- expect(permissions.getState()).toEqual(PERMISSION_STATE.PERMITTED);
31
+ expect(permissions.getState()).toEqual(PERMISSION_STATE.PERMITTED);
32
+ });
32
33
  });
33
34
 
34
35
  test("explicitly denying execution", () => {
35
- expect(permissions.getState()).toEqual(PERMISSION_STATE.UNKNOWN);
36
+ wrapWithAsyncLocalStorage({ permitted: null }, () => {
37
+ expect(permissions.getState()).toEqual(PERMISSION_STATE.UNKNOWN);
36
38
 
37
- expect(() => permissions.deny()).toThrowError(PermissionError);
39
+ expect(() => permissions.deny()).toThrowError(PermissionError);
38
40
 
39
- expect(permissions.getState()).toEqual(PERMISSION_STATE.UNPERMITTED);
41
+ expect(permissions.getState()).toEqual(PERMISSION_STATE.UNPERMITTED);
42
+ });
40
43
  });
41
44
  });
42
45
 
43
46
  describe("prior state", () => {
44
- test("when the prior state is unknown", () => {
45
- permissions = new Permissions({
46
- status: "unknown",
47
- });
48
-
49
- expect(permissions.getState()).toEqual(PERMISSION_STATE.UNKNOWN);
50
- });
51
-
52
47
  test("when the prior state is granted", () => {
53
- permissions = new Permissions({
54
- status: "granted",
55
- reason: "role",
56
- });
57
-
58
- expect(permissions.getState()).toEqual(PERMISSION_STATE.PERMITTED);
48
+ wrapWithAsyncLocalStorage(
49
+ {
50
+ permitted: true,
51
+ },
52
+ () => {
53
+ expect(permissions.getState()).toEqual(PERMISSION_STATE.PERMITTED);
54
+ }
55
+ );
59
56
  });
60
57
  });
61
58
 
@@ -85,7 +82,7 @@ describe("check", () => {
85
82
  ctx,
86
83
  db,
87
84
  functionName,
88
- permissions: [permissionRule1],
85
+ permissionFns: [permissionRule1],
89
86
  })
90
87
  ).resolves.ok;
91
88
  });
@@ -113,8 +110,14 @@ describe("check", () => {
113
110
  ctx,
114
111
  db,
115
112
  functionName,
116
- permissions: [permissionRule1],
113
+ permissionFns: [permissionRule1],
117
114
  })
118
115
  ).rejects.toThrow();
119
116
  });
120
117
  });
118
+
119
+ function wrapWithAsyncLocalStorage(initialState, testFn) {
120
+ permissionsApiInstance.run(initialState, () => {
121
+ testFn();
122
+ });
123
+ }