@budibase/worker 2.3.10 → 2.3.11-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 (124) hide show
  1. package/__mocks__/node-fetch.ts +23 -0
  2. package/coverage/clover.xml +384 -369
  3. package/coverage/coverage-final.json +18 -18
  4. package/coverage/lcov-report/index.html +56 -56
  5. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +1 -1
  6. package/coverage/lcov-report/src/api/controllers/global/configs.ts.html +11 -11
  7. package/coverage/lcov-report/src/api/controllers/global/email.ts.html +1 -1
  8. package/coverage/lcov-report/src/api/controllers/global/events.ts.html +1 -1
  9. package/coverage/lcov-report/src/api/controllers/global/index.html +21 -21
  10. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +1 -1
  11. package/coverage/lcov-report/src/api/controllers/global/roles.ts.html +15 -15
  12. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +1 -1
  13. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +1 -1
  14. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +6 -6
  15. package/coverage/lcov-report/src/api/controllers/global/workspaces.ts.html +1 -1
  16. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +1 -1
  17. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +1 -1
  18. package/coverage/lcov-report/src/api/controllers/system/index.html +1 -1
  19. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +1 -1
  20. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +1 -1
  21. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +1 -1
  22. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +1 -1
  23. package/coverage/lcov-report/src/api/index.html +1 -1
  24. package/coverage/lcov-report/src/api/index.ts.html +6 -6
  25. package/coverage/lcov-report/src/api/routes/global/auth.ts.html +1 -1
  26. package/coverage/lcov-report/src/api/routes/global/configs.ts.html +1 -1
  27. package/coverage/lcov-report/src/api/routes/global/email.ts.html +1 -1
  28. package/coverage/lcov-report/src/api/routes/global/events.ts.html +1 -1
  29. package/coverage/lcov-report/src/api/routes/global/index.html +1 -1
  30. package/coverage/lcov-report/src/api/routes/global/license.ts.html +1 -1
  31. package/coverage/lcov-report/src/api/routes/global/roles.ts.html +1 -1
  32. package/coverage/lcov-report/src/api/routes/global/self.ts.html +1 -1
  33. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +1 -1
  34. package/coverage/lcov-report/src/api/routes/global/users.ts.html +1 -1
  35. package/coverage/lcov-report/src/api/routes/global/workspaces.ts.html +1 -1
  36. package/coverage/lcov-report/src/api/routes/index.html +1 -1
  37. package/coverage/lcov-report/src/api/routes/index.ts.html +1 -1
  38. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +1 -1
  39. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +1 -1
  40. package/coverage/lcov-report/src/api/routes/system/index.html +1 -1
  41. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +1 -1
  42. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +1 -1
  43. package/coverage/lcov-report/src/api/routes/system/status.ts.html +1 -1
  44. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +1 -1
  45. package/coverage/lcov-report/src/api/routes/validation/index.html +1 -1
  46. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +1 -1
  47. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +1 -1
  48. package/coverage/lcov-report/src/constants/index.html +1 -1
  49. package/coverage/lcov-report/src/constants/index.ts.html +1 -1
  50. package/coverage/lcov-report/src/constants/templates/index.html +11 -11
  51. package/coverage/lcov-report/src/constants/templates/index.ts.html +10 -10
  52. package/coverage/lcov-report/src/db/index.html +15 -15
  53. package/coverage/lcov-report/src/db/index.ts.html +10 -10
  54. package/coverage/lcov-report/src/environment.ts.html +8 -8
  55. package/coverage/lcov-report/src/index.html +5 -5
  56. package/coverage/lcov-report/src/index.ts.html +1 -1
  57. package/coverage/lcov-report/src/middleware/cloudRestricted.ts.html +1 -1
  58. package/coverage/lcov-report/src/middleware/index.html +1 -1
  59. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +1 -1
  60. package/coverage/lcov-report/src/migrations/functions/index.html +1 -1
  61. package/coverage/lcov-report/src/migrations/index.html +1 -1
  62. package/coverage/lcov-report/src/migrations/index.ts.html +1 -1
  63. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +1 -1
  64. package/coverage/lcov-report/src/sdk/accounts/index.html +1 -1
  65. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +1 -1
  66. package/coverage/lcov-report/src/sdk/index.html +1 -1
  67. package/coverage/lcov-report/src/sdk/index.ts.html +2 -2
  68. package/coverage/lcov-report/src/sdk/users/events.ts.html +29 -29
  69. package/coverage/lcov-report/src/sdk/users/index.html +15 -15
  70. package/coverage/lcov-report/src/sdk/users/index.ts.html +1 -1
  71. package/coverage/lcov-report/src/sdk/users/users.ts.html +62 -65
  72. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +151 -61
  73. package/coverage/lcov-report/src/tests/api/accounts.ts.html +1 -1
  74. package/coverage/lcov-report/src/tests/api/auth.ts.html +20 -8
  75. package/coverage/lcov-report/src/tests/api/base.ts.html +1 -1
  76. package/coverage/lcov-report/src/tests/api/configs.ts.html +3 -3
  77. package/coverage/lcov-report/src/tests/api/email.ts.html +1 -1
  78. package/coverage/lcov-report/src/tests/api/environment.ts.html +1 -1
  79. package/coverage/lcov-report/src/tests/api/groups.ts.html +1 -1
  80. package/coverage/lcov-report/src/tests/api/index.html +5 -5
  81. package/coverage/lcov-report/src/tests/api/index.ts.html +1 -1
  82. package/coverage/lcov-report/src/tests/api/license.ts.html +1 -1
  83. package/coverage/lcov-report/src/tests/api/migrations.ts.html +1 -1
  84. package/coverage/lcov-report/src/tests/api/restore.ts.html +1 -1
  85. package/coverage/lcov-report/src/tests/api/roles.ts.html +1 -1
  86. package/coverage/lcov-report/src/tests/api/self.ts.html +1 -1
  87. package/coverage/lcov-report/src/tests/api/status.ts.html +1 -1
  88. package/coverage/lcov-report/src/tests/api/templates.ts.html +1 -1
  89. package/coverage/lcov-report/src/tests/api/tenants.ts.html +1 -1
  90. package/coverage/lcov-report/src/tests/api/users.ts.html +3 -3
  91. package/coverage/lcov-report/src/tests/controllers.ts.html +2 -2
  92. package/coverage/lcov-report/src/tests/index.html +18 -18
  93. package/coverage/lcov-report/src/tests/index.ts.html +2 -2
  94. package/coverage/lcov-report/src/tests/mocks/email.ts.html +1 -1
  95. package/coverage/lcov-report/src/tests/mocks/index.html +1 -1
  96. package/coverage/lcov-report/src/tests/mocks/index.ts.html +1 -1
  97. package/coverage/lcov-report/src/tests/structures/configs.ts.html +1 -1
  98. package/coverage/lcov-report/src/tests/structures/groups.ts.html +1 -1
  99. package/coverage/lcov-report/src/tests/structures/index.html +9 -9
  100. package/coverage/lcov-report/src/tests/structures/index.ts.html +8 -14
  101. package/coverage/lcov-report/src/tests/structures/users.ts.html +5 -5
  102. package/coverage/lcov-report/src/utilities/appService.ts.html +7 -7
  103. package/coverage/lcov-report/src/utilities/email.ts.html +1 -1
  104. package/coverage/lcov-report/src/utilities/fileSystem.ts.html +1 -1
  105. package/coverage/lcov-report/src/utilities/index.html +1 -1
  106. package/coverage/lcov-report/src/utilities/index.ts.html +1 -1
  107. package/coverage/lcov-report/src/utilities/redis.ts.html +1 -1
  108. package/coverage/lcov-report/src/utilities/templates.ts.html +1 -1
  109. package/coverage/lcov-report/src/utilities/users.ts.html +6 -6
  110. package/coverage/lcov.info +629 -607
  111. package/jest-testcontainers-config.js +8 -0
  112. package/jest.config.ts +3 -2
  113. package/package.json +7 -6
  114. package/src/api/routes/global/tests/auth.spec.ts +30 -22
  115. package/src/api/routes/global/tests/configs.spec.ts +114 -94
  116. package/src/api/routes/global/tests/roles.spec.ts +19 -14
  117. package/src/api/routes/global/tests/users.spec.ts +33 -34
  118. package/src/sdk/users/users.ts +0 -1
  119. package/src/tests/TestConfiguration.ts +41 -11
  120. package/src/tests/api/auth.ts +7 -3
  121. package/src/tests/api/configs.ts +2 -2
  122. package/src/tests/api/users.ts +2 -2
  123. package/src/tests/jestSetup.ts +9 -2
  124. package/src/tests/structures/index.ts +0 -2
