@friggframework/core 1.1.1-canary.282.4de1ac1.0 → 1.2.0-canary.293.5731c31.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/index.js +11 -3
- package/integrations/index.js +4 -1
- package/integrations/mock-integration.js +83 -0
- package/module-plugin/auther-definition-method-tester.js +45 -0
- package/module-plugin/auther-definition-tester.js +121 -0
- package/module-plugin/index.js +5 -1
- package/package.json +3 -3
- package/bump0.txt +0 -1
package/index.js
CHANGED
|
@@ -36,7 +36,9 @@ const {
|
|
|
36
36
|
IntegrationHelper,
|
|
37
37
|
createIntegrationRouter,
|
|
38
38
|
checkRequiredParams,
|
|
39
|
-
createFriggBackend
|
|
39
|
+
createFriggBackend,
|
|
40
|
+
createMockIntegration,
|
|
41
|
+
createMockApiObject
|
|
40
42
|
} = require('./integrations/index');
|
|
41
43
|
const { TimeoutCatcher } = require('./lambda/index');
|
|
42
44
|
const {
|
|
@@ -55,7 +57,9 @@ const {
|
|
|
55
57
|
Requester,
|
|
56
58
|
ModuleConstants,
|
|
57
59
|
ModuleFactory,
|
|
58
|
-
Auther
|
|
60
|
+
Auther,
|
|
61
|
+
testAutherDefinition,
|
|
62
|
+
testDefinitionRequiredAuthMethods
|
|
59
63
|
} = require('./module-plugin/index');
|
|
60
64
|
|
|
61
65
|
// const {Sync } = require('./syncs/model');
|
|
@@ -103,6 +107,8 @@ module.exports = {
|
|
|
103
107
|
checkRequiredParams,
|
|
104
108
|
createIntegrationRouter,
|
|
105
109
|
createFriggBackend,
|
|
110
|
+
createMockIntegration,
|
|
111
|
+
createMockApiObject,
|
|
106
112
|
// lambda
|
|
107
113
|
TimeoutCatcher,
|
|
108
114
|
// logs
|
|
@@ -120,5 +126,7 @@ module.exports = {
|
|
|
120
126
|
Requester,
|
|
121
127
|
ModuleConstants,
|
|
122
128
|
ModuleFactory,
|
|
123
|
-
Auther
|
|
129
|
+
Auther,
|
|
130
|
+
testAutherDefinition,
|
|
131
|
+
testDefinitionRequiredAuthMethods
|
|
124
132
|
}
|
package/integrations/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const { IntegrationMapping } = require('./integration-mapping');
|
|
|
5
5
|
const { IntegrationFactory, IntegrationHelper } = require('./integration-factory');
|
|
6
6
|
const { createIntegrationRouter, checkRequiredParams } = require('./integration-router');
|
|
7
7
|
const { createFriggBackend } = require('./create-frigg-backend');
|
|
8
|
+
const { createMockApiObject, createMockIntegration } = require('./mock-integration');
|
|
8
9
|
|
|
9
10
|
module.exports = {
|
|
10
11
|
IntegrationBase,
|
|
@@ -15,5 +16,7 @@ module.exports = {
|
|
|
15
16
|
IntegrationHelper,
|
|
16
17
|
createIntegrationRouter,
|
|
17
18
|
checkRequiredParams,
|
|
18
|
-
createFriggBackend
|
|
19
|
+
createFriggBackend,
|
|
20
|
+
createMockIntegration,
|
|
21
|
+
createMockApiObject
|
|
19
22
|
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const { Auther, Credential, Entity } = require('../module-plugin')
|
|
2
|
+
const { IntegrationModel } = require('./integration-model');
|
|
3
|
+
const { createObjectId } = require('../database');
|
|
4
|
+
|
|
5
|
+
async function createMockIntegration(IntegrationClassDef, userId = null, config = {},) {
|
|
6
|
+
const integration = new IntegrationClassDef();
|
|
7
|
+
userId = userId || createObjectId();
|
|
8
|
+
integration.delegateTypes.push(...IntegrationClassDef.Config.events)
|
|
9
|
+
|
|
10
|
+
const insertOptions = {
|
|
11
|
+
new: true,
|
|
12
|
+
upsert: true,
|
|
13
|
+
setDefaultsOnInsert: true,
|
|
14
|
+
}
|
|
15
|
+
const user = {user: userId}
|
|
16
|
+
|
|
17
|
+
const credential = await Credential.findOneAndUpdate(
|
|
18
|
+
user,
|
|
19
|
+
{ $set: user },
|
|
20
|
+
insertOptions
|
|
21
|
+
);
|
|
22
|
+
const entity1 = await Entity.findOneAndUpdate(
|
|
23
|
+
user,
|
|
24
|
+
{
|
|
25
|
+
$set: {
|
|
26
|
+
credential: credential.id,
|
|
27
|
+
user: userId,
|
|
28
|
+
name: 'Test user',
|
|
29
|
+
externalId: '1234567890123456',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
insertOptions
|
|
33
|
+
);
|
|
34
|
+
const entity2 = await Entity.findOneAndUpdate(
|
|
35
|
+
user,
|
|
36
|
+
{
|
|
37
|
+
$set: {
|
|
38
|
+
credential: credential.id,
|
|
39
|
+
user: userId,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
insertOptions
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const entities = [entity1, entity2]
|
|
46
|
+
integration.record = await IntegrationModel.create({
|
|
47
|
+
entities,
|
|
48
|
+
user: userId,
|
|
49
|
+
config: {type: IntegrationClassDef.Config.name, ...config}
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
integration.id = integration.record._id
|
|
53
|
+
|
|
54
|
+
for (const i in entities){
|
|
55
|
+
if (Object.entries(IntegrationClassDef.modules).length <= i) break
|
|
56
|
+
const [moduleName, ModuleDef] = Object.entries(IntegrationClassDef.modules)[i];
|
|
57
|
+
const module = await Auther.getInstance({definition: ModuleDef, userId: userId})
|
|
58
|
+
module.entity = entities[i];
|
|
59
|
+
integration[moduleName] = module;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return integration
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function createMockApiObject(jest, api = {}, mockMethodMap) {
|
|
66
|
+
// take in an api class and object with keys that are method names
|
|
67
|
+
// and values which are the mock response (or implementation)
|
|
68
|
+
const clone = (data) => JSON.parse(JSON.stringify(data));
|
|
69
|
+
|
|
70
|
+
for (const [methodName, mockDataOrImplementation] of Object.entries(mockMethodMap)) {
|
|
71
|
+
if (mockDataOrImplementation instanceof Function) {
|
|
72
|
+
api[methodName] = jest.fn(mockDataOrImplementation);
|
|
73
|
+
}
|
|
74
|
+
else if (api[methodName]?.constructor?.name === "AsyncFunction") {
|
|
75
|
+
api[methodName] = jest.fn().mockResolvedValue(clone(mockDataOrImplementation));
|
|
76
|
+
} else {
|
|
77
|
+
api[methodName] = jest.fn().mockReturnValue(clone(mockDataOrImplementation));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return api;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = {createMockIntegration, createMockApiObject};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const {flushDebugLog} = require('../logs');
|
|
2
|
+
|
|
3
|
+
async function testDefinitionRequiredAuthMethods(api, definition, authCallbackParams, tokenResponse, userId) {
|
|
4
|
+
|
|
5
|
+
// const response = await definition.getToken(api, authCallbackParams);
|
|
6
|
+
// expect(api.setTokens).toHaveBeenCalled();
|
|
7
|
+
// if (tokenResponse) {
|
|
8
|
+
// expect(response).toMatchObject(tokenResponse);
|
|
9
|
+
// }
|
|
10
|
+
|
|
11
|
+
const entityDetails = await definition.requiredAuthMethods.getEntityDetails(api, authCallbackParams, tokenResponse, userId);
|
|
12
|
+
expect(entityDetails).toHaveProperty('identifiers');
|
|
13
|
+
expect(Object.values(entityDetails.identifiers).length).toBeGreaterThan(0);
|
|
14
|
+
for (const key of Object.keys(entityDetails.identifiers)){
|
|
15
|
+
expect(key).toBeDefined();
|
|
16
|
+
expect(entityDetails.identifiers[key]).toBeDefined();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const credentialDetails = await definition.requiredAuthMethods.getCredentialDetails(api);
|
|
21
|
+
expect(credentialDetails).toHaveProperty('identifiers');
|
|
22
|
+
expect(Object.values(entityDetails.identifiers).length).toBeGreaterThan(0);
|
|
23
|
+
for (const key of Object.keys(entityDetails.identifiers)){
|
|
24
|
+
expect(key).toBeDefined();
|
|
25
|
+
expect(entityDetails.identifiers[key]).toBeDefined();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const successResponse = await definition.requiredAuthMethods.testAuthRequest(api);
|
|
29
|
+
expect(successResponse).toBeTruthy();
|
|
30
|
+
const savedKeys = {};
|
|
31
|
+
for (const key of definition.requiredAuthMethods.apiPropertiesToPersist.credential){
|
|
32
|
+
savedKeys[key] = api[key];
|
|
33
|
+
delete api[key];
|
|
34
|
+
}
|
|
35
|
+
let validAuth = false;
|
|
36
|
+
try {
|
|
37
|
+
if (await definition.requiredAuthMethods.testAuthRequest(api)) validAuth = true;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
flushDebugLog(e);
|
|
40
|
+
}
|
|
41
|
+
expect(validAuth).not.toBeTruthy();
|
|
42
|
+
Object.assign(api, savedKeys);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = { testDefinitionRequiredAuthMethods }
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
const { Auther } = require('./auther');
|
|
2
|
+
const { ModuleConstants } = require('./ModuleConstants');
|
|
3
|
+
const { createObjectId,connectToDatabase, disconnectFromDatabase } = require('../database');
|
|
4
|
+
const { createMockApiObject } = require("../integrations/mock-integration");
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function testAutherDefinition(definition, mocks) {
|
|
8
|
+
const getModule = async (params) => {
|
|
9
|
+
const module = await Auther.getInstance({
|
|
10
|
+
definition,
|
|
11
|
+
userId: createObjectId(),
|
|
12
|
+
...params,
|
|
13
|
+
});
|
|
14
|
+
if (mocks.tokenResponse) {
|
|
15
|
+
mocks.getTokenFrom = async function(code) {
|
|
16
|
+
await this.setTokens(mocks.tokenResponse);
|
|
17
|
+
return mocks.tokenResponse
|
|
18
|
+
}
|
|
19
|
+
mocks.getTokenFromCode = mocks.getTokenFromCode || mocks.getTokenFrom
|
|
20
|
+
mocks.getTokenFromCodeBasicAuthHeader = mocks.getTokenFromCodeBasicAuthHeader || mocks.getTokenFrom
|
|
21
|
+
mocks.getTokenFromClientCredentials = mocks.getTokenFromClientCredentials || mocks.getTokenFrom
|
|
22
|
+
mocks.getTokenFromUsernamePassword = mocks.getTokenFromUsernamePassword || mocks.getTokenFrom
|
|
23
|
+
}
|
|
24
|
+
if (mocks.refreshResponse) {
|
|
25
|
+
mocks.refreshAccessToken = async function(code) {
|
|
26
|
+
await this.setTokens(mocks.refreshResponse);
|
|
27
|
+
return mocks.refreshResponse
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
module.api = createMockApiObject(jest, module.api, mocks);
|
|
31
|
+
return module
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
describe(`${definition.moduleName} Module Tests`, () => {
|
|
36
|
+
let module, authUrl;
|
|
37
|
+
beforeAll(async () => {
|
|
38
|
+
await connectToDatabase();
|
|
39
|
+
module = await getModule();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterAll(async () => {
|
|
43
|
+
await disconnectFromDatabase();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
let requirements, authCallbackParams;
|
|
47
|
+
if (definition.API.requesterType === ModuleConstants.authType.oauth2) {
|
|
48
|
+
authCallbackParams = mocks.authorizeResponse || mocks.authorizeParams;
|
|
49
|
+
describe('getAuthorizationRequirements() test', () => {
|
|
50
|
+
it('should return auth requirements', async () => {
|
|
51
|
+
requirements = module.getAuthorizationRequirements();
|
|
52
|
+
expect(requirements).toBeDefined();
|
|
53
|
+
expect(requirements.type).toEqual(ModuleConstants.authType.oauth2);
|
|
54
|
+
expect(requirements.url).toBeDefined();
|
|
55
|
+
authUrl = requirements.url;
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
} else if (definition.API.requesterType === ModuleConstants.authType.basic) {
|
|
59
|
+
// could also confirm authCallbackParams against the auth requirements
|
|
60
|
+
authCallbackParams = mocks.authorizeParams
|
|
61
|
+
describe('getAuthorizationRequirements() test', () => {
|
|
62
|
+
it('should return auth requirements', async () => {
|
|
63
|
+
requirements = module.getAuthorizationRequirements();
|
|
64
|
+
expect(requirements).toBeDefined();
|
|
65
|
+
expect(requirements.type).toEqual(ModuleConstants.authType.basic);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
} else if (definition.API.requesterType === ModuleConstants.authType.apiKey) {
|
|
69
|
+
// could also confirm authCallbackParams against the auth requirements
|
|
70
|
+
authCallbackParams = mocks.authorizeParams
|
|
71
|
+
describe('getAuthorizationRequirements() test', () => {
|
|
72
|
+
it('should return auth requirements', async () => {
|
|
73
|
+
requirements = module.getAuthorizationRequirements();
|
|
74
|
+
expect(requirements).toBeDefined();
|
|
75
|
+
expect(requirements.type).toEqual(ModuleConstants.authType.apiKey);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
describe('Authorization requests', () => {
|
|
81
|
+
let firstRes;
|
|
82
|
+
it('processAuthorizationCallback()', async () => {
|
|
83
|
+
firstRes = await module.processAuthorizationCallback(authCallbackParams);
|
|
84
|
+
expect(firstRes).toBeDefined();
|
|
85
|
+
expect(firstRes.entity_id).toBeDefined();
|
|
86
|
+
expect(firstRes.credential_id).toBeDefined();
|
|
87
|
+
});
|
|
88
|
+
it('retrieves existing entity on subsequent calls', async () => {
|
|
89
|
+
const res = await module.processAuthorizationCallback(authCallbackParams);
|
|
90
|
+
expect(res).toEqual(firstRes);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('Test credential retrieval and module instantiation', () => {
|
|
95
|
+
it('retrieve by entity id', async () => {
|
|
96
|
+
const newModule = await getModule({
|
|
97
|
+
userId: module.userId,
|
|
98
|
+
entityId: module.entity.id
|
|
99
|
+
});
|
|
100
|
+
expect(newModule).toBeDefined();
|
|
101
|
+
expect(newModule.entity).toBeDefined();
|
|
102
|
+
expect(newModule.credential).toBeDefined();
|
|
103
|
+
expect(await newModule.testAuth()).toBeTruthy();
|
|
104
|
+
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('retrieve by credential id', async () => {
|
|
108
|
+
const newModule = await getModule({
|
|
109
|
+
userId: module.userId,
|
|
110
|
+
credentialId: module.credential.id
|
|
111
|
+
});
|
|
112
|
+
expect(newModule).toBeDefined();
|
|
113
|
+
expect(newModule.credential).toBeDefined();
|
|
114
|
+
expect(await newModule.testAuth()).toBeTruthy();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = { testAutherDefinition }
|
|
121
|
+
|
package/module-plugin/index.js
CHANGED
|
@@ -9,6 +9,8 @@ const { Requester } = require('./requester/requester');
|
|
|
9
9
|
const { ModuleConstants } = require('./ModuleConstants');
|
|
10
10
|
const { ModuleFactory } = require('./module-factory');
|
|
11
11
|
const { Auther } = require('./auther');
|
|
12
|
+
const { testAutherDefinition } = require('./auther-definition-tester');
|
|
13
|
+
const { testDefinitionRequiredAuthMethods } = require('./auther-definition-method-tester');
|
|
12
14
|
|
|
13
15
|
module.exports = {
|
|
14
16
|
Credential,
|
|
@@ -21,5 +23,7 @@ module.exports = {
|
|
|
21
23
|
Requester,
|
|
22
24
|
ModuleConstants,
|
|
23
25
|
ModuleFactory,
|
|
24
|
-
Auther
|
|
26
|
+
Auther,
|
|
27
|
+
testAutherDefinition,
|
|
28
|
+
testDefinitionRequiredAuthMethods
|
|
25
29
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0-canary.293.5731c31.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@hapi/boom": "^10.0.1",
|
|
7
7
|
"aws-sdk": "^2.1200.0",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"node-fetch": "^2.6.7"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@friggframework/devtools": "1.
|
|
18
|
+
"@friggframework/devtools": "1.2.0-canary.293.5731c31.0",
|
|
19
19
|
"@types/lodash": "^4.14.191",
|
|
20
20
|
"@typescript-eslint/eslint-plugin": "^5.55.0",
|
|
21
21
|
"chai": "^4.3.6",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/friggframework/frigg#readme",
|
|
49
49
|
"description": "",
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "5731c31f2ef22a9b47b43a626cd8ff0e849bd561"
|
|
51
51
|
}
|
package/bump0.txt
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
2
|