@budibase/worker 2.1.30 → 2.1.32-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 (182) hide show
  1. package/__mocks__/aws-sdk.ts +15 -0
  2. package/__mocks__/oauth.ts +57 -0
  3. package/coverage/clover.xml +2160 -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 +913 -0
  14. package/coverage/lcov-report/src/api/controllers/global/configs.js.html +1390 -0
  15. package/coverage/lcov-report/src/api/controllers/global/email.js.html +193 -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.js.html +289 -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 +796 -0
  22. package/coverage/lcov-report/src/api/controllers/global/workspaces.js.html +253 -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.js.html +349 -0
  33. package/coverage/lcov-report/src/api/routes/global/configs.js.html +460 -0
  34. package/coverage/lcov-report/src/api/routes/global/email.js.html +196 -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.js.html +121 -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.js.html +403 -0
  41. package/coverage/lcov-report/src/api/routes/global/workspaces.js.html +199 -0
  42. package/coverage/lcov-report/src/api/routes/index.html +116 -0
  43. package/coverage/lcov-report/src/api/routes/index.ts.html +193 -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.js.html +304 -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 +364 -0
  61. package/coverage/lcov-report/src/index.html +131 -0
  62. package/coverage/lcov-report/src/index.ts.html +397 -0
  63. package/coverage/lcov-report/src/middleware/cloudRestricted.js.html +136 -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 +1903 -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.js.html +106 -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.js.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.js.html +184 -0
  105. package/coverage/lcov-report/src/utilities/email.ts.html +850 -0
  106. package/coverage/lcov-report/src/utilities/fileSystem.js.html +100 -0
  107. package/coverage/lcov-report/src/utilities/index.html +206 -0
  108. package/coverage/lcov-report/src/utilities/index.js.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.js.html +136 -0
  112. package/coverage/lcov.info +3986 -0
  113. package/jest.config.ts +29 -0
  114. package/package.json +13 -23
  115. package/scripts/localdomain.js +33 -7
  116. package/src/api/controllers/global/auth.ts +35 -31
  117. package/src/api/controllers/global/configs.js +17 -17
  118. package/src/api/controllers/global/self.ts +2 -2
  119. package/src/api/controllers/global/users.ts +3 -1
  120. package/src/api/controllers/system/{environment.js → environment.ts} +3 -2
  121. package/src/api/controllers/system/{status.js → status.ts} +4 -3
  122. package/src/api/controllers/system/tenants.ts +8 -51
  123. package/src/api/index.ts +29 -6
  124. package/src/api/routes/global/auth.js +22 -39
  125. package/src/api/routes/global/configs.js +9 -9
  126. package/src/api/routes/global/templates.ts +2 -2
  127. package/src/api/routes/global/tests/auth.spec.ts +134 -64
  128. package/src/api/routes/global/tests/configs.spec.ts +34 -35
  129. package/src/api/routes/global/tests/email.spec.ts +4 -3
  130. package/src/api/routes/global/tests/license.spec.ts +31 -0
  131. package/src/api/routes/global/tests/realEmail.spec.ts +2 -3
  132. package/src/api/routes/global/tests/roles.spec.ts +27 -0
  133. package/src/api/routes/global/tests/self.spec.ts +3 -4
  134. package/src/api/routes/global/tests/templates.spec.ts +35 -0
  135. package/src/api/routes/global/tests/users.spec.ts +51 -43
  136. package/src/api/routes/global/tests/workspaces.spec.ts +29 -0
  137. package/src/api/routes/system/environment.ts +8 -0
  138. package/src/api/routes/system/status.ts +8 -0
  139. package/src/api/routes/system/tenants.ts +13 -0
  140. package/src/api/routes/system/tests/accounts.spec.ts +5 -6
  141. package/src/api/routes/system/tests/environment.spec.ts +29 -0
  142. package/src/api/routes/system/tests/migrations.spec.ts +63 -0
  143. package/src/api/routes/system/tests/restore.spec.ts +36 -0
  144. package/src/api/routes/system/tests/status.spec.ts +48 -0
  145. package/src/api/routes/system/tests/tenants.spec.ts +61 -0
  146. package/src/constants/{index.js → index.ts} +57 -64
  147. package/src/constants/templates/index.js +2 -2
  148. package/src/db/index.ts +10 -0
  149. package/src/index.ts +1 -1
  150. package/src/middleware/cloudRestricted.js +2 -2
  151. package/src/middleware/tests/tenancy.spec.ts +73 -0
  152. package/src/tests/TestConfiguration.ts +98 -49
  153. package/src/tests/api/accounts.ts +5 -8
  154. package/src/tests/api/auth.ts +3 -6
  155. package/src/tests/api/base.ts +16 -0
  156. package/src/tests/api/configs.ts +15 -8
  157. package/src/tests/api/email.ts +3 -6
  158. package/src/tests/api/environment.ts +15 -0
  159. package/src/tests/api/index.ts +15 -0
  160. package/src/tests/api/migrations.ts +22 -0
  161. package/src/tests/api/restore.ts +14 -0
  162. package/src/tests/api/self.ts +11 -6
  163. package/src/tests/api/status.ts +12 -0
  164. package/src/tests/api/tenants.ts +15 -0
  165. package/src/tests/api/users.ts +38 -6
  166. package/src/tests/index.ts +5 -1
  167. package/{scripts/jestSetup.js → src/tests/jestSetup.ts} +12 -3
  168. package/src/tests/structures/configs.js +6 -6
  169. package/src/tests/structures/index.ts +4 -2
  170. package/src/tests/structures/users.ts +1 -0
  171. package/src/utilities/appService.js +3 -3
  172. package/src/utilities/{email.js → email.ts} +78 -45
  173. package/src/utilities/{redis.js → redis.ts} +29 -24
  174. package/src/utilities/templates.ts +49 -0
  175. package/tsconfig.build.json +2 -2
  176. package/tsconfig.json +3 -1
  177. package/src/api/routes/system/environment.js +0 -8
  178. package/src/api/routes/system/status.js +0 -8
  179. package/src/api/routes/system/tenants.js +0 -12
  180. package/src/db/index.js +0 -11
  181. package/src/tests/structures/accounts.ts +0 -24
  182. package/src/utilities/templates.js +0 -49
