@friggframework/core 0.2.31-v1-alpha-package-update.0 → 1.0.1-v1-alpha-package-update.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.
Files changed (193) hide show
  1. package/assertions/CHANGELOG.md +87 -0
  2. package/assertions/LICENSE.md +9 -0
  3. package/assertions/README.md +3 -0
  4. package/assertions/bump.txt +1 -0
  5. package/assertions/get.js +139 -0
  6. package/assertions/index.js +19 -0
  7. package/assertions/is-equal.js +17 -0
  8. package/associations/LICENSE.md +9 -0
  9. package/associations/README.md +3 -0
  10. package/associations/association.js +78 -0
  11. package/associations/bump3.txt +0 -0
  12. package/associations/jest.config.js +5 -0
  13. package/associations/model.js +54 -0
  14. package/bump.txt +0 -0
  15. package/core/.eslintrc.json +3 -0
  16. package/{Delegate.js → core/Delegate.js} +1 -1
  17. package/core/LICENSE.md +9 -0
  18. package/{Worker.js → core/Worker.js} +2 -2
  19. package/core/bump3.txt +0 -0
  20. package/{create-handler.js → core/create-handler.js} +2 -2
  21. package/core/index.js +6 -0
  22. package/core/jest.config.js +5 -0
  23. package/database/.eslintrc.json +3 -0
  24. package/database/CHANGELOG.md +97 -0
  25. package/database/LICENSE.md +9 -0
  26. package/database/README.md +3 -0
  27. package/database/bump3.txt +0 -0
  28. package/database/index.js +17 -0
  29. package/database/jest.config.js +5 -0
  30. package/database/models/IndividualUser.js +76 -0
  31. package/database/models/OrganizationUser.js +29 -0
  32. package/database/models/State.js +9 -0
  33. package/database/models/Token.js +70 -0
  34. package/database/models/UserModel.js +7 -0
  35. package/database/mongo.js +45 -0
  36. package/database/mongoose.js +5 -0
  37. package/encrypt/.eslintrc.json +3 -0
  38. package/encrypt/CHANGELOG.md +65 -0
  39. package/encrypt/Cryptor.js +236 -0
  40. package/encrypt/Cryptor.test.js +32 -0
  41. package/encrypt/LICENSE.md +9 -0
  42. package/encrypt/README.md +3 -0
  43. package/encrypt/aes.js +27 -0
  44. package/encrypt/bump3.txt +0 -0
  45. package/encrypt/encrypt.js +124 -0
  46. package/encrypt/encrypt.test.js +1068 -0
  47. package/encrypt/index.js +3 -0
  48. package/encrypt/jest.config.js +5 -0
  49. package/encrypt/test-encrypt.js +107 -0
  50. package/errors/.eslintrc.json +3 -0
  51. package/errors/CHANGELOG.md +44 -0
  52. package/errors/LICENSE.md +9 -0
  53. package/errors/README.md +3 -0
  54. package/errors/base-error.js +23 -0
  55. package/errors/base-error.test.js +32 -0
  56. package/errors/bump.txt +1 -0
  57. package/errors/bump3.txt +0 -0
  58. package/errors/fetch-error.js +72 -0
  59. package/errors/fetch-error.test.js +79 -0
  60. package/errors/halt-error.js +10 -0
  61. package/errors/halt-error.test.js +11 -0
  62. package/errors/index.js +15 -0
  63. package/errors/jest.config.js +5 -0
  64. package/errors/validation-errors.js +23 -0
  65. package/errors/validation-errors.test.js +120 -0
  66. package/eslint-config/.eslintrc.json +3 -0
  67. package/eslint-config/CHANGELOG.md +17 -0
  68. package/eslint-config/LICENSE.md +9 -0
  69. package/eslint-config/README.md +3 -0
  70. package/eslint-config/bump3.txt +0 -0
  71. package/eslint-config/index.js +38 -0
  72. package/index.js +29 -5
  73. package/integrations/.eslintrc.json +3 -0
  74. package/integrations/CHANGELOG.md +244 -0
  75. package/integrations/LICENSE.md +9 -0
  76. package/integrations/README.md +3 -0
  77. package/integrations/bump3.txt +0 -0
  78. package/integrations/create-frigg-backend.js +31 -0
  79. package/integrations/index.js +19 -0
  80. package/integrations/integration-base.js +162 -0
  81. package/integrations/integration-factory.js +166 -0
  82. package/integrations/integration-mapping.js +43 -0
  83. package/integrations/integration-model.js +42 -0
  84. package/integrations/integration-router.js +367 -0
  85. package/integrations/integration-user.js +144 -0
  86. package/integrations/jest-setup.js +2 -0
  87. package/integrations/jest-teardown.js +2 -0
  88. package/integrations/jest.config.js +12 -0
  89. package/integrations/options.js +54 -0
  90. package/integrations/test/integration-base.test.js +143 -0
  91. package/lambda/README.md +3 -0
  92. package/lambda/TimeoutCatcher.js +43 -0
  93. package/lambda/TimeoutCatcher.test.js +68 -0
  94. package/lambda/bump3.txt +0 -0
  95. package/lambda/index.js +3 -0
  96. package/lambda/jest.config.js +3 -0
  97. package/logs/.eslintrc.json +3 -0
  98. package/logs/CHANGELOG.md +57 -0
  99. package/logs/LICENSE.md +9 -0
  100. package/logs/README.md +3 -0
  101. package/logs/bump3.txt +0 -0
  102. package/logs/index.js +7 -0
  103. package/logs/jest.config.js +5 -0
  104. package/logs/logger.js +69 -0
  105. package/logs/logger.test.js +76 -0
  106. package/migrations/README.md +3 -0
  107. package/migrations/bump3.txt +0 -0
  108. package/migrations/index.js +9 -0
  109. package/migrations/jest.config.js +3 -0
  110. package/migrations/manager.js +33 -0
  111. package/migrations/migrator.js +170 -0
  112. package/migrations/options.js +28 -0
  113. package/module-plugin/.eslintrc.json +3 -0
  114. package/module-plugin/CHANGELOG.md +224 -0
  115. package/module-plugin/LICENSE.md +9 -0
  116. package/module-plugin/ModuleConstants.js +10 -0
  117. package/module-plugin/README.md +3 -0
  118. package/module-plugin/auther.js +342 -0
  119. package/module-plugin/bump3.txt +0 -0
  120. package/module-plugin/credential.js +22 -0
  121. package/module-plugin/entity-manager.js +70 -0
  122. package/module-plugin/entity.js +46 -0
  123. package/module-plugin/index.js +25 -0
  124. package/module-plugin/jest-setup.js +3 -0
  125. package/module-plugin/jest-teardown.js +2 -0
  126. package/module-plugin/jest.config.js +20 -0
  127. package/module-plugin/manager.js +169 -0
  128. package/module-plugin/module-factory.js +60 -0
  129. package/module-plugin/requester/api-key.js +36 -0
  130. package/module-plugin/requester/basic.js +43 -0
  131. package/module-plugin/requester/oauth-2.js +219 -0
  132. package/module-plugin/requester/requester.js +150 -0
  133. package/module-plugin/requester/requester.test.js +28 -0
  134. package/module-plugin/test/auther.test.js +97 -0
  135. package/module-plugin/test/mock-api/api.js +29 -0
  136. package/module-plugin/test/mock-api/definition.js +48 -0
  137. package/module-plugin/test/mock-api/mocks/hubspot.js +43 -0
  138. package/package.json +37 -12
  139. package/prettier-config/.eslintrc.json +3 -0
  140. package/prettier-config/CHANGELOG.md +17 -0
  141. package/prettier-config/LICENSE.md +9 -0
  142. package/prettier-config/README.md +3 -0
  143. package/prettier-config/bump3.txt +0 -0
  144. package/prettier-config/index.js +6 -0
  145. package/syncs/README.md +3 -0
  146. package/syncs/bump3.txt +0 -0
  147. package/syncs/jest.config.js +5 -0
  148. package/syncs/manager.js +466 -0
  149. package/syncs/model.js +62 -0
  150. package/syncs/sync.js +114 -0
  151. package/test-environment/.eslintrc.json +3 -0
  152. package/test-environment/Authenticator.js +73 -0
  153. package/test-environment/CHANGELOG.md +46 -0
  154. package/test-environment/LICENSE.md +9 -0
  155. package/test-environment/README.md +3 -0
  156. package/test-environment/auther-definition-method-tester.js +45 -0
  157. package/test-environment/auther-definition-tester.js +104 -0
  158. package/test-environment/bump3.txt +0 -0
  159. package/test-environment/index.js +24 -0
  160. package/test-environment/integration-validator.js +2 -0
  161. package/test-environment/jest-global-setup.js +6 -0
  162. package/test-environment/jest-global-teardown.js +3 -0
  163. package/test-environment/jest-preset.js +14 -0
  164. package/test-environment/mock-api-readme.md +102 -0
  165. package/test-environment/mock-api.js +284 -0
  166. package/test-environment/mock-integration.js +82 -0
  167. package/test-environment/mongodb.js +22 -0
  168. package/test-environment/override-environment.js +11 -0
  169. package/types/CHANGELOG.md +49 -0
  170. package/types/README.md +24 -0
  171. package/types/assertions/index.d.ts +83 -0
  172. package/types/associations/index.d.ts +74 -0
  173. package/types/bump3.txt +0 -0
  174. package/types/core/index.d.ts +54 -0
  175. package/types/database/index.d.ts +3 -0
  176. package/types/encrypt/index.d.ts +5 -0
  177. package/types/errors/index.d.ts +66 -0
  178. package/types/eslint-config/index.d.ts +41 -0
  179. package/types/index.d.ts +14 -0
  180. package/types/integrations/index.d.ts +191 -0
  181. package/types/lambda/index.d.ts +31 -0
  182. package/types/logs/index.d.ts +5 -0
  183. package/types/module-plugin/index.d.ts +293 -0
  184. package/types/prettier-config/index.d.ts +6 -0
  185. package/types/syncs/index.d.ts +128 -0
  186. package/types/test-environment/index.d.ts +17 -0
  187. package/types/tsconfig.json +103 -0
  188. /package/{.eslintrc.json → assertions/.eslintrc.json} +0 -0
  189. /package/{bump3.txt → assertions/bump3.txt} +0 -0
  190. /package/{jest.config.js → assertions/jest.config.js} +0 -0
  191. /package/{CHANGELOG.md → core/CHANGELOG.md} +0 -0
  192. /package/{README.md → core/README.md} +0 -0
  193. /package/{load-installed-modules.js → core/load-installed-modules.js} +0 -0
