@budibase/worker 2.3.17 → 2.3.18-alpha.1

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 (167) hide show
  1. package/__mocks__/node-fetch.ts +23 -0
  2. package/jest-testcontainers-config.js +8 -0
  3. package/jest.config.ts +13 -17
  4. package/package.json +11 -8
  5. package/scripts/dev/manage.js +1 -0
  6. package/src/api/controllers/global/auth.ts +77 -70
  7. package/src/api/controllers/global/self.ts +28 -44
  8. package/src/api/controllers/global/users.ts +65 -25
  9. package/src/api/controllers/system/accounts.ts +7 -5
  10. package/src/api/controllers/system/tenants.ts +4 -8
  11. package/src/api/index.ts +4 -20
  12. package/src/api/routes/global/auth.ts +10 -7
  13. package/src/api/routes/global/tests/auth.spec.ts +226 -17
  14. package/src/api/routes/global/tests/configs.spec.ts +5 -5
  15. package/src/api/routes/global/tests/roles.spec.ts +20 -14
  16. package/src/api/routes/global/tests/self.spec.ts +3 -4
  17. package/src/api/routes/global/tests/users.spec.ts +24 -27
  18. package/src/api/routes/system/tenants.ts +1 -1
  19. package/src/api/routes/system/tests/accounts.spec.ts +4 -4
  20. package/src/api/routes/system/tests/migrations.spec.ts +2 -2
  21. package/src/api/routes/system/tests/restore.spec.ts +2 -2
  22. package/src/api/routes/system/tests/status.spec.ts +5 -5
  23. package/src/ddApm.ts +7 -0
  24. package/src/elasticApm.ts +10 -0
  25. package/src/environment.ts +15 -1
  26. package/src/index.ts +12 -7
  27. package/src/middleware/tests/tenancy.spec.ts +4 -4
  28. package/src/migrations/functions/globalInfoSyncUsers.ts +4 -3
  29. package/src/sdk/accounts/index.ts +2 -1
  30. package/src/sdk/accounts/{accounts.ts → metadata.ts} +0 -1
  31. package/src/sdk/auth/auth.ts +86 -0
  32. package/src/sdk/auth/index.ts +1 -0
  33. package/src/sdk/tenants/index.ts +1 -0
  34. package/src/sdk/tenants/tenants.ts +76 -0
  35. package/src/sdk/users/events.ts +4 -0
  36. package/src/sdk/users/index.ts +1 -0
  37. package/src/sdk/users/tests/users.spec.ts +52 -0
  38. package/src/sdk/users/users.ts +53 -46
  39. package/src/tests/TestConfiguration.ts +41 -62
  40. package/src/tests/api/auth.ts +42 -14
  41. package/src/tests/api/base.ts +2 -1
  42. package/src/tests/api/configs.ts +2 -2
  43. package/src/tests/api/restore.ts +1 -0
  44. package/src/tests/api/users.ts +2 -2
  45. package/src/tests/jestEnv.ts +2 -1
  46. package/src/tests/jestSetup.ts +10 -6
  47. package/src/tests/logging.ts +34 -0
  48. package/src/tests/structures/index.ts +0 -4
  49. package/src/utilities/email.ts +3 -3
  50. package/src/utilities/redis.ts +2 -0
  51. package/coverage/clover.xml +0 -2199
  52. package/coverage/coverage-final.json +0 -84
  53. package/coverage/lcov-report/base.css +0 -224
  54. package/coverage/lcov-report/block-navigation.js +0 -87
  55. package/coverage/lcov-report/favicon.png +0 -0
  56. package/coverage/lcov-report/index.html +0 -431
  57. package/coverage/lcov-report/prettify.css +0 -1
  58. package/coverage/lcov-report/prettify.js +0 -2
  59. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  60. package/coverage/lcov-report/sorter.js +0 -196
  61. package/coverage/lcov-report/src/api/controllers/global/auth.ts.html +0 -934
  62. package/coverage/lcov-report/src/api/controllers/global/configs.ts.html +0 -1348
  63. package/coverage/lcov-report/src/api/controllers/global/email.ts.html +0 -196
  64. package/coverage/lcov-report/src/api/controllers/global/events.ts.html +0 -136
  65. package/coverage/lcov-report/src/api/controllers/global/index.html +0 -251
  66. package/coverage/lcov-report/src/api/controllers/global/license.ts.html +0 -187
  67. package/coverage/lcov-report/src/api/controllers/global/roles.ts.html +0 -283
  68. package/coverage/lcov-report/src/api/controllers/global/self.ts.html +0 -577
  69. package/coverage/lcov-report/src/api/controllers/global/templates.ts.html +0 -316
  70. package/coverage/lcov-report/src/api/controllers/global/users.ts.html +0 -838
  71. package/coverage/lcov-report/src/api/controllers/global/workspaces.ts.html +0 -244
  72. package/coverage/lcov-report/src/api/controllers/system/accounts.ts.html +0 -148
  73. package/coverage/lcov-report/src/api/controllers/system/environment.ts.html +0 -124
  74. package/coverage/lcov-report/src/api/controllers/system/index.html +0 -191
  75. package/coverage/lcov-report/src/api/controllers/system/migrations.ts.html +0 -124
  76. package/coverage/lcov-report/src/api/controllers/system/restore.ts.html +0 -124
  77. package/coverage/lcov-report/src/api/controllers/system/status.ts.html +0 -133
  78. package/coverage/lcov-report/src/api/controllers/system/tenants.ts.html +0 -154
  79. package/coverage/lcov-report/src/api/index.html +0 -116
  80. package/coverage/lcov-report/src/api/index.ts.html +0 -595
  81. package/coverage/lcov-report/src/api/routes/global/auth.ts.html +0 -349
  82. package/coverage/lcov-report/src/api/routes/global/configs.ts.html +0 -457
  83. package/coverage/lcov-report/src/api/routes/global/email.ts.html +0 -193
  84. package/coverage/lcov-report/src/api/routes/global/events.ts.html +0 -109
  85. package/coverage/lcov-report/src/api/routes/global/index.html +0 -251
  86. package/coverage/lcov-report/src/api/routes/global/license.ts.html +0 -124
  87. package/coverage/lcov-report/src/api/routes/global/roles.ts.html +0 -133
  88. package/coverage/lcov-report/src/api/routes/global/self.ts.html +0 -139
  89. package/coverage/lcov-report/src/api/routes/global/templates.ts.html +0 -196
  90. package/coverage/lcov-report/src/api/routes/global/users.ts.html +0 -475
  91. package/coverage/lcov-report/src/api/routes/global/workspaces.ts.html +0 -196
  92. package/coverage/lcov-report/src/api/routes/index.html +0 -116
  93. package/coverage/lcov-report/src/api/routes/index.ts.html +0 -202
  94. package/coverage/lcov-report/src/api/routes/system/accounts.ts.html +0 -142
  95. package/coverage/lcov-report/src/api/routes/system/environment.ts.html +0 -109
  96. package/coverage/lcov-report/src/api/routes/system/index.html +0 -191
  97. package/coverage/lcov-report/src/api/routes/system/migrations.ts.html +0 -142
  98. package/coverage/lcov-report/src/api/routes/system/restore.ts.html +0 -109
  99. package/coverage/lcov-report/src/api/routes/system/status.ts.html +0 -109
  100. package/coverage/lcov-report/src/api/routes/system/tenants.ts.html +0 -124
  101. package/coverage/lcov-report/src/api/routes/validation/index.html +0 -131
  102. package/coverage/lcov-report/src/api/routes/validation/index.ts.html +0 -88
  103. package/coverage/lcov-report/src/api/routes/validation/users.ts.html +0 -235
  104. package/coverage/lcov-report/src/constants/index.html +0 -116
  105. package/coverage/lcov-report/src/constants/index.ts.html +0 -637
  106. package/coverage/lcov-report/src/constants/templates/index.html +0 -116
  107. package/coverage/lcov-report/src/constants/templates/index.ts.html +0 -316
  108. package/coverage/lcov-report/src/db/index.html +0 -116
  109. package/coverage/lcov-report/src/db/index.ts.html +0 -115
  110. package/coverage/lcov-report/src/environment.ts.html +0 -388
  111. package/coverage/lcov-report/src/index.html +0 -131
  112. package/coverage/lcov-report/src/index.ts.html +0 -394
  113. package/coverage/lcov-report/src/middleware/cloudRestricted.ts.html +0 -139
  114. package/coverage/lcov-report/src/middleware/index.html +0 -116
  115. package/coverage/lcov-report/src/migrations/functions/globalInfoSyncUsers.ts.html +0 -145
  116. package/coverage/lcov-report/src/migrations/functions/index.html +0 -116
  117. package/coverage/lcov-report/src/migrations/index.html +0 -116
  118. package/coverage/lcov-report/src/migrations/index.ts.html +0 -271
  119. package/coverage/lcov-report/src/sdk/accounts/accounts.ts.html +0 -262
  120. package/coverage/lcov-report/src/sdk/accounts/index.html +0 -131
  121. package/coverage/lcov-report/src/sdk/accounts/index.ts.html +0 -88
  122. package/coverage/lcov-report/src/sdk/index.html +0 -116
  123. package/coverage/lcov-report/src/sdk/index.ts.html +0 -106
  124. package/coverage/lcov-report/src/sdk/users/events.ts.html +0 -601
  125. package/coverage/lcov-report/src/sdk/users/index.html +0 -146
  126. package/coverage/lcov-report/src/sdk/users/index.ts.html +0 -88
  127. package/coverage/lcov-report/src/sdk/users/users.ts.html +0 -1999
  128. package/coverage/lcov-report/src/tests/TestConfiguration.ts.html +0 -1075
  129. package/coverage/lcov-report/src/tests/api/accounts.ts.html +0 -160
  130. package/coverage/lcov-report/src/tests/api/auth.ts.html +0 -220
  131. package/coverage/lcov-report/src/tests/api/base.ts.html +0 -133
  132. package/coverage/lcov-report/src/tests/api/configs.ts.html +0 -226
  133. package/coverage/lcov-report/src/tests/api/email.ts.html +0 -148
  134. package/coverage/lcov-report/src/tests/api/environment.ts.html +0 -130
  135. package/coverage/lcov-report/src/tests/api/groups.ts.html +0 -163
  136. package/coverage/lcov-report/src/tests/api/index.html +0 -356
  137. package/coverage/lcov-report/src/tests/api/index.ts.html +0 -238
  138. package/coverage/lcov-report/src/tests/api/license.ts.html +0 -136
  139. package/coverage/lcov-report/src/tests/api/migrations.ts.html +0 -151
  140. package/coverage/lcov-report/src/tests/api/restore.ts.html +0 -127
  141. package/coverage/lcov-report/src/tests/api/roles.ts.html +0 -181
  142. package/coverage/lcov-report/src/tests/api/self.ts.html +0 -163
  143. package/coverage/lcov-report/src/tests/api/status.ts.html +0 -121
  144. package/coverage/lcov-report/src/tests/api/templates.ts.html +0 -175
  145. package/coverage/lcov-report/src/tests/api/tenants.ts.html +0 -130
  146. package/coverage/lcov-report/src/tests/api/users.ts.html +0 -514
  147. package/coverage/lcov-report/src/tests/controllers.ts.html +0 -100
  148. package/coverage/lcov-report/src/tests/index.html +0 -146
  149. package/coverage/lcov-report/src/tests/index.ts.html +0 -103
  150. package/coverage/lcov-report/src/tests/mocks/email.ts.html +0 -115
  151. package/coverage/lcov-report/src/tests/mocks/index.html +0 -131
  152. package/coverage/lcov-report/src/tests/mocks/index.ts.html +0 -106
  153. package/coverage/lcov-report/src/tests/structures/configs.ts.html +0 -313
  154. package/coverage/lcov-report/src/tests/structures/groups.ts.html +0 -118
  155. package/coverage/lcov-report/src/tests/structures/index.html +0 -161
  156. package/coverage/lcov-report/src/tests/structures/index.ts.html +0 -151
  157. package/coverage/lcov-report/src/tests/structures/users.ts.html +0 -196
  158. package/coverage/lcov-report/src/utilities/appService.ts.html +0 -208
  159. package/coverage/lcov-report/src/utilities/email.ts.html +0 -850
  160. package/coverage/lcov-report/src/utilities/fileSystem.ts.html +0 -100
  161. package/coverage/lcov-report/src/utilities/index.html +0 -206
  162. package/coverage/lcov-report/src/utilities/index.ts.html +0 -112
  163. package/coverage/lcov-report/src/utilities/redis.ts.html +0 -424
  164. package/coverage/lcov-report/src/utilities/templates.ts.html +0 -232
  165. package/coverage/lcov-report/src/utilities/users.ts.html +0 -133
  166. package/coverage/lcov.info +0 -4187
  167. package/src/tests/structures/users.ts +0 -37
