@budibase/worker 1.2.58 → 1.3.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 (42) hide show
  1. package/package.json +7 -6
  2. package/scripts/jestSetup.js +6 -0
  3. package/src/api/controllers/global/auth.ts +14 -1
  4. package/src/api/controllers/global/users.ts +26 -55
  5. package/src/api/controllers/system/accounts.ts +21 -0
  6. package/src/api/{index.js → index.ts} +12 -16
  7. package/src/api/routes/global/configs.js +1 -0
  8. package/src/api/routes/{tests/auth.spec.js → global/tests/auth.spec.ts} +32 -56
  9. package/src/api/routes/{tests/configs.spec.js → global/tests/configs.spec.ts} +76 -47
  10. package/src/api/routes/{tests/email.spec.js → global/tests/email.spec.ts} +7 -15
  11. package/src/api/routes/{tests/realEmail.spec.js → global/tests/realEmail.spec.ts} +20 -21
  12. package/src/api/routes/{tests/self.spec.js → global/tests/self.spec.ts} +10 -18
  13. package/src/api/routes/global/tests/users.spec.ts +512 -0
  14. package/src/api/routes/index.js +2 -0
  15. package/src/api/routes/system/accounts.ts +19 -0
  16. package/src/api/routes/system/tests/accounts.spec.ts +57 -0
  17. package/src/{environment.js → environment.ts} +10 -9
  18. package/src/index.ts +6 -4
  19. package/src/sdk/accounts/accounts.ts +53 -0
  20. package/src/sdk/accounts/index.ts +1 -0
  21. package/src/sdk/index.ts +1 -0
  22. package/src/sdk/users/users.ts +271 -77
  23. package/src/tests/TestConfiguration.ts +273 -0
  24. package/src/tests/api/accounts.ts +28 -0
  25. package/src/tests/api/auth.ts +48 -0
  26. package/src/tests/api/configs.ts +40 -0
  27. package/src/tests/api/email.ts +24 -0
  28. package/src/tests/api/index.ts +25 -0
  29. package/src/tests/api/self.ts +21 -0
  30. package/src/tests/api/users.ts +111 -0
  31. package/src/tests/index.ts +14 -0
  32. package/src/tests/mocks/index.ts +7 -0
  33. package/src/tests/structures/accounts.ts +24 -0
  34. package/src/tests/structures/index.ts +20 -0
  35. package/src/tests/structures/users.ts +12 -4
  36. package/src/utilities/redis.js +1 -0
  37. package/scripts/load/users.js +0 -97
  38. package/src/api/routes/tests/users.spec.js +0 -390
  39. package/src/tests/TestConfiguration.js +0 -231
  40. package/src/tests/index.js +0 -12
  41. package/src/tests/mocks/index.js +0 -5
  42. package/src/tests/structures/index.js +0 -14
@@ -1,11 +1,12 @@
1
1
  // mock the email system
2
2
  jest.mock("nodemailer")
3
- const { config, structures, mocks, request } = require("../../../tests")
3
+ import { TestConfiguration, structures, mocks, API } from "../../../../tests"
4
4
  mocks.email.mock()
5
- const { Configs } = require("@budibase/backend-core/constants")
6
- const { events } = require("@budibase/backend-core")
5
+ import { Configs, events } from "@budibase/backend-core"
7
6
 
