@budibase/worker 2.1.44 → 2.1.46-alpha.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 (209) hide show
  1. package/__mocks__/aws-sdk.ts +15 -0
  2. package/__mocks__/oauth.ts +57 -0
  3. package/coverage/clover.xml +2167 -0
  4. package/coverage/coverage-final.json +78 -0
  5. package/coverage/lcov-report/base.css +224 -0
  6. package/coverage/lcov-report/block-navigation.js +87 -0
  7. package/coverage/lcov-report/favicon.png +0 -0
  8. package/coverage/lcov-report/index.html +431 -0
  9. package/coverage/lcov-report/prettify.css +1 -0
  10. package/coverage/lcov-report/prettify.js +2 -0
  11. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  12. package/coverage/lcov-report/sorter.js +196 -0
  13. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +925 -0
  14. package/coverage/lcov-report/src/api/controllers/global/configs.ts.html +1405 -0
  15. package/coverage/lcov-report/src/api/controllers/global/email.ts.html +196 -0
  16. package/coverage/lcov-report/src/api/controllers/global/index.html +236 -0
  17. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +187 -0
  18. package/coverage/lcov-report/src/api/controllers/global/roles.ts.html +283 -0
  19. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +577 -0
  20. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +316 -0
  21. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +799 -0
  22. package/coverage/lcov-report/src/api/controllers/global/workspaces.ts.html +244 -0
  23. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +148 -0
  24. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +124 -0
  25. package/coverage/lcov-report/src/api/controllers/system/index.html +191 -0
  26. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +124 -0
  27. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +124 -0
  28. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +133 -0
  29. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +154 -0
  30. package/coverage/lcov-report/src/api/index.html +116 -0
  31. package/coverage/lcov-report/src/api/index.ts.html +583 -0
  32. package/coverage/lcov-report/src/api/routes/global/auth.ts.html +349 -0
  33. package/coverage/lcov-report/src/api/routes/global/configs.ts.html +457 -0
  34. package/coverage/lcov-report/src/api/routes/global/email.ts.html +193 -0
  35. package/coverage/lcov-report/src/api/routes/global/index.html +236 -0
  36. package/coverage/lcov-report/src/api/routes/global/license.ts.html +124 -0
  37. package/coverage/lcov-report/src/api/routes/global/roles.ts.html +133 -0
  38. package/coverage/lcov-report/src/api/routes/global/self.ts.html +139 -0
  39. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +196 -0
  40. package/coverage/lcov-report/src/api/routes/global/users.ts.html +433 -0
  41. package/coverage/lcov-report/src/api/routes/global/workspaces.ts.html +196 -0
  42. package/coverage/lcov-report/src/api/routes/index.html +116 -0
  43. package/coverage/lcov-report/src/api/routes/index.ts.html +196 -0
  44. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +142 -0
  45. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +109 -0
  46. package/coverage/lcov-report/src/api/routes/system/index.html +191 -0
  47. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +142 -0
  48. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +109 -0
  49. package/coverage/lcov-report/src/api/routes/system/status.ts.html +109 -0
  50. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +124 -0
  51. package/coverage/lcov-report/src/api/routes/validation/index.html +131 -0
  52. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +88 -0
  53. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +235 -0
  54. package/coverage/lcov-report/src/constants/index.html +116 -0
  55. package/coverage/lcov-report/src/constants/index.ts.html +613 -0
  56. package/coverage/lcov-report/src/constants/templates/index.html +116 -0
  57. package/coverage/lcov-report/src/constants/templates/index.ts.html +316 -0
  58. package/coverage/lcov-report/src/db/index.html +116 -0
  59. package/coverage/lcov-report/src/db/index.ts.html +115 -0
  60. package/coverage/lcov-report/src/environment.ts.html +373 -0
  61. package/coverage/lcov-report/src/index.html +131 -0
  62. package/coverage/lcov-report/src/index.ts.html +394 -0
  63. package/coverage/lcov-report/src/middleware/cloudRestricted.ts.html +139 -0
  64. package/coverage/lcov-report/src/middleware/index.html +116 -0
  65. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +145 -0
  66. package/coverage/lcov-report/src/migrations/functions/index.html +116 -0
  67. package/coverage/lcov-report/src/migrations/index.html +116 -0
  68. package/coverage/lcov-report/src/migrations/index.ts.html +271 -0
  69. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +262 -0
  70. package/coverage/lcov-report/src/sdk/accounts/index.html +131 -0
  71. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +88 -0
  72. package/coverage/lcov-report/src/sdk/index.html +116 -0
  73. package/coverage/lcov-report/src/sdk/index.ts.html +106 -0
  74. package/coverage/lcov-report/src/sdk/users/events.ts.html +577 -0
  75. package/coverage/lcov-report/src/sdk/users/index.html +146 -0
  76. package/coverage/lcov-report/src/sdk/users/index.ts.html +88 -0
  77. package/coverage/lcov-report/src/sdk/users/users.ts.html +1942 -0
  78. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +1051 -0
  79. package/coverage/lcov-report/src/tests/api/accounts.ts.html +160 -0
  80. package/coverage/lcov-report/src/tests/api/auth.ts.html +220 -0
  81. package/coverage/lcov-report/src/tests/api/base.ts.html +133 -0
  82. package/coverage/lcov-report/src/tests/api/configs.ts.html +226 -0
  83. package/coverage/lcov-report/src/tests/api/email.ts.html +148 -0
  84. package/coverage/lcov-report/src/tests/api/environment.ts.html +130 -0
  85. package/coverage/lcov-report/src/tests/api/index.html +296 -0
  86. package/coverage/lcov-report/src/tests/api/index.ts.html +205 -0
  87. package/coverage/lcov-report/src/tests/api/migrations.ts.html +151 -0
  88. package/coverage/lcov-report/src/tests/api/restore.ts.html +127 -0
  89. package/coverage/lcov-report/src/tests/api/self.ts.html +163 -0
  90. package/coverage/lcov-report/src/tests/api/status.ts.html +121 -0
  91. package/coverage/lcov-report/src/tests/api/tenants.ts.html +130 -0
  92. package/coverage/lcov-report/src/tests/api/users.ts.html +511 -0
  93. package/coverage/lcov-report/src/tests/controllers.ts.html +100 -0
  94. package/coverage/lcov-report/src/tests/index.html +146 -0
  95. package/coverage/lcov-report/src/tests/index.ts.html +139 -0
  96. package/coverage/lcov-report/src/tests/mocks/email.js.html +115 -0
  97. package/coverage/lcov-report/src/tests/mocks/index.html +131 -0
  98. package/coverage/lcov-report/src/tests/mocks/index.ts.html +106 -0
  99. package/coverage/lcov-report/src/tests/structures/configs.ts.html +313 -0
  100. package/coverage/lcov-report/src/tests/structures/groups.ts.html +118 -0
  101. package/coverage/lcov-report/src/tests/structures/index.html +161 -0
  102. package/coverage/lcov-report/src/tests/structures/index.ts.html +151 -0
  103. package/coverage/lcov-report/src/tests/structures/users.ts.html +196 -0
  104. package/coverage/lcov-report/src/utilities/appService.ts.html +181 -0
  105. package/coverage/lcov-report/src/utilities/email.ts.html +850 -0
  106. package/coverage/lcov-report/src/utilities/fileSystem.ts.html +100 -0
  107. package/coverage/lcov-report/src/utilities/index.html +206 -0
  108. package/coverage/lcov-report/src/utilities/index.ts.html +112 -0
  109. package/coverage/lcov-report/src/utilities/redis.ts.html +424 -0
  110. package/coverage/lcov-report/src/utilities/templates.ts.html +232 -0
  111. package/coverage/lcov-report/src/utilities/users.ts.html +133 -0
  112. package/coverage/lcov.info +3989 -0
  113. package/jest.config.ts +29 -0
  114. package/package.json +14 -24
  115. package/scripts/localdomain.js +33 -7
  116. package/src/api/controllers/global/auth.ts +51 -43
  117. package/src/api/controllers/global/{configs.js → configs.ts} +119 -114
  118. package/src/api/controllers/global/{email.js → email.ts} +6 -5
  119. package/src/api/controllers/global/roles.ts +66 -0
  120. package/src/api/controllers/global/self.ts +2 -2
  121. package/src/api/controllers/global/users.ts +6 -3
  122. package/src/api/controllers/global/{workspaces.js → workspaces.ts} +15 -18
  123. package/src/api/controllers/system/{environment.js → environment.ts} +3 -2
  124. package/src/api/controllers/system/restore.ts +1 -1
  125. package/src/api/controllers/system/{status.js → status.ts} +4 -3
  126. package/src/api/controllers/system/tenants.ts +8 -51
  127. package/src/api/index.ts +31 -8
  128. package/src/api/routes/global/{auth.js → auth.ts} +31 -48
  129. package/src/api/routes/global/{configs.js → configs.ts} +22 -23
  130. package/src/api/routes/global/{email.js → email.ts} +9 -10
  131. package/src/api/routes/global/license.ts +1 -1
  132. package/src/api/routes/global/roles.ts +16 -0
  133. package/src/api/routes/global/self.ts +2 -2
  134. package/src/api/routes/global/templates.ts +4 -4
  135. package/src/api/routes/global/tests/auth.spec.ts +134 -64
  136. package/src/api/routes/global/tests/configs.spec.ts +34 -35
  137. package/src/api/routes/global/tests/email.spec.ts +4 -3
  138. package/src/api/routes/global/tests/license.spec.ts +31 -0
  139. package/src/api/routes/global/tests/realEmail.spec.ts +2 -3
  140. package/src/api/routes/global/tests/roles.spec.ts +27 -0
  141. package/src/api/routes/global/tests/self.spec.ts +3 -4
  142. package/src/api/routes/global/tests/templates.spec.ts +35 -0
  143. package/src/api/routes/global/tests/users.spec.ts +79 -46
  144. package/src/api/routes/global/tests/workspaces.spec.ts +29 -0
  145. package/src/api/routes/global/{users.js → users.ts} +28 -26
  146. package/src/api/routes/global/{workspaces.js → workspaces.ts} +9 -10
  147. package/src/api/routes/index.ts +2 -1
  148. package/src/api/routes/system/accounts.ts +1 -1
  149. package/src/api/routes/system/environment.ts +8 -0
  150. package/src/api/routes/system/migrations.ts +1 -1
  151. package/src/api/routes/system/restore.ts +1 -1
  152. package/src/api/routes/system/status.ts +8 -0
  153. package/src/api/routes/system/tenants.ts +13 -0
  154. package/src/api/routes/system/tests/accounts.spec.ts +5 -6
  155. package/src/api/routes/system/tests/environment.spec.ts +29 -0
  156. package/src/api/routes/system/tests/migrations.spec.ts +63 -0
  157. package/src/api/routes/system/tests/restore.spec.ts +36 -0
  158. package/src/api/routes/system/tests/status.spec.ts +48 -0
  159. package/src/api/routes/system/tests/tenants.spec.ts +61 -0
  160. package/src/api/routes/validation/users.ts +3 -3
  161. package/src/constants/{index.js → index.ts} +57 -64
  162. package/src/constants/templates/{index.js → index.ts} +23 -19
  163. package/src/db/index.ts +10 -0
  164. package/src/environment.ts +9 -7
  165. package/src/index.ts +15 -16
  166. package/src/middleware/{cloudRestricted.js → cloudRestricted.ts} +5 -4
  167. package/src/middleware/tests/tenancy.spec.ts +73 -0
  168. package/src/sdk/users/users.ts +28 -15
  169. package/src/tests/TestConfiguration.ts +99 -50
  170. package/src/tests/api/accounts.ts +5 -8
  171. package/src/tests/api/auth.ts +3 -6
  172. package/src/tests/api/base.ts +16 -0
  173. package/src/tests/api/configs.ts +15 -8
  174. package/src/tests/api/email.ts +3 -6
  175. package/src/tests/api/environment.ts +15 -0
  176. package/src/tests/api/index.ts +15 -0
  177. package/src/tests/api/migrations.ts +22 -0
  178. package/src/tests/api/restore.ts +14 -0
  179. package/src/tests/api/self.ts +11 -6
  180. package/src/tests/api/status.ts +12 -0
  181. package/src/tests/api/tenants.ts +15 -0
  182. package/src/tests/api/users.ts +38 -6
  183. package/src/tests/controllers.ts +5 -0
  184. package/src/tests/index.ts +5 -1
  185. package/{scripts/jestSetup.js → src/tests/jestSetup.ts} +12 -3
  186. package/src/tests/structures/{configs.js → configs.ts} +11 -11
  187. package/src/tests/structures/index.ts +5 -3
  188. package/src/tests/structures/users.ts +1 -0
  189. package/src/utilities/appService.ts +32 -0
  190. package/src/utilities/{email.js → email.ts} +78 -45
  191. package/src/utilities/fileSystem.ts +5 -0
  192. package/src/utilities/{index.js → index.ts} +1 -1
  193. package/src/utilities/{redis.js → redis.ts} +29 -24
  194. package/src/utilities/templates.ts +49 -0
  195. package/src/utilities/users.ts +16 -0
  196. package/tsconfig.build.json +2 -3
  197. package/tsconfig.json +3 -1
  198. package/src/api/controllers/global/roles.js +0 -68
  199. package/src/api/routes/global/roles.js +0 -12
  200. package/src/api/routes/system/environment.js +0 -8
  201. package/src/api/routes/system/status.js +0 -8
  202. package/src/api/routes/system/tenants.js +0 -12
  203. package/src/db/index.js +0 -11
  204. package/src/tests/controllers.js +0 -7
  205. package/src/tests/structures/accounts.ts +0 -24
  206. package/src/utilities/appService.js +0 -33
  207. package/src/utilities/fileSystem.js +0 -5
  208. package/src/utilities/templates.js +0 -49
  209. package/src/utilities/users.js +0 -17