@@ -7,7 +7,7 @@ import { Config, events } from "@budibase/backend-core"
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
 
@@ -221,7 +221,7 @@ describe("configs", () => {
221
221
  })
222
222
 
223
223
  it("should create settings config with non-default settings", async () => {
224
- config.modeSelf()
224
+ config.selfHosted()
225
225
  await config.deleteConfig(Config.SETTINGS)
226
226
  const conf = {
227
227
  company: "acme",
@@ -234,13 +234,13 @@ describe("configs", () => {
234
234
  expect(events.org.nameUpdated).toBeCalledTimes(1)
235
235
  expect(events.org.logoUpdated).toBeCalledTimes(1)
236
236
  expect(events.org.platformURLUpdated).toBeCalledTimes(1)
237
- config.modeCloud()
237
+ config.cloudHosted()
238
238
  })
239
239
  })
240
240
 
241
241
  describe("update", () => {
242
242
  it("should update settings config", async () => {
243
- config.modeSelf()
243
+ config.selfHosted()
244
244
  await config.deleteConfig(Config.SETTINGS)
245
245
  const settingsConfig = await saveSettingsConfig()
246
246
  settingsConfig.config.company = "acme"
@@ -256,7 +256,7 @@ describe("configs", () => {
256
256
  expect(events.org.nameUpdated).toBeCalledTimes(1)
257
257
  expect(events.org.logoUpdated).toBeCalledTimes(1)
258
258
  expect(events.org.platformURLUpdated).toBeCalledTimes(1)
259
- config.modeCloud()
259
+ config.cloudHosted()
260
260
  })
261
261
  })
262
262
  })
@@ -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",
@@ -32,6 +32,7 @@ async function addAppMetadata() {
32
32
 
33
33
  describe("/api/global/roles", () => {
34
34
  const config = new TestConfiguration()
35
+
35
36
  const role = new roles.Role(
36
37
  db.generateRoleID("newRole"),
37
38
  roles.BUILTIN_ROLE_IDS.BASIC,
@@ -39,12 +40,19 @@ describe("/api/global/roles", () => {
39
40
  )
40
41
 
41
42
  beforeAll(async () => {
42
- console.debug(role)
43
- appDb.put(role)
44
- await addAppMetadata()
45
43
  await config.beforeAll()
46
44
  })
47
45
 
46
+ beforeEach(async () => {
47
+ appId = db.generateAppID(config.tenantId)
48
+ appDb = db.getDB(appId)
49
+ const mockAppDB = context.getAppDB as Mock
50
+ mockAppDB.mockReturnValue(appDb)
51
+
52
+ await addAppMetadata()
53
+ await appDb.put(role)
54
+ })
55
+
48
56
  afterAll(async () => {
49
57
  await config.afterAll()
50
58
  })
@@ -57,16 +65,14 @@ describe("/api/global/roles", () => {
57
65
  it("retrieves roles", async () => {
58
66
  const res = await config.api.roles.get()
59
67
  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
- )
68
+ expect(res.body[appId].roles.length).toEqual(5)
69
+ expect(res.body[appId].roles.map((r: any) => r._id)).toContain(role._id)
64
70
  })
65
71
  })
66
72
 
67
73
  describe("GET api/global/roles/:appId", () => {
68
74
  it("finds a role by appId", async () => {
69
- const res = await config.api.roles.find("app_test")
75
+ const res = await config.api.roles.find(appId)
70
76
  expect(res.body).toBeDefined()
71
77
  expect(res.body.name).toEqual("New App")
72
78
  })
@@ -79,9 +85,9 @@ describe("/api/global/roles", () => {
79
85
  app_test: "role1",
80
86
  }
81
87
  const userResponse = await config.createUser(user)
82
- const res = await config.api.roles.remove("app_test")
88
+ const res = await config.api.roles.remove(appId)
83
89
  const updatedUser = await config.api.users.getUser(userResponse._id!)
84
- expect(updatedUser.body.roles).not.toHaveProperty("app_test")
90
+ expect(updatedUser.body.roles).not.toHaveProperty(appId)
85
91
  expect(res.body.message).toEqual("App role removed from all users")
86
92
  })
87
93
  })
@@ -30,7 +30,7 @@ describe("/api/global/self", () => {
30
30
  user.dayPassRecordedAt = mocks.date.MOCK_DATE.toISOString()
31
31
  expect(res.body._id).toBe(user._id)
32
32
  expect(events.user.updated).toBeCalledTimes(1)
33
- expect(events.user.updated).toBeCalledWith(user)
33
+ expect(events.user.updated).toBeCalledWith(dbUser)
34
34
  expect(events.user.passwordUpdated).not.toBeCalled()
35
35
  })
36
36
 
@@ -44,12 +44,11 @@ describe("/api/global/self", () => {
44
44
  const dbUser = await config.getUser(user.email)
45
45
  user._rev = dbUser._rev
46
46
  user.dayPassRecordedAt = mocks.date.MOCK_DATE.toISOString()
47
- delete user.password
48
47
  expect(res.body._id).toBe(user._id)
49
48
  expect(events.user.updated).toBeCalledTimes(1)
50
- expect(events.user.updated).toBeCalledWith(user)
49
+ expect(events.user.updated).toBeCalledWith(dbUser)
51
50
  expect(events.user.passwordUpdated).toBeCalledTimes(1)
52
- expect(events.user.passwordUpdated).toBeCalledWith(user)
51
+ expect(events.user.passwordUpdated).toBeCalledWith(dbUser)
53
52
  })
54
53
  })
55
54
  })
