@flowerforce/flowerbase 1.0.1-beta.9 → 1.0.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.
Files changed (178) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +1 -1
  3. package/README.md +466 -7
  4. package/dist/auth/controller.d.ts.map +1 -1
  5. package/dist/auth/controller.js +11 -1
  6. package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -1
  7. package/dist/auth/providers/local-userpass/controller.js +15 -15
  8. package/dist/auth/utils.d.ts +2 -1
  9. package/dist/auth/utils.d.ts.map +1 -1
  10. package/dist/auth/utils.js +14 -10
  11. package/dist/constants.d.ts.map +1 -1
  12. package/dist/constants.js +3 -3
  13. package/dist/features/endpoints/utils.d.ts +1 -1
  14. package/dist/features/endpoints/utils.d.ts.map +1 -1
  15. package/dist/features/endpoints/utils.js +5 -2
  16. package/dist/features/functions/controller.d.ts.map +1 -1
  17. package/dist/features/functions/controller.js +12 -8
  18. package/dist/features/functions/dtos.d.ts +2 -1
  19. package/dist/features/functions/dtos.d.ts.map +1 -1
  20. package/dist/features/functions/interface.d.ts +2 -1
  21. package/dist/features/functions/interface.d.ts.map +1 -1
  22. package/dist/features/functions/utils.d.ts +1 -1
  23. package/dist/features/functions/utils.d.ts.map +1 -1
  24. package/dist/features/functions/utils.js +4 -4
  25. package/dist/features/triggers/index.d.ts.map +1 -1
  26. package/dist/features/triggers/index.js +9 -3
  27. package/dist/features/triggers/interface.d.ts +4 -4
  28. package/dist/features/triggers/interface.d.ts.map +1 -1
  29. package/dist/features/triggers/utils.d.ts.map +1 -1
  30. package/dist/features/triggers/utils.js +25 -14
  31. package/dist/global.d.ts +9 -0
  32. package/dist/global.d.ts.map +1 -0
  33. package/dist/global.js +2 -0
  34. package/dist/index.d.ts +1 -1
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +13 -9
  37. package/dist/model.d.ts +1 -0
  38. package/dist/model.d.ts.map +1 -1
  39. package/dist/model.js +15 -0
  40. package/dist/services/api/index.d.ts.map +1 -1
  41. package/dist/services/api/index.js +6 -1
  42. package/dist/services/aws/index.d.ts.map +1 -1
  43. package/dist/services/aws/index.js +5 -5
  44. package/dist/services/index.d.ts.map +1 -1
  45. package/dist/services/mongodb-atlas/index.d.ts.map +1 -1
  46. package/dist/services/mongodb-atlas/index.js +76 -71
  47. package/dist/services/mongodb-atlas/model.d.ts +3 -3
  48. package/dist/services/mongodb-atlas/model.d.ts.map +1 -1
  49. package/dist/services/mongodb-atlas/utils.d.ts.map +1 -1
  50. package/dist/state.d.ts +2 -2
  51. package/dist/state.d.ts.map +1 -1
  52. package/dist/utils/context/helpers.d.ts +4 -4
  53. package/dist/utils/context/helpers.d.ts.map +1 -1
  54. package/dist/utils/context/helpers.js +1 -1
  55. package/dist/utils/context/index.d.ts.map +1 -1
  56. package/dist/utils/context/index.js +3 -5
  57. package/dist/utils/helpers/someAsync.d.ts.map +1 -1
  58. package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -1
  59. package/dist/utils/initializer/exposeRoutes.js +44 -1
  60. package/dist/utils/initializer/registerPlugins.d.ts.map +1 -1
  61. package/dist/utils/initializer/registerPlugins.js +9 -9
  62. package/dist/utils/roles/helpers.d.ts.map +1 -1
  63. package/dist/utils/roles/helpers.js +9 -7
  64. package/dist/utils/roles/interface.d.ts.map +1 -1
  65. package/dist/utils/roles/machines/commonValidators.d.ts +2 -2
  66. package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -1
  67. package/dist/utils/roles/machines/commonValidators.js +5 -6
  68. package/dist/utils/roles/machines/index.d.ts.map +1 -1
  69. package/dist/utils/roles/machines/interface.d.ts.map +1 -1
  70. package/dist/utils/roles/machines/machine.d.ts +3 -3
  71. package/dist/utils/roles/machines/machine.d.ts.map +1 -1
  72. package/dist/utils/roles/machines/machine.js +12 -4
  73. package/dist/utils/roles/machines/read/A/index.d.ts.map +1 -1
  74. package/dist/utils/roles/machines/read/A/index.js +12 -2
  75. package/dist/utils/roles/machines/read/B/index.d.ts.map +1 -1
  76. package/dist/utils/roles/machines/read/B/index.js +20 -5
  77. package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -1
  78. package/dist/utils/roles/machines/read/C/index.js +20 -5
  79. package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -1
  80. package/dist/utils/roles/machines/read/D/index.js +13 -3
  81. package/dist/utils/roles/machines/read/D/validators.d.ts +1 -1
  82. package/dist/utils/roles/machines/read/D/validators.d.ts.map +1 -1
  83. package/dist/utils/roles/machines/read/D/validators.js +8 -4
  84. package/dist/utils/roles/machines/utils.d.ts +4 -4
  85. package/dist/utils/roles/machines/utils.d.ts.map +1 -1
  86. package/dist/utils/roles/machines/utils.js +5 -1
  87. package/dist/utils/roles/machines/write/A/index.d.ts.map +1 -1
  88. package/dist/utils/roles/machines/write/A/index.js +13 -3
  89. package/dist/utils/roles/machines/write/B/index.d.ts.map +1 -1
  90. package/dist/utils/roles/machines/write/B/index.js +37 -10
  91. package/dist/utils/roles/machines/write/C/index.d.ts.map +1 -1
  92. package/dist/utils/roles/machines/write/C/index.js +13 -3
  93. package/dist/utils/roles/machines/write/C/validators.d.ts +1 -1
  94. package/dist/utils/roles/machines/write/C/validators.d.ts.map +1 -1
  95. package/dist/utils/roles/machines/write/C/validators.js +8 -4
  96. package/dist/utils/rules-matcher/interface.d.ts.map +1 -1
  97. package/dist/utils/rules-matcher/utils.d.ts.map +1 -1
  98. package/dist/utils/rules.d.ts.map +1 -1
  99. package/package.json +9 -2
  100. package/src/auth/controller.ts +12 -1
  101. package/src/auth/providers/local-userpass/controller.ts +47 -39
  102. package/src/auth/providers/local-userpass/dtos.ts +1 -1
  103. package/src/auth/utils.ts +24 -18
  104. package/src/constants.ts +7 -3
  105. package/src/features/endpoints/utils.ts +6 -4
  106. package/src/features/functions/controller.ts +23 -22
  107. package/src/features/functions/dtos.ts +9 -9
  108. package/src/features/functions/interface.ts +2 -1
  109. package/src/features/functions/utils.ts +12 -12
  110. package/src/features/triggers/index.ts +9 -6
  111. package/src/features/triggers/interface.ts +9 -10
  112. package/src/features/triggers/utils.ts +57 -29
  113. package/src/global.ts +9 -0
  114. package/src/index.ts +16 -11
  115. package/src/model.ts +3 -1
  116. package/src/services/api/index.ts +6 -1
  117. package/src/services/aws/index.ts +19 -17
  118. package/src/services/mongodb-atlas/index.ts +204 -163
  119. package/src/services/mongodb-atlas/model.ts +13 -13
  120. package/src/services/mongodb-atlas/utils.ts +6 -4
  121. package/src/state.ts +32 -20
  122. package/src/utils/__tests__/STEP_A_STATES.test.ts +78 -47
  123. package/src/utils/__tests__/STEP_B_STATES.test.ts +168 -105
  124. package/src/utils/__tests__/STEP_C_STATES.test.ts +150 -78
  125. package/src/utils/__tests__/STEP_D_STATES.test.ts +129 -86
  126. package/src/utils/__tests__/checkAdditionalFieldsFn.test.ts +35 -35
  127. package/src/utils/__tests__/checkApplyWhen.test.ts +40 -41
  128. package/src/utils/__tests__/checkFieldsPropertyExists.test.ts +40 -40
  129. package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +185 -184
  130. package/src/utils/__tests__/comparePassword.test.ts +27 -30
  131. package/src/utils/__tests__/evaluateDocumentsFiltersReadFn.test.ts +55 -47
  132. package/src/utils/__tests__/evaluateDocumentsFiltersWriteFn.test.ts +61 -47
  133. package/src/utils/__tests__/evaluateTopLevelReadFn.test.ts +48 -48
  134. package/src/utils/__tests__/evaluateTopLevelWriteFn.test.ts +56 -56
  135. package/src/utils/__tests__/exposeRoutes.test.ts +46 -44
  136. package/src/utils/__tests__/generateContextData.test.ts +57 -51
  137. package/src/utils/__tests__/getDefaultRule.test.ts +32 -27
  138. package/src/utils/__tests__/getKey.test.ts +10 -10
  139. package/src/utils/__tests__/getKeys.test.ts +10 -9
  140. package/src/utils/__tests__/getWinningRole.test.ts +57 -50
  141. package/src/utils/__tests__/hashPassword.test.ts +24 -25
  142. package/src/utils/__tests__/isEmpty.test.ts +14 -15
  143. package/src/utils/__tests__/logMachineInfo.test.ts +12 -12
  144. package/src/utils/__tests__/operators.test.ts +94 -96
  145. package/src/utils/__tests__/readFileContent.test.ts +28 -28
  146. package/src/utils/__tests__/registerPlugins.test.ts +44 -32
  147. package/src/utils/__tests__/rule.test.ts +47 -49
  148. package/src/utils/__tests__/rulesMatcherInterfaces.test.ts +57 -52
  149. package/src/utils/__tests__/rulesMatcherUtils.test.ts +64 -53
  150. package/src/utils/__tests__/someAsync.test.ts +46 -49
  151. package/src/utils/context/helpers.ts +1 -1
  152. package/src/utils/context/index.ts +7 -10
  153. package/src/utils/crypto/index.ts +2 -2
  154. package/src/utils/helpers/someAsync.ts +11 -11
  155. package/src/utils/initializer/exposeRoutes.ts +56 -2
  156. package/src/utils/initializer/registerPlugins.ts +11 -13
  157. package/src/utils/roles/helpers.ts +22 -15
  158. package/src/utils/roles/interface.ts +2 -3
  159. package/src/utils/roles/machines/commonValidators.ts +23 -14
  160. package/src/utils/roles/machines/index.ts +7 -4
  161. package/src/utils/roles/machines/interface.ts +17 -5
  162. package/src/utils/roles/machines/machine.ts +97 -72
  163. package/src/utils/roles/machines/read/A/index.ts +12 -4
  164. package/src/utils/roles/machines/read/B/index.ts +20 -7
  165. package/src/utils/roles/machines/read/C/index.ts +24 -8
  166. package/src/utils/roles/machines/read/D/index.ts +14 -8
  167. package/src/utils/roles/machines/read/D/validators.ts +21 -13
  168. package/src/utils/roles/machines/read/index.ts +5 -5
  169. package/src/utils/roles/machines/utils.ts +30 -13
  170. package/src/utils/roles/machines/write/A/index.ts +13 -5
  171. package/src/utils/roles/machines/write/B/index.ts +42 -16
  172. package/src/utils/roles/machines/write/C/index.ts +14 -8
  173. package/src/utils/roles/machines/write/C/validators.ts +21 -13
  174. package/src/utils/roles/machines/write/index.ts +4 -4
  175. package/src/utils/rules-matcher/interface.ts +4 -4
  176. package/src/utils/rules-matcher/utils.ts +10 -6
  177. package/src/utils/rules.ts +12 -5
  178. package/src/global.d.ts +0 -0