@@ -5,6 +5,11 @@ import { ConfigAPI } from "./configs"
5
5
  import { EmailAPI } from "./email"
6
6
  import { SelfAPI } from "./self"
7
7
  import { UserAPI } from "./users"
8
+ import { EnvironmentAPI } from "./environment"
9
+ import { MigrationAPI } from "./migrations"
10
+ import { StatusAPI } from "./status"
11
+ import { RestoreAPI } from "./restore"
12
+ import { TenantAPI } from "./tenants"
8
13
 
9
14
  export default class API {
10
15
  accounts: AccountAPI
@@ -13,6 +18,11 @@ export default class API {
13
18
  emails: EmailAPI
14
19
  self: SelfAPI
15
20
  users: UserAPI
21
+ environment: EnvironmentAPI
22
+ migrations: MigrationAPI
23
+ status: StatusAPI
24
+ restore: RestoreAPI
25
+ tenants: TenantAPI
16
26
 
17
27
  constructor(config: TestConfiguration) {
18
28
  this.accounts = new AccountAPI(config)
@@ -21,5 +31,10 @@ export default class API {
21
31
  this.emails = new EmailAPI(config)
22
32
  this.self = new SelfAPI(config)
23
33
  this.users = new UserAPI(config)
34
+ this.environment = new EnvironmentAPI(config)
35
+ this.migrations = new MigrationAPI(config)
36
+ this.status = new StatusAPI(config)
37
+ this.restore = new RestoreAPI(config)
38
+ this.tenants = new TenantAPI(config)
24
39
  }
25
40
  }
@@ -0,0 +1,22 @@
1
+ import TestConfiguration from "../TestConfiguration"
2
+ import { TestAPI, TestAPIOpts } from "./base"
3
+
4
+ export class MigrationAPI extends TestAPI {
5
+ constructor(config: TestConfiguration) {
6
+ super(config)
7
+ }
8
+
9
+ runMigrations = (opts?: TestAPIOpts) => {
10
+ return this.request
11
+ .post(`/api/system/migrations/run`)
12
+ .set(opts?.headers ? opts.headers : this.config.internalAPIHeaders())
13
+ .expect(opts?.status ? opts.status : 200)
14
+ }
15
+
16
+ getMigrationDefinitions = (opts?: TestAPIOpts) => {
17
+ return this.request
18
+ .get(`/api/system/migrations/definitions`)
19
+ .set(opts?.headers ? opts.headers : this.config.internalAPIHeaders())
20
+ .expect(opts?.status ? opts.status : 200)
21
+ }
22
+ }
@@ -0,0 +1,14 @@
1
+ import TestConfiguration from "../TestConfiguration"
2
+ import { TestAPI, TestAPIOpts } from "./base"
3
+
4
+ export class RestoreAPI extends TestAPI {
5
+ constructor(config: TestConfiguration) {
6
+ super(config)
7
+ }
8
+
9
+ restored = (opts?: TestAPIOpts) => {
10
+ return this.request
11
+ .post(`/api/system/restored`)
12
+ .expect(opts?.status ? opts.status : 200)
13
+ }
14
+ }
@@ -1,13 +1,10 @@
1
1
  import TestConfiguration from "../TestConfiguration"
2
2
  import { User } from "@budibase/types"
3
+ import { TestAPI } from "./base"
3
4
 
4
- export class SelfAPI {
5
- config: TestConfiguration
6
- request: any
7
-
5
+ export class SelfAPI extends TestAPI {
8
6
  constructor(config: TestConfiguration) {
9
- this.config = config
10
- this.request = config.request
7
+ super(config)
11
8
  }
12
9
 
13
10
  updateSelf = (user: User) => {
@@ -18,4 +15,12 @@ export class SelfAPI {
18
15
  .expect("Content-Type", /json/)
19
16
  .expect(200)
20
17
  }
18
+
19
+ getSelf = (user: User) => {
20
+ return this.request
21
+ .get(`/api/global/self`)
22
+ .set(this.config.authHeaders(user))
23
+ .expect("Content-Type", /json/)
24
+ .expect(200)
25
+ }
21
26
  }
@@ -0,0 +1,12 @@
1
+ import TestConfiguration from "../TestConfiguration"
2
+ import { TestAPI } from "./base"
3
+
4
+ export class StatusAPI extends TestAPI {
5
+ constructor(config: TestConfiguration) {
6
+ super(config)
7
+ }
8
+
9
+ getStatus = () => {
10
+ return this.request.get(`/api/system/status`).expect(200)
11
+ }
12
+ }
@@ -0,0 +1,15 @@
1
+ import TestConfiguration from "../TestConfiguration"
2
+ import { TestAPI, TestAPIOpts } from "./base"
3
+
4
+ export class TenantAPI extends TestAPI {
5
+ constructor(config: TestConfiguration) {
6
+ super(config)
7
+ }
8
+
9
+ delete = (tenantId: string, opts?: TestAPIOpts) => {
10
+ return this.request
11
+ .delete(`/api/system/tenants/${tenantId}`)
12
+ .set(opts?.headers)
13
+ .expect(opts?.status ? opts.status : 204)
14
+ }
15
+ }
@@ -3,16 +3,16 @@ import {
3
3
  BulkUserRequest,
4
4
  InviteUsersRequest,
5
5
  User,
6
+ CreateAdminUserRequest,
6
7
  } from "@budibase/types"
8
+ import * as structures from "../structures"
9
+ import { generator } from "@budibase/backend-core/tests"
7
10
  import TestConfiguration from "../TestConfiguration"
11
+ import { TestAPI, TestAPIOpts } from "./base"
8
12
 
9
- export class UserAPI {
10
- config: TestConfiguration
11
- request: any
12
-
13
+ export class UserAPI extends TestAPI {
13
14
  constructor(config: TestConfiguration) {
14
- this.config = config
15
- this.request = config.request
15
+ super(config)
16
16
  }
17
17
 
18
18
  // INVITE
@@ -91,6 +91,30 @@ export class UserAPI {
91
91
 
92
92
  // USER
93
93
 
94
+ createAdminUser = async (
95
+ request?: CreateAdminUserRequest,
96
+ opts?: TestAPIOpts
97
+ ) => {
98
+ if (!request) {
99
+ request = {
100
+ email: structures.email(),
101
+ password: generator.string(),
102
+ tenantId: structures.uuid(),
103
+ }
104
+ }
105
+ const res = await this.request
106
+ .post(`/api/global/users/init`)
107
+ .send(request)
108
+ .set(this.config.internalAPIHeaders())
109
+ .expect("Content-Type", /json/)
110
+ .expect(opts?.status ? opts.status : 200)
111
+
112
+ return {
113
+ ...request,
114
+ userId: res.body._id,
115
+ }
116
+ }
117
+
94
118
  saveUser = (user: User, status?: number, headers?: any) => {
95
119
  return this.request
96
120
  .post(`/api/global/users`)
@@ -107,4 +131,12 @@ export class UserAPI {
107
131
  .expect("Content-Type", /json/)
108
132
  .expect(status ? status : 200)
109
133
  }
134
+
135
+ getUser = (userId: string, opts?: TestAPIOpts) => {
136
+ return this.request
137
+ .get(`/api/global/users/${userId}`)
138
+ .set(opts?.headers ? opts.headers : this.config.defaultHeaders())
139
+ .expect("Content-Type", /json/)
140
+ .expect(opts?.status ? opts.status : 200)
141
+ }
110
142
  }
@@ -0,0 +1,5 @@
1
+ export * as email from "../api/controllers/global/email"
2
+ export * as workspaces from "../api/controllers/global/workspaces"
3
+ export * as config from "../api/controllers/global/configs"
4
+ export * as templates from "../api/controllers/global/templates"
5
+ export * as users from "../api/controllers/global/users"
@@ -1,10 +1,14 @@
1
+ import mocks from "./mocks"
2
+ import { generator } from "@budibase/backend-core/tests"
1
3
  import TestConfiguration from "./TestConfiguration"
2
4
  import structures from "./structures"
3
- import mocks from "./mocks"
4
5
  import API from "./api"
6
+ import { v4 as uuid } from "uuid"
5
7
 
6
8
  const pkg = {
7
9
  structures,
10
+ generator,
11
+ uuid,
8
12
  TENANT_1: structures.TENANT_1,
9
13
  mocks,
10
14
  TestConfiguration,
@@ -1,12 +1,21 @@
1
- const env = require("../src/environment")
1
+ import env from "../environment"
2
2
 
3
- env._set("SELF_HOSTED", "1")
3
+ env._set("SELF_HOSTED", "0")
4
4
  env._set("NODE_ENV", "jest")
5
5
  env._set("JWT_SECRET", "test-jwtsecret")
6
6
  env._set("LOG_LEVEL", "silent")
7
7
  env._set("MULTI_TENANCY", true)
8
+ env._set("MINIO_URL", "http://localhost")
9
+ env._set("MINIO_ACCESS_KEY", "test")
10
+ env._set("MINIO_SECRET_KEY", "test")
11
+ env._set("PLATFORM_URL", "http://localhost:10000")
12
+ env._set("INTERNAL_API_KEY", "test")
13
+ env._set("DISABLE_ACCOUNT_PORTAL", false)
8
14
 
9
- const { mocks } = require("@budibase/backend-core/tests")
15
+ import { mocks } from "@budibase/backend-core/tests"
16
+
17
+ // must explicitly enable fetch mock
18
+ mocks.fetch.enable()
10
19
 
11
20
  // mock all dates to 2020-01-01T00:00:00.000Z
12
21
  // use tk.reset() to use real dates in individual tests
@@ -1,9 +1,9 @@
1
- const { Configs } = require("../../constants")
1
+ const { Config } = require("../../constants")
2
2
  const { utils } = require("@budibase/backend-core")
3
3
 
4
- exports.oidc = conf => {
4
+ export function oidc(conf?: any) {
5
5
  return {
6
- type: Configs.OIDC,
6
+ type: Config.OIDC,
7
7
  config: {
8
8
  configs: [
9
9
  {
@@ -21,9 +21,9 @@ exports.oidc = conf => {
21
21
  }
22
22
  }
23
23
 
24
- exports.google = conf => {
24
+ export function google(conf?: any) {
25
25
  return {
26
- type: Configs.GOOGLE,
26
+ type: Config.GOOGLE,
27
27
  config: {
28
28
  clientID: "clientId",
29
29
  clientSecret: "clientSecret",
@@ -33,9 +33,9 @@ exports.google = conf => {
33
33
  }
34
34
  }
35
35
 
36
- exports.smtp = conf => {
36
+ export function smtp(conf?: any) {
37
37
  return {
38
- type: Configs.SMTP,
38
+ type: Config.SMTP,
39
39
  config: {
40
40
  port: 12345,
41
41
  host: "smtptesthost.com",
@@ -47,9 +47,9 @@ exports.smtp = conf => {
47
47
  }
48
48
  }
49
49
 
50
- exports.smtpEthereal = () => {
50
+ export function smtpEthereal() {
51
51
  return {
52
- type: Configs.SMTP,
52
+ type: Config.SMTP,
53
53
  config: {
54
54
  port: 587,
55
55
  host: "smtp.ethereal.email",
@@ -63,9 +63,9 @@ exports.smtpEthereal = () => {
63
63
  }
64
64
  }
65
65
 
66
- exports.settings = conf => {
66
+ export function settings(conf?: any) {
67
67
  return {
68
- type: Configs.SETTINGS,
68
+ type: Config.SETTINGS,
69
69
  config: {
70
70
  platformUrl: "http://localhost:10000",
71
71
  logoUrl: "",
@@ -1,16 +1,18 @@
1
- import configs from "./configs"
1
+ import { structures } from "@budibase/backend-core/tests"
2
+ import * as configs from "./configs"
2
3
  import * as users from "./users"
3
4
  import * as groups from "./groups"
4
- import * as accounts from "./accounts"
5
+ import { v4 as uuid } from "uuid"
5
6
 
6
7
  const TENANT_ID = "default"
7
8
  const TENANT_1 = "tenant1"
8
9
  const CSRF_TOKEN = "e3727778-7af0-4226-b5eb-f43cbe60a306"
9
10
 
10
11
  const pkg = {
12
+ ...structures,
13
+ uuid,
11
14
  configs,
12
15
  users,
13
- accounts,
14
16
  TENANT_ID,
15
17
  TENANT_1,
16
18
  CSRF_TOKEN,
@@ -5,6 +5,7 @@ import { v4 as uuid } from "uuid"
5
5
  export const newEmail = () => {
6
6
  return `${uuid()}@test.com`
7
7
  }
8
+
8
9
  export const user = (userProps?: any): User => {
9
10
  return {
10
11
  email: newEmail(),
@@ -0,0 +1,32 @@
1
+ import fetch from "node-fetch"
2
+ import { constants, tenancy } from "@budibase/backend-core"
3
+ import { checkSlashesInUrl } from "../utilities"
4
+ import env from "../environment"
5
+
6
+ async function makeAppRequest(url: string, method: string, body: any) {
7
+ if (env.isTest()) {
8
+ return
9
+ }
10
+ const request: any = { headers: {} }
11
+ request.headers[constants.Header.API_KEY] = env.INTERNAL_API_KEY
12
+ if (tenancy.isTenantIdSet()) {
13
+ request.headers[constants.Header.TENANT_ID] = tenancy.getTenantId()
14
+ }
15
+ if (body) {
16
+ request.headers["Content-Type"] = "application/json"
17
+ request.body = JSON.stringify(body)
18
+ }
19
+ request.method = method
20
+ return fetch(checkSlashesInUrl(env.APPS_URL + url), request)
21
+ }
22
+
23
+ export async function syncUserInApps(userId: string) {
24
+ const response = await makeAppRequest(
25
+ `/api/users/metadata/sync/${userId}`,
26
+ "POST",
27
+ {}
28
+ )
29
+ if (response && response.status !== 200) {
30
+ throw "Unable to sync user."
31
+ }
32
+ }
@@ -1,15 +1,33 @@
1
+ import env from "../environment"
2
+ import { EmailTemplatePurpose, TemplateType, Config } from "../constants"
3
+ import { getTemplateByPurpose } from "../constants/templates"
4
+ import { getSettingsTemplateContext } from "./templates"
5
+ import { processString } from "@budibase/string-templates"
6
+ import { getResetPasswordCode, getInviteCode } from "./redis"
7
+ import { User, Database } from "@budibase/types"
8
+ import { tenancy, db as dbCore } from "@budibase/backend-core"
1
9
  const nodemailer = require("nodemailer")
2
- const env = require("../environment")
3
- const { getScopedConfig } = require("@budibase/backend-core/db")
4
- const { EmailTemplatePurpose, TemplateTypes, Configs } = require("../constants")
5
- const { getTemplateByPurpose } = require("../constants/templates")
6
- const { getSettingsTemplateContext } = require("./templates")
7
- const { processString } = require("@budibase/string-templates")
8
- const { getResetPasswordCode, getInviteCode } = require("../utilities/redis")
9
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
10
+
11
+ type SendEmailOpts = {
12
+ // workspaceId If finer grain controls being used then this will lookup config for workspace.
13
+ workspaceId?: string
14
+ // user If sending to an existing user the object can be provided, this is used in the context.
15
+ user: User
16
+ // from If sending from an address that is not what is configured in the SMTP config.
17
+ from?: string
18
+ // contents If sending a custom email then can supply contents which will be added to it.
19
+ contents?: string
20
+ // subject A custom subject can be specified if the config one is not desired.
21
+ subject?: string
22
+ // info Pass in a structure of information to be stored alongside the invitation.
23
+ info?: any
24
+ cc?: boolean
25
+ bcc?: boolean
26
+ automation?: boolean
27
+ }
10
28
 
11
29
  const TEST_MODE = false
12
- const TYPE = TemplateTypes.EMAIL
30
+ const TYPE = TemplateType.EMAIL
13
31
 
14
32
  const FULL_EMAIL_PURPOSES = [
15
33
  EmailTemplatePurpose.INVITATION,
@@ -18,8 +36,8 @@ const FULL_EMAIL_PURPOSES = [
18
36
  EmailTemplatePurpose.CUSTOM,
19
37
  ]
20
38
 
21
- function createSMTPTransport(config) {
22
- let options
39
+ function createSMTPTransport(config: any) {
40
+ let options: any
23
41
  let secure = config.secure
24
42
  // default it if not specified
25
43
  if (secure == null) {
@@ -52,10 +70,15 @@ function createSMTPTransport(config) {
52
70
  return nodemailer.createTransport(options)
53
71
  }
54
72
 
55
- async function getLinkCode(purpose, email, user, info = null) {
73
+ async function getLinkCode(
74
+ purpose: EmailTemplatePurpose,
75
+ email: string,
76
+ user: User,
77
+ info: any = null
78
+ ) {
56
79
  switch (purpose) {
57
80
  case EmailTemplatePurpose.PASSWORD_RECOVERY:
58
- return getResetPasswordCode(user._id, info)
81
+ return getResetPasswordCode(user._id!, info)
59
82
  case EmailTemplatePurpose.INVITATION:
60
83
  return getInviteCode(email, info)
61
84
  default:
@@ -72,7 +95,12 @@ async function getLinkCode(purpose, email, user, info = null) {
72
95
  * @param {string|null} contents if using a custom template can supply contents for context.
73
96
  * @return {Promise<string>} returns the built email HTML if all provided parameters were valid.
74
97
  */
75
- async function buildEmail(purpose, email, context, { user, contents } = {}) {
98
+ async function buildEmail(
99
+ purpose: EmailTemplatePurpose,
100
+ email: string,
101
+ context: any,
102
+ { user, contents }: any = {}
103
+ ) {
76
104
  // this isn't a full email
77
105
  if (FULL_EMAIL_PURPOSES.indexOf(purpose) === -1) {
78
106
  throw `Unable to build an email of type ${purpose}`
@@ -113,15 +141,19 @@ async function buildEmail(purpose, email, context, { user, contents } = {}) {
113
141
  * @param {boolean|null} automation Whether or not the configuration is being fetched for an email automation.
114
142
  * @return {Promise<object|null>} returns the SMTP configuration if it exists
115
143
  */
116
- async function getSmtpConfiguration(db, workspaceId = null, automation) {
117
- const params = {
118
- type: Configs.SMTP,
144
+ async function getSmtpConfiguration(
145
+ db: Database,
146
+ workspaceId?: string,
147
+ automation?: boolean
148
+ ) {
149
+ const params: any = {
150
+ type: Config.SMTP,
119
151
  }
120
152
  if (workspaceId) {
121
153
  params.workspace = workspaceId
122
154
  }
123
155
 
124
- const customConfig = await getScopedConfig(db, params)
156
+ const customConfig = await dbCore.getScopedConfig(db, params)
125
157
 
126
158
  if (customConfig) {
127
159
  return customConfig
@@ -146,12 +178,12 @@ async function getSmtpConfiguration(db, workspaceId = null, automation) {
146
178
  * Checks if a SMTP config exists based on passed in parameters.
147
179
  * @return {Promise<boolean>} returns true if there is a configuration that can be used.
148
180
  */
149
- exports.isEmailConfigured = async (workspaceId = null) => {
181
+ export async function isEmailConfigured(workspaceId?: string) {
150
182
  // when "testing" or smtp fallback is enabled simply return true
151
183
  if (TEST_MODE || env.SMTP_FALLBACK_ENABLED) {
152
184
  return true
153
185
  }
154
- const db = getGlobalDB()
186
+ const db = tenancy.getGlobalDB()
155
187
  const config = await getSmtpConfiguration(db, workspaceId)
156
188
  return config != null
157
189
  }
@@ -161,48 +193,49 @@ exports.isEmailConfigured = async (workspaceId = null) => {
161
193
  * send an email using it.
162
194
  * @param {string} email The email address to send to.
163
195
  * @param {string} purpose The purpose of the email being sent (e.g. reset password).
164
- * @param {string|undefined} workspaceId If finer grain controls being used then this will lookup config for workspace.
165
- * @param {object|undefined} user If sending to an existing user the object can be provided, this is used in the context.
166
- * @param {string|undefined} from If sending from an address that is not what is configured in the SMTP config.
167
- * @param {string|undefined} contents If sending a custom email then can supply contents which will be added to it.
168
- * @param {string|undefined} subject A custom subject can be specified if the config one is not desired.
169
- * @param {object|undefined} info Pass in a structure of information to be stored alongside the invitation.
170
- * @param {boolean|undefined} disableFallback Prevent email being sent from SMTP fallback to avoid spam.
196
+ * @param {object} opts The options for sending the email.
171
197
  * @return {Promise<object>} returns details about the attempt to send email, e.g. if it is successful; based on
172
198
  * nodemailer response.
173
199
  */
174
- exports.sendEmail = async (
175
- email,
176
- purpose,
177
- { workspaceId, user, from, contents, subject, info, cc, bcc, automation } = {}
178
- ) => {
179
- const db = getGlobalDB()
180
- let config = (await getSmtpConfiguration(db, workspaceId, automation)) || {}
200
+ export async function sendEmail(
201
+ email: string,
202
+ purpose: EmailTemplatePurpose,
203
+ opts: SendEmailOpts
204
+ ) {
205
+ const db = tenancy.getGlobalDB()
206
+ let config =
207
+ (await getSmtpConfiguration(db, opts?.workspaceId, opts?.automation)) || {}
181
208
  if (Object.keys(config).length === 0 && !TEST_MODE) {
182
209
  throw "Unable to find SMTP configuration."
183
210
  }
184
211
  const transport = createSMTPTransport(config)
185
212
  // if there is a link code needed this will retrieve it
186
- const code = await getLinkCode(purpose, email, user, info)
187
- const context = await getSettingsTemplateContext(purpose, code)
213
+ const code = await getLinkCode(purpose, email, opts.user, opts?.info)
214
+ let context
215
+ if (code) {
216
+ context = await getSettingsTemplateContext(purpose, code)
217
+ }
188
218
 
189
- let message = {
190
- from: from || config.from,
219
+ let message: any = {
220
+ from: opts?.from || config.from,
191
221
  html: await buildEmail(purpose, email, context, {
192
- user,
193
- contents,
222
+ user: opts?.user,
223
+ contents: opts?.contents,
194
224
  }),
195
225
  }
196
226
 
197
227
  message = {
198
228
  ...message,
199
229
  to: email,
200
- cc: cc,
201
- bcc: bcc,
230
+ cc: opts?.cc,
231
+ bcc: opts?.bcc,
202
232
  }
203
233
 
204
- if (subject || config.subject) {
205
- message.subject = await processString(subject || config.subject, context)
234
+ if (opts?.subject || config.subject) {
235
+ message.subject = await processString(
236
+ opts?.subject || config.subject,
237
+ context
238
+ )
206
239
  }
207
240
  const response = await transport.sendMail(message)
208
241
  if (TEST_MODE) {
@@ -216,7 +249,7 @@ exports.sendEmail = async (
216
249
  * @param {object} config an SMTP configuration - this is based on the nodemailer API.
217
250
  * @return {Promise<boolean>} returns true if the configuration is valid.
218
251
  */
219
- exports.verifyConfig = async config => {
252
+ export async function verifyConfig(config: any) {
220
253
  const transport = createSMTPTransport(config)
221
254
  await transport.verify()
222
255
  }
@@ -0,0 +1,5 @@
1
+ import { readFileSync } from "fs"
2
+
3
+ export function readStaticFile(path: string) {
4
+ return readFileSync(path, "utf-8")
5
+ }
@@ -4,6 +4,6 @@
4
4
  * @param {string} url The URL to test and remove any extra double slashes.
5
5
  * @return {string} The updated url.
6
6
  */
7
- exports.checkSlashesInUrl = url => {
7
+ export function checkSlashesInUrl(url: string) {
8
8
  return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
9
9
  }