@@ -0,0 +1,73 @@
1
+ // "use strict";
2
+ const http = require('http');
3
+ const url = require('url');
4
+ const open = require('open');
5
+
6
+ class Authenticator {
7
+ static searchParamsToDictionary(params) {
8
+ const entries = params.entries();
9
+ const result = {};
10
+ for (const entry of entries) {
11
+ const [key, value] = entry;
12
+ result[key] = value;
13
+ }
14
+ return result;
15
+ }
16
+
17
+ static async oauth2(authorizeUrl, port = 3000) {
18
+ return new Promise((resolve, reject) => {
19
+ const server = http
20
+ .createServer(async (req, res) => {
21
+ try {
22
+ const qs = new url.URL(
23
+ req.url,
24
+ `http://localhost:${port}`
25
+ ).searchParams;
26
+
27
+ // gets the last parameter in the slash
28
+ const urlPostfix = req.url.split('?')[0];
29
+
30
+ const params =
31
+ Authenticator.searchParamsToDictionary(qs);
32
+
33
+ res.end(
34
+ `<h1 style="position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);">Feel free to close the Brow Brow now</h1>
35
+ <style>@media (prefers-color-scheme: dark) {
36
+ body {
37
+ color: #b0b0b0;
38
+ background-color: #101010;
39
+ }
40
+ </style>`
41
+ );
42
+ server.close();
43
+ resolve({
44
+ base: urlPostfix,
45
+ data: params,
46
+ });
47
+ } catch (e) {
48
+ reject(e);
49
+ }
50
+ })
51
+ .listen(port, () => {
52
+ // open the browser to the authorize url to start the workflow
53
+ open(authorizeUrl).then((childProcess) => {
54
+ childProcess.unref();
55
+ clearTimeout(timeoutId);
56
+ });
57
+ });
58
+
59
+ const timeoutId = setTimeout(() => {
60
+ if (server.listening) {
61
+ try {
62
+ server.close();
63
+ } finally {
64
+ throw new Error(
65
+ 'Authenticator timed out before authentication completed in the browser.'
66
+ );
67
+ }
68
+ }
69
+ }, 59_000);
70
+ });
71
+ }
72
+ }
73
+ module.exports = Authenticator;
@@ -0,0 +1,46 @@
1
+ # v1.1.6 (Tue Jan 31 2023)
2
+
3
+ #### 🐛 Bug Fix
4
+
5
+ - Updates/api module yotpo [#108](https://github.com/friggframework/frigg/pull/108) ([@seanspeaks](https://github.com/seanspeaks))
6
+ - # mock-api.js Work in Progress ([@seanspeaks](https://github.com/seanspeaks))
7
+ - Bump independent versions \[skip ci\] ([@seanspeaks](https://github.com/seanspeaks))
8
+
9
+ #### Authors: 1
10
+
11
+ - Sean Matthews ([@seanspeaks](https://github.com/seanspeaks))
12
+
13
+ ---
14
+
15
+ # v1.1.5 (Mon Jan 09 2023)
16
+
17
+ #### 🐛 Bug Fix
18
+
19
+ - Merge remote-tracking branch 'origin/main' into gitbook-updates [#48](https://github.com/friggframework/frigg/pull/48) ([@seanspeaks](https://github.com/seanspeaks))
20
+ - Bump independent versions \[skip ci\] ([@seanspeaks](https://github.com/seanspeaks))
21
+ - Merge remote-tracking branch 'origin/main' into simplify-mongoose-models ([@seanspeaks](https://github.com/seanspeaks))
22
+ - Add READMEs for all packages and api-modules [#20](https://github.com/friggframework/frigg/pull/20) ([@seanspeaks](https://github.com/seanspeaks))
23
+ - Add READMEs for all packages and api-modules ([@seanspeaks](https://github.com/seanspeaks))
24
+
25
+ #### ⚠️ Pushed to `main`
26
+
27
+ - Merge branch 'main' into gitbook-updates ([@seanspeaks](https://github.com/seanspeaks))
28
+ - Refactored for more conventional naming (at least for packages) ([@seanspeaks](https://github.com/seanspeaks))
29
+
30
+ #### Authors: 1
31
+
32
+ - Sean Matthews ([@seanspeaks](https://github.com/seanspeaks))
33
+
34
+ ---
35
+
36
+ # v1.1.4 (Mon Sep 19 2022)
37
+
38
+ #### 🐛 Bug Fix
39
+
40
+ - Test environment setup for all modules [#49](https://github.com/friggframework/frigg/pull/49) ([@seanspeaks](https://github.com/seanspeaks))
41
+ - Test environment setup for all modules ([@seanspeaks](https://github.com/seanspeaks))
42
+ - Merge remote-tracking branch 'origin/main' into gitbook-updates [#48](https://github.com/friggframework/frigg/pull/48) ([@seanspeaks](https://github.com/seanspeaks))
43
+
44
+ #### Authors: 1
45
+
46
+ - Sean Matthews ([@seanspeaks](https://github.com/seanspeaks))
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Left Hook Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ # test-environment
2
+
3
+ This package exports the `test-environment` defaults used in [Frigg](https://friggframework.org). You can find its documentation [on Frigg's website](https://docs.friggframework.org/packages/test-environment).
@@ -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,104 @@
1
+ const {Auther, ModuleConstants} = require('../module-plugin');
2
+ const { createObjectId, connectToDatabase, disconnectFromDatabase } = require('../database/mongo');
3
+ const { createMockApiObject } = require("./mock-integration");
4
+
5
+
6
+ function testAutherDefinition(definition, mocks) {
7
+ const getModule = async (params) => {
8
+ const module = await Auther.getInstance({
9
+ definition,
10
+ userId: createObjectId(),
11
+ ...params,
12
+ });
13
+ module.api = createMockApiObject(jest, module.api, mocks);
14
+ return module
15
+ }
16
+
17
+
18
+ describe(`${definition.moduleName} Module Tests`, () => {
19
+ let module, authUrl;
20
+ beforeAll(async () => {
21
+ await connectToDatabase();
22
+ module = await getModule();
23
+ });
24
+
25
+ afterAll(async () => {
26
+ await disconnectFromDatabase();
27
+ });
28
+
29
+ let requirements, authorizeParams;
30
+ if (definition.API.requesterType === ModuleConstants.authType.oauth2) {
31
+ authorizeParams = mocks.authorizeResponse;
32
+ describe('getAuthorizationRequirements() test', () => {
33
+ it('should return auth requirements', async () => {
34
+ requirements = module.getAuthorizationRequirements();
35
+ expect(requirements).toBeDefined();
36
+ expect(requirements.type).toEqual(ModuleConstants.authType.oauth2);
37
+ expect(requirements.url).toBeDefined();
38
+ authUrl = requirements.url;
39
+ });
40
+ });
41
+ } else if (definition.API.requesterType === ModuleConstants.authType.basic) {
42
+ // could also confirm authorizeParams against the auth requirements
43
+ authorizeParams = mocks.authorizeParams
44
+ describe('getAuthorizationRequirements() test', () => {
45
+ it('should return auth requirements', async () => {
46
+ requirements = module.getAuthorizationRequirements();
47
+ expect(requirements).toBeDefined();
48
+ expect(requirements.type).toEqual(ModuleConstants.authType.basic);
49
+ });
50
+ });
51
+ } else if (definition.API.requesterType === ModuleConstants.authType.apiKey) {
52
+ // could also confirm authorizeParams against the auth requirements
53
+ authorizeParams = mocks.authorizeParams
54
+ describe('getAuthorizationRequirements() test', () => {
55
+ it('should return auth requirements', async () => {
56
+ requirements = module.getAuthorizationRequirements();
57
+ expect(requirements).toBeDefined();
58
+ expect(requirements.type).toEqual(ModuleConstants.authType.apiKey);
59
+ });
60
+ });
61
+ }
62
+
63
+ describe('Authorization requests', () => {
64
+ let firstRes;
65
+ it('processAuthorizationCallback()', async () => {
66
+ firstRes = await module.processAuthorizationCallback(authorizeParams);
67
+ expect(firstRes).toBeDefined();
68
+ expect(firstRes.entity_id).toBeDefined();
69
+ expect(firstRes.credential_id).toBeDefined();
70
+ });
71
+ it('retrieves existing entity on subsequent calls', async () => {
72
+ const res = await module.processAuthorizationCallback(authorizeParams);
73
+ expect(res).toEqual(firstRes);
74
+ });
75
+ });
76
+
77
+ describe('Test credential retrieval and module instantiation', () => {
78
+ it('retrieve by entity id', async () => {
79
+ const newModule = await getModule({
80
+ userId: module.userId,
81
+ entityId: module.entity.id
82
+ });
83
+ expect(newModule).toBeDefined();
84
+ expect(newModule.entity).toBeDefined();
85
+ expect(newModule.credential).toBeDefined();
86
+ expect(await newModule.testAuth()).toBeTruthy();
87
+
88
+ });
89
+
90
+ it('retrieve by credential id', async () => {
91
+ const newModule = await getModule({
92
+ userId: module.userId,
93
+ credentialId: module.credential.id
94
+ });
95
+ expect(newModule).toBeDefined();
96
+ expect(newModule.credential).toBeDefined();
97
+ expect(await newModule.testAuth()).toBeTruthy();
98
+ });
99
+ });
100
+ });
101
+ }
102
+
103
+ module.exports = { testAutherDefinition }
104
+
File without changes
@@ -0,0 +1,24 @@
1
+ const { TestMongo } = require('./mongodb');
2
+ const {
3
+ overrideEnvironment,
4
+ restoreEnvironment,
5
+ } = require('./override-environment');
6
+ const globalTeardown = require('./jest-global-teardown');
7
+ const globalSetup = require('./jest-global-setup');
8
+ const {testDefinitionRequiredAuthMethods} = require('./auther-definition-method-tester');
9
+ const {createMockIntegration, createMockApiObject} = require('./mock-integration');
10
+ const { testAutherDefinition } = require('./auther-definition-tester');
11
+ const Authenticator = require('./Authenticator')
12
+
13
+ module.exports = {
14
+ TestMongo,
15
+ overrideEnvironment,
16
+ restoreEnvironment,
17
+ globalTeardown,
18
+ globalSetup,
19
+ createMockIntegration,
20
+ createMockApiObject,
21
+ testDefinitionRequiredAuthMethods,
22
+ testAutherDefinition,
23
+ Authenticator
24
+ };
@@ -0,0 +1,2 @@
1
+ // TODO input for poll worker (etc), run as if in staging, set up env just right, enable debugging with debugger,
2
+ // TODO write up or help automate (?) knowing where to update tests/mocks when a problem is found
@@ -0,0 +1,6 @@
1
+ const { TestMongo } = require('./mongodb');
2
+
3
+ module.exports = async function () {
4
+ global.testMongo = new TestMongo();
5
+ await global.testMongo.start();
6
+ };
@@ -0,0 +1,3 @@
1
+ module.exports = async function () {
2
+ await global.testMongo.stop();
3
+ };
@@ -0,0 +1,14 @@
1
+ module.exports = {
2
+ collectCoverage: true,
3
+ testTimeout: 20_000,
4
+ runner: 'groups',
5
+ coverageReporters: ['text'],
6
+ coverageThreshold: {
7
+ global: {
8
+ statements: 80,
9
+ branches: 80,
10
+ functions: 80,
11
+ lines: 80,
12
+ },
13
+ },
14
+ };
@@ -0,0 +1,102 @@
1
+ # Mocked Requests Set Up for Your API Module
2
+
3
+ ## Add Tooling to the Test File
4
+
5
+ Example code:
6
+
7
+ ```js
8
+ const Api = require('./Api');
9
+ const { mockApi } = require('../../../../test/utils/mockApi');
10
+
11
+ const MockedApi = mockApi(Api, {
12
+ authenticationMode: 'browser',
13
+ });
14
+
15
+ describe('DemoNock API', async () => {
16
+ before(async function () {
17
+ await MockedApi.initialize({ test: this.test });
18
+ });
19
+
20
+ after(async function () {
21
+ await MockedApi.clean({ test: this.test });
22
+ });
23
+
24
+ it('tests a nice thing', async () => {
25
+ const api = await MockedApi.mock();
26
+ const users = await api.getUsers();
27
+ expect(users).to.have.length(2);
28
+ });
29
+ });
30
+ ```
31
+
32
+ Api - the LH API class that is being tested/mocked
33
+
34
+ mockApi - tool to record/playback requests and automate login to API
35
+
36
+ MockedApi - Api class with wrapper around it to record/playback requests and automate login to API. `authenticationMode` sets how to handle authentication when recording requests. Browser means it will require a manual step in the browser (uses Authenticator). Client credentials mode gets a token automcaitcally with machine-to-machine token. Manual means the developer will handle authentication manually, like setting an API key. By default mockApi will use the class name of the mocked API to name the output file. This can be overidden by passing in `displayName`.
37
+ test/recorded-requests - the directory where recorded API requests are saved
38
+
39
+ before - make sure to use the `async function` form here, so we can access mocha's `this` object. The call to `initialize` sets up the tooling for recording/playback
40
+
41
+ after - same here need to use the `async function` form and pass in `this.test` to `clean`. This stops recording/playback and removes all hooks and other tooling from Node's HTTP code.
42
+
43
+ test - instead of using `new Api` in your tests, use `await MockedApi.mock`. Any parameters you would normally pass into `new Api` will be passed through with the call to `mock`. The instance of the Api class that is returned will already be authenticated (unless using manual mode).
44
+
45
+ ## Creating new API tests
46
+
47
+ When running your tests while creating them, use `npm --record-apis=demonock test`.
48
+
49
+ This tells the tooling to record the requests made while running the tests for the DemoNockApi class.
50
+
51
+ ## Finalizing API tests
52
+
53
+ When satisified with your tests, run `npm test` without adding the `--record-apis` flag to make sure the recorded requests worked. The tests should pass without needing you to perform a login step in the browser.
54
+
55
+ ## Tests that fail...
56
+
57
+ Sometimes a "finished" test that was previously working starts to fail. Or, a test passes when it should fail (false negative). There are a few reasons this might happen.
58
+
59
+ ### Test fails after code updates
60
+
61
+ If you made updates to the code, this may cause a test to fail. In this case the developer doesn't need to update mocks. To make the test pass, the updated code should be fixed, or the test should be updated to reflect the new data shape.
62
+
63
+ One way to test for this scenario is running `npm test` in the main branch, seeing that all tests pass, and then confirming tests fail in your feature branch. This would show that the test is almost certainly failing due to a change in your branch.
64
+
65
+ ### Test should fail but passes
66
+
67
+ If a test is passing, but in the "real world" the code is failing, the API response may have changed. Re-record mocks by running tests with `--record-apis=nockdemo`. Hopefully, one or more tests will now fail. These tests and related code should be updated to work with the new API response data shape.
68
+
69
+ When creating the PR for this update, flag that the mocks were re-recorded so the updated mocks will be sure to be reviewed by another team member as part of code review.
70
+
71
+ ### Mocks in error state
72
+
73
+ If the recorded files get messed up when updating (sometimes unavoidable under certain error conditions) just `git restore test/recorded-requests` or delete the file(s) in that directory and re-record.
74
+
75
+ ## Miscellaneous Notes
76
+
77
+ More than one API can be set to record mode at a time: `npm --record-apis=activecampaign,hubspot test`
78
+
79
+ You can use .only when --record-apis is passed, but only for the root level test suite. Otherwise an error will be thrown to prevent partial recording of the mocks. .skip is fine to use to skip tests that should not be recorded.
80
+
81
+ CAN UPDATE RECORDED REQUESTS:
82
+
83
+ ```js
84
+ describe('Nock Demo API', () => {
85
+ it('does x');
86
+ it.skip('does y');
87
+ });
88
+ ```
89
+
90
+ CANNOT UPDATE RECORDED REQUESTS:
91
+
92
+ ```js
93
+ describe('Nock Demo API', () => {
94
+ it('does x');
95
+ it('does y');
96
+ });
97
+ ```
98
+
99
+ ## Caveats
100
+
101
+ - Client credential mode may only work with Crossbeam currently
102
+ - Puppet mode (browser automation) for login is not yet implemented