@@ -0,0 +1,8 @@
1
+ const { join } = require("path")
2
+ require("dotenv").config({
3
+ path: join(__dirname, "..", "..", "hosting", ".env"),
4
+ })
5
+
6
+ const jestTestcontainersConfigGenerator = require("../../jestTestcontainersConfigGenerator")
7
+
8
+ module.exports = jestTestcontainersConfigGenerator()
package/jest.config.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import { Config } from "@jest/types"
2
2
  import * as fs from "fs"
3
+ const preset = require("ts-jest/jest-preset")
3
4
 
4
5
  const config: Config.InitialOptions = {
5
- testEnvironment: "node",
6
+ ...preset,
7
+ preset: "@trendyol/jest-testcontainers",
6
8
  setupFiles: ["./src/tests/jestEnv.ts"],
7
9
  setupFilesAfterEnv: ["./src/tests/jestSetup.ts"],
8
10
  collectCoverageFrom: ["src/**/*.{js,ts}"],
@@ -18,7 +20,6 @@ if (!process.env.CI) {
18
20
  "@budibase/backend-core/(.*)": "<rootDir>/../backend-core/$1",
19
21
  "@budibase/backend-core": "<rootDir>/../backend-core/src",
20
22
  "@budibase/types": "<rootDir>/../types/src",
21
- "^axios.*$": "<rootDir>/node_modules/axios/lib/axios.js",
22
23
  }
23
24
  // add pro sources if they exist
24
25
  if (fs.existsSync("../../../budibase-pro")) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.3.10",
4
+ "version": "2.3.11-alpha.0",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -36,10 +36,10 @@
36
36
  "author": "Budibase",
37
37
  "license": "GPL-3.0",
38
38
  "dependencies": {
39
- "@budibase/backend-core": "^2.3.10",
40
- "@budibase/pro": "2.3.9",
41
- "@budibase/string-templates": "^2.3.10",
42
- "@budibase/types": "^2.3.10",
39
+ "@budibase/backend-core": "2.3.11-alpha.0",
40
+ "@budibase/pro": "2.3.10",
41
+ "@budibase/string-templates": "2.3.11-alpha.0",
42
+ "@budibase/types": "2.3.11-alpha.0",
43
43
  "@koa/router": "8.0.8",
44
44
  "@sentry/node": "6.17.7",
45
45
  "@techpass/passport-openidconnect": "0.3.2",
@@ -72,6 +72,7 @@
72
72
  "devDependencies": {
73
73
  "@swc/core": "^1.3.25",
74
74
  "@swc/jest": "^0.2.24",
75
+ "@trendyol/jest-testcontainers": "^2.1.1",
75
76
  "@types/jest": "26.0.23",
76
77
  "@types/jsonwebtoken": "8.5.1",
77
78
  "@types/koa": "2.13.4",
@@ -97,5 +98,5 @@
97
98
  "typescript": "4.7.3",
98
99
  "update-dotenv": "1.1.1"
99
100
  },
100
- "gitHead": "bf7e6e3eebcfac71672a84b00aa04c74669e4923"
101
+ "gitHead": "78a458fe5d03d78aabde3712e9b320a4be174ec6"
101
102
  }
@@ -1,7 +1,8 @@
1
1
  jest.mock("nodemailer")
2
2
  import { TestConfiguration, mocks } from "../../../../tests"
3
3
  const sendMailMock = mocks.email.mock()
4
- import { events } from "@budibase/backend-core"
4
+ import { events, tenancy } from "@budibase/backend-core"
5
+ import { structures } from "@budibase/backend-core/tests"
5
6
 
6
7
  const expectSetAuthCookie = (res: any) => {
7
8
  expect(
@@ -40,36 +41,43 @@ describe("/api/global/auth", () => {
40
41
 
41
42
  describe("POST /api/global/auth/:tenantId/reset", () => {
42
43
  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")
44
+ await tenancy.doInTenant(config.tenant1User!.tenantId, async () => {
45
+ const userEmail = structures.email()
46
+ const { res, code } = await config.api.auth.requestPasswordReset(
47
+ sendMailMock,
48
+ userEmail
49
+ )
50
+ const user = await config.getUser(userEmail)
47
51
 
48
- expect(res.body).toEqual({
49
- message: "Please check your email for a reset link.",
50
- })
51
- expect(sendMailMock).toHaveBeenCalled()
52
+ expect(res.body).toEqual({
53
+ message: "Please check your email for a reset link.",
54
+ })
55
+ expect(sendMailMock).toHaveBeenCalled()
52
56
 
53
- expect(code).toBeDefined()
54
- expect(events.user.passwordResetRequested).toBeCalledTimes(1)
55
- expect(events.user.passwordResetRequested).toBeCalledWith(user)
57
+ expect(code).toBeDefined()
58
+ expect(events.user.passwordResetRequested).toBeCalledTimes(1)
59
+ expect(events.user.passwordResetRequested).toBeCalledWith(user)
60
+ })
56
61
  })
57
62
  })
58
63
 
59
64
  describe("POST /api/global/auth/:tenantId/reset/update", () => {
60
65
  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)
66
+ await tenancy.doInTenant(config.tenant1User!.tenantId, async () => {
67
+ const userEmail = structures.email()
68
+ const { code } = await config.api.auth.requestPasswordReset(
69
+ sendMailMock,
70
+ userEmail
71
+ )
72
+ const user = await config.getUser(userEmail)
73
+ delete user.password
68
74
 
69
- expect(res.body).toEqual({ message: "password reset successfully." })
70
- expect(events.user.passwordReset).toBeCalledTimes(1)
71
- expect(events.user.passwordReset).toBeCalledWith(user)
75
+ const res = await config.api.auth.updatePassword(code)
72
76
 
77
+ expect(res.body).toEqual({ message: "password reset successfully." })
78
+ expect(events.user.passwordReset).toBeCalledTimes(1)
79
+ expect(events.user.passwordReset).toBeCalledWith(user)
80
+ })
73
81
  // TODO: Login using new password
74
82
  })
75
83
  })
