@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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +1 -1
- package/README.md +466 -7
- package/dist/auth/controller.d.ts.map +1 -1
- package/dist/auth/controller.js +11 -1
- package/dist/auth/providers/local-userpass/controller.d.ts.map +1 -1
- package/dist/auth/providers/local-userpass/controller.js +15 -15
- package/dist/auth/utils.d.ts +2 -1
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/auth/utils.js +14 -10
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +3 -3
- package/dist/features/endpoints/utils.d.ts +1 -1
- package/dist/features/endpoints/utils.d.ts.map +1 -1
- package/dist/features/endpoints/utils.js +5 -2
- package/dist/features/functions/controller.d.ts.map +1 -1
- package/dist/features/functions/controller.js +12 -8
- package/dist/features/functions/dtos.d.ts +2 -1
- package/dist/features/functions/dtos.d.ts.map +1 -1
- package/dist/features/functions/interface.d.ts +2 -1
- package/dist/features/functions/interface.d.ts.map +1 -1
- package/dist/features/functions/utils.d.ts +1 -1
- package/dist/features/functions/utils.d.ts.map +1 -1
- package/dist/features/functions/utils.js +4 -4
- package/dist/features/triggers/index.d.ts.map +1 -1
- package/dist/features/triggers/index.js +9 -3
- package/dist/features/triggers/interface.d.ts +4 -4
- package/dist/features/triggers/interface.d.ts.map +1 -1
- package/dist/features/triggers/utils.d.ts.map +1 -1
- package/dist/features/triggers/utils.js +25 -14
- package/dist/global.d.ts +9 -0
- package/dist/global.d.ts.map +1 -0
- package/dist/global.js +2 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -9
- package/dist/model.d.ts +1 -0
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +15 -0
- package/dist/services/api/index.d.ts.map +1 -1
- package/dist/services/api/index.js +6 -1
- package/dist/services/aws/index.d.ts.map +1 -1
- package/dist/services/aws/index.js +5 -5
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/index.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/index.js +76 -71
- package/dist/services/mongodb-atlas/model.d.ts +3 -3
- package/dist/services/mongodb-atlas/model.d.ts.map +1 -1
- package/dist/services/mongodb-atlas/utils.d.ts.map +1 -1
- package/dist/state.d.ts +2 -2
- package/dist/state.d.ts.map +1 -1
- package/dist/utils/context/helpers.d.ts +4 -4
- package/dist/utils/context/helpers.d.ts.map +1 -1
- package/dist/utils/context/helpers.js +1 -1
- package/dist/utils/context/index.d.ts.map +1 -1
- package/dist/utils/context/index.js +3 -5
- package/dist/utils/helpers/someAsync.d.ts.map +1 -1
- package/dist/utils/initializer/exposeRoutes.d.ts.map +1 -1
- package/dist/utils/initializer/exposeRoutes.js +44 -1
- package/dist/utils/initializer/registerPlugins.d.ts.map +1 -1
- package/dist/utils/initializer/registerPlugins.js +9 -9
- package/dist/utils/roles/helpers.d.ts.map +1 -1
- package/dist/utils/roles/helpers.js +9 -7
- package/dist/utils/roles/interface.d.ts.map +1 -1
- package/dist/utils/roles/machines/commonValidators.d.ts +2 -2
- package/dist/utils/roles/machines/commonValidators.d.ts.map +1 -1
- package/dist/utils/roles/machines/commonValidators.js +5 -6
- package/dist/utils/roles/machines/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/interface.d.ts.map +1 -1
- package/dist/utils/roles/machines/machine.d.ts +3 -3
- package/dist/utils/roles/machines/machine.d.ts.map +1 -1
- package/dist/utils/roles/machines/machine.js +12 -4
- package/dist/utils/roles/machines/read/A/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/A/index.js +12 -2
- package/dist/utils/roles/machines/read/B/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/B/index.js +20 -5
- package/dist/utils/roles/machines/read/C/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/C/index.js +20 -5
- package/dist/utils/roles/machines/read/D/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/D/index.js +13 -3
- package/dist/utils/roles/machines/read/D/validators.d.ts +1 -1
- package/dist/utils/roles/machines/read/D/validators.d.ts.map +1 -1
- package/dist/utils/roles/machines/read/D/validators.js +8 -4
- package/dist/utils/roles/machines/utils.d.ts +4 -4
- package/dist/utils/roles/machines/utils.d.ts.map +1 -1
- package/dist/utils/roles/machines/utils.js +5 -1
- package/dist/utils/roles/machines/write/A/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/write/A/index.js +13 -3
- package/dist/utils/roles/machines/write/B/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/write/B/index.js +37 -10
- package/dist/utils/roles/machines/write/C/index.d.ts.map +1 -1
- package/dist/utils/roles/machines/write/C/index.js +13 -3
- package/dist/utils/roles/machines/write/C/validators.d.ts +1 -1
- package/dist/utils/roles/machines/write/C/validators.d.ts.map +1 -1
- package/dist/utils/roles/machines/write/C/validators.js +8 -4
- package/dist/utils/rules-matcher/interface.d.ts.map +1 -1
- package/dist/utils/rules-matcher/utils.d.ts.map +1 -1
- package/dist/utils/rules.d.ts.map +1 -1
- package/package.json +9 -2
- package/src/auth/controller.ts +12 -1
- package/src/auth/providers/local-userpass/controller.ts +47 -39
- package/src/auth/providers/local-userpass/dtos.ts +1 -1
- package/src/auth/utils.ts +24 -18
- package/src/constants.ts +7 -3
- package/src/features/endpoints/utils.ts +6 -4
- package/src/features/functions/controller.ts +23 -22
- package/src/features/functions/dtos.ts +9 -9
- package/src/features/functions/interface.ts +2 -1
- package/src/features/functions/utils.ts +12 -12
- package/src/features/triggers/index.ts +9 -6
- package/src/features/triggers/interface.ts +9 -10
- package/src/features/triggers/utils.ts +57 -29
- package/src/global.ts +9 -0
- package/src/index.ts +16 -11
- package/src/model.ts +3 -1
- package/src/services/api/index.ts +6 -1
- package/src/services/aws/index.ts +19 -17
- package/src/services/mongodb-atlas/index.ts +204 -163
- package/src/services/mongodb-atlas/model.ts +13 -13
- package/src/services/mongodb-atlas/utils.ts +6 -4
- package/src/state.ts +32 -20
- package/src/utils/__tests__/STEP_A_STATES.test.ts +78 -47
- package/src/utils/__tests__/STEP_B_STATES.test.ts +168 -105
- package/src/utils/__tests__/STEP_C_STATES.test.ts +150 -78
- package/src/utils/__tests__/STEP_D_STATES.test.ts +129 -86
- package/src/utils/__tests__/checkAdditionalFieldsFn.test.ts +35 -35
- package/src/utils/__tests__/checkApplyWhen.test.ts +40 -41
- package/src/utils/__tests__/checkFieldsPropertyExists.test.ts +40 -40
- package/src/utils/__tests__/checkIsValidFieldNameFn.test.ts +185 -184
- package/src/utils/__tests__/comparePassword.test.ts +27 -30
- package/src/utils/__tests__/evaluateDocumentsFiltersReadFn.test.ts +55 -47
- package/src/utils/__tests__/evaluateDocumentsFiltersWriteFn.test.ts +61 -47
- package/src/utils/__tests__/evaluateTopLevelReadFn.test.ts +48 -48
- package/src/utils/__tests__/evaluateTopLevelWriteFn.test.ts +56 -56
- package/src/utils/__tests__/exposeRoutes.test.ts +46 -44
- package/src/utils/__tests__/generateContextData.test.ts +57 -51
- package/src/utils/__tests__/getDefaultRule.test.ts +32 -27
- package/src/utils/__tests__/getKey.test.ts +10 -10
- package/src/utils/__tests__/getKeys.test.ts +10 -9
- package/src/utils/__tests__/getWinningRole.test.ts +57 -50
- package/src/utils/__tests__/hashPassword.test.ts +24 -25
- package/src/utils/__tests__/isEmpty.test.ts +14 -15
- package/src/utils/__tests__/logMachineInfo.test.ts +12 -12
- package/src/utils/__tests__/operators.test.ts +94 -96
- package/src/utils/__tests__/readFileContent.test.ts +28 -28
- package/src/utils/__tests__/registerPlugins.test.ts +44 -32
- package/src/utils/__tests__/rule.test.ts +47 -49
- package/src/utils/__tests__/rulesMatcherInterfaces.test.ts +57 -52
- package/src/utils/__tests__/rulesMatcherUtils.test.ts +64 -53
- package/src/utils/__tests__/someAsync.test.ts +46 -49
- package/src/utils/context/helpers.ts +1 -1
- package/src/utils/context/index.ts +7 -10
- package/src/utils/crypto/index.ts +2 -2
- package/src/utils/helpers/someAsync.ts +11 -11
- package/src/utils/initializer/exposeRoutes.ts +56 -2
- package/src/utils/initializer/registerPlugins.ts +11 -13
- package/src/utils/roles/helpers.ts +22 -15
- package/src/utils/roles/interface.ts +2 -3
- package/src/utils/roles/machines/commonValidators.ts +23 -14
- package/src/utils/roles/machines/index.ts +7 -4
- package/src/utils/roles/machines/interface.ts +17 -5
- package/src/utils/roles/machines/machine.ts +97 -72
- package/src/utils/roles/machines/read/A/index.ts +12 -4
- package/src/utils/roles/machines/read/B/index.ts +20 -7
- package/src/utils/roles/machines/read/C/index.ts +24 -8
- package/src/utils/roles/machines/read/D/index.ts +14 -8
- package/src/utils/roles/machines/read/D/validators.ts +21 -13
- package/src/utils/roles/machines/read/index.ts +5 -5
- package/src/utils/roles/machines/utils.ts +30 -13
- package/src/utils/roles/machines/write/A/index.ts +13 -5
- package/src/utils/roles/machines/write/B/index.ts +42 -16
- package/src/utils/roles/machines/write/C/index.ts +14 -8
- package/src/utils/roles/machines/write/C/validators.ts +21 -13
- package/src/utils/roles/machines/write/index.ts +4 -4
- package/src/utils/rules-matcher/interface.ts +4 -4
- package/src/utils/rules-matcher/utils.ts +10 -6
- package/src/utils/rules.ts +12 -5
- 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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
23
|
-
}))
|
|
26
|
+
evaluateExpression: jest.fn()
|
|
27
|
+
}))
|
|
24
28
|
|
|
25
29
|
describe('evaluateDocumentFiltersWriteFn', () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
13
|
+
type: 'read'
|
|
14
14
|
} as Params
|
|
15
15
|
|
|
16
16
|
describe('evaluateTopLevelReadFn', () => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
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
|
-
|
|
13
|
+
type: 'read'
|
|
14
14
|
} as Params
|
|
15
15
|
|
|
16
16
|
describe('evaluateTopLevelWriteFn', () => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
6
|
+
API_VERSION: '/api/client/v2'
|
|
7
7
|
}))
|
|
8
8
|
|
|
9
9
|
const config: {
|
|
10
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
await config.app.ready()
|
|
19
|
-
jest.clearAllMocks();
|
|
20
|
-
})
|
|
21
|
+
afterAll(async () => {
|
|
22
|
+
await config.app!.close()
|
|
23
|
+
})
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
|
6
|
-
import { generateContextData } from
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
30
|
+
...originalEnv,
|
|
31
|
+
test: 'someTestVariable',
|
|
32
|
+
NODE_ENV: 'dev'
|
|
33
|
+
}
|
|
35
34
|
|
|
36
35
|
describe('generateContextData', () => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
process.env = originalEnv
|
|
44
|
+
})
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
54
|
-
|
|
59
|
+
const testVariable = context.values.get('test')
|
|
60
|
+
expect(testVariable).toBe(mockEnv.test)
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
expect(context.environment.tag).toBe(mockEnv.NODE_ENV)
|
|
57
63
|
|
|
58
|
-
|
|
64
|
+
expect(context.user).toEqual(mockUser)
|
|
59
65
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
})
|
|
78
|
+
context.functions.execute('test')
|
|
79
|
+
expect(GenerateContextMock).toHaveBeenCalled()
|
|
80
|
+
})
|
|
81
|
+
})
|