@flowerforce/flowerbase 1.0.1-beta.3

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 (292) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/LICENSE +3 -0
  3. package/README.md +18 -0
  4. package/dist/auth/controller.d.ts +8 -0
  5. package/dist/auth/controller.d.ts.map +1 -0
  6. package/dist/auth/controller.js +76 -0
  7. package/dist/auth/dtos.d.ts +6 -0
  8. package/dist/auth/dtos.d.ts.map +1 -0
  9. package/dist/auth/dtos.js +2 -0
  10. package/dist/auth/plugins/jwt.d.ts +14 -0
  11. package/dist/auth/plugins/jwt.d.ts.map +1 -0
  12. package/dist/auth/plugins/jwt.js +68 -0
  13. package/dist/auth/providers/local-userpass/controller.d.ts +8 -0
  14. package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -0
  15. package/dist/auth/providers/local-userpass/controller.js +184 -0
  16. package/dist/auth/providers/local-userpass/dtos.d.ts +35 -0
  17. package/dist/auth/providers/local-userpass/dtos.d.ts.map +1 -0
  18. package/dist/auth/providers/local-userpass/dtos.js +2 -0
  19. package/dist/auth/utils.d.ts +126 -0
  20. package/dist/auth/utils.d.ts.map +1 -0
  21. package/dist/auth/utils.js +122 -0
  22. package/dist/constants.d.ts +18 -0
  23. package/dist/constants.d.ts.map +1 -0
  24. package/dist/constants.js +34 -0
  25. package/dist/features/endpoints/index.d.ts +10 -0
  26. package/dist/features/endpoints/index.d.ts.map +1 -0
  27. package/dist/features/endpoints/index.js +31 -0
  28. package/dist/features/endpoints/interface.d.ts +27 -0
  29. package/dist/features/endpoints/interface.d.ts.map +1 -0
  30. package/dist/features/endpoints/interface.js +2 -0
  31. package/dist/features/endpoints/utils.d.ts +31 -0
  32. package/dist/features/endpoints/utils.d.ts.map +1 -0
  33. package/dist/features/endpoints/utils.js +85 -0
  34. package/dist/features/functions/controller.d.ts +9 -0
  35. package/dist/features/functions/controller.d.ts.map +1 -0
  36. package/dist/features/functions/controller.js +88 -0
  37. package/dist/features/functions/dtos.d.ts +34 -0
  38. package/dist/features/functions/dtos.d.ts.map +1 -0
  39. package/dist/features/functions/dtos.js +2 -0
  40. package/dist/features/functions/index.d.ts +9 -0
  41. package/dist/features/functions/index.d.ts.map +1 -0
  42. package/dist/features/functions/index.js +28 -0
  43. package/dist/features/functions/interface.d.ts +32 -0
  44. package/dist/features/functions/interface.d.ts.map +1 -0
  45. package/dist/features/functions/interface.js +2 -0
  46. package/dist/features/functions/utils.d.ts +23 -0
  47. package/dist/features/functions/utils.d.ts.map +1 -0
  48. package/dist/features/functions/utils.js +75 -0
  49. package/dist/features/rules/index.d.ts +1 -0
  50. package/dist/features/rules/index.d.ts.map +1 -0
  51. package/dist/features/rules/index.js +1 -0
  52. package/dist/features/rules/interface.d.ts +22 -0
  53. package/dist/features/rules/interface.d.ts.map +1 -0
  54. package/dist/features/rules/interface.js +2 -0
  55. package/dist/features/rules/utils.d.ts +3 -0
  56. package/dist/features/rules/utils.d.ts.map +1 -0
  57. package/dist/features/rules/utils.js +31 -0
  58. package/dist/features/triggers/dtos.d.ts +9 -0
  59. package/dist/features/triggers/dtos.d.ts.map +1 -0
  60. package/dist/features/triggers/dtos.js +2 -0
  61. package/dist/features/triggers/index.d.ts +10 -0
  62. package/dist/features/triggers/index.d.ts.map +1 -0
  63. package/dist/features/triggers/index.js +57 -0
  64. package/dist/features/triggers/interface.d.ts +44 -0
  65. package/dist/features/triggers/interface.d.ts.map +1 -0
  66. package/dist/features/triggers/interface.js +2 -0
  67. package/dist/features/triggers/utils.d.ts +16 -0
  68. package/dist/features/triggers/utils.d.ts.map +1 -0
  69. package/dist/features/triggers/utils.js +153 -0
  70. package/dist/index.d.ts +19 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +84 -0
  73. package/dist/model.d.ts +2 -0
  74. package/dist/model.d.ts.map +1 -0
  75. package/dist/model.js +2 -0
  76. package/dist/services/api/index.d.ts +36 -0
  77. package/dist/services/api/index.d.ts.map +1 -0
  78. package/dist/services/api/index.js +36 -0
  79. package/dist/services/api/model.d.ts +33 -0
  80. package/dist/services/api/model.d.ts.map +1 -0
  81. package/dist/services/api/model.js +2 -0
  82. package/dist/services/api/utils.d.ts +16 -0
  83. package/dist/services/api/utils.d.ts.map +1 -0
  84. package/dist/services/api/utils.js +45 -0
  85. package/dist/services/aws/index.d.ts +13 -0
  86. package/dist/services/aws/index.d.ts.map +1 -0
  87. package/dist/services/aws/index.js +50 -0
  88. package/dist/services/index.d.ts +41 -0
  89. package/dist/services/index.d.ts.map +1 -0
  90. package/dist/services/index.js +14 -0
  91. package/dist/services/interface.d.ts +3 -0
  92. package/dist/services/interface.d.ts.map +1 -0
  93. package/dist/services/interface.js +2 -0
  94. package/dist/services/mongodb-atlas/index.d.ts +4 -0
  95. package/dist/services/mongodb-atlas/index.d.ts.map +1 -0
  96. package/dist/services/mongodb-atlas/index.js +483 -0
  97. package/dist/services/mongodb-atlas/model.d.ts +39 -0
  98. package/dist/services/mongodb-atlas/model.d.ts.map +1 -0
  99. package/dist/services/mongodb-atlas/model.js +2 -0
  100. package/dist/services/mongodb-atlas/utils.d.ts +8 -0
  101. package/dist/services/mongodb-atlas/utils.d.ts.map +1 -0
  102. package/dist/services/mongodb-atlas/utils.js +33 -0
  103. package/dist/state.d.ts +6 -0
  104. package/dist/state.d.ts.map +1 -0
  105. package/dist/state.js +18 -0
  106. package/dist/utils/context/helpers.d.ts +74 -0
  107. package/dist/utils/context/helpers.d.ts.map +1 -0
  108. package/dist/utils/context/helpers.js +60 -0
  109. package/dist/utils/context/index.d.ts +14 -0
  110. package/dist/utils/context/index.d.ts.map +1 -0
  111. package/dist/utils/context/index.js +50 -0
  112. package/dist/utils/context/interface.d.ts +18 -0
  113. package/dist/utils/context/interface.d.ts.map +1 -0
  114. package/dist/utils/context/interface.js +2 -0
  115. package/dist/utils/crypto/index.d.ts +19 -0
  116. package/dist/utils/crypto/index.d.ts.map +1 -0
  117. package/dist/utils/crypto/index.js +50 -0
  118. package/dist/utils/helpers/someAsync.d.ts +12 -0
  119. package/dist/utils/helpers/someAsync.d.ts.map +1 -0
  120. package/dist/utils/helpers/someAsync.js +56 -0
  121. package/dist/utils/index.d.ts +3 -0
  122. package/dist/utils/index.d.ts.map +1 -0
  123. package/dist/utils/index.js +11 -0
  124. package/dist/utils/initializer/exposeRoutes.d.ts +8 -0
  125. package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -0
  126. package/dist/utils/initializer/exposeRoutes.js +41 -0
  127. package/dist/utils/initializer/registerPlugins.d.ts +19 -0
  128. package/dist/utils/initializer/registerPlugins.d.ts.map +1 -0
  129. package/dist/utils/initializer/registerPlugins.js +84 -0
  130. package/dist/utils/roles/helpers.d.ts +4 -0
  131. package/dist/utils/roles/helpers.d.ts.map +1 -0
  132. package/dist/utils/roles/helpers.js +47 -0
  133. package/dist/utils/roles/interface.d.ts +33 -0
  134. package/dist/utils/roles/interface.d.ts.map +1 -0
  135. package/dist/utils/roles/interface.js +2 -0
  136. package/dist/utils/roles/machines/commonValidators.d.ts +6 -0
  137. package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -0
  138. package/dist/utils/roles/machines/commonValidators.js +34 -0
  139. package/dist/utils/roles/machines/index.d.ts +14 -0
  140. package/dist/utils/roles/machines/index.d.ts.map +1 -0
  141. package/dist/utils/roles/machines/index.js +27 -0
  142. package/dist/utils/roles/machines/interface.d.ts +46 -0
  143. package/dist/utils/roles/machines/interface.d.ts.map +1 -0
  144. package/dist/utils/roles/machines/interface.js +2 -0
  145. package/dist/utils/roles/machines/machine.d.ts +15 -0
  146. package/dist/utils/roles/machines/machine.d.ts.map +1 -0
  147. package/dist/utils/roles/machines/machine.js +97 -0
  148. package/dist/utils/roles/machines/read/A/index.d.ts +3 -0
  149. package/dist/utils/roles/machines/read/A/index.d.ts.map +1 -0
  150. package/dist/utils/roles/machines/read/A/index.js +27 -0
  151. package/dist/utils/roles/machines/read/B/index.d.ts +3 -0
  152. package/dist/utils/roles/machines/read/B/index.d.ts.map +1 -0
  153. package/dist/utils/roles/machines/read/B/index.js +36 -0
  154. package/dist/utils/roles/machines/read/C/index.d.ts +3 -0
  155. package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -0
  156. package/dist/utils/roles/machines/read/C/index.js +38 -0
  157. package/dist/utils/roles/machines/read/D/index.d.ts +3 -0
  158. package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -0
  159. package/dist/utils/roles/machines/read/D/index.js +26 -0
  160. package/dist/utils/roles/machines/read/D/validators.d.ts +4 -0
  161. package/dist/utils/roles/machines/read/D/validators.d.ts.map +1 -0
  162. package/dist/utils/roles/machines/read/D/validators.js +24 -0
  163. package/dist/utils/roles/machines/read/index.d.ts +2 -0
  164. package/dist/utils/roles/machines/read/index.d.ts.map +1 -0
  165. package/dist/utils/roles/machines/read/index.js +8 -0
  166. package/dist/utils/roles/machines/utils.d.ts +37 -0
  167. package/dist/utils/roles/machines/utils.d.ts.map +1 -0
  168. package/dist/utils/roles/machines/utils.js +54 -0
  169. package/dist/utils/roles/machines/write/A/index.d.ts +3 -0
  170. package/dist/utils/roles/machines/write/A/index.d.ts.map +1 -0
  171. package/dist/utils/roles/machines/write/A/index.js +29 -0
  172. package/dist/utils/roles/machines/write/B/index.d.ts +3 -0
  173. package/dist/utils/roles/machines/write/B/index.d.ts.map +1 -0
  174. package/dist/utils/roles/machines/write/B/index.js +47 -0
  175. package/dist/utils/roles/machines/write/C/index.d.ts +3 -0
  176. package/dist/utils/roles/machines/write/C/index.d.ts.map +1 -0
  177. package/dist/utils/roles/machines/write/C/index.js +26 -0
  178. package/dist/utils/roles/machines/write/C/validators.d.ts +4 -0
  179. package/dist/utils/roles/machines/write/C/validators.d.ts.map +1 -0
  180. package/dist/utils/roles/machines/write/C/validators.js +24 -0
  181. package/dist/utils/roles/machines/write/index.d.ts +2 -0
  182. package/dist/utils/roles/machines/write/index.d.ts.map +1 -0
  183. package/dist/utils/roles/machines/write/index.js +7 -0
  184. package/dist/utils/rules-matcher/interface.d.ts +338 -0
  185. package/dist/utils/rules-matcher/interface.d.ts.map +1 -0
  186. package/dist/utils/rules-matcher/interface.js +26 -0
  187. package/dist/utils/rules-matcher/utils.d.ts +11 -0
  188. package/dist/utils/rules-matcher/utils.d.ts.map +1 -0
  189. package/dist/utils/rules-matcher/utils.js +214 -0
  190. package/dist/utils/rules.d.ts +2 -0
  191. package/dist/utils/rules.d.ts.map +1 -0
  192. package/dist/utils/rules.js +22 -0
  193. package/jest.config.ts +24 -0
  194. package/package.json +63 -0
  195. package/project.json +10 -0
  196. package/rollup.config.js +17 -0
  197. package/src/auth/controller.ts +78 -0
  198. package/src/auth/dtos.ts +6 -0
  199. package/src/auth/plugins/jwt.ts +68 -0
  200. package/src/auth/providers/local-userpass/controller.ts +226 -0
  201. package/src/auth/providers/local-userpass/dtos.ts +40 -0
  202. package/src/auth/utils.ts +165 -0
  203. package/src/babel.config.json +3 -0
  204. package/src/constants.ts +22 -0
  205. package/src/fastify.d.ts +28 -0
  206. package/src/features/endpoints/index.ts +27 -0
  207. package/src/features/endpoints/interface.ts +29 -0
  208. package/src/features/endpoints/utils.ts +72 -0
  209. package/src/features/functions/controller.ts +102 -0
  210. package/src/features/functions/dtos.ts +41 -0
  211. package/src/features/functions/index.ts +21 -0
  212. package/src/features/functions/interface.ts +38 -0
  213. package/src/features/functions/utils.ts +82 -0
  214. package/src/features/rules/index.tsx +0 -0
  215. package/src/features/rules/interface.ts +24 -0
  216. package/src/features/rules/utils.ts +20 -0
  217. package/src/features/triggers/dtos.ts +9 -0
  218. package/src/features/triggers/index.ts +34 -0
  219. package/src/features/triggers/interface.ts +44 -0
  220. package/src/features/triggers/utils.ts +157 -0
  221. package/src/global.d.ts +0 -0
  222. package/src/index.ts +75 -0
  223. package/src/model.ts +1 -0
  224. package/src/services/api/index.ts +50 -0
  225. package/src/services/api/model.ts +38 -0
  226. package/src/services/api/utils.ts +39 -0
  227. package/src/services/aws/index.ts +48 -0
  228. package/src/services/index.ts +9 -0
  229. package/src/services/interface.ts +3 -0
  230. package/src/services/mongodb-atlas/index.ts +569 -0
  231. package/src/services/mongodb-atlas/model.ts +67 -0
  232. package/src/services/mongodb-atlas/utils.ts +44 -0
  233. package/src/state.ts +24 -0
  234. package/src/utils/__tests__/STEP_A_STATES.test.ts +54 -0
  235. package/src/utils/__tests__/STEP_B_STATES.test.ts +113 -0
  236. package/src/utils/__tests__/STEP_C_STATES.test.ts +87 -0
  237. package/src/utils/__tests__/STEP_D_STATES.test.ts +93 -0
  238. package/src/utils/__tests__/checkAdditionalFieldsFn.test.ts +45 -0
  239. package/src/utils/__tests__/checkApplyWhen.test.ts +49 -0
  240. package/src/utils/__tests__/checkFieldsPropertyExists.test.ts +47 -0
  241. package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +190 -0
  242. package/src/utils/__tests__/comparePassword.test.ts +38 -0
  243. package/src/utils/__tests__/evaluateDocumentsFiltersReadFn.test.ts +57 -0
  244. package/src/utils/__tests__/evaluateDocumentsFiltersWriteFn.test.ts +57 -0
  245. package/src/utils/__tests__/evaluateTopLevelReadFn.test.ts +58 -0
  246. package/src/utils/__tests__/evaluateTopLevelWriteFn.test.ts +66 -0
  247. package/src/utils/__tests__/exposeRoutes.test.ts +65 -0
  248. package/src/utils/__tests__/generateContextData.test.ts +75 -0
  249. package/src/utils/__tests__/getDefaultRule.test.ts +29 -0
  250. package/src/utils/__tests__/getKey.test.ts +12 -0
  251. package/src/utils/__tests__/getKeys.test.ts +11 -0
  252. package/src/utils/__tests__/getWinningRole.test.ts +66 -0
  253. package/src/utils/__tests__/hashPassword.test.ts +28 -0
  254. package/src/utils/__tests__/isEmpty.test.ts +17 -0
  255. package/src/utils/__tests__/logMachineInfo.test.ts +15 -0
  256. package/src/utils/__tests__/operators.test.ts +99 -0
  257. package/src/utils/__tests__/readFileContent.test.ts +35 -0
  258. package/src/utils/__tests__/registerPlugins.test.ts +59 -0
  259. package/src/utils/__tests__/rule.test.ts +51 -0
  260. package/src/utils/__tests__/rulesMatcherInterfaces.test.ts +57 -0
  261. package/src/utils/__tests__/rulesMatcherUtils.test.ts +56 -0
  262. package/src/utils/__tests__/someAsync.test.ts +55 -0
  263. package/src/utils/context/helpers.ts +71 -0
  264. package/src/utils/context/index.ts +52 -0
  265. package/src/utils/context/interface.ts +19 -0
  266. package/src/utils/crypto/index.ts +36 -0
  267. package/src/utils/helpers/someAsync.ts +24 -0
  268. package/src/utils/index.ts +5 -0
  269. package/src/utils/initializer/exposeRoutes.ts +26 -0
  270. package/src/utils/initializer/registerPlugins.ts +97 -0
  271. package/src/utils/roles/helpers.ts +47 -0
  272. package/src/utils/roles/interface.ts +42 -0
  273. package/src/utils/roles/machines/commonValidators.ts +24 -0
  274. package/src/utils/roles/machines/index.ts +20 -0
  275. package/src/utils/roles/machines/interface.ts +46 -0
  276. package/src/utils/roles/machines/machine.ts +85 -0
  277. package/src/utils/roles/machines/read/A/index.ts +19 -0
  278. package/src/utils/roles/machines/read/B/index.ts +31 -0
  279. package/src/utils/roles/machines/read/C/index.ts +30 -0
  280. package/src/utils/roles/machines/read/D/index.ts +20 -0
  281. package/src/utils/roles/machines/read/D/validators.ts +24 -0
  282. package/src/utils/roles/machines/read/index.ts +6 -0
  283. package/src/utils/roles/machines/utils.ts +54 -0
  284. package/src/utils/roles/machines/write/A/index.ts +25 -0
  285. package/src/utils/roles/machines/write/B/index.ts +43 -0
  286. package/src/utils/roles/machines/write/C/index.ts +20 -0
  287. package/src/utils/roles/machines/write/C/validators.ts +24 -0
  288. package/src/utils/roles/machines/write/index.ts +5 -0
  289. package/src/utils/rules-matcher/interface.ts +365 -0
  290. package/src/utils/rules-matcher/utils.ts +281 -0
  291. package/src/utils/rules.ts +19 -0
  292. package/tsconfig.json +28 -0