@@ -2,12 +2,12 @@
2
2
  jest.mock("nodemailer")
3
3
  import { TestConfiguration, structures, mocks } from "../../../../tests"
4
4
  mocks.email.mock()
5
- import { Config, events } from "@budibase/backend-core"
5
+ import { Config, context, events } from "@budibase/backend-core"
6
6
 
7
7
  describe("configs", () => {
8
8
  const config = new TestConfiguration()
9
9
 
10
- beforeAll(async () => {
10
+ beforeEach(async () => {
11
11
  await config.beforeAll()
12
12
  })
13
13
 
@@ -113,56 +113,64 @@ describe("configs", () => {
113
113
 
114
114
  describe("create", () => {
115
115
  it("should create activated OIDC config", async () => {
116
- await saveOIDCConfig()
117
- expect(events.auth.SSOCreated).toBeCalledTimes(1)
118
- expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC)
119
- expect(events.auth.SSODeactivated).not.toBeCalled()
120
- expect(events.auth.SSOActivated).toBeCalledTimes(1)
121
- expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC)
122
- await config.deleteConfig(Config.OIDC)
116
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
117
+ await saveOIDCConfig()
118
+ expect(events.auth.SSOCreated).toBeCalledTimes(1)
119
+ expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC)
120
+ expect(events.auth.SSODeactivated).not.toBeCalled()
121
+ expect(events.auth.SSOActivated).toBeCalledTimes(1)
122
+ expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC)
123
+ await config.deleteConfig(Config.OIDC)
124
+ })
123
125
  })
124
126
 
125
127
  it("should create deactivated OIDC config", async () => {
126
- await saveOIDCConfig({ activated: false })
127
- expect(events.auth.SSOCreated).toBeCalledTimes(1)
128
- expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC)
129
- expect(events.auth.SSOActivated).not.toBeCalled()
130
- expect(events.auth.SSODeactivated).not.toBeCalled()
131
- await config.deleteConfig(Config.OIDC)
128
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
129
+ await saveOIDCConfig({ activated: false })
130
+ expect(events.auth.SSOCreated).toBeCalledTimes(1)
131
+ expect(events.auth.SSOCreated).toBeCalledWith(Config.OIDC)
132
+ expect(events.auth.SSOActivated).not.toBeCalled()
133
+ expect(events.auth.SSODeactivated).not.toBeCalled()
134
+ await config.deleteConfig(Config.OIDC)
135
+ })
132
136
  })