@@ -1,57 +1,71 @@
1
- import { someAsync } from '../helpers/someAsync';
2
- import { evaluateExpression } from '../roles/helpers';
3
- import { MachineContext } from '../roles/machines/interface';
4
- import { evaluateDocumentFiltersWriteFn } from '../roles/machines/read/B/validators';
1
+ import { someAsync } from '../helpers/someAsync'
2
+ import { evaluateExpression } from '../roles/helpers'
3
+ import { MachineContext } from '../roles/machines/interface'
4
+ import { evaluateDocumentFiltersWriteFn } from '../roles/machines/read/B/validators'
5
5
 
6
6
  const mockContext = {
7
- params: { type: 'write' },
8
- role: { document_filters: { write: 'someFilterExpression' } },
9
- user: { id: 'user123', name: 'Test User' },
10
- } as unknown as MachineContext;
7
+ params: { type: 'write' },
8
+ role: { document_filters: { write: 'someFilterExpression' } },
9
+ user: { id: 'user123', name: 'Test User' }
10
+ } as unknown as MachineContext
11
11
 
12
12
  jest.mock('../helpers/someAsync', () => ({
13
- someAsync: jest.fn().mockImplementation(async (array: Array<unknown>, callback: (item: unknown) => Promise<boolean>) => {
13
+ someAsync: jest
14
+ .fn()
15
+ .mockImplementation(
16
+ async (array: Array<unknown>, callback: (item: unknown) => Promise<boolean>) => {
14
17
  for (const item of array) {
15
- if (await callback(item)) return true;
18
+ if (await callback(item)) return true
16
19
  }
17
- return false;
18
- })
19
- }));
20
+ return false
21
+ }
22
+ )
23
+ }))
20
24
 