8
7
  describe("configs", () => {
8
+ const config = new TestConfiguration()
9
+ const api = new API(config)
9
10
 
10
11
  beforeAll(async () => {
11
12
  await config.beforeAll()
@@ -20,35 +21,33 @@ describe("configs", () => {
20
21
  })
21
22
 
22
23
  describe("post /api/global/configs", () => {
23
-
24
- const saveConfig = async (conf, _id, _rev) => {
24
+ const saveConfig = async (conf: any, _id?: string, _rev?: string) => {
25
25
  const data = {
26
26
  ...conf,
27
27
  _id,
28
- _rev
28
+ _rev,
29
29
  }
30
30
 
31
- const res = await request
32
- .post(`/api/global/configs`)
33
- .send(data)
34
- .set(config.defaultHeaders())
35
- .expect("Content-Type", /json/)
36
- .expect(200)
31
+ const res = await api.configs.saveConfig(data)
37
32
 
38
33
  return {
39
34
  ...data,
40
- ...res.body
35
+ ...res.body,
41
36
  }
42
37
  }
43
38
 
44
39
  describe("google", () => {
45
- const saveGoogleConfig = async (conf, _id, _rev) => {
40
+ const saveGoogleConfig = async (
41
+ conf?: any,
42
+ _id?: string,
43
+ _rev?: string
44
+ ) => {
46
45
  const googleConfig = structures.configs.google(conf)
47
46
  return saveConfig(googleConfig, _id, _rev)
48
47
  }
49
-
48
+
50
49
  describe("create", () => {
51
- it ("should create activated google config", async () => {
50
+ it("should create activated google config", async () => {
52
51
  await saveGoogleConfig()
53
52
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
54
53
  expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE)
@@ -58,7 +57,7 @@ describe("configs", () => {
58
57
  await config.deleteConfig(Configs.GOOGLE)
59
58
  })
60
59
 
61
- it ("should create deactivated google config", async () => {
60
+ it("should create deactivated google config", async () => {
62
61
  await saveGoogleConfig({ activated: false })
63
62
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
64
63
  expect(events.auth.SSOCreated).toBeCalledWith(Configs.GOOGLE)
@@ -69,10 +68,14 @@ describe("configs", () => {
69
68
  })
70
69
 
71
70
  describe("update", () => {
72
- it ("should update google config to deactivated", async () => {
71
+ it("should update google config to deactivated", async () => {
73
72
  const googleConf = await saveGoogleConfig()
74
73
  jest.clearAllMocks()
75
- await saveGoogleConfig({ ...googleConf.config, activated: false }, googleConf._id, googleConf._rev)
74
+ await saveGoogleConfig(
75
+ { ...googleConf.config, activated: false },
76
+ googleConf._id,
77
+ googleConf._rev
78
+ )
76
79
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
77
80
  expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE)
78
81
  expect(events.auth.SSOActivated).not.toBeCalled()
@@ -81,10 +84,14 @@ describe("configs", () => {
81
84
  await config.deleteConfig(Configs.GOOGLE)
82
85
  })
83
86
 
84
- it ("should update google config to activated", async () => {
87
+ it("should update google config to activated", async () => {
85
88
  const googleConf = await saveGoogleConfig({ activated: false })
86
89
  jest.clearAllMocks()
87
- await saveGoogleConfig({ ...googleConf.config, activated: true}, googleConf._id, googleConf._rev)
90
+ await saveGoogleConfig(
91
+ { ...googleConf.config, activated: true },
92
+ googleConf._id,
93
+ googleConf._rev
94
+ )
88
95
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
89
96
  expect(events.auth.SSOUpdated).toBeCalledWith(Configs.GOOGLE)
90
97
  expect(events.auth.SSODeactivated).not.toBeCalled()
@@ -92,17 +99,21 @@ describe("configs", () => {
92
99
  expect(events.auth.SSOActivated).toBeCalledWith(Configs.GOOGLE)
93
100
  await config.deleteConfig(Configs.GOOGLE)
94
101
  })
95
- })
102
+ })
96
103
  })
97
104
 
98
105
  describe("oidc", () => {
99
- const saveOIDCConfig = async (conf, _id, _rev) => {
106
+ const saveOIDCConfig = async (
107
+ conf?: any,
108
+ _id?: string,
109
+ _rev?: string
110
+ ) => {
100
111
  const oidcConfig = structures.configs.oidc(conf)
101
112
  return saveConfig(oidcConfig, _id, _rev)
102
113
  }
103
114
 
104
115
  describe("create", () => {
105
- it ("should create activated OIDC config", async () => {
116
+ it("should create activated OIDC config", async () => {
106
117
  await saveOIDCConfig()
107
118
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
108
119
  expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC)
@@ -112,7 +123,7 @@ describe("configs", () => {
112
123
  await config.deleteConfig(Configs.OIDC)
113
124
  })
114
125
 
115
- it ("should create deactivated OIDC config", async () => {
126
+ it("should create deactivated OIDC config", async () => {
116
127
  await saveOIDCConfig({ activated: false })
117
128
  expect(events.auth.SSOCreated).toBeCalledTimes(1)
118
129
  expect(events.auth.SSOCreated).toBeCalledWith(Configs.OIDC)
@@ -123,10 +134,14 @@ describe("configs", () => {
123
134
  })
124
135
 
125
136
  describe("update", () => {
126
- it ("should update OIDC config to deactivated", async () => {
137
+ it("should update OIDC config to deactivated", async () => {
127
138
  const oidcConf = await saveOIDCConfig()
128
139
  jest.clearAllMocks()
129
- await saveOIDCConfig({ ...oidcConf.config.configs[0], activated: false }, oidcConf._id, oidcConf._rev)
140
+ await saveOIDCConfig(
141
+ { ...oidcConf.config.configs[0], activated: false },
142
+ oidcConf._id,
143
+ oidcConf._rev
144
+ )
130
145
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
131
146
  expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC)
132
147
  expect(events.auth.SSOActivated).not.toBeCalled()
@@ -135,10 +150,14 @@ describe("configs", () => {
135
150
  await config.deleteConfig(Configs.OIDC)
136
151
  })
137
152
 
138
- it ("should update OIDC config to activated", async () => {
153
+ it("should update OIDC config to activated", async () => {
139
154
  const oidcConf = await saveOIDCConfig({ activated: false })
140
155
  jest.clearAllMocks()
141
- await saveOIDCConfig({ ...oidcConf.config.configs[0], activated: true}, oidcConf._id, oidcConf._rev)
156
+ await saveOIDCConfig(
157
+ { ...oidcConf.config.configs[0], activated: true },
158
+ oidcConf._id,
159
+ oidcConf._rev
160
+ )
142
161
  expect(events.auth.SSOUpdated).toBeCalledTimes(1)
143
162
  expect(events.auth.SSOUpdated).toBeCalledWith(Configs.OIDC)
144
163
  expect(events.auth.SSODeactivated).not.toBeCalled()
@@ -147,17 +166,20 @@ describe("configs", () => {
147
166
  await config.deleteConfig(Configs.OIDC)
148
167
  })
149
168
  })
150
-
151
169
  })
152
170
 
153
171
  describe("smtp", () => {
154
- const saveSMTPConfig = async (conf, _id, _rev) => {
172
+ const saveSMTPConfig = async (
173
+ conf?: any,
174
+ _id?: string,
175
+ _rev?: string
176
+ ) => {
155
177
  const smtpConfig = structures.configs.smtp(conf)
156
178
  return saveConfig(smtpConfig, _id, _rev)
157
179
  }
158
180
 
159
181
  describe("create", () => {
160
- it ("should create SMTP config", async () => {
182
+ it("should create SMTP config", async () => {
161
183
  await config.deleteConfig(Configs.SMTP)
162
184
  await saveSMTPConfig()
163
185
  expect(events.email.SMTPUpdated).not.toBeCalled()
@@ -167,7 +189,7 @@ describe("configs", () => {
167
189
  })
168
190
 
169
191
  describe("update", () => {
170
- it ("should update SMTP config", async () => {
192
+ it("should update SMTP config", async () => {
171
193
  const smtpConf = await saveSMTPConfig()
172
194
  jest.clearAllMocks()
173
195
  await saveSMTPConfig(smtpConf.config, smtpConf._id, smtpConf._rev)
@@ -179,15 +201,19 @@ describe("configs", () => {
179
201
  })
180
202
 
181
203
  describe("settings", () => {
182
- const saveSettingsConfig = async (conf, _id, _rev) => {
204
+ const saveSettingsConfig = async (
205
+ conf?: any,
206
+ _id?: string,
207
+ _rev?: string
208
+ ) => {
183
209
  const settingsConfig = structures.configs.settings(conf)
184
210
  return saveConfig(settingsConfig, _id, _rev)
185
211
  }
186
212
 
187
213
  describe("create", () => {
188
- it ("should create settings config with default settings", async () => {
214
+ it("should create settings config with default settings", async () => {
189
215
  await config.deleteConfig(Configs.SETTINGS)
190
-
216
+
191
217
  await saveSettingsConfig()
192
218
 
193
219
  expect(events.org.nameUpdated).not.toBeCalled()
@@ -195,35 +221,43 @@ describe("configs", () => {
195
221
  expect(events.org.platformURLUpdated).not.toBeCalled()
196
222
  })
197
223
 
198
- it ("should create settings config with non-default settings", async () => {
224
+ it("should create settings config with non-default settings", async () => {
225
+ config.modeSelf()
199
226
  await config.deleteConfig(Configs.SETTINGS)
200
227
  const conf = {
201
228
  company: "acme",
202
229
  logoUrl: "http://example.com",
203
- platformUrl: "http://example.com"
230
+ platformUrl: "http://example.com",
204
231
  }
205
232
 
206
233
  await saveSettingsConfig(conf)
207
-
234
+
208
235
  expect(events.org.nameUpdated).toBeCalledTimes(1)
209
236
  expect(events.org.logoUpdated).toBeCalledTimes(1)
210
237
  expect(events.org.platformURLUpdated).toBeCalledTimes(1)
238
+ config.modeAccount()
211
239
  })
212
240
  })
213
241
 
214
242
  describe("update", () => {
215
- it ("should update settings config", async () => {
243
+ it("should update settings config", async () => {
244
+ config.modeSelf()
216
245
  await config.deleteConfig(Configs.SETTINGS)
217
246
  const settingsConfig = await saveSettingsConfig()
218
247
  settingsConfig.config.company = "acme"
219
248
  settingsConfig.config.logoUrl = "http://example.com"
220
249
  settingsConfig.config.platformUrl = "http://example.com"
221
250
 
222
- await saveSettingsConfig(settingsConfig.config, settingsConfig._id, settingsConfig._rev)
251
+ await saveSettingsConfig(
252
+ settingsConfig.config,
253
+ settingsConfig._id,
254
+ settingsConfig._rev
255
+ )
223
256
 
224
257
  expect(events.org.nameUpdated).toBeCalledTimes(1)
225
258
  expect(events.org.logoUpdated).toBeCalledTimes(1)
226
259
  expect(events.org.platformURLUpdated).toBeCalledTimes(1)
260
+ config.modeAccount()
227
261
  })
228
262
  })
229
263
  })
@@ -232,12 +266,7 @@ describe("configs", () => {
232
266
  it("should return the correct checklist status based on the state of the budibase installation", async () => {
233
267
  await config.saveSmtpConfig()
234
268
 
235
- const res = await request
236
- .get(`/api/global/configs/checklist`)
237
- .set(config.defaultHeaders())
238
- .expect("Content-Type", /json/)
239
- .expect(200)
240
-
269
+ const res = await api.configs.getConfigChecklist()
241
270
  const checklist = res.body
242
271
 
243
272
  expect(checklist.apps.checked).toBeFalsy()
@@ -1,12 +1,11 @@
1
1
  jest.mock("nodemailer")
2
- const { config, mocks, structures, request } = require("../../../tests")
2
+ import { TestConfiguration, mocks, API } from "../../../../tests"
3
3
  const sendMailMock = mocks.email.mock()
4
-
5
- const { EmailTemplatePurpose } = require("../../../constants")
6
-
7
- const TENANT_ID = structures.TENANT_ID
4
+ import { EmailTemplatePurpose } from "../../../../constants"
8
5
 
9
6
  describe("/api/global/email", () => {
7
+ const config = new TestConfiguration()
8
+ const api = new API(config)
10
9
 
11
10
  beforeAll(async () => {
12
11
  await config.beforeAll()
@@ -20,16 +19,9 @@ describe("/api/global/email", () => {
20
19
  // initially configure settings
21
20
  await config.saveSmtpConfig()
22
21
  await config.saveSettingsConfig()
23
- const res = await request
24
- .post(`/api/global/email/send`)
25
- .send({
26
- email: "test@test.com",
27
- purpose: EmailTemplatePurpose.INVITATION,
28
- tenantId: TENANT_ID,
29
- })
30
- .set(config.defaultHeaders())
31
- .expect("Content-Type", /json/)
32
- .expect(200)
22
+
23
+ const res = await api.emails.sendEmail(EmailTemplatePurpose.INVITATION)
24
+
33
25
  expect(res.body.message).toBeDefined()
34
26
  expect(sendMailMock).toHaveBeenCalled()
35
27
  const emailCall = sendMailMock.mock.calls[0][0]
@@ -1,5 +1,5 @@
1
- const { config, request } = require("../../../tests")
2
- const { EmailTemplatePurpose } = require("../../../constants")
1
+ import { TestConfiguration, API } from "../../../../tests"
2
+ import { EmailTemplatePurpose } from "../../../../constants"
3
3
  const nodemailer = require("nodemailer")
4
4
  const fetch = require("node-fetch")
5
5
 
@@ -7,6 +7,8 @@ const fetch = require("node-fetch")
7
7
  jest.setTimeout(30000)
8
8
 
9
9
  describe("/api/global/email", () => {
10
+ const config = new TestConfiguration()
11
+ const api = new API(config)
10
12
 
11
13
  beforeAll(async () => {
12
14
  await config.beforeAll()
@@ -16,27 +18,24 @@ describe("/api/global/email", () => {
16
18
  await config.afterAll()
17
19
  })
18
20
 
19
- async function sendRealEmail(purpose) {
21
+ async function sendRealEmail(purpose: string) {
20
22
  let response, text
21
23
  try {
22
- const timeout = () => new Promise((resolve, reject) =>
23
- setTimeout(() => reject({
24
- status: 301,
25
- errno: "ETIME"
26
- }), 20000)
27
- )
24
+ const timeout = () =>
25
+ new Promise((resolve, reject) =>
26
+ setTimeout(
27
+ () =>
28
+ reject({
29
+ status: 301,
30
+ errno: "ETIME",
31
+ }),
32
+ 20000
33
+ )
34
+ )
28
35
  await Promise.race([config.saveEtherealSmtpConfig(), timeout()])
29
36
  await Promise.race([config.saveSettingsConfig(), timeout()])
30
- const user = await config.getUser("test@test.com")
31
- const res = await request
32
- .post(`/api/global/email/send`)
33
- .send({
34
- email: "test@test.com",
35
- purpose,
36
- userId: user._id,
37
- })
38
- .set(config.defaultHeaders())
39
- .timeout(20000)
37
+
38
+ const res = await api.emails.sendEmail(purpose).timeout(20000)
40
39
  // ethereal hiccup, can't test right now
41
40
  if (res.status >= 300) {
42
41
  return
@@ -47,7 +46,7 @@ describe("/api/global/email", () => {
47
46
  expect(testUrl).toBeDefined()
48
47
  response = await fetch(testUrl)
49
48
  text = await response.text()
50
- } catch (err) {
49
+ } catch (err: any) {
51
50
  // ethereal hiccup, can't test right now
52
51
  if (parseInt(err.status) >= 300 || (err && err.errno === "ETIME")) {
53
52
  return
@@ -81,4 +80,4 @@ describe("/api/global/email", () => {
81
80
  it("should be able to send a password recovery email", async () => {
82
81
  await sendRealEmail(EmailTemplatePurpose.PASSWORD_RECOVERY)
83
82
  })
84
- })
83
+ })
@@ -1,8 +1,10 @@
1
1
  jest.mock("nodemailer")
2
- const { config, request } = require("../../../tests")
3
- const { events } = require("@budibase/backend-core")
2
+ import { TestConfiguration, API } from "../../../../tests"
3
+ import { events } from "@budibase/backend-core"
4
4
 
5
5
  describe("/api/global/self", () => {
6
+ const config = new TestConfiguration()
7
+ const api = new API(config)
6
8
 
7
9
  beforeAll(async () => {
8
10
  await config.beforeAll()
@@ -16,23 +18,13 @@ describe("/api/global/self", () => {
16
18
  jest.clearAllMocks()
17
19
  })
18
20
 
19
- const updateSelf = async (user) => {
20
- const res = await request
21
- .post(`/api/global/self`)
22
- .send(user)
23
- .set(config.defaultHeaders())
24
- .expect("Content-Type", /json/)
25
- .expect(200)
26
- return res
27
- }
28
-
29
21
  describe("update", () => {
30
-
31
22
  it("should update self", async () => {
32
23
  const user = await config.createUser()
24
+ await config.createSession(user)
33
25
 
34
26
  delete user.password
35
- const res = await updateSelf(user)
27
+ const res = await api.self.updateSelf(user)
36
28
 
37
29
  expect(res.body._id).toBe(user._id)
38
30
  expect(events.user.updated).toBeCalledTimes(1)
@@ -42,10 +34,10 @@ describe("/api/global/self", () => {
42
34
 
43
35
  it("should update password", async () => {
44
36
  const user = await config.createUser()
45
- const password = "newPassword"
46
- user.password = password
37
+ await config.createSession(user)
47
38
 
48
- const res = await updateSelf(user)
39
+ user.password = "newPassword"
40
+ const res = await api.self.updateSelf(user)
49
41
 
50
42
  delete user.password
51
43
  expect(res.body._id).toBe(user._id)
@@ -55,4 +47,4 @@ describe("/api/global/self", () => {
55
47
  expect(events.user.passwordUpdated).toBeCalledWith(user)
56
48
  })
57
49
  })
58
- })
50
+ })