@@ -2,7 +2,6 @@ const Router = require("@koa/router")
2
2
  const authController = require("../../controllers/global/auth")
3
3
  const { joiValidator } = require("@budibase/backend-core/auth")
4
4
  const Joi = require("joi")
5
- const { updateTenantId } = require("@budibase/backend-core/tenancy")
6
5
 
7
6
  const router = new Router()
8
7
 
@@ -29,77 +28,61 @@ function buildResetUpdateValidation() {
29
28
  }).required().unknown(false))
30
29
  }
31
30
 
32
- function updateTenant(ctx, next) {
33
- if (ctx.params) {
34
- updateTenantId(ctx.params.tenantId)
35
- }
36
- return next()
37
- }
38
-
39
31
  router
32
+ // PASSWORD
40
33
  .post(
41
34
  "/api/global/auth/:tenantId/login",
42
35
  buildAuthValidation(),
43
- updateTenant,
44
36
  authController.authenticate
45
37
  )
38
+ .post("/api/global/auth/logout", authController.logout)
46
39
  .post(
47
40
  "/api/global/auth/:tenantId/reset",
48
41
  buildResetValidation(),
49
- updateTenant,
50
42
  authController.reset
51
43
  )
52
44
  .post(
53
45
  "/api/global/auth/:tenantId/reset/update",
54
46
  buildResetUpdateValidation(),
55
- updateTenant,
56
47
  authController.resetUpdate
57
48
  )
58
- .post("/api/global/auth/logout", authController.logout)
49
+ // INIT
59
50
  .post("/api/global/auth/init", authController.setInitInfo)
60
51
  .get("/api/global/auth/init", authController.getInitInfo)
61
- .get(
62
- "/api/global/auth/:tenantId/google",
63
- updateTenant,
64
- authController.googlePreAuth
65
- )
52
+
53
+ // DATASOURCE - MULTI TENANT
66
54
  .get(
67
55
  "/api/global/auth/:tenantId/datasource/:provider",
68
- updateTenant,
69
56
  authController.datasourcePreAuth
70
57
  )
71
- // single tenancy endpoint
72
- .get("/api/global/auth/google/callback", authController.googleAuth)
73
58
  .get(
74
- "/api/global/auth/datasource/:provider/callback",
59
+ "/api/global/auth/:tenantId/datasource/:provider/callback",
75
60
  authController.datasourceAuth
76
61
  )