133
137
  })
134
138
 
135
139
  describe("update", () => {
136
140
  it("should update OIDC config to deactivated", async () => {
137
- const oidcConf = await saveOIDCConfig()
138
- jest.clearAllMocks()
139
- await saveOIDCConfig(
140
- { ...oidcConf.config.configs[0], activated: false },
141
- oidcConf._id,
142
- oidcConf._rev
143
- )
144
- expect(events.auth.SSOUpdated).toBeCalledTimes(1)
145
- expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC)
146
- expect(events.auth.SSOActivated).not.toBeCalled()
147
- expect(events.auth.SSODeactivated).toBeCalledTimes(1)
148
- expect(events.auth.SSODeactivated).toBeCalledWith(Config.OIDC)
149
- await config.deleteConfig(Config.OIDC)
141
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
142
+ const oidcConf = await saveOIDCConfig()
143
+ jest.clearAllMocks()
144
+ await saveOIDCConfig(
145
+ { ...oidcConf.config.configs[0], activated: false },
146
+ oidcConf._id,
147
+ oidcConf._rev
148
+ )
149
+ expect(events.auth.SSOUpdated).toBeCalledTimes(1)
150
+ expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC)
151
+ expect(events.auth.SSOActivated).not.toBeCalled()
152
+ expect(events.auth.SSODeactivated).toBeCalledTimes(1)
153
+ expect(events.auth.SSODeactivated).toBeCalledWith(Config.OIDC)
154
+ await config.deleteConfig(Config.OIDC)
155
+ })
150
156
  })