@@ -1,14 +1,11 @@
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 { events, tenancy, accounts as _accounts } from "@budibase/backend-core"
7
+
8
+ const accounts = jest.mocked(_accounts)
12
9
 
13
10
  describe("/api/global/users", () => {
14
11
  const config = new TestConfiguration()
@@ -25,7 +22,7 @@ describe("/api/global/users", () => {
25
22
  jest.clearAllMocks()
26
23
  })
27
24
 
28
- describe("invite", () => {
25
+ describe("POST /api/global/users/invite", () => {
29
26
  it("should be able to generate an invitation", async () => {
30
27
  const email = structures.users.newEmail()
31
28
  const { code, res } = await config.api.users.sendUserInvite(
@@ -42,7 +39,7 @@ describe("/api/global/users", () => {
42
39
  it("should not be able to generate an invitation for existing user", async () => {
43
40
  const { code, res } = await config.api.users.sendUserInvite(
44
41
  sendMailMock,
45
- config.defaultUser!.email,
42
+ config.user!.email,
46
43
  400
47
44
  )
48
45
 
@@ -70,7 +67,7 @@ describe("/api/global/users", () => {
70
67
  })
71
68
  })
72
69
 
73
- describe("inviteMultiple", () => {
70
+ describe("POST /api/global/users/multi/invite", () => {
74
71
  it("should be able to generate an invitation", async () => {
75
72
  const newUserInvite = () => ({
76
73
  email: structures.users.newEmail(),
@@ -88,7 +85,7 @@ describe("/api/global/users", () => {
88
85
  })
89
86
 
90
87
  it("should not be able to generate an invitation for existing user", async () => {
91
- const request = [{ email: config.defaultUser!.email, userInfo: {} }]
88
+ const request = [{ email: config.user!.email, userInfo: {} }]
92
89
 
93
90
  const res = await config.api.users.sendMultiUserInvite(request)
94
91
 
@@ -101,7 +98,7 @@ describe("/api/global/users", () => {
101
98
  })
102
99
  })
103
100
 
104
- describe("bulk (create)", () => {
101
+ describe("POST /api/global/users/bulk", () => {
105
102
  it("should ignore users existing in the same tenant", async () => {
106
103
  const user = await config.createUser()
107
104
  jest.clearAllMocks()
@@ -118,7 +115,7 @@ describe("/api/global/users", () => {
118
115
  const user = await config.createUser()
119
116
  jest.clearAllMocks()
120
117
 
121
- await tenancy.doInTenant(TENANT_1, async () => {
118
+ await tenancy.doInTenant(config.getTenantId(), async () => {
122
119
  const response = await config.api.users.bulkCreateUsers([user])
123
120
 
124
121
  expect(response.created?.successful.length).toBe(0)
@@ -164,7 +161,7 @@ describe("/api/global/users", () => {
164
161
  })
165
162
  })
166
163
 
167
- describe("create", () => {
164
+ describe("POST /api/global/users", () => {
168
165
  it("should be able to create a basic user", async () => {
169
166
  const user = structures.users.user()
170
167
 
@@ -231,7 +228,7 @@ describe("/api/global/users", () => {
231
228
  const user = await config.createUser()
232
229
  jest.clearAllMocks()
233
230
 
234
- await tenancy.doInTenant(TENANT_1, async () => {
231
+ await tenancy.doInTenant(config.getTenantId(), async () => {
235
232
  delete user._id
236
233
  const response = await config.api.users.saveUser(user, 400)
237
234
 
@@ -243,7 +240,7 @@ describe("/api/global/users", () => {
243
240
  it("should not be able to create user with the same email as an account", async () => {
244
241
  const user = structures.users.user()
245
242
  const account = structures.accounts.cloudAccount()
246
- mocks.accounts.getAccount.mockReturnValueOnce(account)
243
+ accounts.getAccount.mockReturnValueOnce(Promise.resolve(account))
247
244
 
248
245
  const response = await config.api.users.saveUser(user, 400)
249
246
 
@@ -284,7 +281,7 @@ describe("/api/global/users", () => {
284
281
  })
285
282
  })
286
283
 
287
- describe("update", () => {
284
+ describe("POST /api/global/users (update)", () => {
288
285
  it("should be able to update a basic user", async () => {
289
286
  const user = await config.createUser()
290
287
  jest.clearAllMocks()
@@ -299,7 +296,7 @@ describe("/api/global/users", () => {
299
296
  })
300
297
 
301
298
  it("should not allow a user to update their own admin/builder status", async () => {
302
- const user = (await config.api.users.getUser(config.defaultUser?._id!))
299
+ const user = (await config.api.users.getUser(config.user?._id!))
303
300
  .body as User
304
301
  await config.api.users.saveUser({
305
302
  ...user,
@@ -444,7 +441,7 @@ describe("/api/global/users", () => {
444
441
  })
445
442
 
446
443
  it("should not be able to update email address", async () => {
447
- const email = "email@test.com"
444
+ const email = structures.email()
448
445
  const user = await config.createUser(structures.users.user({ email }))
449
446
  user.email = "new@test.com"
450
447
 
@@ -469,9 +466,9 @@ describe("/api/global/users", () => {
469
466
  })
470
467
  })
471
468
 
472
- describe("bulk (delete)", () => {
469
+ describe("POST /api/global/users/bulk (delete)", () => {
473
470
  it("should not be able to bulk delete current user", async () => {
474
- const user = await config.defaultUser!
471
+ const user = await config.user!
475
472
 
476
473
  const response = await config.api.users.bulkDeleteUsers([user._id!], 400)
477
474
 
@@ -483,7 +480,7 @@ describe("/api/global/users", () => {
483
480
  const user = await config.createUser()
484
481
  const account = structures.accounts.cloudAccount()
485
482
  account.budibaseUserId = user._id!
486
- mocks.accounts.getAccountByTenantId.mockReturnValue(account)
483
+ accounts.getAccountByTenantId.mockReturnValue(Promise.resolve(account))
487
484
 
488
485
  const response = await config.api.users.bulkDeleteUsers([user._id!])
489
486
 
@@ -498,7 +495,7 @@ describe("/api/global/users", () => {
498
495
 
499
496
  it("should be able to bulk delete users", async () => {
500
497
  const account = structures.accounts.cloudAccount()
501
- mocks.accounts.getAccountByTenantId.mockReturnValue(account)
498
+ accounts.getAccountByTenantId.mockReturnValue(Promise.resolve(account))
502
499
 
503
500
  const builder = structures.users.builderUser()
504
501
  const admin = structures.users.adminUser()
@@ -522,7 +519,7 @@ describe("/api/global/users", () => {
522
519
  })
523
520
  })
524
521
 
525
- describe("destroy", () => {
522
+ describe("DELETE /api/global/users/:userId", () => {
526
523
  it("should be able to destroy a basic user", async () => {
527
524
  const user = await config.createUser()
528
525
  jest.clearAllMocks()
@@ -559,7 +556,7 @@ describe("/api/global/users", () => {
559
556
  it("should not be able to destroy account owner", async () => {
560
557
  const user = await config.createUser()
561
558
  const account = structures.accounts.cloudAccount()
562
- mocks.accounts.getAccount.mockReturnValueOnce(account)
559
+ accounts.getAccount.mockReturnValueOnce(Promise.resolve(account))
563
560
 
564
561
  const response = await config.api.users.deleteUser(user._id!, 400)
565
562
 
@@ -567,10 +564,10 @@ describe("/api/global/users", () => {
567
564
  })
568
565
 
569
566
  it("should not be able to destroy account owner as account owner", async () => {
570
- const user = await config.defaultUser!
567
+ const user = await config.user!
571
568
  const account = structures.accounts.cloudAccount()
572
569
  account.email = user.email
573
- mocks.accounts.getAccount.mockReturnValueOnce(account)
570
+ accounts.getAccount.mockReturnValueOnce(Promise.resolve(account))
574
571
 
575
572
  const response = await config.api.users.deleteUser(user._id!, 400)
576
573
 
@@ -7,7 +7,7 @@ const router: Router = new Router()
7
7
  router.delete(
8
8
  "/api/system/tenants/:tenantId",
9
9
  middleware.adminOnly,
10
- controller.delete
10
+ controller.destroy
11
11
  )
12
12
 
13
13
  export default router
@@ -1,4 +1,4 @@
1
- import sdk from "../../../../sdk"
1
+ import * as accounts from "../../../../sdk/accounts"
2
2
  import { TestConfiguration, structures } from "../../../../tests"
3
3
  import { v4 as uuid } from "uuid"
4
4
 
@@ -24,8 +24,8 @@ describe("accounts", () => {
24
24
 
25
25
  const response = await config.api.accounts.saveMetadata(account)
26
26
 
27
- const id = sdk.accounts.formatAccountMetadataId(account.accountId)
28
- const metadata = await sdk.accounts.getMetadata(id)
27
+ const id = accounts.metadata.formatAccountMetadataId(account.accountId)
28
+ const metadata = await accounts.metadata.getMetadata(id)
29
29
  expect(response).toStrictEqual(metadata)
30
30
  })
31
31
  })
@@ -37,7 +37,7 @@ describe("accounts", () => {
37
37
 
38
38
  await config.api.accounts.destroyMetadata(account.accountId)
39
39
 
40
- const deleted = await sdk.accounts.getMetadata(account.accountId)
40
+ const deleted = await accounts.metadata.getMetadata(account.accountId)
41
41
  expect(deleted).toBe(undefined)
42
42
  })
43
43
 
@@ -30,7 +30,7 @@ describe("/api/system/migrations", () => {
30
30
  headers: {},
31
31
  status: 403,
32
32
  })
33
- expect(res.text).toBe("Unauthorized - no public worker access")
33
+ expect(res.body).toEqual({ message: "Unauthorized", status: 403 })
34
34
  expect(migrateFn).toBeCalledTimes(0)
35
35
  })
36
36
 
@@ -47,7 +47,7 @@ describe("/api/system/migrations", () => {
47
47
  headers: {},
48
48
  status: 403,
49
49
  })
50
- expect(res.text).toBe("Unauthorized - no public worker access")
50
+ expect(res.body).toEqual({ message: "Unauthorized", status: 403 })
51
51
  })
52
52
 
53
53
  it("returns definitions", async () => {
@@ -25,12 +25,12 @@ describe("/api/system/restore", () => {
25
25
  })
26
26
 
27
27
  it("restores in self host", async () => {
28
- config.modeSelf()
28
+ config.selfHosted()
29
29
  const res = await config.api.restore.restored()
30
30
  expect(res.body).toEqual({
31
31
  message: "System prepared after restore.",
32
32
  })
33
- config.modeCloud()
33
+ config.cloudHosted()
34
34
  })
35
35
  })
36
36
  })
@@ -1,6 +1,6 @@
1
1
  import { TestConfiguration } from "../../../../tests"
2
- import { accounts } from "@budibase/backend-core"
3
- import { mocks } from "@budibase/backend-core/tests"
2
+ import { accounts as _accounts } from "@budibase/backend-core"
3
+ const accounts = jest.mocked(_accounts)
4
4
 
5
5
  describe("/api/system/status", () => {
6
6
  const config = new TestConfiguration()
@@ -19,7 +19,7 @@ describe("/api/system/status", () => {
19
19
 
20
20
  describe("GET /api/system/status", () => {
21
21
  it("returns status in self host", async () => {
22
- config.modeSelf()
22
+ config.selfHosted()
23
23
  const res = await config.api.status.getStatus()
24
24
  expect(res.body).toEqual({
25
25
  health: {
@@ -27,7 +27,7 @@ describe("/api/system/status", () => {
27
27
  },
28
28
  })
29
29
  expect(accounts.getStatus).toBeCalledTimes(0)
30
- config.modeCloud()
30
+ config.cloudHosted()
31
31
  })
32
32
 
33
33
  it("returns status in cloud", async () => {
@@ -37,7 +37,7 @@ describe("/api/system/status", () => {
37
37
  },
38
38
  }
39
39
 
40
- mocks.accounts.getStatus.mockReturnValueOnce(value)
40
+ accounts.getStatus.mockReturnValueOnce(Promise.resolve(value))
41
41
 
42
42
  const res = await config.api.status.getStatus()
43
43
 
package/src/ddApm.ts ADDED
@@ -0,0 +1,7 @@
1
+ import apm from "dd-trace"
2
+
3
+ // enable APM if configured
4
+ if (process.env.DD_APM_ENABLED) {
5
+ console.log("Starting dd-trace")
6
+ apm.init()
7
+ }
@@ -0,0 +1,10 @@
1
+ import apm from "elastic-apm-node"
2
+
3
+ // enable APM if configured
4
+ if (process.env.ELASTIC_APM_ENABLED) {
5
+ console.log("Starting elastic-apm-node")
6
+ apm.start({
7
+ serviceName: process.env.SERVICE,
8
+ environment: process.env.BUDIBASE_ENVIRONMENT,
9
+ })
10
+ }
@@ -26,6 +26,8 @@ function parseIntSafe(number: any) {
26
26
  }
27
27
  }
28
28
 
29
+ const selfHosted = !!parseInt(process.env.SELF_HOSTED || "")
30
+
29
31
  const environment = {
30
32
  // auth
31
33
  MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY,
@@ -49,7 +51,7 @@ const environment = {
49
51
  CLUSTER_PORT: process.env.CLUSTER_PORT,
50
52
  // flags
51
53
  NODE_ENV: process.env.NODE_ENV,
52
- SELF_HOSTED: !!parseInt(process.env.SELF_HOSTED || ""),
54
+ SELF_HOSTED: selfHosted,
53
55
  LOG_LEVEL: process.env.LOG_LEVEL,
54
56
  MULTI_TENANCY: process.env.MULTI_TENANCY,
55
57
  DISABLE_ACCOUNT_PORTAL: process.env.DISABLE_ACCOUNT_PORTAL,
@@ -65,6 +67,18 @@ const environment = {
65
67
  CHECKLIST_CACHE_TTL: parseIntSafe(process.env.CHECKLIST_CACHE_TTL) || 3600,
66
68
  SESSION_UPDATE_PERIOD: process.env.SESSION_UPDATE_PERIOD,
67
69
  ENCRYPTED_TEST_PUBLIC_API_KEY: process.env.ENCRYPTED_TEST_PUBLIC_API_KEY,
70
+ /**
71
+ * Mock the email service in use - links to ethereal hosted emails are logged instead.
72
+ */
73
+ ENABLE_EMAIL_TEST_MODE: process.env.ENABLE_EMAIL_TEST_MODE,
74
+ /**
75
+ * Enable to allow an admin user to login using a password.
76
+ * This can be useful to prevent lockout when configuring SSO.
77
+ * However, this should be turned OFF by default for security purposes.
78
+ */
79
+ ENABLE_SSO_MAINTENANCE_MODE: selfHosted
80
+ ? process.env.ENABLE_SSO_MAINTENANCE_MODE
81
+ : false,
68
82
  _set(key: any, value: any) {
69
83
  process.env[key] = value
70
84
  // @ts-ignore
package/src/index.ts CHANGED
@@ -1,14 +1,13 @@
1
- // need to load environment first
2
- import env from "./environment"
1
+ if (process.env.DD_APM_ENABLED) {
2
+ require("./ddApm")
3
+ }
3
4
 
4
- // enable APM if configured
5
5
  if (process.env.ELASTIC_APM_ENABLED) {
6
- const apm = require("elastic-apm-node").start({
7
- serviceName: process.env.SERVICE,
8
- environment: process.env.BUDIBASE_ENVIRONMENT,
9
- })
6
+ require("./elasticApm")
10
7
  }
11
8
 
9
+ // need to load environment first
10
+ import env from "./environment"
12
11
  import { Scope } from "@sentry/node"
13
12
  import { Event } from "@sentry/types/dist/event"
14
13
  import Application from "koa"
@@ -26,6 +25,12 @@ const koaSession = require("koa-session")
26
25
  const logger = require("koa-pino-logger")
27
26
  import destroyable from "server-destroy"
28
27
 
28
+ if (env.ENABLE_SSO_MAINTENANCE_MODE) {
29
+ console.warn(
30
+ "Warning: ENABLE_SSO_MAINTENANCE_MODE is set. It is recommended this flag is disabled if maintenance is not in progress"
31
+ )
32
+ }
33
+
29
34
  // this will setup http and https proxies form env variables
30
35
  bootstrap()
31
36
 
@@ -24,7 +24,7 @@ describe("tenancy middleware", () => {
24
24
  })
25
25
 
26
26
  it("should get tenant id from header", async () => {
27
- const tenantId = structures.uuid()
27
+ const tenantId = structures.tenant.id()
28
28
  const headers = {
29
29
  [constants.Header.TENANT_ID]: tenantId,
30
30
  }
@@ -35,7 +35,7 @@ describe("tenancy middleware", () => {
35
35
  })
36
36
 
37
37
  it("should get tenant id from query param", async () => {
38
- const tenantId = structures.uuid()
38
+ const tenantId = structures.tenant.id()
39
39
  const res = await config.request.get(
40
40
  `/api/global/configs/checklist?tenantId=${tenantId}`
41
41
  )
@@ -43,7 +43,7 @@ describe("tenancy middleware", () => {
43
43
  })
44
44
 
45
45
  it("should get tenant id from subdomain", async () => {
46
- const tenantId = structures.uuid()
46
+ const tenantId = structures.tenant.id()
47
47
  const headers = {
48
48
  host: `${tenantId}.localhost:10000`,
49
49
  }
@@ -67,7 +67,7 @@ describe("tenancy middleware", () => {
67
67
  it("should throw when no tenant id is found", async () => {
68
68
  const res = await config.request.get(`/api/global/configs/checklist`)
69
69
  expect(res.status).toBe(403)
70
- expect(res.text).toBe("Tenant id not set")
70
+ expect(res.body).toEqual({ message: "Tenant id not set", status: 403 })
71
71
  expect(res.headers[constants.Header.TENANT_ID]).toBe(undefined)
72
72
  })
73
73
  })
@@ -1,5 +1,6 @@
1
1
  import { User } from "@budibase/types"
2
- import sdk from "../../sdk"
2
+ import * as usersSdk from "../../sdk/users"
3
+ import { platform } from "@budibase/backend-core"
3
4
 
4
5
  /**
5
6
  * Date:
@@ -9,11 +10,11 @@ import sdk from "../../sdk"
9
10
  * Re-sync the global-db users to the global-info db users
10
11
  */
11
12
  export const run = async (globalDb: any) => {
12
- const users = (await sdk.users.allUsers()) as User[]
13
+ const users = (await usersSdk.allUsers()) as User[]
13
14
  const promises = []
14
15
  for (let user of users) {
15
16
  promises.push(
16
- sdk.users.addTenant(user.tenantId, user._id as string, user.email)
17
+ platform.users.addUser(user.tenantId, user._id as string, user.email)
17
18
  )
18
19
  }
19
20
  await Promise.all(promises)
@@ -1 +1,2 @@
1
- export * from "./accounts"
1
+ export * as metadata from "./metadata"
2
+ export { accounts as api } from "@budibase/backend-core"
@@ -2,7 +2,6 @@ import { AccountMetadata } from "@budibase/types"
2
2
  import {
3
3
  db,
4
4
  StaticDatabases,
5
- HTTPError,
6
5
  DocumentType,
7
6
  SEPARATOR,
8
7
  } from "@budibase/backend-core"