21
25
  jest.mock('../roles/helpers', () => ({
22
- evaluateExpression: jest.fn(),
23
- }));
26
+ evaluateExpression: jest.fn()
27
+ }))
24
28
 
25
29
  describe('evaluateDocumentFiltersWriteFn', () => {
26
- beforeEach(() => {
27
- jest.clearAllMocks();
28
- });
29
-
30
- it('should return true if at least one filter evaluates to true', async () => {
31
- (evaluateExpression as jest.Mock).mockResolvedValue(true);
32
- const result = await evaluateDocumentFiltersWriteFn(mockContext);
33
- expect(result).toBe(true);
34
- expect(evaluateExpression).toHaveBeenCalledWith(mockContext.params, mockContext.role.document_filters?.write, mockContext.user);
35
- expect(someAsync).toHaveBeenCalled();
36
- });
37
-
38
- it('should return false if no filters evaluate to true', async () => {
39
- (evaluateExpression as jest.Mock).mockResolvedValue(false);
40
- const result = await evaluateDocumentFiltersWriteFn(mockContext);
41
- expect(result).toBe(false);
42
- });
43
-
44
- it('should return false if type is not in valid', async () => {
45
- const mockClone = { ...mockContext, params: { type: 'test' as MachineContext["params"]["type"] } } as MachineContext
46
- const result = await evaluateDocumentFiltersWriteFn(mockClone);
47
- expect(result).toBe(false);
48
- expect(evaluateExpression).not.toHaveBeenCalled();
49
- });
50
-
51
- it('should return false if role has no write filter', async () => {
52
- const mockClone = { ...mockContext, role: { ...mockContext.role, document_filters: {} } } as MachineContext
53
- const result = await evaluateDocumentFiltersWriteFn(mockClone);
54
- expect(result).toBe(false);
55
- expect(evaluateExpression).not.toHaveBeenCalled();
56
- });
57
- });
30
+ beforeEach(() => {
31
+ jest.clearAllMocks()
32
+ })
33
+
34
+ it('should return true if at least one filter evaluates to true', async () => {
35
+ (evaluateExpression as jest.Mock).mockResolvedValue(true)
36
+ const result = await evaluateDocumentFiltersWriteFn(mockContext)
37
+ expect(result).toBe(true)
38
+ expect(evaluateExpression).toHaveBeenCalledWith(
39
+ mockContext.params,
40
+ mockContext.role.document_filters?.write,
41
+ mockContext.user
42
+ )
43
+ expect(someAsync).toHaveBeenCalled()
44
+ })
45
+
46
+ it('should return false if no filters evaluate to true', async () => {
47
+ (evaluateExpression as jest.Mock).mockResolvedValue(false)
48
+ const result = await evaluateDocumentFiltersWriteFn(mockContext)
49
+ expect(result).toBe(false)
50
+ })
51
+
52
+ it('should return false if type is not in valid', async () => {
53
+ const mockClone = {
54
+ ...mockContext,
55
+ params: { type: 'test' as MachineContext['params']['type'] }
56
+ } as MachineContext
57
+ const result = await evaluateDocumentFiltersWriteFn(mockClone)
58
+ expect(result).toBe(false)
59
+ expect(evaluateExpression).not.toHaveBeenCalled()
60
+ })
61
+
62
+ it('should return false if role has no write filter', async () => {
63
+ const mockClone = {
64
+ ...mockContext,
65
+ role: { ...mockContext.role, document_filters: {} }
66
+ } as MachineContext
67
+ const result = await evaluateDocumentFiltersWriteFn(mockClone)
68
+ expect(result).toBe(false)
69
+ expect(evaluateExpression).not.toHaveBeenCalled()
70
+ })
71
+ })
@@ -1,58 +1,58 @@
1
- import { evaluateExpression } from '../roles/helpers';
2
- import { Params, Role } from '../roles/interface';
3
- import { evaluateTopLevelReadFn } from '../roles/machines/read/C/validators';
1
+ import { evaluateExpression } from '../roles/helpers'
2
+ import { Params, Role } from '../roles/interface'
3
+ import { evaluateTopLevelReadFn } from '../roles/machines/read/C/validators'
4
4
 