151
157
 
152
158
  it("should update OIDC config to activated", async () => {
153
- const oidcConf = await saveOIDCConfig({ activated: false })
154
- jest.clearAllMocks()
155
- await saveOIDCConfig(
156
- { ...oidcConf.config.configs[0], activated: true },
157
- oidcConf._id,
158
- oidcConf._rev
159
- )
160
- expect(events.auth.SSOUpdated).toBeCalledTimes(1)
161
- expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC)
162
- expect(events.auth.SSODeactivated).not.toBeCalled()
163
- expect(events.auth.SSOActivated).toBeCalledTimes(1)
164
- expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC)
165
- await config.deleteConfig(Config.OIDC)
159
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
160
+ const oidcConf = await saveOIDCConfig({ activated: false })
161
+ jest.clearAllMocks()
162
+ await saveOIDCConfig(
163
+ { ...oidcConf.config.configs[0], activated: true },
164
+ oidcConf._id,
165
+ oidcConf._rev
166
+ )
167
+ expect(events.auth.SSOUpdated).toBeCalledTimes(1)
168
+ expect(events.auth.SSOUpdated).toBeCalledWith(Config.OIDC)
169
+ expect(events.auth.SSODeactivated).not.toBeCalled()
170
+ expect(events.auth.SSOActivated).toBeCalledTimes(1)
171
+ expect(events.auth.SSOActivated).toBeCalledWith(Config.OIDC)
172
+ await config.deleteConfig(Config.OIDC)
173
+ })
166
174
  })
167
175
  })
168
176
  })
@@ -179,22 +187,26 @@ describe("configs", () => {
179
187
 
180
188
  describe("create", () => {
181
189
  it("should create SMTP config", async () => {
182
- await config.deleteConfig(Config.SMTP)
183
- await saveSMTPConfig()
184
- expect(events.email.SMTPUpdated).not.toBeCalled()
185
- expect(events.email.SMTPCreated).toBeCalledTimes(1)
186
- await config.deleteConfig(Config.SMTP)
190
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
191
+ await config.deleteConfig(Config.SMTP)
192
+ await saveSMTPConfig()
193
+ expect(events.email.SMTPUpdated).not.toBeCalled()
194
+ expect(events.email.SMTPCreated).toBeCalledTimes(1)
195
+ await config.deleteConfig(Config.SMTP)
196
+ })
187
197
  })
188
198
  })
189
199
 
190
200
  describe("update", () => {
191
201
  it("should update SMTP config", async () => {
192
- const smtpConf = await saveSMTPConfig()
193
- jest.clearAllMocks()
194
- await saveSMTPConfig(smtpConf.config, smtpConf._id, smtpConf._rev)
195
- expect(events.email.SMTPCreated).not.toBeCalled()
196
- expect(events.email.SMTPUpdated).toBeCalledTimes(1)
197
- await config.deleteConfig(Config.SMTP)
202
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
203
+ const smtpConf = await saveSMTPConfig()
204
+ jest.clearAllMocks()
205
+ await saveSMTPConfig(smtpConf.config, smtpConf._id, smtpConf._rev)
206
+ expect(events.email.SMTPCreated).not.toBeCalled()
207
+ expect(events.email.SMTPUpdated).toBeCalledTimes(1)
208
+ await config.deleteConfig(Config.SMTP)
209
+ })
198
210
  })