package/src/state.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { FastifyInstance } from "fastify";
2
+ import { Endpoints } from "./features/endpoints/interface";
3
+ import { Functions } from "./features/functions/interface";
4
+ import { Rules } from "./features/rules/interface";
5
+ import { Triggers } from "./features/triggers/interface";
6
+ import { Services } from "./services/interface";
7
+
8
+ type State = { functions: Functions; triggers: Triggers; services?: Services; endpoints: Endpoints, rules: Rules, app?: FastifyInstance }
9
+
10
+ export class StateManager {
11
+ private static _state: State = {
12
+ functions: {},
13
+ triggers: [],
14
+ endpoints: [],
15
+ rules: {}
16
+ };
17
+ static select<K extends keyof typeof this._state>(key: K): NonNullable<typeof this._state[K]> {
18
+ return this._state[key] as NonNullable<typeof this._state[K]>;
19
+ }
20
+
21
+ static setData<K extends keyof typeof this._state>(key: K, value: typeof this._state[K]) {
22
+ this._state[key] = value;
23
+ }
24
+ }
@@ -0,0 +1,54 @@
1
+ import { MachineContext } from "../roles/machines/interface";
2
+ import { STEP_A_STATES } from "../roles/machines/read/A";
3
+ import * as Utils from "../roles/machines/utils";
4
+ const { evaluateSearch, checkSearchRequest } = STEP_A_STATES
5
+
6
+ const endValidation = jest.fn()
7
+ const goToNextValidationStage = jest.fn()
8
+ const next = jest.fn()
9
+
10
+
11
+ describe('STEP_A_STATES', () => {
12
+ beforeEach(() => {
13
+ jest.clearAllMocks();
14
+ });
15
+ it('checkSearchRequest should go to evaluateSearch if is a search operation', async () => {
16
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
17
+ const mockContext = {
18
+ params: {
19
+ type: "search"
20
+ },
21
+ role: {
22
+ name: "test"
23
+ }
24
+ } as MachineContext
25
+ await checkSearchRequest({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
26
+ expect(next).toHaveBeenCalledWith("evaluateSearch")
27
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "A", step: 1, stepName: "checkSearchRequest" })
28
+ mockedLogInfo.mockRestore()
29
+ });
30
+ it('checkSearchRequest should go to next stage if operation is not search', async () => {
31
+ const mockContext = {
32
+ params: {
33
+ type: "read"
34
+ },
35
+ role: {
36
+ name: "test"
37
+ }
38
+ } as MachineContext
39
+ await checkSearchRequest({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
40
+ expect(goToNextValidationStage).toHaveBeenCalled()
41
+ });
42
+ it('evaluateSearch should end a failed validation ', () => {
43
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
44
+ const mockContext = {
45
+ role: {
46
+ name: "test"
47
+ }
48
+ } as MachineContext
49
+ evaluateSearch({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
50
+ expect(endValidation).toHaveBeenCalledWith({ success: false })
51
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "A", step: 2, stepName: "evaluateSearch" })
52
+ mockedLogInfo.mockRestore()
53
+ });
54
+ });
@@ -0,0 +1,113 @@
1
+ import { MachineContext } from "../roles/machines/interface";
2
+ import { STEP_B_STATES } from "../roles/machines/read/B";
3
+ import { evaluateDocumentFiltersReadFn, evaluateDocumentFiltersWriteFn } from "../roles/machines/read/B/validators"
4
+ import * as Utils from "../roles/machines/utils";
5
+ const { checkDocumentsFilters, evaluateDocumentsFiltersRead, evaluateDocumentsFiltersWrite } = STEP_B_STATES
6
+
7
+ jest.mock('../roles/machines/B/validators', () => ({
8
+ evaluateDocumentFiltersReadFn: jest.fn(),
9
+ evaluateDocumentFiltersWriteFn: jest.fn(),
10
+ }));
11
+
12
+
13
+ const endValidation = jest.fn()
14
+ const goToNextValidationStage = jest.fn()
15
+ const next = jest.fn()
16
+
17
+ describe('STEP_B_STATES', () => {
18
+ beforeEach(() => {
19
+ jest.clearAllMocks();
20
+ });
21
+ it('checkDocumentsFilters should go to next stage if document_filters is not defined', async () => {
22
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
23
+ const mockContext = {
24
+ params: {
25
+ type: "search"
26
+ },
27
+ role: {
28
+ name: "test"
29
+ }
30
+ } as MachineContext
31
+ await checkDocumentsFilters({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
32
+ expect(goToNextValidationStage).toHaveBeenCalled()
33
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "B", step: 1, stepName: "checkDocumentsFilters" })
34
+ mockedLogInfo.mockRestore()
35
+ });
36
+ it('checkDocumentsFilters should go to evaluateDocumentsFiltersRead step if document_filters is defined', async () => {
37
+ const mockContext = {
38
+ params: {
39
+ type: "search"
40
+ },
41
+ role: {
42
+ name: "test",
43
+ document_filters: {}
44
+ }
45
+ } as MachineContext
46
+ await checkDocumentsFilters({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
47
+ expect(next).toHaveBeenCalledWith("evaluateDocumentsFiltersRead")
48
+ });
49
+ it('evaluateDocumentsFiltersRead should go to next stage if document_filters read is valid', async () => {
50
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
51
+ const mockContext = {
52
+ params: {
53
+ type: "search"
54
+ },
55
+ role: {
56
+ name: "test",
57
+ document_filters: {
58
+ read: true
59
+ }
60
+ }
61
+ } as MachineContext
62
+ (evaluateDocumentFiltersReadFn as jest.Mock).mockReturnValue(true);
63
+ await evaluateDocumentsFiltersRead({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
64
+ expect(goToNextValidationStage).toHaveBeenCalled()
65
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "B", step: 2, stepName: "evaluateDocumentsFiltersRead" })
66
+ mockedLogInfo.mockRestore()
67
+ });
68
+ it('evaluateDocumentsFiltersRead should go to evaluateDocumentsFiltersWrite if document_filters read is not defined or not valid', async () => {
69
+ const mockContext = {
70
+ params: {
71
+ type: "search"
72
+ },
73
+ role: {
74
+ name: "test",
75
+ }
76
+ } as MachineContext
77
+ (evaluateDocumentFiltersReadFn as jest.Mock).mockReturnValue(false);
78
+ await evaluateDocumentsFiltersRead({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
79
+ expect(next).toHaveBeenCalledWith("evaluateDocumentsFiltersWrite")
80
+ });
81
+ it('evaluateDocumentFiltersWriteFn should go to next stage if document_filters write is valid', async () => {
82
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
83
+ const mockContext = {
84
+ params: {
85
+ type: "search"
86
+ },
87
+ role: {
88
+ name: "test",
89
+ document_filters: {
90
+ write: true
91
+ }
92
+ }
93
+ } as MachineContext
94
+ (evaluateDocumentFiltersWriteFn as jest.Mock).mockReturnValue(true);
95
+ await evaluateDocumentsFiltersWrite({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
96
+ expect(goToNextValidationStage).toHaveBeenCalled()
97
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "B", step: 3, stepName: "evaluateDocumentsFiltersWrite" })
98
+ mockedLogInfo.mockRestore()
99
+ });
100
+ it('evaluateDocumentFiltersWriteFn should end a failed validation if document_filters write is not defined or not valid', async () => {
101
+ const mockContext = {
102
+ params: {
103
+ type: "search"
104
+ },
105
+ role: {
106
+ name: "test",
107
+ }
108
+ } as MachineContext
109
+ (evaluateDocumentFiltersWriteFn as jest.Mock).mockReturnValue(false);
110
+ await evaluateDocumentsFiltersWrite({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
111
+ expect(endValidation).toHaveBeenCalledWith({ success: false })
112
+ });
113
+ });
@@ -0,0 +1,87 @@
1
+ import { MachineContext } from "../roles/machines/interface";
2
+ import { STEP_C_STATES } from "../roles/machines/read/C";
3
+ import { checkFieldsPropertyExists, evaluateTopLevelReadFn, evaluateTopLevelWriteFn } from "../roles/machines/read/C/validators";
4
+ import * as Utils from "../roles/machines/utils";
5
+ const { evaluateTopLevelRead, checkFieldsProperty, evaluateTopLevelWrite } = STEP_C_STATES
6
+
7
+ const endValidation = jest.fn()
8
+ const goToNextValidationStage = jest.fn()
9
+ const next = jest.fn()
10
+
11
+
12
+ jest.mock('../roles/machines/C/validators', () => ({
13
+ evaluateTopLevelReadFn: jest.fn(),
14
+ checkFieldsPropertyExists: jest.fn(),
15
+ evaluateTopLevelWriteFn: jest.fn()
16
+ }));
17
+
18
+ describe('STEP_C_STATES', () => {
19
+ beforeEach(() => {
20
+ jest.clearAllMocks();
21
+ });
22
+ it('evaluateTopLevelRead should go to evaluateTopLevelWrite if evaluateTopLevelReadFn returns false ', async () => {
23
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
24
+ (evaluateTopLevelReadFn as jest.Mock).mockReturnValueOnce(false)
25
+ const mockContext = {} as MachineContext
26
+ await evaluateTopLevelRead({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
27
+ expect(next).toHaveBeenCalledWith("evaluateTopLevelWrite", { check: false })
28
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "C", step: 1, stepName: "evaluateTopLevelRead" })
29
+ mockedLogInfo.mockRestore()
30
+ });
31
+ it('evaluateTopLevelRead should go to evaluateTopLevelWrite if evaluateTopLevelReadFn returns undefined ', async () => {
32
+ (evaluateTopLevelReadFn as jest.Mock).mockReturnValueOnce(undefined)
33
+ const mockContext = {} as MachineContext
34
+ await evaluateTopLevelRead({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
35
+ expect(next).toHaveBeenCalledWith("evaluateTopLevelWrite", { check: undefined })
36
+ });
37
+ it('evaluateTopLevelRead should endValidation validation if evaluateTopLevelReadFn returns true ', async () => {
38
+ (evaluateTopLevelReadFn as jest.Mock).mockReturnValueOnce(true)
39
+ const mockContext = {} as MachineContext
40
+ await evaluateTopLevelRead({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
41
+ expect(endValidation).toHaveBeenCalledWith({ success: true })
42
+ });
43
+ it('checkFieldsProperty should go to next validation stage if checkFieldsPropertyExists returns true with initialStep checkIsValidFieldName', async () => {
44
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
45
+ (checkFieldsPropertyExists as jest.Mock).mockReturnValue(true)
46
+ const mockContext = {} as MachineContext
47
+ await checkFieldsProperty({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
48
+ expect(goToNextValidationStage).toHaveBeenCalledWith("checkIsValidFieldName")
49
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "C", step: 3, stepName: "checkFieldsProperty" })
50
+ mockedLogInfo.mockRestore()
51
+ });
52
+ it('checkFieldsProperty should go to next validation stage if checkFieldsPropertyExists returns fslse with initialStep checkAdditionalFields', async () => {
53
+ (checkFieldsPropertyExists as jest.Mock).mockReturnValue(false)
54
+ const mockContext = {} as MachineContext
55
+ await checkFieldsProperty({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
56
+ expect(goToNextValidationStage).toHaveBeenCalledWith("checkAdditionalFields")
57
+ });
58
+ it('evaluateTopLevelWrite should end a success validation if evaluateTopLevelWriteFn returns true', async () => {
59
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
60
+ (evaluateTopLevelWriteFn as jest.Mock).mockReturnValue(true)
61
+ const mockContext = {} as MachineContext
62
+ await evaluateTopLevelWrite({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
63
+ expect(endValidation).toHaveBeenCalledWith({ success: true })
64
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "C", step: 2, stepName: "evaluateTopLevelWrite" })
65
+ mockedLogInfo.mockRestore()
66
+ });
67
+ it('evaluateTopLevelWrite should end a failed validation if evaluateTopLevelWriteFn returns false and prevParams.check is false', async () => {
68
+ (evaluateTopLevelWriteFn as jest.Mock).mockReturnValue(false)
69
+ const mockContext = {
70
+ prevParams: {
71
+ check: false
72
+ }
73
+ } as unknown as MachineContext
74
+ await evaluateTopLevelWrite({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
75
+ expect(endValidation).toHaveBeenCalledWith({ success: false })
76
+ });
77
+ it('evaluateTopLevelWrite should go to next step checkFieldsProperty if evaluateTopLevelWriteFn returns false and prevParams.check is not false ', async () => {
78
+ (evaluateTopLevelWriteFn as jest.Mock).mockReturnValue(false)
79
+ const mockContext = {
80
+ prevParams: {
81
+ check: true
82
+ }
83
+ } as unknown as MachineContext
84
+ await evaluateTopLevelWrite({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
85
+ expect(next).toHaveBeenCalledWith("checkFieldsProperty")
86
+ });
87
+ });
@@ -0,0 +1,93 @@
1
+ import { Role } from "../roles/interface";
2
+ import { MachineContext } from "../roles/machines/interface";
3
+ import { STEP_D_STATES } from "../roles/machines/read/D";
4
+ import * as Utils from "../roles/machines/utils";
5
+ const { checkAdditionalFields, checkIsValidFieldName } = STEP_D_STATES
6
+
7
+ const endValidation = jest.fn()
8
+ const goToNextValidationStage = jest.fn()
9
+ const next = jest.fn()
10
+
11
+
12
+ describe('STEP_D_STATES', () => {
13
+ beforeEach(() => {
14
+ jest.clearAllMocks();
15
+ });
16
+ it('checkAdditionalFields should end validation if additional fields is not defined', () => {
17
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
18
+ const mockContext = {
19
+ role: {
20
+ name: "test"
21
+ }
22
+ } as MachineContext
23
+ checkAdditionalFields({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
24
+ expect(endValidation).toHaveBeenCalledWith({ success: false })
25
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "D", step: 1, stepName: "checkAdditionalFields" })
26
+ mockedLogInfo.mockRestore()
27
+ });
28
+ it('checkAdditionalFields should end validation if additional fields are empty', () => {
29
+ const mockContext = {
30
+ role: {
31
+ name: "test",
32
+ additional_fields: {}
33
+ }
34
+ } as MachineContext
35
+ checkAdditionalFields({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
36
+ expect(endValidation).toHaveBeenCalledWith({ success: false })
37
+
38
+ });
39
+ it('checkAdditionalFields should go to evaluateRead step if additional fields are defined and not empty', () => {
40
+ const mockContext = {
41
+ role: {
42
+ name: "test",
43
+ apply_when: {
44
+ "%%true": true
45
+ },
46
+ additional_fields: {
47
+ name: {
48
+ write: true
49
+ }
50
+ }
51
+ } as Role
52
+ } as MachineContext
53
+ checkAdditionalFields({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
54
+ expect(next).toHaveBeenCalledWith("evaluateRead")
55
+
56
+ });
57
+ it('checkIsValidFieldName should end a failed validation, with an empty document', async () => {
58
+ const mockedLogInfo = jest.spyOn(Utils, 'logMachineInfo').mockImplementation(() => 'Mocked Value');
59
+ const mockContext = {
60
+ params: {
61
+ cursor: { name: "test" }
62
+ },
63
+ role: {
64
+ name: "test"
65
+ }
66
+ } as MachineContext
67
+ await checkIsValidFieldName({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
68
+ expect(endValidation).toHaveBeenCalledWith({ success: false, document: {} })
69
+ expect(mockedLogInfo).toHaveBeenCalledWith({ enabled: mockContext.enableLog, machine: "D", step: 2, stepName: "checkIsValidFieldName" })
70
+ mockedLogInfo.mockRestore()
71
+ });
72
+ it('checkIsValidFieldName should end a success validation, with document', async () => {
73
+
74
+ const mockContext = {
75
+ params: {
76
+ cursor: { name: "test" },
77
+ },
78
+ role: {
79
+ name: "testRole",
80
+ apply_when: {
81
+ "%%true": true
82
+ },
83
+ fields: {
84
+ name: {
85
+ read: true
86
+ }
87
+ }
88
+ } as Role
89
+ } as MachineContext
90
+ await checkIsValidFieldName({ endValidation, context: mockContext, goToNextValidationStage, next, initialStep: null })
91
+ expect(endValidation).toHaveBeenCalledWith({ success: true, document: { name: "test" } })
92
+ });
93
+ });
@@ -0,0 +1,45 @@
1
+ import { Params } from '../roles/interface';
2
+ import { checkAdditionalFieldsFn } from '../roles/machines/read/D/validators';
3
+
4
+ const mockedRole = {
5
+ name: "mock",
6
+ apply_when: {},
7
+ }
8
+ const additionalFields = {
9
+ name: {
10
+ write: true
11
+ }
12
+ }
13
+
14
+ const mockUser = {}
15
+ const mockParams = {} as Params
16
+
17
+ describe('comparePassword', () => {
18
+ beforeEach(() => {
19
+ jest.clearAllMocks();
20
+ });
21
+ it('should return true for existing additional fields', () => {
22
+ const isDefined = checkAdditionalFieldsFn({
23
+ role: { ...mockedRole, additional_fields: additionalFields },
24
+ user: mockUser,
25
+ params: mockParams
26
+ })
27
+ expect(isDefined).toBe(true)
28
+ });
29
+ it('should return false for missing additional fields', () => {
30
+ const isDefined = checkAdditionalFieldsFn({
31
+ role: mockedRole,
32
+ user: mockUser,
33
+ params: mockParams
34
+ })
35
+ expect(isDefined).toBe(false)
36
+ });
37
+ it('should return false for empty additional fields', () => {
38
+ const isDefined = checkAdditionalFieldsFn({
39
+ role: { ...mockedRole, additional_fields: {} },
40
+ user: mockUser,
41
+ params: mockParams
42
+ })
43
+ expect(isDefined).toBe(false)
44
+ });
45
+ });
@@ -0,0 +1,49 @@
1
+ import { Document, ObjectId, WithId } from "mongodb"
2
+ import { User } from '../../auth/dtos';
3
+ import { getValidRule } from '../../services/mongodb-atlas/utils';
4
+ import { checkApplyWhen } from "../roles/machines/utils";
5
+
6
+ jest.mock('../../services/mongodb-atlas/utils', () => ({
7
+ getValidRule: jest.fn(),
8
+ }));
9
+
10
+ const mockUser = {
11
+ id: 'user123',
12
+ role: 'admin',
13
+ name: 'Test User',
14
+ } as User;
15
+
16
+ const mockDocument = {
17
+ _id: new ObjectId(),
18
+ field: 'value',
19
+ } as WithId<Document>;
20
+
21
+ describe('checkApplyWhen', () => {
22
+
23
+ beforeEach(() => {
24
+ jest.clearAllMocks();
25
+ });
26
+
27
+ it('should return true if getValidRule returns a valid rule', () => {
28
+ (getValidRule as jest.Mock).mockReturnValue([{ rule: 'some rule' }]);
29
+ const result = checkApplyWhen({ condition: 'test' }, mockUser, mockDocument);
30
+ expect(result).toBe(true);
31
+ expect(getValidRule).toHaveBeenCalledWith({
32
+ filters: [{ apply_when: { condition: 'test' } }],
33
+ user: mockUser,
34
+ record: mockDocument,
35
+ });
36
+ });
37
+
38
+ it('should return false if getValidRule returns an empty array', () => {
39
+ (getValidRule as jest.Mock).mockReturnValue([]);
40
+ const result = checkApplyWhen({ condition: 'test' }, mockUser, mockDocument);
41
+ expect(result).toBe(false);
42
+ });
43
+
44
+ it('should return false if document is null', () => {
45
+ (getValidRule as jest.Mock).mockReturnValue([]);
46
+ const result = checkApplyWhen({ condition: 'test' }, mockUser, null);
47
+ expect(result).toBe(false);
48
+ });
49
+ });
@@ -0,0 +1,47 @@
1
+ import { Params, Role } from '../roles/interface';
2
+ import { checkFieldsPropertyExists } from '../roles/machines/read/C/validators';
3
+
4
+ jest.mock('../roles/helpers', () => ({
5
+ evaluateExpression: jest.fn(),
6
+ }));
7
+
8
+ const mockUser = {}
9
+ const mockParams = {} as Params
10
+
11
+ describe('checkFieldsPropertyExists', () => {
12
+ beforeEach(() => {
13
+ jest.clearAllMocks();
14
+ });
15
+ it('should return false if role fields is not defined', () => {
16
+ const isValid = checkFieldsPropertyExists({
17
+ role: {} as Role,
18
+ user: mockUser,
19
+ params: mockParams
20
+ })
21
+ expect(isValid).toBe(false)
22
+ });
23
+ it('should return false if role fields is empty', () => {
24
+ const isValid = checkFieldsPropertyExists({
25
+ role: {
26
+ fields: {}
27
+ } as Role,
28
+ user: mockUser,
29
+ params: mockParams
30
+ })
31
+ expect(isValid).toBe(false)
32
+ });
33
+ it('should return true if role fields is note empty', () => {
34
+ const isValid = checkFieldsPropertyExists({
35
+ role: {
36
+ fields: {
37
+ test: {}
38
+ },
39
+ apply_when: {},
40
+ name: "test",
41
+ } as Role,
42
+ user: mockUser,
43
+ params: mockParams
44
+ })
45
+ expect(isValid).toBe(true)
46
+ });
47
+ });