5
5
  jest.mock('../roles/helpers', () => ({
6
- evaluateExpression: jest.fn(),
7
- }));
6
+ evaluateExpression: jest.fn()
7
+ }))
8
8
 
9
9
  const mockedRole = {} as Role
10
10
 
11
11
  const mockUser = {}
12
12
  const mockParams = {
13
- type: "read"
13
+ type: 'read'
14
14
  } as Params
15
15
 
16
16
  describe('evaluateTopLevelReadFn', () => {
17
- beforeEach(() => {
18
- jest.clearAllMocks();
19
- });
20
- it('should return false if type is different from read', async () => {
21
- const isValid = await evaluateTopLevelReadFn({
22
- role: mockedRole,
23
- user: mockUser,
24
- params: { type: "write" } as Params
25
- })
26
- expect(isValid).toBe(false)
27
- expect(evaluateExpression).not.toHaveBeenCalled()
28
- });
29
- it('should return undefined if type is read and role read is not defined', async () => {
30
- const isValid = await evaluateTopLevelReadFn({
31
- role: mockedRole,
32
- user: mockUser,
33
- params: mockParams
34
- })
35
- expect(isValid).toBe(undefined)
36
- expect(evaluateExpression).not.toHaveBeenCalled()
37
- });
38
- it('should return false if type is read and role read defined but evaluate expression returns false', async () => {
39
- (evaluateExpression as jest.Mock).mockResolvedValueOnce(false)
40
- const isValid = await evaluateTopLevelReadFn({
41
- role: { ...mockedRole, read: false },
42
- user: mockUser,
43
- params: mockParams
44
- })
45
- expect(isValid).toBe(false)
46
- expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
47
- });
48
- it('should return true if type is read and role read defined and evaluate expression returns true', async () => {
49
- (evaluateExpression as jest.Mock).mockResolvedValueOnce(true)
50
- const isValid = await evaluateTopLevelReadFn({
51
- role: { ...mockedRole, read: false },
52
- user: mockUser,
53
- params: mockParams
54
- })
55
- expect(isValid).toBe(true)
56
- expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
57
- });
58
- });
17
+ beforeEach(() => {
18
+ jest.clearAllMocks()
19
+ })
20
+ it('should return false if type is different from read', async () => {
21
+ const isValid = await evaluateTopLevelReadFn({
22
+ role: mockedRole,
23
+ user: mockUser,
24
+ params: { type: 'write' } as Params
25
+ })
26
+ expect(isValid).toBe(false)
27
+ expect(evaluateExpression).not.toHaveBeenCalled()
28
+ })
29
+ it('should return undefined if type is read and role read is not defined', async () => {
30
+ const isValid = await evaluateTopLevelReadFn({
31
+ role: mockedRole,
32
+ user: mockUser,
33
+ params: mockParams
34
+ })
35
+ expect(isValid).toBe(undefined)
36
+ expect(evaluateExpression).not.toHaveBeenCalled()
37
+ })
38
+ it('should return false if type is read and role read defined but evaluate expression returns false', async () => {
39
+ (evaluateExpression as jest.Mock).mockResolvedValueOnce(false)
40
+ const isValid = await evaluateTopLevelReadFn({
41
+ role: { ...mockedRole, read: false },
42
+ user: mockUser,
43
+ params: mockParams
44
+ })
45
+ expect(isValid).toBe(false)
46
+ expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
47
+ })
48
+ it('should return true if type is read and role read defined and evaluate expression returns true', async () => {
49
+ (evaluateExpression as jest.Mock).mockResolvedValueOnce(true)
50
+ const isValid = await evaluateTopLevelReadFn({
51
+ role: { ...mockedRole, read: false },
52
+ user: mockUser,
53
+ params: mockParams
54
+ })
55
+ expect(isValid).toBe(true)
56
+ expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
57
+ })
58
+ })
@@ -1,66 +1,66 @@
1
- import { evaluateExpression } from '../roles/helpers';
2
- import { Params, Role } from '../roles/interface';
3
- import { evaluateTopLevelWriteFn } from '../roles/machines/read/C/validators';
1
+ import { evaluateExpression } from '../roles/helpers'
2
+ import { Params, Role } from '../roles/interface'
3
+ import { evaluateTopLevelWriteFn } from '../roles/machines/read/C/validators'
4
4
 