77
- // multi-tenancy endpoint
78
- .get(
79
- "/api/global/auth/:tenantId/google/callback",
80
- updateTenant,
81
- authController.googleAuth
82
- )
62
+
63
+ // DATASOURCE - SINGLE TENANT - DEPRECATED
83
64
  .get(
84
- "/api/global/auth/:tenantId/datasource/:provider/callback",
85
- updateTenant,
65
+ "/api/global/auth/datasource/:provider/callback",
86
66
  authController.datasourceAuth
87
67
  )
68
+
69
+ // GOOGLE - MULTI TENANT
70
+ .get("/api/global/auth/:tenantId/google", authController.googlePreAuth)
71
+ .get("/api/global/auth/:tenantId/google/callback", authController.googleAuth)
72
+
73
+ // GOOGLE - SINGLE TENANT - DEPRECATED
74
+ .get("/api/global/auth/google/callback", authController.googleAuth)
75
+ .get("/api/admin/auth/google/callback", authController.googleAuth)
76
+
77
+ // OIDC - MULTI TENANT
88
78
  .get(
89
79
  "/api/global/auth/:tenantId/oidc/configs/:configId",
90
- updateTenant,
91
80
  authController.oidcPreAuth
92
81
  )
93
- // single tenancy endpoint
82
+ .get("/api/global/auth/:tenantId/oidc/callback", authController.oidcAuth)
83
+
84
+ // OIDC - SINGLE TENANT - DEPRECATED
94
85
  .get("/api/global/auth/oidc/callback", authController.oidcAuth)
95
- // multi-tenancy endpoint
96
- .get(
97
- "/api/global/auth/:tenantId/oidc/callback",
98
- updateTenant,
99
- authController.oidcAuth
100
- )
101
- // deprecated - used by the default system before tenancy
102
- .get("/api/admin/auth/google/callback", authController.googleAuth)
103
86
  .get("/api/admin/auth/oidc/callback", authController.oidcAuth)
104
87
 
105
88
  module.exports = router
@@ -3,7 +3,7 @@ const controller = require("../../controllers/global/configs")
3
3
  const { joiValidator } = require("@budibase/backend-core/auth")
4
4
  const { adminOnly } = require("@budibase/backend-core/auth")
5
5
  const Joi = require("joi")
6
- const { Configs } = require("../../../constants")
6
+ const { Config } = require("../../../constants")
7
7
 
8
8
  const router = new Router()
9
9
 
@@ -65,17 +65,17 @@ function buildConfigSaveValidation() {
65
65
  _id: Joi.string().optional(),
66
66
  _rev: Joi.string().optional(),
67
67
  workspace: Joi.string().optional(),
68
- type: Joi.string().valid(...Object.values(Configs)).required(),
68
+ type: Joi.string().valid(...Object.values(Config)).required(),
69
69
  createdAt: Joi.string().optional(),
70
70
  updatedAt: Joi.string().optional(),
71
71
  config: Joi.alternatives()
72
72
  .conditional("type", {
73
73
  switch: [
74
- { is: Configs.SMTP, then: smtpValidation() },
75
- { is: Configs.SETTINGS, then: settingValidation() },
76
- { is: Configs.ACCOUNT, then: Joi.object().unknown(true) },
77
- { is: Configs.GOOGLE, then: googleValidation() },
78
- { is: Configs.OIDC, then: oidcValidation() }
74
+ { is: Config.SMTP, then: smtpValidation() },
75
+ { is: Config.SETTINGS, then: settingValidation() },
76
+ { is: Config.ACCOUNT, then: Joi.object().unknown(true) },
77
+ { is: Config.GOOGLE, then: googleValidation() },
78
+ { is: Config.OIDC, then: oidcValidation() }
79
79
  ],
80
80
  }),
81
81
  }).required().unknown(true),