199
211
  })
200
212
  })
@@ -211,65 +223,73 @@ describe("configs", () => {
211
223
 
212
224
  describe("create", () => {
213
225
  it("should create settings config with default settings", async () => {
214
- await config.deleteConfig(Config.SETTINGS)
226
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
227
+ await config.deleteConfig(Config.SETTINGS)
215
228
 
216
- await saveSettingsConfig()
229
+ await saveSettingsConfig()
217
230
 
218
- expect(events.org.nameUpdated).not.toBeCalled()
219
- expect(events.org.logoUpdated).not.toBeCalled()
220
- expect(events.org.platformURLUpdated).not.toBeCalled()
231
+ expect(events.org.nameUpdated).not.toBeCalled()
232
+ expect(events.org.logoUpdated).not.toBeCalled()
233
+ expect(events.org.platformURLUpdated).not.toBeCalled()
234
+ })
221
235
  })
222
236
 
223
237
  it("should create settings config with non-default settings", async () => {
224
- config.modeSelf()
225
- await config.deleteConfig(Config.SETTINGS)
226
- const conf = {
227
- company: "acme",
228
- logoUrl: "http://example.com",
229
- platformUrl: "http://example.com",
230
- }
231
-
232
- await saveSettingsConfig(conf)
233
-
234
- expect(events.org.nameUpdated).toBeCalledTimes(1)
235
- expect(events.org.logoUpdated).toBeCalledTimes(1)
236
- expect(events.org.platformURLUpdated).toBeCalledTimes(1)
237
- config.modeCloud()
238
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
239
+ config.modeSelf()
240
+ await config.deleteConfig(Config.SETTINGS)
241
+ const conf = {
242
+ company: "acme",
243
+ logoUrl: "http://example.com",
244
+ platformUrl: "http://example.com",
245
+ }
246
+
247
+ await saveSettingsConfig(conf)
248
+
249
+ expect(events.org.nameUpdated).toBeCalledTimes(1)
250
+ expect(events.org.logoUpdated).toBeCalledTimes(1)
251
+ expect(events.org.platformURLUpdated).toBeCalledTimes(1)
252
+ config.modeCloud()
253
+ })
238
254
  })
239
255
  })
240
256
 
241
257
  describe("update", () => {
242
258
  it("should update settings config", async () => {
243
- config.modeSelf()
244
- await config.deleteConfig(Config.SETTINGS)
245
- const settingsConfig = await saveSettingsConfig()
246
- settingsConfig.config.company = "acme"
247
- settingsConfig.config.logoUrl = "http://example.com"
248
- settingsConfig.config.platformUrl = "http://example.com"
249
-
250
- await saveSettingsConfig(
251
- settingsConfig.config,
252
- settingsConfig._id,
253
- settingsConfig._rev
254
- )
255
-
256
- expect(events.org.nameUpdated).toBeCalledTimes(1)
257
- expect(events.org.logoUpdated).toBeCalledTimes(1)
258
- expect(events.org.platformURLUpdated).toBeCalledTimes(1)
259
- config.modeCloud()
259
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
260
+ config.modeSelf()
261
+ await config.deleteConfig(Config.SETTINGS)
262
+ const settingsConfig = await saveSettingsConfig()
263
+ settingsConfig.config.company = "acme"
264
+ settingsConfig.config.logoUrl = "http://example.com"
265
+ settingsConfig.config.platformUrl = "http://example.com"
266
+
267
+ await saveSettingsConfig(
268
+ settingsConfig.config,
269
+ settingsConfig._id,
270
+ settingsConfig._rev
271
+ )
272
+
273
+ expect(events.org.nameUpdated).toBeCalledTimes(1)
274
+ expect(events.org.logoUpdated).toBeCalledTimes(1)
275
+ expect(events.org.platformURLUpdated).toBeCalledTimes(1)
276
+ config.modeCloud()
277
+ })
260
278
  })
261
279
  })
262
280
  })
263
281
  })
264
282
 
