@friggframework/core 2.0.0--canary.398.53eac55.0 → 2.0.0--canary.397.878fefa.0
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 +931 -50
- package/core/create-handler.js +1 -0
- package/credential/credential-repository.js +42 -0
- package/credential/use-cases/get-credential-for-user.js +21 -0
- package/credential/use-cases/update-authentication-status.js +15 -0
- package/database/models/WebsocketConnection.js +0 -5
- package/handlers/app-definition-loader.js +38 -0
- package/handlers/app-handler-helpers.js +0 -3
- package/handlers/backend-utils.js +35 -34
- package/handlers/routers/auth.js +3 -14
- package/handlers/routers/integration-defined-routers.js +8 -5
- package/handlers/routers/user.js +25 -5
- package/handlers/workers/integration-defined-workers.js +6 -3
- package/index.js +1 -16
- package/integrations/index.js +0 -5
- package/integrations/integration-base.js +42 -44
- package/integrations/integration-repository.js +67 -0
- package/integrations/integration-router.js +301 -178
- package/integrations/integration.js +233 -0
- package/integrations/options.js +1 -1
- package/integrations/tests/doubles/dummy-integration-class.js +90 -0
- package/integrations/tests/doubles/test-integration-repository.js +89 -0
- package/integrations/tests/use-cases/create-integration.test.js +124 -0
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +143 -0
- package/integrations/tests/use-cases/get-integration-for-user.test.js +143 -0
- package/integrations/tests/use-cases/get-integration-instance.test.js +169 -0
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +169 -0
- package/integrations/tests/use-cases/get-possible-integrations.test.js +188 -0
- package/integrations/tests/use-cases/update-integration-messages.test.js +142 -0
- package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
- package/integrations/tests/use-cases/update-integration.test.js +134 -0
- package/integrations/use-cases/create-integration.js +72 -0
- package/integrations/use-cases/delete-integration-for-user.js +73 -0
- package/integrations/use-cases/get-integration-for-user.js +79 -0
- package/integrations/use-cases/get-integration-instance.js +84 -0
- package/integrations/use-cases/get-integrations-for-user.js +77 -0
- package/integrations/use-cases/get-possible-integrations.js +27 -0
- package/integrations/use-cases/index.js +11 -0
- package/integrations/use-cases/update-integration-messages.js +31 -0
- package/integrations/use-cases/update-integration-status.js +28 -0
- package/integrations/use-cases/update-integration.js +92 -0
- package/integrations/utils/map-integration-dto.js +36 -0
- package/{module-plugin → modules}/index.js +0 -8
- package/modules/module-factory.js +54 -0
- package/modules/module-repository.js +107 -0
- package/modules/module.js +221 -0
- package/{module-plugin → modules}/test/mock-api/api.js +8 -3
- package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
- package/modules/tests/doubles/test-module-factory.js +16 -0
- package/modules/tests/doubles/test-module-repository.js +19 -0
- package/modules/use-cases/get-entities-for-user.js +32 -0
- package/modules/use-cases/get-entity-options-by-id.js +58 -0
- package/modules/use-cases/get-entity-options-by-type.js +34 -0
- package/modules/use-cases/get-module-instance-from-type.js +31 -0
- package/modules/use-cases/get-module.js +56 -0
- package/modules/use-cases/process-authorization-callback.js +114 -0
- package/modules/use-cases/refresh-entity-options.js +58 -0
- package/modules/use-cases/test-module-auth.js +54 -0
- package/modules/utils/map-module-dto.js +18 -0
- package/package.json +5 -5
- package/syncs/sync.js +0 -1
- package/types/integrations/index.d.ts +2 -6
- package/types/module-plugin/index.d.ts +4 -56
- package/types/syncs/index.d.ts +0 -2
- package/user/tests/doubles/test-user-repository.js +72 -0
- package/user/tests/use-cases/create-individual-user.test.js +24 -0
- package/user/tests/use-cases/create-organization-user.test.js +28 -0
- package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
- package/user/tests/use-cases/login-user.test.js +140 -0
- package/user/use-cases/create-individual-user.js +61 -0
- package/user/use-cases/create-organization-user.js +47 -0
- package/user/use-cases/create-token-for-user-id.js +30 -0
- package/user/use-cases/get-user-from-bearer-token.js +77 -0
- package/user/use-cases/login-user.js +122 -0
- package/user/user-repository.js +62 -0
- package/user/user.js +77 -0
- package/handlers/routers/middleware/loadUser.js +0 -15
- package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
- package/integrations/create-frigg-backend.js +0 -31
- package/integrations/integration-factory.js +0 -251
- package/integrations/integration-user.js +0 -144
- package/integrations/test/integration-base.test.js +0 -144
- package/module-plugin/auther.js +0 -393
- package/module-plugin/entity-manager.js +0 -70
- package/module-plugin/manager.js +0 -169
- package/module-plugin/module-factory.js +0 -61
- /package/{module-plugin → modules}/ModuleConstants.js +0 -0
- /package/{module-plugin → modules}/credential.js +0 -0
- /package/{module-plugin → modules}/entity.js +0 -0
- /package/{module-plugin → modules}/requester/api-key.js +0 -0
- /package/{module-plugin → modules}/requester/basic.js +0 -0
- /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
- /package/{module-plugin → modules}/requester/requester.js +0 -0
- /package/{module-plugin → modules}/requester/requester.test.js +0 -0
- /package/{module-plugin → modules}/test/auther.test.js +0 -0
- /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
|
|
3
|
+
class RefreshEntityOptions {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Object} params
|
|
6
|
+
* @param {import('../module-repository').ModuleRepository} params.moduleRepository
|
|
7
|
+
* @param {} params.moduleDefinitions
|
|
8
|
+
*/
|
|
9
|
+
constructor({ moduleRepository, moduleDefinitions }) {
|
|
10
|
+
this.moduleRepository = moduleRepository;
|
|
11
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Retrieve a Module instance for a given user and entity/module type.
|
|
16
|
+
* @param {string} userId
|
|
17
|
+
* @param {string} entityId
|
|
18
|
+
*/
|
|
19
|
+
async execute(entityId, userId, options) {
|
|
20
|
+
const entity = await this.moduleRepository.findEntityById(
|
|
21
|
+
entityId,
|
|
22
|
+
userId
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
if (!entity) {
|
|
26
|
+
throw new Error(`Entity ${entityId} not found`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (entity.userId !== userId) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Entity ${entityId} does not belong to user ${userId}`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const entityType = entity.type;
|
|
36
|
+
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
37
|
+
const modelName = Module.getEntityModelFromDefinition(def).modelName;
|
|
38
|
+
return entityType === modelName;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!moduleDefinition) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Module definition not found for entity type: ${entityType}`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const module = new Module({
|
|
48
|
+
userId,
|
|
49
|
+
entity,
|
|
50
|
+
definition: moduleDefinition,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await module.refreshEntityOptions(options);
|
|
54
|
+
return module.getEntityOptions();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { RefreshEntityOptions };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const { Module } = require('../module');
|
|
2
|
+
|
|
3
|
+
class TestModuleAuth {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Object} params - Configuration parameters.
|
|
6
|
+
* @param {import('./module-repository').ModuleRepository} params.moduleRepository - Repository for module data operations.
|
|
7
|
+
* @param {Array<Object>} params.moduleDefinitions - Array of module definitions.
|
|
8
|
+
*/
|
|
9
|
+
constructor({ moduleRepository, moduleDefinitions }) {
|
|
10
|
+
this.moduleRepository = moduleRepository;
|
|
11
|
+
this.moduleDefinitions = moduleDefinitions;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async execute(entityId, userId) {
|
|
15
|
+
const entity = await this.moduleRepository.findEntityById(
|
|
16
|
+
entityId,
|
|
17
|
+
userId
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
if (!entity) {
|
|
21
|
+
throw new Error(`Entity ${entityId} not found`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (entity.userId !== userId) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Entity ${entityId} does not belong to user ${userId}`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const entityType = entity.type;
|
|
31
|
+
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
32
|
+
const modelName = Module.getEntityModelFromDefinition(def).modelName;
|
|
33
|
+
return entityType === modelName;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (!moduleDefinition) {
|
|
37
|
+
throw new Error(
|
|
38
|
+
`Module definition not found for entity type: ${entityType}`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const module = new Module({
|
|
43
|
+
userId,
|
|
44
|
+
entity,
|
|
45
|
+
definition: moduleDefinition,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const testAuthResponse = await module.testAuth();
|
|
49
|
+
|
|
50
|
+
return testAuthResponse;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
module.exports = { TestModuleAuth };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {import('../module').Module} moduleInstance
|
|
3
|
+
* Convert a Module domain instance to a plain DTO suitable for JSON responses.
|
|
4
|
+
*/
|
|
5
|
+
function mapModuleClassToModuleDTO(moduleInstance) {
|
|
6
|
+
if (!moduleInstance) return null;
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
id: moduleInstance.entity.id,
|
|
10
|
+
name: moduleInstance.name,
|
|
11
|
+
userId: moduleInstance.userId,
|
|
12
|
+
entity: moduleInstance.entity,
|
|
13
|
+
credentialId: moduleInstance.credential?._id?.toString(),
|
|
14
|
+
type: moduleInstance.getName()
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = { mapModuleClassToModuleDTO };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.
|
|
4
|
+
"version": "2.0.0--canary.397.878fefa.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@hapi/boom": "^10.0.1",
|
|
7
7
|
"aws-sdk": "^2.1200.0",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"uuid": "^9.0.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@friggframework/eslint-config": "2.0.0--canary.
|
|
26
|
-
"@friggframework/prettier-config": "2.0.0--canary.
|
|
27
|
-
"@friggframework/test": "2.0.0--canary.
|
|
25
|
+
"@friggframework/eslint-config": "2.0.0--canary.397.878fefa.0",
|
|
26
|
+
"@friggframework/prettier-config": "2.0.0--canary.397.878fefa.0",
|
|
27
|
+
"@friggframework/test": "2.0.0--canary.397.878fefa.0",
|
|
28
28
|
"@types/lodash": "4.17.15",
|
|
29
29
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
30
30
|
"chai": "^4.3.6",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
},
|
|
54
54
|
"homepage": "https://github.com/friggframework/frigg#readme",
|
|
55
55
|
"description": "",
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "878fefa87b447613ea7ec353ee5b0f4e06559f57"
|
|
57
57
|
}
|
package/syncs/sync.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
declare module "@friggframework/integrations" {
|
|
2
2
|
import { Delegate, IFriggDelegate } from "@friggframework/core";
|
|
3
3
|
import { Model } from "mongoose";
|
|
4
|
-
import { EntityManager } from "@friggframework/module-plugin";
|
|
5
4
|
|
|
6
5
|
export class Integration extends Model {
|
|
7
6
|
entities: any[];
|
|
@@ -19,8 +18,7 @@ declare module "@friggframework/integrations" {
|
|
|
19
18
|
|
|
20
19
|
export class IntegrationManager
|
|
21
20
|
extends Delegate
|
|
22
|
-
implements IFriggIntegrationManager
|
|
23
|
-
{
|
|
21
|
+
implements IFriggIntegrationManager {
|
|
24
22
|
integration: Integration;
|
|
25
23
|
primaryInstance: any;
|
|
26
24
|
targetInstance: any;
|
|
@@ -56,7 +54,6 @@ declare module "@friggframework/integrations" {
|
|
|
56
54
|
entities: { id: string; user: any },
|
|
57
55
|
userId: string,
|
|
58
56
|
config: any,
|
|
59
|
-
EntityManager: EntityManager
|
|
60
57
|
): Promise<any>;
|
|
61
58
|
|
|
62
59
|
static getFormattedIntegration(
|
|
@@ -116,8 +113,7 @@ declare module "@friggframework/integrations" {
|
|
|
116
113
|
}
|
|
117
114
|
|
|
118
115
|
export class IntegrationConfigManager
|
|
119
|
-
implements IFriggIntegrationConfigManager
|
|
120
|
-
{
|
|
116
|
+
implements IFriggIntegrationConfigManager {
|
|
121
117
|
options: IntegrationOptions[];
|
|
122
118
|
primary: any;
|
|
123
119
|
|
|
@@ -9,21 +9,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
9
9
|
externalId: string;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
static primaryEntityClass: any;
|
|
14
|
-
static entityManagerClasses: any[];
|
|
15
|
-
static entityTypes: string[];
|
|
16
|
-
static getEntitiesForUser(userId: string): Promise<any[]>;
|
|
17
|
-
static checkIsValidType(entityType: string): boolean;
|
|
18
|
-
static getEntityManagerClass(entityType?: string): any;
|
|
19
|
-
|
|
20
|
-
static getEntityManagerInstanceFromEntityId(
|
|
21
|
-
entityId: string,
|
|
22
|
-
userId: string
|
|
23
|
-
): Promise<any>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface IFriggEntityManager {}
|
|
12
|
+
interface IFriggEntityManager { }
|
|
27
13
|
|
|
28
14
|
export class Entity extends Model {
|
|
29
15
|
credentialId: string;
|
|
@@ -34,42 +20,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
34
20
|
}
|
|
35
21
|
|
|
36
22
|
export type MappedEntity = Entity & { id: string; type: any };
|
|
37
|
-
export class ModuleManager extends Delegate implements IFriggModuleManager {
|
|
38
|
-
static Entity: Entity;
|
|
39
|
-
static Credential: Credential;
|
|
40
|
-
|
|
41
|
-
constructor(params: { userId: string });
|
|
42
23
|
|
|
43
|
-
static getName(): any;
|
|
44
|
-
static getInstance(params: any): Promise<any>;
|
|
45
|
-
static getEntitiesForUserId(userId: string): Promise<MappedEntity[]>;
|
|
46
|
-
|
|
47
|
-
batchCreateSyncObjects(syncObjects: any, syncManager: any): Promise<any>;
|
|
48
|
-
batchUpdateSyncObjects(syncObjects: any, syncManager: any): Promise<any>;
|
|
49
|
-
findOrCreateEntity(params: any): Promise<any>;
|
|
50
|
-
getAllSyncObjects(SyncClass: any): Promise<any>;
|
|
51
|
-
getAuthorizationRequirements(params: any): Promise<any>;
|
|
52
|
-
getEntityId(): Promise<string>;
|
|
53
|
-
getEntityOptions(): Promise<any>;
|
|
54
|
-
markCredentialsInvalid(): Promise<Credential>;
|
|
55
|
-
processAuthorizationCallback(params: any): Promise<any>;
|
|
56
|
-
testAuth(params: any): Promise<any>;
|
|
57
|
-
validateAuthorizationRequirements(): Promise<boolean>;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
interface IFriggModuleManager extends IFriggDelegate {
|
|
61
|
-
getEntityId(): Promise<string>;
|
|
62
|
-
validateAuthorizationRequirements(): Promise<boolean>;
|
|
63
|
-
getAuthorizationRequirements(params: any): Promise<any>;
|
|
64
|
-
testAuth(params: any): Promise<any>;
|
|
65
|
-
processAuthorizationCallback(params: any): Promise<any>;
|
|
66
|
-
getEntityOptions(): Promise<any>;
|
|
67
|
-
findOrCreateEntity(params: any): Promise<any>;
|
|
68
|
-
getAllSyncObjects(SyncClass: any): Promise<any>;
|
|
69
|
-
batchCreateSyncObjects(syncObjects: any, syncManager: any): Promise<any>;
|
|
70
|
-
batchUpdateSyncObjects(syncObjects: any, syncManager: any): Promise<any>;
|
|
71
|
-
markCredentialsInvalid(): Promise<Credential>;
|
|
72
|
-
}
|
|
73
24
|
|
|
74
25
|
export class Requester implements IFriggRequester {
|
|
75
26
|
DLGT_INVALID_AUTH: string;
|
|
@@ -138,8 +89,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
138
89
|
|
|
139
90
|
export class ApiKeyRequester
|
|
140
91
|
extends Requester
|
|
141
|
-
implements IFriggApiKeyRequester
|
|
142
|
-
{
|
|
92
|
+
implements IFriggApiKeyRequester {
|
|
143
93
|
API_KEY_NAME: string;
|
|
144
94
|
API_KEY_VALUE: any;
|
|
145
95
|
|
|
@@ -160,8 +110,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
160
110
|
|
|
161
111
|
export class BasicAuthRequester
|
|
162
112
|
extends Requester
|
|
163
|
-
implements IFriggBasicAuthRequester
|
|
164
|
-
{
|
|
113
|
+
implements IFriggBasicAuthRequester {
|
|
165
114
|
password: string;
|
|
166
115
|
username: string;
|
|
167
116
|
|
|
@@ -189,8 +138,7 @@ declare module "@friggframework/module-plugin" {
|
|
|
189
138
|
|
|
190
139
|
export class OAuth2Requester
|
|
191
140
|
extends Requester
|
|
192
|
-
implements IFriggOAuth2Requester
|
|
193
|
-
{
|
|
141
|
+
implements IFriggOAuth2Requester {
|
|
194
142
|
DLGT_TOKEN_DEAUTHORIZED: string;
|
|
195
143
|
DLGT_TOKEN_UPDATE: string;
|
|
196
144
|
accessTokenExpire: any;
|
package/types/syncs/index.d.ts
CHANGED
|
@@ -28,7 +28,6 @@ declare module "@friggframework/syncs/manager" {
|
|
|
28
28
|
confirmCreate(
|
|
29
29
|
syncObj: Sync,
|
|
30
30
|
createdId: string,
|
|
31
|
-
moduleManager: any
|
|
32
31
|
): Promise<any>;
|
|
33
32
|
confirmUpdate(syncObj: Sync): Promise<any>;
|
|
34
33
|
createSyncDBObject(objArr: any[], entities: any[]): Promise<any>;
|
|
@@ -50,7 +49,6 @@ declare module "@friggframework/syncs/manager" {
|
|
|
50
49
|
confirmCreate(
|
|
51
50
|
syncObj: Sync,
|
|
52
51
|
createdId: string,
|
|
53
|
-
moduleManager: any
|
|
54
52
|
): Promise<any>;
|
|
55
53
|
confirmUpdate(syncObj: Sync): Promise<any>;
|
|
56
54
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const Boom = require('@hapi/boom');
|
|
2
|
+
const { User } = require('../../user');
|
|
3
|
+
|
|
4
|
+
class TestUserRepository {
|
|
5
|
+
constructor({ userConfig }) {
|
|
6
|
+
this.individualUsers = new Map();
|
|
7
|
+
this.organizationUsers = new Map();
|
|
8
|
+
this.tokens = new Map();
|
|
9
|
+
this.userConfig = userConfig;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getSessionToken(token) {
|
|
13
|
+
return this.tokens.get(token);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async findOrganizationUserById(userId) {
|
|
17
|
+
return this.organizationUsers.get(userId);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async findIndividualUserById(userId) {
|
|
21
|
+
return this.individualUsers.get(userId);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async createToken(userId, rawToken, minutes = 120) {
|
|
25
|
+
const token = `token-for-${userId}-for-${minutes}-mins`;
|
|
26
|
+
this.tokens.set(token, { user: userId, rawToken });
|
|
27
|
+
return token;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async createIndividualUser(params) {
|
|
31
|
+
const individualUserData = { id: `individual-${Date.now()}`, ...params };
|
|
32
|
+
this.individualUsers.set(individualUserData.id, individualUserData);
|
|
33
|
+
return individualUserData;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async createOrganizationUser(params) {
|
|
37
|
+
const orgUserData = { ...params, id: `org-${Date.now()}` };
|
|
38
|
+
this.organizationUsers.set(orgUserData.id, orgUserData);
|
|
39
|
+
return orgUserData;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async findIndividualUserByUsername(username) {
|
|
43
|
+
for (const userDoc of this.individualUsers.values()) {
|
|
44
|
+
if (userDoc.username === username) {
|
|
45
|
+
return userDoc;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async findIndividualUserByAppUserId(appUserId) {
|
|
52
|
+
if (!appUserId) return null;
|
|
53
|
+
for (const userDoc of this.individualUsers.values()) {
|
|
54
|
+
if (userDoc.appUserId === appUserId) {
|
|
55
|
+
return userDoc;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async findOrganizationUserByAppOrgId(appOrgId) {
|
|
62
|
+
if (!appOrgId) return null;
|
|
63
|
+
for (const userDoc of this.organizationUsers.values()) {
|
|
64
|
+
if (userDoc.appOrgId === appOrgId) {
|
|
65
|
+
return userDoc;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = { TestUserRepository };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const {
|
|
2
|
+
CreateIndividualUser,
|
|
3
|
+
} = require('../../use-cases/create-individual-user');
|
|
4
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
5
|
+
|
|
6
|
+
describe('CreateIndividualUser Use Case', () => {
|
|
7
|
+
it('should create and return an individual user via the repository', async () => {
|
|
8
|
+
const userConfig = { usePassword: true };
|
|
9
|
+
const userRepository = new TestUserRepository({ userConfig });
|
|
10
|
+
const createIndividualUser = new CreateIndividualUser({
|
|
11
|
+
userRepository,
|
|
12
|
+
userConfig,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const params = {
|
|
16
|
+
username: 'test-user',
|
|
17
|
+
password: 'password123',
|
|
18
|
+
};
|
|
19
|
+
const user = await createIndividualUser.execute(params);
|
|
20
|
+
|
|
21
|
+
expect(user).toBeDefined();
|
|
22
|
+
expect(user.getIndividualUser().username).toBe(params.username);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const {
|
|
2
|
+
CreateOrganizationUser,
|
|
3
|
+
} = require('../../use-cases/create-organization-user');
|
|
4
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
5
|
+
|
|
6
|
+
describe('CreateOrganizationUser Use Case', () => {
|
|
7
|
+
it('should create and return an organization user via the repository', async () => {
|
|
8
|
+
const userConfig = {
|
|
9
|
+
primary: 'organization',
|
|
10
|
+
organizationUserRequired: true,
|
|
11
|
+
individualUserRequired: false,
|
|
12
|
+
};
|
|
13
|
+
const userRepository = new TestUserRepository({ userConfig });
|
|
14
|
+
const createOrganizationUser = new CreateOrganizationUser({
|
|
15
|
+
userRepository,
|
|
16
|
+
userConfig,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const params = {
|
|
20
|
+
name: 'Test Org',
|
|
21
|
+
appOrgId: 'org-123',
|
|
22
|
+
};
|
|
23
|
+
const user = await createOrganizationUser.execute(params);
|
|
24
|
+
|
|
25
|
+
expect(user).toBeDefined();
|
|
26
|
+
expect(user.getOrganizationUser().name).toBe(params.name);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const {
|
|
2
|
+
CreateTokenForUserId,
|
|
3
|
+
} = require('../../use-cases/create-token-for-user-id');
|
|
4
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
5
|
+
|
|
6
|
+
describe('CreateTokenForUserId Use Case', () => {
|
|
7
|
+
it('should create and return a token via the repository', async () => {
|
|
8
|
+
const userConfig = {}; // Not used by this use case, but required by the test repo
|
|
9
|
+
const userRepository = new TestUserRepository({ userConfig });
|
|
10
|
+
const createTokenForUserId = new CreateTokenForUserId({ userRepository });
|
|
11
|
+
|
|
12
|
+
const userId = 'user-123';
|
|
13
|
+
const token = await createTokenForUserId.execute(userId);
|
|
14
|
+
|
|
15
|
+
expect(token).toBeDefined();
|
|
16
|
+
// The mock token is deterministic, so we can check it
|
|
17
|
+
expect(token).toContain(`token-for-${userId}`);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const {
|
|
2
|
+
GetUserFromBearerToken,
|
|
3
|
+
} = require('../../use-cases/get-user-from-bearer-token');
|
|
4
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
5
|
+
|
|
6
|
+
describe('GetUserFromBearerToken Use Case', () => {
|
|
7
|
+
let userRepository;
|
|
8
|
+
let getUserFromBearerToken;
|
|
9
|
+
let userConfig;
|
|
10
|
+
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
userConfig = {
|
|
13
|
+
usePassword: true,
|
|
14
|
+
primary: 'individual',
|
|
15
|
+
individualUserRequired: true,
|
|
16
|
+
organizationUserRequired: false,
|
|
17
|
+
};
|
|
18
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
19
|
+
getUserFromBearerToken = new GetUserFromBearerToken({
|
|
20
|
+
userRepository,
|
|
21
|
+
userConfig
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should retrieve a user for a valid bearer token', async () => {
|
|
26
|
+
const userId = 'user-123';
|
|
27
|
+
const token = await userRepository.createToken(userId);
|
|
28
|
+
const createdUserData = await userRepository.createIndividualUser({
|
|
29
|
+
id: userId,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const user = await getUserFromBearerToken.execute(`Bearer ${token}`);
|
|
33
|
+
|
|
34
|
+
expect(user).toBeDefined();
|
|
35
|
+
expect(user.getId()).toBe(createdUserData.id);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should throw an unauthorized error if the bearer token is missing', async () => {
|
|
39
|
+
await expect(getUserFromBearerToken.execute(null)).rejects.toThrow(
|
|
40
|
+
'Missing Authorization Header'
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should throw an unauthorized error for an invalid token format', async () => {
|
|
45
|
+
await expect(
|
|
46
|
+
getUserFromBearerToken.execute('InvalidToken')
|
|
47
|
+
).rejects.toThrow('Invalid Token Format');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should throw an unauthorized error if the Session Token is not found', async () => {
|
|
51
|
+
userRepository.getSessionToken = jest.fn().mockResolvedValue(null);
|
|
52
|
+
await expect(
|
|
53
|
+
getUserFromBearerToken.execute('Bearer invalid-token')
|
|
54
|
+
).rejects.toThrow('Session Token Not Found');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should throw an unauthorized error if the token is valid but finds no user', async () => {
|
|
58
|
+
userRepository.getSessionToken = jest.fn().mockResolvedValue(null);
|
|
59
|
+
const token = await userRepository.createToken('user-dne');
|
|
60
|
+
await expect(
|
|
61
|
+
getUserFromBearerToken.execute(`Bearer ${token}`)
|
|
62
|
+
).rejects.toThrow('Session Token Not Found');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const bcrypt = require('bcryptjs');
|
|
2
|
+
const { LoginUser } = require('../../use-cases/login-user');
|
|
3
|
+
const { TestUserRepository } = require('../doubles/test-user-repository');
|
|
4
|
+
|
|
5
|
+
jest.mock('bcryptjs', () => ({
|
|
6
|
+
compareSync: jest.fn(),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
describe('LoginUser Use Case', () => {
|
|
10
|
+
let userRepository;
|
|
11
|
+
let loginUser;
|
|
12
|
+
let userConfig;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
userConfig = { usePassword: true, individualUserRequired: true, organizationUserRequired: false };
|
|
16
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
17
|
+
loginUser = new LoginUser({ userRepository, userConfig });
|
|
18
|
+
|
|
19
|
+
bcrypt.compareSync.mockClear();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('With Password Authentication', () => {
|
|
23
|
+
it('should successfully log in a user with correct credentials', async () => {
|
|
24
|
+
const username = 'test-user';
|
|
25
|
+
const password = 'password123';
|
|
26
|
+
await userRepository.createIndividualUser({
|
|
27
|
+
username,
|
|
28
|
+
hashword: 'hashed-password',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
bcrypt.compareSync.mockReturnValue(true);
|
|
32
|
+
|
|
33
|
+
const user = await loginUser.execute({ username, password });
|
|
34
|
+
|
|
35
|
+
expect(bcrypt.compareSync).toHaveBeenCalledWith(
|
|
36
|
+
password,
|
|
37
|
+
'hashed-password'
|
|
38
|
+
);
|
|
39
|
+
expect(user).toBeDefined();
|
|
40
|
+
expect(user.getIndividualUser().username).toBe(username);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should throw an unauthorized error for an incorrect password', async () => {
|
|
44
|
+
const username = 'test-user';
|
|
45
|
+
const password = 'wrong-password';
|
|
46
|
+
await userRepository.createIndividualUser({
|
|
47
|
+
username,
|
|
48
|
+
hashword: 'hashed-password',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
bcrypt.compareSync.mockReturnValue(false);
|
|
52
|
+
|
|
53
|
+
await expect(
|
|
54
|
+
loginUser.execute({ username, password })
|
|
55
|
+
).rejects.toThrow('Incorrect username or password');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should throw an unauthorized error for a non-existent user', async () => {
|
|
59
|
+
const username = 'non-existent-user';
|
|
60
|
+
const password = 'password123';
|
|
61
|
+
|
|
62
|
+
await expect(
|
|
63
|
+
loginUser.execute({ username, password })
|
|
64
|
+
).rejects.toThrow('user not found');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('Without Password (appUserId)', () => {
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
userConfig = { usePassword: false, individualUserRequired: true, organizationUserRequired: false };
|
|
71
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
72
|
+
loginUser = new LoginUser({
|
|
73
|
+
userRepository,
|
|
74
|
+
userConfig,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('should successfully retrieve a user by appUserId', async () => {
|
|
79
|
+
const appUserId = 'app-user-123';
|
|
80
|
+
const createdUserData = await userRepository.createIndividualUser({
|
|
81
|
+
appUserId,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const result = await loginUser.execute({ appUserId });
|
|
85
|
+
expect(result.getId()).toBe(createdUserData.id);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('With Organization User', () => {
|
|
90
|
+
beforeEach(() => {
|
|
91
|
+
userConfig = {
|
|
92
|
+
primary: 'organization',
|
|
93
|
+
individualUserRequired: false,
|
|
94
|
+
organizationUserRequired: true,
|
|
95
|
+
};
|
|
96
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
97
|
+
loginUser = new LoginUser({
|
|
98
|
+
userRepository,
|
|
99
|
+
userConfig,
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should successfully retrieve an organization user by appOrgId', async () => {
|
|
104
|
+
const appOrgId = 'app-org-123';
|
|
105
|
+
const createdUserData = await userRepository.createOrganizationUser({
|
|
106
|
+
name: 'Test Org',
|
|
107
|
+
appOrgId,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const result = await loginUser.execute({ appOrgId });
|
|
111
|
+
expect(result.getId()).toBe(createdUserData.id);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should throw an unauthorized error for a non-existent organization user', async () => {
|
|
115
|
+
const appOrgId = 'non-existent-org';
|
|
116
|
+
|
|
117
|
+
await expect(loginUser.execute({ appOrgId })).rejects.toThrow(
|
|
118
|
+
'org user non-existent-org not found'
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('Required User Checks', () => {
|
|
124
|
+
it('should throw an error if a required individual user is not found', async () => {
|
|
125
|
+
userConfig = {
|
|
126
|
+
individualUserRequired: true,
|
|
127
|
+
usePassword: false,
|
|
128
|
+
};
|
|
129
|
+
userRepository = new TestUserRepository({ userConfig });
|
|
130
|
+
loginUser = new LoginUser({
|
|
131
|
+
userRepository,
|
|
132
|
+
userConfig,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
await expect(
|
|
136
|
+
loginUser.execute({ appUserId: 'a-non-existent-user-id' })
|
|
137
|
+
).rejects.toThrow('user not found');
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
});
|