@@ -85,7 +85,7 @@ function buildConfigSaveValidation() {
85
85
  function buildUploadValidation() {
86
86
  // prettier-ignore
87
87
  return joiValidator.params(Joi.object({
88
- type: Joi.string().valid(...Object.values(Configs)).required(),
88
+ type: Joi.string().valid(...Object.values(Config)).required(),
89
89
  name: Joi.string().required(),
90
90
  }).required().unknown(true))
91
91
  }
@@ -93,7 +93,7 @@ function buildUploadValidation() {
93
93
  function buildConfigGetValidation() {
94
94
  // prettier-ignore
95
95
  return joiValidator.params(Joi.object({
96
- type: Joi.string().valid(...Object.values(Configs)).required()
96
+ type: Joi.string().valid(...Object.values(Config)).required()
97
97
  }).required().unknown(true))
98
98
  }
99
99
 
@@ -1,6 +1,6 @@
1
1
  import Router from "@koa/router"
2
2
  import * as controller from "../../controllers/global/templates"
3
- import { TemplatePurpose, TemplateTypes } from "../../../constants"
3
+ import { TemplatePurpose, TemplateType } from "../../../constants"
4
4
  import { auth as authCore } from "@budibase/backend-core"
5
5
  import Joi from "joi"
6
6
  const { adminOnly, joiValidator } = authCore
@@ -16,7 +16,7 @@ function buildTemplateSaveValidation() {
16
16
  name: Joi.string().allow(null, ""),
17
17
  contents: Joi.string().required(),
18
18
  purpose: Joi.string().required().valid(...Object.values(TemplatePurpose)),
19
- type: Joi.string().required().valid(...Object.values(TemplateTypes)),
19
+ type: Joi.string().required().valid(...Object.values(TemplateType)),
20
20
  }).required().unknown(true).optional())
21
21
  }
22
22
 
@@ -1,11 +1,16 @@
1
1
  jest.mock("nodemailer")
2
- import { TestConfiguration, mocks, API } from "../../../../tests"
2
+ import { TestConfiguration, mocks } from "../../../../tests"
3
3
  const sendMailMock = mocks.email.mock()
4
4
  import { events } from "@budibase/backend-core"
5
5
 