5
5
  jest.mock('../roles/helpers', () => ({
6
- evaluateExpression: jest.fn(),
7
- }));
6
+ evaluateExpression: jest.fn()
7
+ }))
8
8
 
9
9
  const mockedRole = {} as Role
10
10
 
11
11
  const mockUser = {}
12
12
  const mockParams = {
13
- type: "read"
13
+ type: 'read'
14
14
  } as Params
15
15
 
16
16
  describe('evaluateTopLevelWriteFn', () => {
17
- it('should return undefined if type is different from read and write', async () => {
18
- const isValid = await evaluateTopLevelWriteFn({
19
- role: mockedRole,
20
- user: mockUser,
21
- params: { type: "delete" } as Params
22
- })
23
- expect(isValid).toBe(undefined)
24
- expect(evaluateExpression).not.toHaveBeenCalled()
25
- });
26
- it('should return false if type is read but evaluate expression returns false', async () => {
27
- (evaluateExpression as jest.Mock).mockResolvedValueOnce(false)
28
- const isValid = await evaluateTopLevelWriteFn({
29
- role: { ...mockedRole, write: false },
30
- user: mockUser,
31
- params: mockParams
32
- })
33
- expect(isValid).toBe(false)
34
- expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
35
- });
36
- it('should return true if type is read and evaluate expression returns true', async () => {
37
- (evaluateExpression as jest.Mock).mockResolvedValueOnce(true)
38
- const isValid = await evaluateTopLevelWriteFn({
39
- role: { ...mockedRole, write: false },
40
- user: mockUser,
41
- params: mockParams
42
- })
43
- expect(isValid).toBe(true)
44
- expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
45
- });
46
- it('should return false if type is write but evaluate expression returns false', async () => {
47
- (evaluateExpression as jest.Mock).mockResolvedValueOnce(false)
48
- const isValid = await evaluateTopLevelWriteFn({
49
- role: { ...mockedRole, write: false },
50
- user: mockUser,
51
- params: { type: "write" } as Params
52
- })
53
- expect(isValid).toBe(false)
54
- expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
55
- });
56
- it('should return true if type is write and evaluate expression returns true', async () => {
57
- (evaluateExpression as jest.Mock).mockResolvedValueOnce(true)
58
- const isValid = await evaluateTopLevelWriteFn({
59
- role: { ...mockedRole, write: false },
60
- user: mockUser,
61
- params: { type: "write" } as Params
62
- })
63
- expect(isValid).toBe(true)
64
- expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
65
- });
66
- });
17
+ it('should return undefined if type is different from read and write', async () => {
18
+ const isValid = await evaluateTopLevelWriteFn({
19
+ role: mockedRole,
20
+ user: mockUser,
21
+ params: { type: 'delete' } as Params
22
+ })
23
+ expect(isValid).toBe(undefined)
24
+ expect(evaluateExpression).not.toHaveBeenCalled()
25
+ })
26
+ it('should return false if type is read but evaluate expression returns false', async () => {
27
+ (evaluateExpression as jest.Mock).mockResolvedValueOnce(false)
28
+ const isValid = await evaluateTopLevelWriteFn({
29
+ role: { ...mockedRole, write: false },
30
+ user: mockUser,
31
+ params: mockParams
32
+ })
33
+ expect(isValid).toBe(false)
34
+ expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
35
+ })
36
+ it('should return true if type is read and evaluate expression returns true', async () => {
37
+ (evaluateExpression as jest.Mock).mockResolvedValueOnce(true)
38
+ const isValid = await evaluateTopLevelWriteFn({
39
+ role: { ...mockedRole, write: false },
40
+ user: mockUser,
41
+ params: mockParams
42
+ })
43
+ expect(isValid).toBe(true)
44
+ expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
45
+ })
46
+ it('should return false if type is write but evaluate expression returns false', async () => {
47
+ (evaluateExpression as jest.Mock).mockResolvedValueOnce(false)
48
+ const isValid = await evaluateTopLevelWriteFn({
49
+ role: { ...mockedRole, write: false },
50
+ user: mockUser,
51
+ params: { type: 'write' } as Params
52
+ })
53
+ expect(isValid).toBe(false)
54
+ expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
55
+ })
56
+ it('should return true if type is write and evaluate expression returns true', async () => {
57
+ (evaluateExpression as jest.Mock).mockResolvedValueOnce(true)
58
+ const isValid = await evaluateTopLevelWriteFn({
59
+ role: { ...mockedRole, write: false },
60
+ user: mockUser,
61
+ params: { type: 'write' } as Params
62
+ })
63
+ expect(isValid).toBe(true)
64
+ expect(evaluateExpression).toHaveBeenCalledWith(mockParams, false, mockUser)
65
+ })
66
+ })
@@ -3,63 +3,65 @@ import { API_VERSION } from '../../constants'
3
3
  import { exposeRoutes } from '../initializer/exposeRoutes'