265
283
  it("should return the correct checklist status based on the state of the budibase installation", async () => {
266
- await config.saveSmtpConfig()
284
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
285
+ await config.saveSmtpConfig()
267
286
 
268
- const res = await config.api.configs.getConfigChecklist()
269
- const checklist = res.body
287
+ const res = await config.api.configs.getConfigChecklist()
288
+ const checklist = res.body
270
289
 
271
- expect(checklist.apps.checked).toBeFalsy()
272
- expect(checklist.smtp.checked).toBeTruthy()
273
- expect(checklist.adminUser.checked).toBeTruthy()
290
+ expect(checklist.apps.checked).toBeFalsy()
291
+ expect(checklist.smtp.checked).toBeTruthy()
292
+ expect(checklist.adminUser.checked).toBeTruthy()
293
+ })
274
294
  })
275
295
  })
@@ -1,6 +1,7 @@
1
1
  import { structures, TestConfiguration } from "../../../../tests"
2
2
  import { context, db, permissions, roles } from "@budibase/backend-core"
3
3
  import { Mock } from "jest-mock"
4
+ import { Database } from "@budibase/types"
4
5
 
5
6
  jest.mock("@budibase/backend-core", () => {
6
7
  const core = jest.requireActual("@budibase/backend-core")
@@ -16,14 +17,13 @@ jest.mock("@budibase/backend-core", () => {
16
17
  }
17
18
  })
18
19
 
19
- const appDb = db.getDB("app_test")
20
- const mockAppDB = context.getAppDB as Mock
21
- mockAppDB.mockReturnValue(appDb)
20
+ let appId: string
21
+ let appDb: Database
22
22
 
23
23
  async function addAppMetadata() {
24
24
  await appDb.put({
25
25
  _id: "app_metadata",
26
- appId: "app_test",
26
+ appId: appId,
27
27
  name: "New App",
28
28
  version: "version",
29
29
  url: "url",
@@ -39,12 +39,19 @@ describe("/api/global/roles", () => {
39
39
  )
40
40
 
41
41
  beforeAll(async () => {
42
- console.debug(role)
43
- appDb.put(role)
44
- await addAppMetadata()
45
42
  await config.beforeAll()
46
43
  })
47
44
 
45
+ beforeEach(async () => {
46
+ appId = db.generateAppID()
47
+ appDb = db.getDB(appId)
48
+ const mockAppDB = context.getAppDB as Mock
49
+ mockAppDB.mockReturnValue(appDb)
50
+
51
+ await addAppMetadata()
52
+ appDb.put(role)
53
+ })
54
+
48
55
  afterAll(async () => {
49
56
  await config.afterAll()
50
57
  })
@@ -57,16 +64,14 @@ describe("/api/global/roles", () => {
57
64
  it("retrieves roles", async () => {
58
65
  const res = await config.api.roles.get()
59
66
  expect(res.body).toBeDefined()
60
- expect(res.body["app_test"].roles.length).toEqual(5)
61
- expect(res.body["app_test"].roles.map((r: any) => r._id)).toContain(
62
- role._id
63
- )
67
+ expect(res.body[appId].roles.length).toEqual(5)
68
+ expect(res.body[appId].roles.map((r: any) => r._id)).toContain(role._id)
64
69
  })
65
70
  })
66
71
 
67
72
  describe("GET api/global/roles/:appId", () => {
68
73
  it("finds a role by appId", async () => {
69
- const res = await config.api.roles.find("app_test")
74
+ const res = await config.api.roles.find(appId)
70
75
  expect(res.body).toBeDefined()
71
76
  expect(res.body.name).toEqual("New App")
72
77
  })
@@ -79,9 +84,9 @@ describe("/api/global/roles", () => {
79
84
  app_test: "role1",
80
85
  }
81
86
  const userResponse = await config.createUser(user)
82
- const res = await config.api.roles.remove("app_test")
87
+ const res = await config.api.roles.remove(appId)
83
88
  const updatedUser = await config.api.users.getUser(userResponse._id!)
84
- expect(updatedUser.body.roles).not.toHaveProperty("app_test")
89
+ expect(updatedUser.body.roles).not.toHaveProperty(appId)
85
90
  expect(res.body.message).toEqual("App role removed from all users")
86
91
  })
87
92
  })
@@ -1,14 +1,9 @@
1
1
  import { InviteUsersResponse, User } from "@budibase/types"
2
2
 
3
3
  jest.mock("nodemailer")
4
- import {
5
- TestConfiguration,
6
- mocks,
7
- structures,
8
- TENANT_1,
9
- } from "../../../../tests"
4
+ import { TestConfiguration, mocks, structures } from "../../../../tests"
10
5
  const sendMailMock = mocks.email.mock()
11
- import { events, tenancy } from "@budibase/backend-core"
6
+ import { context, events, tenancy } from "@budibase/backend-core"
12
7
 
13
8
  describe("/api/global/users", () => {
14
9
  const config = new TestConfiguration()
@@ -27,16 +22,18 @@ describe("/api/global/users", () => {
27
22
 
28
23
  describe("invite", () => {
29
24
  it("should be able to generate an invitation", async () => {
30
- const email = structures.users.newEmail()
31
- const { code, res } = await config.api.users.sendUserInvite(
32
- sendMailMock,
33
- email
34
- )
35
-
36
- expect(res.body).toEqual({ message: "Invitation has been sent." })
37
- expect(sendMailMock).toHaveBeenCalled()
38
- expect(code).toBeDefined()
39
- expect(events.user.invited).toBeCalledTimes(1)
25
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
26
+ const email = structures.users.newEmail()
27
+ const { code, res } = await config.api.users.sendUserInvite(
28
+ sendMailMock,
29
+ email
30
+ )
31
+
32
+ expect(res.body).toEqual({ message: "Invitation has been sent." })
33
+ expect(sendMailMock).toHaveBeenCalled()
34
+ expect(code).toBeDefined()
35
+ expect(events.user.invited).toBeCalledTimes(1)
36
+ })
40
37
  })
41
38
 
42
39
  it("should not be able to generate an invitation for existing user", async () => {
@@ -53,20 +50,22 @@ describe("/api/global/users", () => {
53
50
  })
54
51
 
55
52
  it("should be able to create new user from invite", async () => {
56
- const email = structures.users.newEmail()
57
- const { code } = await config.api.users.sendUserInvite(
58
- sendMailMock,
59
- email
60
- )
61
-
62
- const res = await config.api.users.acceptInvite(code)
63
-
64
- expect(res.body._id).toBeDefined()
65
- const user = await config.getUser(email)
66
- expect(user).toBeDefined()
67
- expect(user._id).toEqual(res.body._id)
68
- expect(events.user.inviteAccepted).toBeCalledTimes(1)
69
- expect(events.user.inviteAccepted).toBeCalledWith(user)
53
+ await context.doInTenant(config.tenant1User!.tenantId, async () => {
54
+ const email = structures.users.newEmail()
55
+ const { code } = await config.api.users.sendUserInvite(
56
+ sendMailMock,
57
+ email
58
+ )
59
+
60
+ const res = await config.api.users.acceptInvite(code)
61
+
62
+ expect(res.body._id).toBeDefined()
63
+ const user = await config.getUser(email)
64
+ expect(user).toBeDefined()
65
+ expect(user._id).toEqual(res.body._id)
66
+ expect(events.user.inviteAccepted).toBeCalledTimes(1)
67
+ expect(events.user.inviteAccepted).toBeCalledWith(user)
68
+ })
70
69
  })
71
70
  })
72
71
 
@@ -118,7 +117,7 @@ describe("/api/global/users", () => {
118
117
  const user = await config.createUser()
119
118
  jest.clearAllMocks()
120
119
 
121
- await tenancy.doInTenant(TENANT_1, async () => {
120
+ await tenancy.doInTenant(config.getTenantId(), async () => {
122
121
  const response = await config.api.users.bulkCreateUsers([user])
123
122
 
124
123
  expect(response.created?.successful.length).toBe(0)
@@ -231,7 +230,7 @@ describe("/api/global/users", () => {
231
230
  const user = await config.createUser()
232
231
  jest.clearAllMocks()
233
232
 
234
- await tenancy.doInTenant(TENANT_1, async () => {
233
+ await tenancy.doInTenant(config.getTenantId(), async () => {
235
234
  delete user._id
236
235
  const response = await config.api.users.saveUser(user, 400)
237
236
 
@@ -444,7 +443,7 @@ describe("/api/global/users", () => {
444
443
  })
445
444
 
446
445
  it("should not be able to update email address", async () => {
447
- const email = "email@test.com"
446
+ const email = structures.email()
448
447
  const user = await config.createUser(structures.users.user({ email }))
449
448
  user.email = "new@test.com"
450
449
 
@@ -264,7 +264,6 @@ export const save = async (
264
264
  builtUser._rev = response.rev
265
265
 
266
266
  await eventHelpers.handleSaveEvents(builtUser, dbUser)
267
- await addTenant(tenantId, _id, email)
268
267
  await cache.user.invalidateUser(response.id)
269
268
 
270
269
  // let server know to sync user