6
+ const expectSetAuthCookie = (res: any) => {
7
+ expect(
8
+ res.get("Set-Cookie").find((c: string) => c.startsWith("budibase:auth"))
9
+ ).toBeDefined()
10
+ }
11
+
6
12
  describe("/api/global/auth", () => {
7
13
  const config = new TestConfiguration()
8
- const api = new API(config)
9
14
 
10
15
  beforeAll(async () => {
11
16
  await config.beforeAll()
@@ -19,90 +24,155 @@ describe("/api/global/auth", () => {
19
24
  jest.clearAllMocks()
20
25
  })
21
26
 
22
- it("should logout", async () => {
23
- await api.auth.logout()
24
- expect(events.auth.logout).toBeCalledTimes(1)
25
- })
27
+ describe("password", () => {
28
+ describe("POST /api/global/auth/:tenantId/login", () => {
29
+ it("should login", () => {})
30
+ })
26
31
 
27
- it("should be able to generate password reset email", async () => {
28
- const { res, code } = await api.auth.requestPasswordReset(sendMailMock)
29
- const user = await config.getUser("test@test.com")
32
+ describe("POST /api/global/auth/logout", () => {
33
+ it("should logout", async () => {
34
+ await config.api.auth.logout()
35
+ expect(events.auth.logout).toBeCalledTimes(1)
30
36
 
31
- expect(res.body).toEqual({
32
- message: "Please check your email for a reset link.",
37
+ // TODO: Verify sessions deleted
38
+ })
33
39
  })
34
- expect(sendMailMock).toHaveBeenCalled()
35
40
 
36
- expect(code).toBeDefined()
37
- expect(events.user.passwordResetRequested).toBeCalledTimes(1)
38
- expect(events.user.passwordResetRequested).toBeCalledWith(user)
41
+ describe("POST /api/global/auth/:tenantId/reset", () => {
42
+ it("should generate password reset email", async () => {
43
+ const { res, code } = await config.api.auth.requestPasswordReset(
44
+ sendMailMock
45
+ )
46
+ const user = await config.getUser("test@test.com")
47
+
48
+ expect(res.body).toEqual({
49
+ message: "Please check your email for a reset link.",
50
+ })
51
+ expect(sendMailMock).toHaveBeenCalled()
52
+
53
+ expect(code).toBeDefined()
54
+ expect(events.user.passwordResetRequested).toBeCalledTimes(1)
55
+ expect(events.user.passwordResetRequested).toBeCalledWith(user)
56
+ })
57
+ })
58
+
59
+ describe("POST /api/global/auth/:tenantId/reset/update", () => {
60
+ it("should reset password", async () => {
61
+ const { code } = await config.api.auth.requestPasswordReset(
62
+ sendMailMock
63
+ )
64
+ const user = await config.getUser("test@test.com")
65
+ delete user.password
66
+
67
+ const res = await config.api.auth.updatePassword(code)
68
+
69
+ expect(res.body).toEqual({ message: "password reset successfully." })
70
+ expect(events.user.passwordReset).toBeCalledTimes(1)
71
+ expect(events.user.passwordReset).toBeCalledWith(user)
72
+
73
+ // TODO: Login using new password
74
+ })
75
+ })
76
+ })
77
+
78
+ describe("init", () => {
79
+ describe("POST /api/global/auth/init", () => {})
80
+
81
+ describe("GET /api/global/auth/init", () => {})
39
82
  })
40
83
 
41
- it("should allow resetting user password with code", async () => {
42
- const { code } = await api.auth.requestPasswordReset(sendMailMock)
43
- const user = await config.getUser("test@test.com")
44
- delete user.password
84
+ describe("datasource", () => {
85
+ // MULTI TENANT
86
+
87
+ describe("GET /api/global/auth/:tenantId/datasource/:provider", () => {})
88
+
89
+ describe("GET /api/global/auth/:tenantId/datasource/:provider/callback", () => {})
45
90
 
46
- const res = await api.auth.updatePassword(code)
91
+ // SINGLE TENANT
47
92
 
48
- expect(res.body).toEqual({ message: "password reset successfully." })
49
- expect(events.user.passwordReset).toBeCalledTimes(1)
50
- expect(events.user.passwordReset).toBeCalledWith(user)
93
+ describe("GET /api/global/auth/datasource/:provider/callback", () => {})
51
94
  })
52
95
 
53
- describe("oidc", () => {
54
- const auth = require("@budibase/backend-core/auth")
96
+ describe("google", () => {
97
+ // MULTI TENANT
98
+
99
+ describe("GET /api/global/auth/:tenantId/google", () => {})
55
100
 
56
- const passportSpy = jest.spyOn(auth.passport, "authenticate")
57
- let oidcConf
58
- let chosenConfig: any
59
- let configId: string
101
+ describe("GET /api/global/auth/:tenantId/google/callback", () => {})
60
102
 
61
- // mock the oidc strategy implementation and return value
62
- let strategyFactory = jest.fn()
63
- let mockStrategyReturn = jest.fn()
64
- let mockStrategyConfig = jest.fn()
65
- auth.oidc.fetchStrategyConfig = mockStrategyConfig
103
+ // SINGLE TENANT
66
104
 
67
- strategyFactory.mockReturnValue(mockStrategyReturn)
68
- auth.oidc.strategyFactory = strategyFactory
105
+ describe("GET /api/global/auth/google/callback", () => {})
69
106
 
107
+ describe("GET /api/admin/auth/google/callback", () => {})
108
+ })
109
+
110
+ describe("oidc", () => {
70
111
  beforeEach(async () => {
71
- oidcConf = await config.saveOIDCConfig()
72
- chosenConfig = oidcConf.config.configs[0]
73
- configId = chosenConfig.uuid
74
- mockStrategyConfig.mockReturnValue(chosenConfig)
75
- })
112
+ jest.clearAllMocks()
113
+ mockGetWellKnownConfig()
76
114
 
77
- afterEach(() => {
78
- expect(strategyFactory).toBeCalledWith(chosenConfig, expect.any(Function))
115
+ // see: __mocks__/oauth
116
+ // for associated mocking inside passport
79
117
  })
80
118
 
81
- describe("oidc configs", () => {
82
- it("should load strategy and delegate to passport", async () => {
83
- await api.configs.getOIDCConfig(configId)
84
-
85
- expect(passportSpy).toBeCalledWith(mockStrategyReturn, {
86
- scope: ["profile", "email", "offline_access"],
87
- })
88
- expect(passportSpy.mock.calls.length).toBe(1)
119
+ const generateOidcConfig = async () => {
120
+ const oidcConf = await config.saveOIDCConfig()
121
+ const chosenConfig = oidcConf.config.configs[0]
122
+ return chosenConfig.uuid
123
+ }
124
+
125
+ const mockGetWellKnownConfig = () => {
126
+ mocks.fetch.mockReturnValue({
127
+ ok: true,
128
+ json: () => ({
129
+ issuer: "test",
130
+ authorization_endpoint: "http://localhost/auth",
131
+ token_endpoint: "http://localhost/token",
132
+ userinfo_endpoint: "http://localhost/userinfo",
133
+ }),
134
+ })
135
+ }
136
+
137
+ // MULTI TENANT
138
+ describe("GET /api/global/auth/:tenantId/oidc/configs/:configId", () => {
139
+ it("redirects to auth provider", async () => {
140
+ const configId = await generateOidcConfig()
141
+
142
+ const res = await config.api.configs.getOIDCConfig(configId)
143
+
144
+ expect(res.status).toBe(302)
145
+ const location: string = res.get("location")
146
+ expect(
147
+ location.startsWith(
148
+ "http://localhost/auth?response_type=code&client_id=clientId&redirect_uri=http%3A%2F%2Flocalhost%3A10000%2Fapi%2Fglobal%2Fauth%2Fdefault%2Foidc%2Fcallback&scope=openid%20profile%20email%20offline_access"
149
+ )
150
+ ).toBe(true)
89
151
  })
90
152
  })
91
153
 
92
- describe("oidc callback", () => {
93
- it("should load strategy and delegate to passport", async () => {
94
- await api.configs.OIDCCallback(configId)
95
-
96
- expect(passportSpy).toBeCalledWith(
97
- mockStrategyReturn,
98
- {
99
- successRedirect: "/",
100
- failureRedirect: "/error",
101
- },
102
- expect.anything()
103
- )
104
- expect(passportSpy.mock.calls.length).toBe(1)
154
+ describe("GET /api/global/auth/:tenantId/oidc/callback", () => {
155
+ it("logs in", async () => {
156
+ const configId = await generateOidcConfig()
157
+ const preAuthRes = await config.api.configs.getOIDCConfig(configId)
158
+
159
+ const res = await config.api.configs.OIDCCallback(configId, preAuthRes)
160
+
161
+ expect(events.auth.login).toBeCalledWith("oidc")
162
+ expect(events.auth.login).toBeCalledTimes(1)
163
+ expect(res.status).toBe(302)
164
+ const location: string = res.get("location")
165
+ expect(location).toBe("/")
166
+ expectSetAuthCookie(res)
105
167
  })
106
168
  })
169
+
170
+ // SINGLE TENANT
171
+
172
+ describe("GET /api/global/auth/oidc/callback", () => {})
173
+
174
+ describe("GET /api/global/auth/oidc/callback", () => {})
175
+
176
+ describe("GET /api/admin/auth/oidc/callback", () => {})
107
177
  })
108
178
  })
@@ -1,12 +1,11 @@
1
1
  // mock the email system
2
2
  jest.mock("nodemailer")
3
- import { TestConfiguration, structures, mocks, API } from "../../../../tests"
3
+ import { TestConfiguration, structures, mocks } from "../../../../tests"
4
4
  mocks.email.mock()
5
- import { Configs, events } from "@budibase/backend-core"
5
+ import { Config, events } from "@budibase/backend-core"
6
6
 
7
7
  describe("configs", () => {
8
8
  const config = new TestConfiguration()
9
- const api = new API(config)
10
9
 
11
10
  beforeAll(async () => {
12
11
  await config.beforeAll()
@@ -28,7 +27,7 @@ describe("configs", () => {
28
27
  _rev,
29
28
  }
30
29
 
31
- const res = await api.configs.saveConfig(data)
30
+ const res = await config.api.configs.saveConfig(data)
32
31
 
33
32
  return {
34
33
  ...data,
@@ -50,20 +49,20 @@ describe("configs", () => {
50
49
  it("should create activated google config", async () => {
51
50
  await saveGoogleConfig()
52
51
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
53
- expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE)
52
+ expect(events.auth.SSOCreated).toBeCalledWith(Config.GOOGLE)
54
53
  expect(events.auth.SSODeactivated).not.toBeCalled()
55
54
  expect(events.auth.SSOActivated).toBeCalledTimes(1)
56
- expect(events.auth.SSOActivated).toBeCalledWith(Configs.GOOGLE)
57
- await config.deleteConfig(Configs.GOOGLE)
55
+ expect(events.auth.SSOActivated).toBeCalledWith(Config.GOOGLE)
56
+ await config.deleteConfig(Config.GOOGLE)
58
57
  })
59
58
 
60
59
  it("should create deactivated google config", async () => {
61
60
  await saveGoogleConfig({ activated: false })
62
61
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
63
- expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE)
62
+ expect(events.auth.SSOCreated).toBeCalledWith(Config.GOOGLE)
64
63
  expect(events.auth.SSOActivated).not.toBeCalled()
65
64
  expect(events.auth.SSODeactivated).not.toBeCalled()
66
- await config.deleteConfig(Configs.GOOGLE)
65
+ await config.deleteConfig(Config.GOOGLE)
67
66
  })
68
67
  })
69
68
 
@@ -77,11 +76,11 @@ describe("configs", () => {
77
76
  googleConf._rev
78
77
  )
79
78
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
80
- expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE)
79
+ expect(events.auth.SSOUpdated).toBeCalledWith(Config.GOOGLE)
81
80
  expect(events.auth.SSOActivated).not.toBeCalled()
82
81
  expect(events.auth.SSODeactivated).toBeCalledTimes(1)
83
- expect(events.auth.SSODeactivated).toBeCalledWith(Configs.GOOGLE)
84
- await config.deleteConfig(Configs.GOOGLE)
82
+ expect(events.auth.SSODeactivated).toBeCalledWith(Config.GOOGLE)
83
+ await config.deleteConfig(Config.GOOGLE)
85
84
  })
86
85
 
87
86
  it("should update google config to activated", async () => {
@@ -93,11 +92,11 @@ describe("configs", () => {
93
92
  googleConf._rev
94
93
  )
95
94
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
96
- expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE)
95
+ expect(events.auth.SSOUpdated).toBeCalledWith(Config.GOOGLE)
97
96
  expect(events.auth.SSODeactivated).not.toBeCalled()
98
97
  expect(events.auth.SSOActivated).toBeCalledTimes(1)
99
- expect(events.auth.SSOActivated).toBeCalledWith(Configs.GOOGLE)
100
- await config.deleteConfig(Configs.GOOGLE)
98
+ expect(events.auth.SSOActivated).toBeCalledWith(Config.GOOGLE)
99
+ await config.deleteConfig(Config.GOOGLE)
101
100
  })
102
101
  })
103
102
  })
@@ -116,20 +115,20 @@ describe("configs", () => {
116
115
  it("should create activated OIDC config", async () => {
117
116
  await saveOIDCConfig()
118
117
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
119
- expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC)
118
+ expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC)
120
119
  expect(events.auth.SSODeactivated).not.toBeCalled()
121
120
  expect(events.auth.SSOActivated).toBeCalledTimes(1)
122
- expect(events.auth.SSOActivated).toBeCalledWith(Configs.OIDC)
123
- await config.deleteConfig(Configs.OIDC)
121
+ expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC)
122
+ await config.deleteConfig(Config.OIDC)
124
123
  })
125
124
 
126
125
  it("should create deactivated OIDC config", async () => {
127
126
  await saveOIDCConfig({ activated: false })
128
127
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
129
- expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC)
128
+ expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC)
130
129
  expect(events.auth.SSOActivated).not.toBeCalled()
131
130
  expect(events.auth.SSODeactivated).not.toBeCalled()
132
- await config.deleteConfig(Configs.OIDC)
131
+ await config.deleteConfig(Config.OIDC)
133
132
  })
134
133
  })
135
134
 
@@ -143,11 +142,11 @@ describe("configs", () => {
143
142
  oidcConf._rev
144
143
  )
145
144
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
146
- expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC)
145
+ expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC)
147
146
  expect(events.auth.SSOActivated).not.toBeCalled()
148
147
  expect(events.auth.SSODeactivated).toBeCalledTimes(1)
149
- expect(events.auth.SSODeactivated).toBeCalledWith(Configs.OIDC)
150
- await config.deleteConfig(Configs.OIDC)
148
+ expect(events.auth.SSODeactivated).toBeCalledWith(Config.OIDC)
149
+ await config.deleteConfig(Config.OIDC)
151
150
  })
152
151
 
153
152
  it("should update OIDC config to activated", async () => {
@@ -159,11 +158,11 @@ describe("configs", () => {
159
158
  oidcConf._rev
160
159
  )
161
160
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
162
- expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC)
161
+ expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC)
163
162
  expect(events.auth.SSODeactivated).not.toBeCalled()
164
163
  expect(events.auth.SSOActivated).toBeCalledTimes(1)
165
- expect(events.auth.SSOActivated).toBeCalledWith(Configs.OIDC)
166
- await config.deleteConfig(Configs.OIDC)
164
+ expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC)
165
+ await config.deleteConfig(Config.OIDC)
167
166
  })
168
167
  })
169
168
  })
@@ -180,11 +179,11 @@ describe("configs", () => {
180
179
 
181
180
  describe("create", () => {
182
181
  it("should create SMTP config", async () => {
183
- await config.deleteConfig(Configs.SMTP)
182
+ await config.deleteConfig(Config.SMTP)
184
183
  await saveSMTPConfig()
185
184
  expect(events.email.SMTPUpdated).not.toBeCalled()
186
185
  expect(events.email.SMTPCreated).toBeCalledTimes(1)
187
- await config.deleteConfig(Configs.SMTP)
186
+ await config.deleteConfig(Config.SMTP)
188
187
  })
189
188
  })
190
189
 
@@ -195,7 +194,7 @@ describe("configs", () => {
195
194
  await saveSMTPConfig(smtpConf.config, smtpConf._id, smtpConf._rev)
196
195
  expect(events.email.SMTPCreated).not.toBeCalled()
197
196
  expect(events.email.SMTPUpdated).toBeCalledTimes(1)
198
- await config.deleteConfig(Configs.SMTP)
197
+ await config.deleteConfig(Config.SMTP)
199
198
  })
200
199
  })
201
200
  })