4
4
 
5
5
  jest.mock('../../constants', () => ({
6
- API_VERSION: '/api/client/v2'
6
+ API_VERSION: '/api/client/v2'
7
7
  }))
8
8
 
9
9
  const config: {
10
- app?: FastifyInstance
10
+ app?: FastifyInstance
11
11
  } = {}
12
12
 
13
13
  describe('exposeRoutes', () => {
14
+ beforeAll(async () => {
15
+ config.app = Fastify()
16
+ await exposeRoutes(config.app)
17
+ await config.app.ready()
18
+ jest.clearAllMocks()
19
+ })
14
20
 
15
- beforeAll(async () => {
16
- config.app = Fastify()
17
- await exposeRoutes(config.app)
18
- await config.app.ready()
19
- jest.clearAllMocks();
20
- })
21
+ afterAll(async () => {
22
+ await config.app!.close()
23
+ })
21
24
 
22
- afterAll(async () => {
23
- await config.app!.close()
25
+ it('GET /health should return status ok and uptime', async () => {
26
+ const response = await config.app!.inject({
27
+ method: 'GET',
28
+ url: '/health'
24
29
  })
30
+ expect(response.statusCode).toBe(200)
31
+ const body = JSON.parse(response.body)
32
+ expect(body).toHaveProperty('status', 'ok')
33
+ expect(body).toHaveProperty('uptime')
34
+ expect(typeof body.uptime).toBe('number')
35
+ })
25
36
 
26
- it('GET /health should return status ok and uptime', async () => {
27
- const response = await config.app!.inject({
28
- method: 'GET',
29
- url: '/health',
30
- })
31
- expect(response.statusCode).toBe(200)
32
- const body = JSON.parse(response.body)
33
- expect(body).toHaveProperty('status', 'ok')
34
- expect(body).toHaveProperty('uptime')
35
- expect(typeof body.uptime).toBe('number')
37
+ it(`GET ${API_VERSION}/app/:appId/location should return correct location data`, async () => {
38
+ const appId = 'it'
39
+ const response = await config.app!.inject({
40
+ method: 'GET',
41
+ url: `${API_VERSION}/app/${appId}/location`
36
42
  })
37
43
 
38
- it(`GET ${API_VERSION}/app/:appId/location should return correct location data`, async () => {
39
- const appId = 'it'
40
- const response = await config.app!.inject({
41
- method: 'GET',
42
- url: `${API_VERSION}/app/${appId}/location`,
43
- })
44
-
45
- expect(response.statusCode).toBe(200)
46
- expect(JSON.parse(response.body)).toEqual({
47
- deployment_model: 'LOCAL',
48
- location: 'IE',
49
- hostname: 'http://localhost:3000',
50
- ws_hostname: 'wss://localhost:3000'
51
- })
44
+ expect(response.statusCode).toBe(200)
45
+ expect(JSON.parse(response.body)).toEqual({
46
+ deployment_model: 'LOCAL',
47
+ location: 'IE',
48
+ hostname: 'http://localhost:3000',
49
+ ws_hostname: 'wss://localhost:3000'
52
50
  })
51
+ })
53
52
 
54
- it('exposeRoutes should handle errors in the catch block', async () => {
55
- const mockedApp = Fastify()
56
- // Forced fail on get method
57
- jest.spyOn(mockedApp, 'get').mockImplementation(() => {
58
- throw new Error('Route registration failed')
59
- })
60
- const mockErrorLog = jest.spyOn(console, 'error').mockImplementation(() => { })
61
- await exposeRoutes(mockedApp)
62
- expect(mockErrorLog).toHaveBeenCalledWith('Error while exposing routes', 'Route registration failed')
63
- mockErrorLog.mockRestore()
53
+ it('exposeRoutes should handle errors in the catch block', async () => {
54
+ const mockedApp = Fastify()
55
+ // Forced fail on get method
56
+ jest.spyOn(mockedApp, 'get').mockImplementation(() => {
57
+ throw new Error('Route registration failed')
64
58
  })
59
+ const mockErrorLog = jest.spyOn(console, 'error').mockImplementation(() => {})
60
+ await exposeRoutes(mockedApp)
61
+ expect(mockErrorLog).toHaveBeenCalledWith(
62
+ 'Error while exposing routes',
63
+ 'Route registration failed'
64
+ )
65
+ mockErrorLog.mockRestore()
66
+ })
65
67
  })
@@ -1,25 +1,25 @@
1
1
  import Fastify from 'fastify'
2
- import { User } from '../../auth/dtos';
3
- import { Functions } from '../../features/functions/interface';
2
+ import { User } from '../../auth/dtos'
3
+ import { Functions } from '../../features/functions/interface'
4
4
  import { Rules } from '../../features/rules/interface'
5
- import { services } from "../../services";
6
- import { generateContextData } from "../context/helpers";
5
+ import { services } from '../../services'
6
+ import { generateContextData } from '../context/helpers'
7
7
 
8
- const originalEnv = process.env;
8
+ const originalEnv = process.env
9
9
 
10
10
  jest.mock('../../services', () => ({
11
- services: {
12
- api: jest.fn(),
13
- aws: jest.fn(),
14
- 'mongodb-atlas': jest.fn(),
15
- },
16
- }));
11
+ services: {
12
+ api: jest.fn(),
13
+ aws: jest.fn(),
14
+ 'mongodb-atlas': jest.fn()
15
+ }
16
+ }))
17
17
 
18
18
  const mockFunctions = {
19
- test: {
20
- name: "test",
21
- code: "test"
22
- }
19
+ test: {
20
+ name: 'test',
21
+ code: 'test'
22
+ }
23
23
  } as Functions
24
24
 
25
25
  const currentFunction = mockFunctions.test
@@ -27,49 +27,55 @@ const GenerateContextMock = jest.fn()
27
27
  const mockUser = {} as User
28
28
  const mockRules = {} as Rules
29
29
  const mockEnv = {
30
- ...originalEnv,
31
- test: "someTestVariable",
32
- NODE_ENV: "dev",
33
- };
34
-
30
+ ...originalEnv,
31
+ test: 'someTestVariable',
32
+ NODE_ENV: 'dev'
33
+ }
35
34
 
36
35
  describe('generateContextData', () => {
37
- beforeEach(() => {
38
- jest.resetModules();
39
- process.env = mockEnv
40
- jest.clearAllMocks();
41
- });
42
-
43
- afterEach(() => {
44
- process.env = originalEnv;
45
- });
36
+ beforeEach(() => {
37
+ jest.resetModules()
38
+ process.env = mockEnv
39
+ jest.clearAllMocks()
40
+ })
46
41
 
47
- it('should return an object with context configuration', async () => {
42
+ afterEach(() => {
43
+ process.env = originalEnv
44
+ })
48
45
 
49
- const mockApp = Fastify()
50
- const { context, console: contextConsole } = generateContextData({ services, app: mockApp, functionsList: mockFunctions, currentFunction, GenerateContext: GenerateContextMock, user: mockUser, rules: mockRules })
51
- expect(context.user).toEqual(mockUser)
46
+ it('should return an object with context configuration', async () => {
47
+ const mockApp = Fastify()
48
+ const { context, console: contextConsole } = generateContextData({
49
+ services,
50
+ app: mockApp,
51
+ functionsList: mockFunctions,
52
+ currentFunction,
53
+ GenerateContext: GenerateContextMock,
54
+ user: mockUser,
55
+ rules: mockRules
56
+ })
57
+ expect(context.user).toEqual(mockUser)
52
58
 
53
- const testVariable = context.values.get("test")
54
- expect(testVariable).toBe(mockEnv.test)
59
+ const testVariable = context.values.get('test')
60
+ expect(testVariable).toBe(mockEnv.test)
55
61
 
56
- expect(context.environment.tag).toBe(mockEnv.NODE_ENV)
62
+ expect(context.environment.tag).toBe(mockEnv.NODE_ENV)
57
63
 
58
- expect(context.user).toEqual(mockUser)
64
+ expect(context.user).toEqual(mockUser)
59
65
 
60
- const mockedLog = jest.spyOn(console, 'log').mockImplementation(() => { });
61
- contextConsole.log('Test', 'generateContextData')
62
- expect(mockedLog).toHaveBeenCalledWith('Test', 'generateContextData');
63
- mockedLog.mockRestore();
66
+ const mockedLog = jest.spyOn(console, 'log').mockImplementation(() => {})
67
+ contextConsole.log('Test', 'generateContextData')
68
+ expect(mockedLog).toHaveBeenCalledWith('Test', 'generateContextData')
69
+ mockedLog.mockRestore()
64
70
 
65
- context.services.get("api")
66
- expect(services.api).toHaveBeenCalled()
67
- const mockErrorLog = jest.spyOn(console, 'error').mockImplementation(() => { })
68
- context.services.get("notfound" as keyof typeof services)
69
- expect(mockErrorLog).toHaveBeenCalled()
70
- mockErrorLog.mockRestore()
71
+ context.services.get('api')
72
+ expect(services.api).toHaveBeenCalled()
73
+ const mockErrorLog = jest.spyOn(console, 'error').mockImplementation(() => {})
74
+ context.services.get('notfound' as keyof typeof services)
75
+ expect(mockErrorLog).toHaveBeenCalled()
76
+ mockErrorLog.mockRestore()
71
77
 
72
- context.functions.execute("test")
73
- expect(GenerateContextMock).toHaveBeenCalled()
74
- });
75
- });
78
+ context.functions.execute('test')
79
+ expect(GenerateContextMock).toHaveBeenCalled()
80
+ })
81
+ })