@friggframework/core 2.0.0-next.45 → 2.0.0-next.47
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/README.md +28 -0
- package/application/commands/integration-commands.js +19 -0
- package/core/Worker.js +8 -21
- package/credential/repositories/credential-repository-mongo.js +14 -8
- package/credential/repositories/credential-repository-postgres.js +14 -8
- package/credential/repositories/credential-repository.js +3 -8
- package/database/MONGODB_TRANSACTION_FIX.md +198 -0
- package/database/adapters/lambda-invoker.js +97 -0
- package/database/config.js +11 -2
- package/database/models/WebsocketConnection.js +11 -10
- package/database/prisma.js +63 -3
- package/database/repositories/health-check-repository-mongodb.js +3 -0
- package/database/repositories/migration-status-repository-s3.js +137 -0
- package/database/use-cases/check-database-state-use-case.js +81 -0
- package/database/use-cases/check-encryption-health-use-case.js +3 -2
- package/database/use-cases/get-database-state-via-worker-use-case.js +61 -0
- package/database/use-cases/get-migration-status-use-case.js +93 -0
- package/database/use-cases/run-database-migration-use-case.js +137 -0
- package/database/use-cases/trigger-database-migration-use-case.js +157 -0
- package/database/utils/mongodb-collection-utils.js +91 -0
- package/database/utils/mongodb-schema-init.js +106 -0
- package/database/utils/prisma-runner.js +400 -0
- package/database/utils/prisma-schema-parser.js +182 -0
- package/encrypt/Cryptor.js +14 -16
- package/generated/prisma-mongodb/client.d.ts +1 -0
- package/generated/prisma-mongodb/client.js +4 -0
- package/generated/prisma-mongodb/default.d.ts +1 -0
- package/generated/prisma-mongodb/default.js +4 -0
- package/generated/prisma-mongodb/edge.d.ts +1 -0
- package/generated/prisma-mongodb/edge.js +334 -0
- package/generated/prisma-mongodb/index-browser.js +316 -0
- package/generated/prisma-mongodb/index.d.ts +22897 -0
- package/generated/prisma-mongodb/index.js +359 -0
- package/generated/prisma-mongodb/package.json +183 -0
- package/generated/prisma-mongodb/query-engine-debian-openssl-3.0.x +0 -0
- package/generated/prisma-mongodb/query-engine-rhel-openssl-3.0.x +0 -0
- package/generated/prisma-mongodb/runtime/binary.d.ts +1 -0
- package/generated/prisma-mongodb/runtime/binary.js +289 -0
- package/generated/prisma-mongodb/runtime/edge-esm.js +34 -0
- package/generated/prisma-mongodb/runtime/edge.js +34 -0
- package/generated/prisma-mongodb/runtime/index-browser.d.ts +370 -0
- package/generated/prisma-mongodb/runtime/index-browser.js +16 -0
- package/generated/prisma-mongodb/runtime/library.d.ts +3977 -0
- package/generated/prisma-mongodb/runtime/react-native.js +83 -0
- package/generated/prisma-mongodb/runtime/wasm-compiler-edge.js +84 -0
- package/generated/prisma-mongodb/runtime/wasm-engine-edge.js +36 -0
- package/generated/prisma-mongodb/schema.prisma +362 -0
- package/generated/prisma-mongodb/wasm-edge-light-loader.mjs +4 -0
- package/generated/prisma-mongodb/wasm-worker-loader.mjs +4 -0
- package/generated/prisma-mongodb/wasm.d.ts +1 -0
- package/generated/prisma-mongodb/wasm.js +341 -0
- package/generated/prisma-postgresql/client.d.ts +1 -0
- package/generated/prisma-postgresql/client.js +4 -0
- package/generated/prisma-postgresql/default.d.ts +1 -0
- package/generated/prisma-postgresql/default.js +4 -0
- package/generated/prisma-postgresql/edge.d.ts +1 -0
- package/generated/prisma-postgresql/edge.js +356 -0
- package/generated/prisma-postgresql/index-browser.js +338 -0
- package/generated/prisma-postgresql/index.d.ts +25071 -0
- package/generated/prisma-postgresql/index.js +381 -0
- package/generated/prisma-postgresql/package.json +183 -0
- package/generated/prisma-postgresql/query-engine-debian-openssl-3.0.x +0 -0
- package/generated/prisma-postgresql/query-engine-rhel-openssl-3.0.x +0 -0
- package/generated/prisma-postgresql/query_engine_bg.js +2 -0
- package/generated/prisma-postgresql/query_engine_bg.wasm +0 -0
- package/generated/prisma-postgresql/runtime/binary.d.ts +1 -0
- package/generated/prisma-postgresql/runtime/binary.js +289 -0
- package/generated/prisma-postgresql/runtime/edge-esm.js +34 -0
- package/generated/prisma-postgresql/runtime/edge.js +34 -0
- package/generated/prisma-postgresql/runtime/index-browser.d.ts +370 -0
- package/generated/prisma-postgresql/runtime/index-browser.js +16 -0
- package/generated/prisma-postgresql/runtime/library.d.ts +3977 -0
- package/generated/prisma-postgresql/runtime/react-native.js +83 -0
- package/generated/prisma-postgresql/runtime/wasm-compiler-edge.js +84 -0
- package/generated/prisma-postgresql/runtime/wasm-engine-edge.js +36 -0
- package/generated/prisma-postgresql/schema.prisma +345 -0
- package/generated/prisma-postgresql/wasm-edge-light-loader.mjs +4 -0
- package/generated/prisma-postgresql/wasm-worker-loader.mjs +4 -0
- package/generated/prisma-postgresql/wasm.d.ts +1 -0
- package/generated/prisma-postgresql/wasm.js +363 -0
- package/handlers/database-migration-handler.js +227 -0
- package/handlers/routers/auth.js +1 -1
- package/handlers/routers/db-migration.handler.js +29 -0
- package/handlers/routers/db-migration.js +256 -0
- package/handlers/routers/health.js +41 -6
- package/handlers/routers/integration-webhook-routers.js +2 -2
- package/handlers/use-cases/check-integrations-health-use-case.js +22 -10
- package/handlers/workers/db-migration.js +352 -0
- package/index.js +12 -0
- package/integrations/integration-router.js +60 -70
- package/integrations/repositories/integration-repository-interface.js +12 -0
- package/integrations/repositories/integration-repository-mongo.js +32 -0
- package/integrations/repositories/integration-repository-postgres.js +33 -0
- package/integrations/repositories/process-repository-postgres.js +2 -2
- package/integrations/tests/doubles/test-integration-repository.js +2 -2
- package/logs/logger.js +0 -4
- package/modules/entity.js +0 -1
- package/modules/repositories/module-repository-mongo.js +3 -12
- package/modules/repositories/module-repository-postgres.js +0 -11
- package/modules/repositories/module-repository.js +1 -12
- package/modules/use-cases/get-entity-options-by-id.js +1 -1
- package/modules/use-cases/get-module.js +1 -2
- package/modules/use-cases/refresh-entity-options.js +1 -1
- package/modules/use-cases/test-module-auth.js +1 -1
- package/package.json +82 -66
- package/prisma-mongodb/schema.prisma +21 -21
- package/prisma-postgresql/schema.prisma +15 -15
- package/queues/queuer-util.js +24 -21
- package/types/core/index.d.ts +2 -2
- package/types/module-plugin/index.d.ts +0 -2
- package/user/use-cases/authenticate-user.js +127 -0
- package/user/use-cases/authenticate-with-shared-secret.js +48 -0
- package/user/use-cases/get-user-from-adopter-jwt.js +149 -0
- package/user/use-cases/get-user-from-x-frigg-headers.js +106 -0
- package/user/user.js +16 -0
- package/websocket/repositories/websocket-connection-repository-mongo.js +11 -10
- package/websocket/repositories/websocket-connection-repository-postgres.js +11 -10
- package/websocket/repositories/websocket-connection-repository.js +11 -10
- package/application/commands/integration-commands.test.js +0 -123
- package/database/encryption/encryption-integration.test.js +0 -553
- package/database/encryption/encryption-schema-registry.test.js +0 -392
- package/database/encryption/field-encryption-service.test.js +0 -525
- package/database/encryption/mongo-decryption-fix-verification.test.js +0 -348
- package/database/encryption/postgres-decryption-fix-verification.test.js +0 -371
- package/database/encryption/postgres-relation-decryption.test.js +0 -245
- package/database/encryption/prisma-encryption-extension.test.js +0 -439
- package/errors/base-error.test.js +0 -32
- package/errors/fetch-error.test.js +0 -79
- package/errors/halt-error.test.js +0 -11
- package/errors/validation-errors.test.js +0 -120
- package/handlers/auth-flow.integration.test.js +0 -147
- package/handlers/integration-event-dispatcher.test.js +0 -209
- package/handlers/routers/health.test.js +0 -210
- package/handlers/routers/integration-webhook-routers.test.js +0 -126
- package/handlers/webhook-flow.integration.test.js +0 -356
- package/handlers/workers/integration-defined-workers.test.js +0 -184
- package/integrations/tests/use-cases/create-integration.test.js +0 -131
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -150
- package/integrations/tests/use-cases/find-integration-context-by-external-entity-id.test.js +0 -92
- package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -150
- package/integrations/tests/use-cases/get-integration-instance.test.js +0 -176
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -176
- package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -188
- package/integrations/tests/use-cases/update-integration-messages.test.js +0 -142
- package/integrations/tests/use-cases/update-integration-status.test.js +0 -103
- package/integrations/tests/use-cases/update-integration.test.js +0 -141
- package/integrations/use-cases/create-process.test.js +0 -178
- package/integrations/use-cases/get-process.test.js +0 -190
- package/integrations/use-cases/load-integration-context-full.test.js +0 -329
- package/integrations/use-cases/load-integration-context.test.js +0 -114
- package/integrations/use-cases/update-process-metrics.test.js +0 -308
- package/integrations/use-cases/update-process-state.test.js +0 -256
- package/lambda/TimeoutCatcher.test.js +0 -68
- package/logs/logger.test.js +0 -76
- package/modules/module-hydration.test.js +0 -205
- package/modules/requester/requester.test.js +0 -28
- package/user/tests/use-cases/create-individual-user.test.js +0 -24
- package/user/tests/use-cases/create-organization-user.test.js +0 -28
- package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
- package/user/tests/use-cases/login-user.test.js +0 -220
- package/user/tests/user-password-encryption-isolation.test.js +0 -237
- package/user/tests/user-password-hashing.test.js +0 -235
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
const Boom = require('@hapi/boom');
|
|
2
|
+
const { User } = require('../user');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Use case for retrieving or creating a user from x-frigg header identifiers.
|
|
6
|
+
* Supports backend-to-backend API communication using application user IDs.
|
|
7
|
+
*
|
|
8
|
+
* @class GetUserFromXFriggHeaders
|
|
9
|
+
*/
|
|
10
|
+
class GetUserFromXFriggHeaders {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new GetUserFromXFriggHeaders instance.
|
|
13
|
+
* @param {Object} params - Configuration parameters.
|
|
14
|
+
* @param {import('../repositories/user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
|
|
15
|
+
* @param {Object} params.userConfig - The user config in the app definition.
|
|
16
|
+
*/
|
|
17
|
+
constructor({ userRepository, userConfig }) {
|
|
18
|
+
this.userRepository = userRepository;
|
|
19
|
+
this.userConfig = userConfig;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Executes the use case.
|
|
24
|
+
* @async
|
|
25
|
+
* @param {string} [appUserId] - The app user ID from x-frigg-appUserId header.
|
|
26
|
+
* @param {string} [appOrgId] - The app organization ID from x-frigg-appOrgId header.
|
|
27
|
+
* @returns {Promise<import('../user').User>} The authenticated user object.
|
|
28
|
+
* @throws {Boom} 400 Bad Request if neither ID is provided or if both IDs are provided but belong to different users.
|
|
29
|
+
*/
|
|
30
|
+
async execute(appUserId, appOrgId) {
|
|
31
|
+
// At least one header must be provided
|
|
32
|
+
if (!appUserId && !appOrgId) {
|
|
33
|
+
throw Boom.badRequest(
|
|
34
|
+
'At least one of x-frigg-appUserId or x-frigg-appOrgId headers is required for backend-to-backend authentication'
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Find users by both IDs if both are provided
|
|
39
|
+
let individualUserData = null;
|
|
40
|
+
let organizationUserData = null;
|
|
41
|
+
|
|
42
|
+
if (appUserId && this.userConfig.individualUserRequired !== false) {
|
|
43
|
+
individualUserData =
|
|
44
|
+
await this.userRepository.findIndividualUserByAppUserId(
|
|
45
|
+
appUserId
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (appOrgId && this.userConfig.organizationUserRequired) {
|
|
50
|
+
organizationUserData =
|
|
51
|
+
await this.userRepository.findOrganizationUserByAppOrgId(
|
|
52
|
+
appOrgId
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// VALIDATION: If both IDs provided and both users exist, verify they match
|
|
57
|
+
if (
|
|
58
|
+
appUserId &&
|
|
59
|
+
appOrgId &&
|
|
60
|
+
individualUserData &&
|
|
61
|
+
organizationUserData
|
|
62
|
+
) {
|
|
63
|
+
// Check if individual user is linked to the org user
|
|
64
|
+
const individualOrgId =
|
|
65
|
+
individualUserData.organizationUser?.toString();
|
|
66
|
+
const expectedOrgId = organizationUserData.id?.toString();
|
|
67
|
+
|
|
68
|
+
if (individualOrgId !== expectedOrgId) {
|
|
69
|
+
throw Boom.badRequest(
|
|
70
|
+
'User ID mismatch: x-frigg-appUserId and x-frigg-appOrgId refer to different users. ' +
|
|
71
|
+
'Provide only one identifier or ensure they belong to the same user.'
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Auto-create user if not found
|
|
77
|
+
if (!individualUserData && !organizationUserData) {
|
|
78
|
+
if (appUserId) {
|
|
79
|
+
individualUserData =
|
|
80
|
+
await this.userRepository.createIndividualUser({
|
|
81
|
+
appUserId,
|
|
82
|
+
username: `app-user-${appUserId}`,
|
|
83
|
+
email: `${appUserId}@app.local`,
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
organizationUserData =
|
|
87
|
+
await this.userRepository.createOrganizationUser({
|
|
88
|
+
appOrgId,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return new User(
|
|
94
|
+
individualUserData,
|
|
95
|
+
organizationUserData,
|
|
96
|
+
this.userConfig.usePassword,
|
|
97
|
+
this.userConfig.primary,
|
|
98
|
+
this.userConfig.individualUserRequired,
|
|
99
|
+
this.userConfig.organizationUserRequired
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
module.exports = { GetUserFromXFriggHeaders };
|
|
105
|
+
|
|
106
|
+
|
package/user/user.js
CHANGED
|
@@ -72,6 +72,22 @@ class User {
|
|
|
72
72
|
getOrganizationUser() {
|
|
73
73
|
return this.organizationUser;
|
|
74
74
|
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets the appUserId from the individual user if present.
|
|
78
|
+
* @returns {string|null} The app user ID or null
|
|
79
|
+
*/
|
|
80
|
+
getAppUserId() {
|
|
81
|
+
return this.individualUser?.appUserId || null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Gets the appOrgId from the organization user if present.
|
|
86
|
+
* @returns {string|null} The app organization ID or null
|
|
87
|
+
*/
|
|
88
|
+
getAppOrgId() {
|
|
89
|
+
return this.organizationUser?.appOrgId || null;
|
|
90
|
+
}
|
|
75
91
|
}
|
|
76
92
|
|
|
77
93
|
module.exports = { User };
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const
|
|
2
|
+
const {
|
|
3
|
+
ApiGatewayManagementApiClient,
|
|
4
|
+
PostToConnectionCommand,
|
|
5
|
+
} = require('@aws-sdk/client-apigatewaymanagementapi');
|
|
3
6
|
const {
|
|
4
7
|
WebsocketConnectionRepositoryInterface,
|
|
5
8
|
} = require('./websocket-connection-repository-interface');
|
|
@@ -74,20 +77,18 @@ class WebsocketConnectionRepositoryMongo extends WebsocketConnectionRepositoryIn
|
|
|
74
77
|
return connections.map((conn) => ({
|
|
75
78
|
connectionId: conn.connectionId,
|
|
76
79
|
send: async (data) => {
|
|
77
|
-
const apigwManagementApi = new
|
|
78
|
-
apiVersion: '2018-11-29',
|
|
80
|
+
const apigwManagementApi = new ApiGatewayManagementApiClient({
|
|
79
81
|
endpoint: process.env.WEBSOCKET_API_ENDPOINT,
|
|
80
82
|
});
|
|
81
83
|
|
|
82
84
|
try {
|
|
83
|
-
|
|
84
|
-
.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
.promise();
|
|
85
|
+
const command = new PostToConnectionCommand({
|
|
86
|
+
ConnectionId: conn.connectionId,
|
|
87
|
+
Data: JSON.stringify(data),
|
|
88
|
+
});
|
|
89
|
+
await apigwManagementApi.send(command);
|
|
89
90
|
} catch (error) {
|
|
90
|
-
if (error.statusCode === 410) {
|
|
91
|
+
if (error.statusCode === 410 || error.$metadata?.httpStatusCode === 410) {
|
|
91
92
|
console.log(
|
|
92
93
|
`Stale connection ${conn.connectionId}`
|
|
93
94
|
);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const
|
|
2
|
+
const {
|
|
3
|
+
ApiGatewayManagementApiClient,
|
|
4
|
+
PostToConnectionCommand,
|
|
5
|
+
} = require('@aws-sdk/client-apigatewaymanagementapi');
|
|
3
6
|
const {
|
|
4
7
|
WebsocketConnectionRepositoryInterface,
|
|
5
8
|
} = require('./websocket-connection-repository-interface');
|
|
@@ -108,20 +111,18 @@ class WebsocketConnectionRepositoryPostgres extends WebsocketConnectionRepositor
|
|
|
108
111
|
return connections.map((conn) => ({
|
|
109
112
|
connectionId: conn.connectionId,
|
|
110
113
|
send: async (data) => {
|
|
111
|
-
const apigwManagementApi = new
|
|
112
|
-
apiVersion: '2018-11-29',
|
|
114
|
+
const apigwManagementApi = new ApiGatewayManagementApiClient({
|
|
113
115
|
endpoint: process.env.WEBSOCKET_API_ENDPOINT,
|
|
114
116
|
});
|
|
115
117
|
|
|
116
118
|
try {
|
|
117
|
-
|
|
118
|
-
.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
.promise();
|
|
119
|
+
const command = new PostToConnectionCommand({
|
|
120
|
+
ConnectionId: conn.connectionId,
|
|
121
|
+
Data: JSON.stringify(data),
|
|
122
|
+
});
|
|
123
|
+
await apigwManagementApi.send(command);
|
|
123
124
|
} catch (error) {
|
|
124
|
-
if (error.statusCode === 410) {
|
|
125
|
+
if (error.statusCode === 410 || error.$metadata?.httpStatusCode === 410) {
|
|
125
126
|
console.log(
|
|
126
127
|
`Stale connection ${conn.connectionId}`
|
|
127
128
|
);
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { prisma } = require('../../database/prisma');
|
|
2
|
-
const
|
|
2
|
+
const {
|
|
3
|
+
ApiGatewayManagementApiClient,
|
|
4
|
+
PostToConnectionCommand,
|
|
5
|
+
} = require('@aws-sdk/client-apigatewaymanagementapi');
|
|
3
6
|
const {
|
|
4
7
|
WebsocketConnectionRepositoryInterface,
|
|
5
8
|
} = require('./websocket-connection-repository-interface');
|
|
@@ -79,20 +82,18 @@ class WebsocketConnectionRepository extends WebsocketConnectionRepositoryInterfa
|
|
|
79
82
|
return connections.map((conn) => ({
|
|
80
83
|
connectionId: conn.connectionId,
|
|
81
84
|
send: async (data) => {
|
|
82
|
-
const apigwManagementApi = new
|
|
83
|
-
apiVersion: '2018-11-29',
|
|
85
|
+
const apigwManagementApi = new ApiGatewayManagementApiClient({
|
|
84
86
|
endpoint: process.env.WEBSOCKET_API_ENDPOINT,
|
|
85
87
|
});
|
|
86
88
|
|
|
87
89
|
try {
|
|
88
|
-
|
|
89
|
-
.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
.promise();
|
|
90
|
+
const command = new PostToConnectionCommand({
|
|
91
|
+
ConnectionId: conn.connectionId,
|
|
92
|
+
Data: JSON.stringify(data),
|
|
93
|
+
});
|
|
94
|
+
await apigwManagementApi.send(command);
|
|
94
95
|
} catch (error) {
|
|
95
|
-
if (error.statusCode === 410) {
|
|
96
|
+
if (error.statusCode === 410 || error.$metadata?.httpStatusCode === 410) {
|
|
96
97
|
console.log(
|
|
97
98
|
`Stale connection ${conn.connectionId}`
|
|
98
99
|
);
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
jest.mock('../../database/config', () => ({
|
|
2
|
-
DB_TYPE: 'mongodb',
|
|
3
|
-
getDatabaseType: jest.fn(() => 'mongodb'),
|
|
4
|
-
PRISMA_LOG_LEVEL: 'error,warn',
|
|
5
|
-
PRISMA_QUERY_LOGGING: false,
|
|
6
|
-
}));
|
|
7
|
-
|
|
8
|
-
const mockFindExecute = jest.fn();
|
|
9
|
-
|
|
10
|
-
jest.mock('../../integrations/use-cases/find-integration-context-by-external-entity-id', () => {
|
|
11
|
-
return {
|
|
12
|
-
FindIntegrationContextByExternalEntityIdUseCase: jest
|
|
13
|
-
.fn()
|
|
14
|
-
.mockImplementation(() => ({
|
|
15
|
-
execute: mockFindExecute,
|
|
16
|
-
})),
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const {
|
|
21
|
-
createIntegrationCommands,
|
|
22
|
-
findIntegrationContextByExternalEntityId,
|
|
23
|
-
} = require('./integration-commands');
|
|
24
|
-
const {
|
|
25
|
-
FindIntegrationContextByExternalEntityIdUseCase,
|
|
26
|
-
} = require('../../integrations/use-cases/find-integration-context-by-external-entity-id');
|
|
27
|
-
const { DummyIntegration } = require('../../integrations/tests/doubles/dummy-integration-class');
|
|
28
|
-
|
|
29
|
-
describe('integration commands', () => {
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
jest.clearAllMocks();
|
|
32
|
-
mockFindExecute.mockReset();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('requires an integrationClass when creating commands', () => {
|
|
36
|
-
expect(() => createIntegrationCommands()).toThrow(
|
|
37
|
-
'integrationClass is required',
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('creates use cases with default repositories', () => {
|
|
42
|
-
createIntegrationCommands({
|
|
43
|
-
integrationClass: DummyIntegration,
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// Verify that the use case is created with default repositories instantiated internally
|
|
47
|
-
expect(
|
|
48
|
-
FindIntegrationContextByExternalEntityIdUseCase,
|
|
49
|
-
).toHaveBeenCalledWith({
|
|
50
|
-
integrationRepository: expect.any(Object),
|
|
51
|
-
moduleRepository: expect.any(Object),
|
|
52
|
-
loadIntegrationContextUseCase: expect.any(Object),
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('returns context when findIntegrationContextByExternalEntityId succeeds', async () => {
|
|
57
|
-
const expectedContext = { record: { id: 'integration-1' } };
|
|
58
|
-
mockFindExecute.mockResolvedValue({ context: expectedContext });
|
|
59
|
-
const commands = createIntegrationCommands({
|
|
60
|
-
integrationClass: DummyIntegration,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const result = await commands.findIntegrationContextByExternalEntityId(
|
|
64
|
-
'ext-1',
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
expect(mockFindExecute).toHaveBeenCalledWith({
|
|
68
|
-
externalEntityId: 'ext-1',
|
|
69
|
-
});
|
|
70
|
-
expect(result).toEqual({ context: expectedContext });
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('maps known errors to status codes', async () => {
|
|
74
|
-
const error = Object.assign(new Error('Entity missing'), {
|
|
75
|
-
code: 'ENTITY_NOT_FOUND',
|
|
76
|
-
});
|
|
77
|
-
mockFindExecute.mockRejectedValue(error);
|
|
78
|
-
const commands = createIntegrationCommands({
|
|
79
|
-
integrationClass: DummyIntegration,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const result = await commands.findIntegrationContextByExternalEntityId(
|
|
83
|
-
'ext-1',
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
expect(result).toEqual({
|
|
87
|
-
error: 401,
|
|
88
|
-
reason: 'Entity missing',
|
|
89
|
-
code: 'ENTITY_NOT_FOUND',
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it('delegates loadIntegrationContextById to the loader use case', async () => {
|
|
94
|
-
// This test verifies that the command properly delegates to the use case
|
|
95
|
-
// We can't easily mock the internal use case, so we'll test the integration
|
|
96
|
-
const commands = createIntegrationCommands({
|
|
97
|
-
integrationClass: DummyIntegration,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// The actual use case will be called - this is more of an integration test
|
|
101
|
-
// For unit testing, we'd need to refactor to allow DI of the use case
|
|
102
|
-
// But since we've decided to always use default use cases, this is acceptable
|
|
103
|
-
const result = await commands.loadIntegrationContextById('integration-1');
|
|
104
|
-
|
|
105
|
-
// Result will have error since we don't have a real database
|
|
106
|
-
expect(result).toHaveProperty('error');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('exposes a one-off helper for finding integration context by external entity id', async () => {
|
|
110
|
-
const expectedContext = { record: { id: 'integration-1' } };
|
|
111
|
-
mockFindExecute.mockResolvedValue({ context: expectedContext });
|
|
112
|
-
|
|
113
|
-
const result = await findIntegrationContextByExternalEntityId({
|
|
114
|
-
integrationClass: DummyIntegration,
|
|
115
|
-
externalEntityId: 'ext-2',
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
expect(mockFindExecute).toHaveBeenCalledWith({
|
|
119
|
-
externalEntityId: 'ext-2',
|
|
120
|
-
});
|
|
121
|
-
expect(result).toEqual({ context: expectedContext });
|
|
122
|
-
});
|
|
123
|
-
});
|