@@ -212,7 +211,7 @@ describe("configs", () => {
212
211
 
213
212
  describe("create", () => {
214
213
  it("should create settings config with default settings", async () => {
215
- await config.deleteConfig(Configs.SETTINGS)
214
+ await config.deleteConfig(Config.SETTINGS)
216
215
 
217
216
  await saveSettingsConfig()
218
217
 
@@ -223,7 +222,7 @@ describe("configs", () => {
223
222
 
224
223
  it("should create settings config with non-default settings", async () => {
225
224
  config.modeSelf()
226
- await config.deleteConfig(Configs.SETTINGS)
225
+ await config.deleteConfig(Config.SETTINGS)
227
226
  const conf = {
228
227
  company: "acme",
229
228
  logoUrl: "http://example.com",
@@ -235,14 +234,14 @@ describe("configs", () => {
235
234
  expect(events.org.nameUpdated).toBeCalledTimes(1)
236
235
  expect(events.org.logoUpdated).toBeCalledTimes(1)
237
236
  expect(events.org.platformURLUpdated).toBeCalledTimes(1)
238
- config.modeAccount()
237
+ config.modeCloud()
239
238
  })
240
239
  })
241
240
 
242
241
  describe("update", () => {
243
242
  it("should update settings config", async () => {
244
243
  config.modeSelf()
245
- await config.deleteConfig(Configs.SETTINGS)
244
+ await config.deleteConfig(Config.SETTINGS)
246
245
  const settingsConfig = await saveSettingsConfig()
247
246
  settingsConfig.config.company = "acme"
248
247
  settingsConfig.config.logoUrl = "http://example.com"
@@ -257,7 +256,7 @@ describe("configs", () => {
257
256
  expect(events.org.nameUpdated).toBeCalledTimes(1)
258
257
  expect(events.org.logoUpdated).toBeCalledTimes(1)
259
258
  expect(events.org.platformURLUpdated).toBeCalledTimes(1)
260
- config.modeAccount()
259
+ config.modeCloud()
261
260
  })
262
261
  })
263
262
  })
@@ -266,7 +265,7 @@ describe("configs", () => {
266
265
  it("should return the correct checklist status based on the state of the budibase installation", async () => {
267
266
  await config.saveSmtpConfig()
268
267
 
269
- const res = await api.configs.getConfigChecklist()
268
+ const res = await config.api.configs.getConfigChecklist()
270
269
  const checklist = res.body
271
270
 
272
271
  expect(checklist.apps.checked).toBeFalsy()
@@ -1,11 +1,10 @@
1
1
  jest.mock("nodemailer")
2
- import { TestConfiguration, mocks, API } from "../../../../tests"
2
+ import { TestConfiguration, mocks } from "../../../../tests"
3
3
  const sendMailMock = mocks.email.mock()
4
4
  import { EmailTemplatePurpose } from "../../../../constants"
5
5
 
6
6
  describe("/api/global/email", () => {
7
7
  const config = new TestConfiguration()
8
- const api = new API(config)
9
8
 
10
9
  beforeAll(async () => {
11
10
  await config.beforeAll()
@@ -20,7 +19,9 @@ describe("/api/global/email", () => {
20
19
  await config.saveSmtpConfig()
21
20
  await config.saveSettingsConfig()
22
21
 
23
- const res = await api.emails.sendEmail(EmailTemplatePurpose.INVITATION)
22
+ const res = await config.api.emails.sendEmail(
23
+ EmailTemplatePurpose.INVITATION
24
+ )
24
25
 
25
26
  expect(res.body.message).toBeDefined()
26
27
  expect(sendMailMock).toHaveBeenCalled()