wabe 0.6.9 → 0.6.10

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 (158) hide show
  1. package/README.md +138 -32
  2. package/bucket/b.txt +1 -0
  3. package/dev/index.ts +215 -0
  4. package/dist/authentication/Session.d.ts +4 -1
  5. package/dist/authentication/interface.d.ts +16 -0
  6. package/dist/email/interface.d.ts +1 -1
  7. package/dist/graphql/resolvers.d.ts +4 -2
  8. package/dist/hooks/index.d.ts +1 -0
  9. package/dist/index.d.ts +0 -1
  10. package/dist/index.js +8713 -8867
  11. package/dist/server/index.d.ts +4 -2
  12. package/dist/utils/crypto.d.ts +7 -0
  13. package/dist/utils/helper.d.ts +4 -1
  14. package/generated/schema.graphql +16 -14
  15. package/generated/wabe.ts +4 -4
  16. package/package.json +15 -15
  17. package/src/authentication/OTP.test.ts +69 -0
  18. package/src/authentication/OTP.ts +66 -0
  19. package/src/authentication/Session.test.ts +665 -0
  20. package/src/authentication/Session.ts +529 -0
  21. package/src/authentication/defaultAuthentication.ts +214 -0
  22. package/src/authentication/index.ts +3 -0
  23. package/src/authentication/interface.ts +157 -0
  24. package/src/authentication/oauth/GitHub.test.ts +105 -0
  25. package/src/authentication/oauth/GitHub.ts +133 -0
  26. package/src/authentication/oauth/Google.test.ts +105 -0
  27. package/src/authentication/oauth/Google.ts +110 -0
  28. package/src/authentication/oauth/Oauth2Client.test.ts +225 -0
  29. package/src/authentication/oauth/Oauth2Client.ts +140 -0
  30. package/src/authentication/oauth/index.ts +2 -0
  31. package/src/authentication/oauth/utils.test.ts +35 -0
  32. package/src/authentication/oauth/utils.ts +28 -0
  33. package/src/authentication/providers/EmailOTP.test.ts +138 -0
  34. package/src/authentication/providers/EmailOTP.ts +93 -0
  35. package/src/authentication/providers/EmailPassword.test.ts +187 -0
  36. package/src/authentication/providers/EmailPassword.ts +130 -0
  37. package/src/authentication/providers/EmailPasswordSRP.test.ts +206 -0
  38. package/src/authentication/providers/EmailPasswordSRP.ts +184 -0
  39. package/src/authentication/providers/GitHub.ts +30 -0
  40. package/src/authentication/providers/Google.ts +30 -0
  41. package/src/authentication/providers/OAuth.test.ts +185 -0
  42. package/src/authentication/providers/OAuth.ts +112 -0
  43. package/src/authentication/providers/PhonePassword.test.ts +187 -0
  44. package/src/authentication/providers/PhonePassword.ts +129 -0
  45. package/src/authentication/providers/QRCodeOTP.test.ts +79 -0
  46. package/src/authentication/providers/QRCodeOTP.ts +65 -0
  47. package/src/authentication/providers/index.ts +6 -0
  48. package/src/authentication/resolvers/refreshResolver.test.ts +37 -0
  49. package/src/authentication/resolvers/refreshResolver.ts +20 -0
  50. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  51. package/src/authentication/resolvers/signInWithResolver.test.ts +307 -0
  52. package/src/authentication/resolvers/signInWithResolver.ts +102 -0
  53. package/src/authentication/resolvers/signOutResolver.test.ts +41 -0
  54. package/src/authentication/resolvers/signOutResolver.ts +22 -0
  55. package/src/authentication/resolvers/signUpWithResolver.test.ts +186 -0
  56. package/src/authentication/resolvers/signUpWithResolver.ts +69 -0
  57. package/src/authentication/resolvers/verifyChallenge.test.ts +136 -0
  58. package/src/authentication/resolvers/verifyChallenge.ts +69 -0
  59. package/src/authentication/roles.test.ts +59 -0
  60. package/src/authentication/roles.ts +40 -0
  61. package/src/authentication/utils.test.ts +99 -0
  62. package/src/authentication/utils.ts +43 -0
  63. package/src/cache/InMemoryCache.test.ts +62 -0
  64. package/src/cache/InMemoryCache.ts +45 -0
  65. package/src/cron/index.test.ts +17 -0
  66. package/src/cron/index.ts +46 -0
  67. package/src/database/DatabaseController.test.ts +625 -0
  68. package/src/database/DatabaseController.ts +983 -0
  69. package/src/database/index.test.ts +1230 -0
  70. package/src/database/index.ts +9 -0
  71. package/src/database/interface.ts +312 -0
  72. package/src/email/DevAdapter.ts +8 -0
  73. package/src/email/EmailController.test.ts +29 -0
  74. package/src/email/EmailController.ts +13 -0
  75. package/src/email/index.ts +2 -0
  76. package/src/email/interface.ts +36 -0
  77. package/src/email/templates/sendOtpCode.ts +120 -0
  78. package/src/file/FileController.ts +28 -0
  79. package/src/file/FileDevAdapter.ts +54 -0
  80. package/src/file/hookDeleteFile.ts +27 -0
  81. package/src/file/hookReadFile.ts +70 -0
  82. package/src/file/hookUploadFile.ts +53 -0
  83. package/src/file/index.test.ts +979 -0
  84. package/src/file/index.ts +2 -0
  85. package/src/file/interface.ts +42 -0
  86. package/src/graphql/GraphQLSchema.test.ts +4399 -0
  87. package/src/graphql/GraphQLSchema.ts +928 -0
  88. package/src/graphql/index.ts +2 -0
  89. package/src/graphql/parseGraphqlSchema.ts +94 -0
  90. package/src/graphql/parser.test.ts +217 -0
  91. package/src/graphql/parser.ts +566 -0
  92. package/src/graphql/pointerAndRelationFunction.ts +200 -0
  93. package/src/graphql/resolvers.ts +467 -0
  94. package/src/graphql/tests/aggregation.test.ts +1123 -0
  95. package/src/graphql/tests/e2e.test.ts +596 -0
  96. package/src/graphql/tests/scalars.test.ts +250 -0
  97. package/src/graphql/types.ts +219 -0
  98. package/src/hooks/HookObject.test.ts +122 -0
  99. package/src/hooks/HookObject.ts +168 -0
  100. package/src/hooks/authentication.ts +76 -0
  101. package/src/hooks/createUser.test.ts +77 -0
  102. package/src/hooks/createUser.ts +10 -0
  103. package/src/hooks/defaultFields.test.ts +187 -0
  104. package/src/hooks/defaultFields.ts +40 -0
  105. package/src/hooks/deleteSession.test.ts +181 -0
  106. package/src/hooks/deleteSession.ts +20 -0
  107. package/src/hooks/hashFieldHook.test.ts +163 -0
  108. package/src/hooks/hashFieldHook.ts +97 -0
  109. package/src/hooks/index.test.ts +207 -0
  110. package/src/hooks/index.ts +430 -0
  111. package/src/hooks/permissions.test.ts +424 -0
  112. package/src/hooks/permissions.ts +113 -0
  113. package/src/hooks/protected.test.ts +551 -0
  114. package/src/hooks/protected.ts +72 -0
  115. package/src/hooks/searchableFields.test.ts +166 -0
  116. package/src/hooks/searchableFields.ts +98 -0
  117. package/src/hooks/session.test.ts +138 -0
  118. package/src/hooks/session.ts +78 -0
  119. package/src/hooks/setEmail.test.ts +216 -0
  120. package/src/hooks/setEmail.ts +35 -0
  121. package/src/hooks/setupAcl.test.ts +589 -0
  122. package/src/hooks/setupAcl.ts +29 -0
  123. package/src/index.ts +9 -0
  124. package/src/schema/Schema.test.ts +484 -0
  125. package/src/schema/Schema.ts +795 -0
  126. package/src/schema/defaultResolvers.ts +94 -0
  127. package/src/schema/index.ts +1 -0
  128. package/src/schema/resolvers/meResolver.test.ts +62 -0
  129. package/src/schema/resolvers/meResolver.ts +14 -0
  130. package/src/schema/resolvers/newFile.ts +0 -0
  131. package/src/schema/resolvers/resetPassword.test.ts +345 -0
  132. package/src/schema/resolvers/resetPassword.ts +64 -0
  133. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  134. package/src/schema/resolvers/sendEmail.ts +21 -0
  135. package/src/schema/resolvers/sendOtpCode.test.ts +153 -0
  136. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  137. package/src/security.test.ts +3461 -0
  138. package/src/server/defaultSessionHandler.test.ts +66 -0
  139. package/src/server/defaultSessionHandler.ts +115 -0
  140. package/src/server/generateCodegen.ts +476 -0
  141. package/src/server/index.test.ts +552 -0
  142. package/src/server/index.ts +354 -0
  143. package/src/server/interface.ts +11 -0
  144. package/src/server/routes/authHandler.ts +187 -0
  145. package/src/server/routes/index.ts +40 -0
  146. package/src/utils/crypto.test.ts +41 -0
  147. package/src/utils/crypto.ts +121 -0
  148. package/src/utils/export.ts +13 -0
  149. package/src/utils/helper.ts +195 -0
  150. package/src/utils/index.test.ts +11 -0
  151. package/src/utils/index.ts +201 -0
  152. package/src/utils/preload.ts +8 -0
  153. package/src/utils/testHelper.ts +117 -0
  154. package/tsconfig.json +32 -0
  155. package/bunfig.toml +0 -4
  156. package/dist/ai/index.d.ts +0 -1
  157. package/dist/ai/interface.d.ts +0 -9
  158. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,94 @@
1
+ import type { MutationResolver, QueryResolver } from './Schema'
2
+ import { meResolver } from './resolvers/meResolver'
3
+ import { sendEmailResolver } from './resolvers/sendEmail'
4
+ import { resetPasswordResolver } from './resolvers/resetPassword'
5
+ import { sendOtpCodeResolver } from './resolvers/sendOtpCode'
6
+
7
+ export const defaultQueries: {
8
+ [key: string]: QueryResolver<any>
9
+ } = {
10
+ me: {
11
+ type: 'Object',
12
+ outputObject: {
13
+ name: 'MeOutput',
14
+ fields: {
15
+ user: {
16
+ type: 'Pointer',
17
+ class: 'User',
18
+ },
19
+ },
20
+ },
21
+ resolve: meResolver,
22
+ },
23
+ }
24
+
25
+ export const defaultMutations: {
26
+ [key: string]: MutationResolver<any>
27
+ } = {
28
+ resetPassword: {
29
+ type: 'Boolean',
30
+ description: 'Mutation to reset the password of the user',
31
+ args: {
32
+ input: {
33
+ password: {
34
+ type: 'String',
35
+ required: true,
36
+ },
37
+ email: {
38
+ type: 'Email',
39
+ },
40
+ phone: {
41
+ type: 'String',
42
+ },
43
+ otp: {
44
+ type: 'String',
45
+ required: true,
46
+ },
47
+ },
48
+ },
49
+ resolve: resetPasswordResolver,
50
+ },
51
+ sendOtpCode: {
52
+ type: 'Boolean',
53
+ description: 'Send an OTP code by email to the user',
54
+ args: {
55
+ input: {
56
+ email: {
57
+ type: 'Email',
58
+ required: true,
59
+ },
60
+ },
61
+ },
62
+ resolve: sendOtpCodeResolver,
63
+ },
64
+ sendEmail: {
65
+ type: 'String',
66
+ description:
67
+ 'Send basic email with text and html, returns the id of the email',
68
+ args: {
69
+ input: {
70
+ from: {
71
+ type: 'String',
72
+ required: true,
73
+ },
74
+ to: {
75
+ type: 'Array',
76
+ typeValue: 'String',
77
+ required: true,
78
+ requiredValue: true,
79
+ },
80
+ subject: {
81
+ type: 'String',
82
+ required: true,
83
+ },
84
+ text: {
85
+ type: 'String',
86
+ },
87
+ html: {
88
+ type: 'String',
89
+ },
90
+ },
91
+ },
92
+ resolve: sendEmailResolver,
93
+ },
94
+ }
@@ -0,0 +1 @@
1
+ export * from './Schema'
@@ -0,0 +1,62 @@
1
+ import { describe, beforeAll, afterAll, it, expect } from 'bun:test'
2
+ import type { Wabe } from '../../server'
3
+ import { getAdminUserClient, type DevWabeTypes } from '../../utils/helper'
4
+ import { setupTests, closeTests } from '../../utils/testHelper'
5
+ import { gql } from 'graphql-request'
6
+
7
+ describe('me', () => {
8
+ let wabe: Wabe<DevWabeTypes>
9
+
10
+ beforeAll(async () => {
11
+ const setup = await setupTests()
12
+ wabe = setup.wabe
13
+ })
14
+
15
+ afterAll(async () => {
16
+ await closeTests(wabe)
17
+ })
18
+
19
+ it('should return information about current user', async () => {
20
+ const adminClient = await getAdminUserClient(wabe.config.port, wabe, {
21
+ email: 'admin@wabe.dev',
22
+ password: 'admin',
23
+ })
24
+
25
+ const {
26
+ me: { user },
27
+ } = await adminClient.request<any>(graphql.me)
28
+
29
+ expect(user.role.name).toBe('Admin')
30
+
31
+ expect(user.authentication.emailPassword.email).toBe('admin@wabe.dev')
32
+ })
33
+ })
34
+
35
+ const graphql = {
36
+ signUpWith: gql`
37
+ mutation signUpWith($input: SignUpWithInput!) {
38
+ signUpWith(input: $input){
39
+ id
40
+ accessToken
41
+ refreshToken
42
+ }
43
+ }
44
+ `,
45
+ me: gql`
46
+ query me {
47
+ me {
48
+ user{
49
+ id
50
+ authentication{
51
+ emailPassword{
52
+ email
53
+ }
54
+ }
55
+ role {
56
+ name
57
+ }
58
+ }
59
+ }
60
+ }
61
+ `,
62
+ }
@@ -0,0 +1,14 @@
1
+ import type { WabeContext } from '../../server/interface'
2
+ import type { DevWabeTypes } from '../../utils/helper'
3
+
4
+ export const meResolver = (
5
+ _: any,
6
+ __: any,
7
+ context: WabeContext<DevWabeTypes>,
8
+ ) => {
9
+ if (!context.user?.id) return { user: undefined }
10
+
11
+ return {
12
+ user: context.user,
13
+ }
14
+ }
File without changes
@@ -0,0 +1,345 @@
1
+ import { describe, it, afterAll, beforeAll, expect, beforeEach } from 'bun:test'
2
+ import { gql, type GraphQLClient } from 'graphql-request'
3
+ import {
4
+ type DevWabeTypes,
5
+ getAnonymousClient,
6
+ getGraphqlClient,
7
+ } from '../../utils/helper'
8
+ import { setupTests, closeTests } from '../../utils/testHelper'
9
+ import type { Wabe } from '../../server'
10
+ import { OTP } from 'src'
11
+
12
+ describe('resetPasswordResolver', () => {
13
+ let wabe: Wabe<DevWabeTypes>
14
+ let port: number
15
+ let client: GraphQLClient
16
+
17
+ beforeAll(async () => {
18
+ const setup = await setupTests()
19
+ wabe = setup.wabe
20
+ port = setup.port
21
+ client = getGraphqlClient(port)
22
+ })
23
+
24
+ afterAll(async () => {
25
+ await closeTests(wabe)
26
+ })
27
+
28
+ beforeEach(async () => {
29
+ await wabe.controllers.database.clearDatabase()
30
+ })
31
+
32
+ it('should let an anonymous reset the password of an user', async () => {
33
+ process.env.NODE_ENV = 'production'
34
+
35
+ const anonymousClient = getAnonymousClient(port)
36
+
37
+ await anonymousClient.request<any>(graphql.createUser, {
38
+ input: {
39
+ fields: {
40
+ authentication: {
41
+ emailPassword: {
42
+ email: 'toto@toto.fr',
43
+ password: 'totototo',
44
+ },
45
+ },
46
+ },
47
+ },
48
+ })
49
+
50
+ const {
51
+ users: { edges },
52
+ } = await getGraphqlClient(port).request<any>(gql`
53
+ query users {
54
+ users (where: {email: {equalTo: "toto@toto.fr"}}) {
55
+ edges {
56
+ node {
57
+ id
58
+ }
59
+ }
60
+ }
61
+ }
62
+ `)
63
+
64
+ const userId = edges[0].node.id
65
+
66
+ const otp = new OTP(wabe.config.rootKey)
67
+
68
+ await anonymousClient.request<any>(graphql.resetPassword, {
69
+ input: {
70
+ email: 'toto@toto.fr',
71
+ password: 'tata',
72
+ otp: otp.generate(userId),
73
+ },
74
+ })
75
+
76
+ const res = await anonymousClient.request<any>(graphql.signInWith, {
77
+ input: {
78
+ authentication: {
79
+ emailPassword: {
80
+ email: 'toto@toto.fr',
81
+ password: 'tata',
82
+ },
83
+ },
84
+ },
85
+ })
86
+
87
+ expect(res.signInWith.user.id).toEqual(userId)
88
+
89
+ process.env.NODE_ENV = 'test'
90
+ })
91
+
92
+ it('should reset password of an user if the OTP code is valid', async () => {
93
+ process.env.NODE_ENV = 'production'
94
+
95
+ const {
96
+ createUser: { user },
97
+ } = await client.request<any>(graphql.createUserWithRoot, {
98
+ input: {
99
+ fields: {
100
+ authentication: {
101
+ emailPassword: {
102
+ email: 'toto@toto.fr',
103
+ password: 'totototo',
104
+ },
105
+ },
106
+ },
107
+ },
108
+ })
109
+
110
+ const userId = user.id
111
+
112
+ const otp = new OTP(wabe.config.rootKey)
113
+
114
+ await client.request<any>(graphql.resetPassword, {
115
+ input: {
116
+ email: 'toto@toto.fr',
117
+ password: 'tata',
118
+ otp: otp.generate(userId),
119
+ },
120
+ })
121
+
122
+ const res = await client.request<any>(graphql.signInWith, {
123
+ input: {
124
+ authentication: {
125
+ emailPassword: {
126
+ email: 'toto@toto.fr',
127
+ password: 'tata',
128
+ },
129
+ },
130
+ },
131
+ })
132
+
133
+ expect(res.signInWith.user.id).toEqual(userId)
134
+
135
+ process.env.NODE_ENV = 'test'
136
+ })
137
+
138
+ it('should reset password in dev mode with valid normal code', async () => {
139
+ process.env.NODE_ENV = 'test'
140
+
141
+ const {
142
+ createUser: { user },
143
+ } = await client.request<any>(graphql.createUserWithRoot, {
144
+ input: {
145
+ fields: {
146
+ authentication: {
147
+ emailPassword: {
148
+ email: 'toto@toto.fr',
149
+ password: 'totototo',
150
+ },
151
+ },
152
+ },
153
+ },
154
+ })
155
+
156
+ const userId = user.id
157
+
158
+ const otp = new OTP(wabe.config.rootKey)
159
+
160
+ await client.request<any>(graphql.resetPassword, {
161
+ input: {
162
+ email: 'toto@toto.fr',
163
+ password: 'tata',
164
+ otp: otp.generate(userId),
165
+ },
166
+ })
167
+
168
+ const res = await client.request<any>(graphql.signInWith, {
169
+ input: {
170
+ authentication: {
171
+ emailPassword: {
172
+ email: 'toto@toto.fr',
173
+ password: 'tata',
174
+ },
175
+ },
176
+ },
177
+ })
178
+
179
+ expect(res.signInWith.user.id).toEqual(userId)
180
+ })
181
+
182
+ it('should reset password in dev mode with code 000000', async () => {
183
+ process.env.NODE_ENV = 'test'
184
+
185
+ const {
186
+ createUser: { user },
187
+ } = await client.request<any>(graphql.createUserWithRoot, {
188
+ input: {
189
+ fields: {
190
+ authentication: {
191
+ emailPassword: {
192
+ email: 'toto2@toto.fr',
193
+ password: 'totototo',
194
+ },
195
+ },
196
+ },
197
+ },
198
+ })
199
+
200
+ const userId = user.id
201
+
202
+ await client.request<any>(graphql.resetPassword, {
203
+ input: {
204
+ email: 'toto2@toto.fr',
205
+ password: 'tata',
206
+ otp: '000000',
207
+ },
208
+ })
209
+
210
+ const res = await client.request<any>(graphql.signInWith, {
211
+ input: {
212
+ authentication: {
213
+ emailPassword: {
214
+ email: 'toto2@toto.fr',
215
+ password: 'tata',
216
+ },
217
+ },
218
+ },
219
+ })
220
+
221
+ expect(res.signInWith.user.id).toEqual(userId)
222
+ })
223
+
224
+ it("should return true if the user doesn't exist (hide sensitive data)", async () => {
225
+ process.env.NODE_ENV = 'test'
226
+
227
+ const res = await client.request<any>(graphql.resetPassword, {
228
+ input: {
229
+ email: 'invalidUser@toto.fr',
230
+ password: 'tata',
231
+ otp: '000000',
232
+ },
233
+ })
234
+
235
+ expect(res.resetPassword).toEqual(true)
236
+ })
237
+
238
+ it('should not reset password of an user if the OTP code is invalid', async () => {
239
+ process.env.NODE_ENV = 'production'
240
+
241
+ await client.request<any>(graphql.createUserWithRoot, {
242
+ input: {
243
+ fields: {
244
+ authentication: {
245
+ emailPassword: {
246
+ email: 'toto3@toto.fr',
247
+ password: 'totototo',
248
+ },
249
+ },
250
+ },
251
+ },
252
+ })
253
+
254
+ expect(
255
+ client.request<any>(graphql.resetPassword, {
256
+ input: {
257
+ email: 'toto3@toto.fr',
258
+ password: 'tata',
259
+ otp: 'invalidOtp',
260
+ },
261
+ }),
262
+ ).rejects.toThrow('Invalid OTP code')
263
+
264
+ process.env.NODE_ENV = 'test'
265
+ })
266
+
267
+ it('should reset password of another provider than emailPassword', async () => {
268
+ process.env.NODE_ENV = 'production'
269
+
270
+ const {
271
+ createUser: { user },
272
+ } = await client.request<any>(graphql.createUserWithRoot, {
273
+ input: {
274
+ fields: {
275
+ authentication: {
276
+ phonePassword: {
277
+ phone: '+33600000000',
278
+ password: 'totototo',
279
+ },
280
+ },
281
+ },
282
+ },
283
+ })
284
+
285
+ const userId = user.id
286
+
287
+ const otp = new OTP(wabe.config.rootKey)
288
+
289
+ await client.request<any>(graphql.resetPassword, {
290
+ input: {
291
+ phone: '+33600000000',
292
+ password: 'tata',
293
+ otp: otp.generate(userId),
294
+ },
295
+ })
296
+
297
+ const res = await client.request<any>(graphql.signInWith, {
298
+ input: {
299
+ authentication: {
300
+ phonePassword: {
301
+ phone: '+33600000000',
302
+ password: 'tata',
303
+ },
304
+ },
305
+ },
306
+ })
307
+
308
+ expect(res.signInWith.user.id).toEqual(userId)
309
+
310
+ process.env.NODE_ENV = 'test'
311
+ })
312
+ })
313
+
314
+ const graphql = {
315
+ signInWith: gql`
316
+ mutation signInWith($input: SignInWithInput!) {
317
+ signInWith(input: $input){
318
+ user {
319
+ id
320
+ }
321
+ }
322
+ }
323
+ `,
324
+ createUser: gql`
325
+ mutation createUser($input: CreateUserInput!) {
326
+ createUser(input: $input) {
327
+ ok
328
+ }
329
+ }
330
+ `,
331
+ createUserWithRoot: gql`
332
+ mutation createUser($input: CreateUserInput!) {
333
+ createUser(input: $input) {
334
+ user {
335
+ id
336
+ }
337
+ }
338
+ }
339
+ `,
340
+ resetPassword: gql`
341
+ mutation resetPassword($input: ResetPasswordInput!) {
342
+ resetPassword(input: $input)
343
+ }
344
+ `,
345
+ }
@@ -0,0 +1,64 @@
1
+ import type { MutationResetPasswordArgs } from '../../../generated/wabe'
2
+ import { OTP } from '../../authentication/OTP'
3
+ import type { WabeContext } from '../../server/interface'
4
+ import { contextWithRoot } from '../../utils/export'
5
+ import type { DevWabeTypes } from '../../utils/helper'
6
+
7
+ const DUMMY_USER_ID = '00000000-0000-0000-0000-000000000000'
8
+
9
+ export const resetPasswordResolver = async (
10
+ _: any,
11
+ { input: { email, phone, password, otp } }: MutationResetPasswordArgs,
12
+ context: WabeContext<DevWabeTypes>,
13
+ ) => {
14
+ if (!email && !phone) throw new Error('Email or phone is required')
15
+
16
+ const users = await context.wabe.controllers.database.getObjects({
17
+ className: 'User',
18
+ where: {
19
+ ...(email && { email: { equalTo: email } }),
20
+ ...(phone && {
21
+ authentication: { phonePassword: { phone: { equalTo: phone } } },
22
+ }),
23
+ },
24
+ select: { id: true, authentication: true },
25
+ first: 1,
26
+ context: contextWithRoot(context),
27
+ })
28
+
29
+ const realUser = users.length > 0 ? users[0] : null
30
+ const userId = realUser?.id ?? DUMMY_USER_ID
31
+
32
+ const otpClass = new OTP(context.wabe.config.rootKey)
33
+ const isOtpValid = otpClass.verify(otp, userId)
34
+
35
+ if (realUser) {
36
+ const inProd = process.env.NODE_ENV === 'production'
37
+ const devBypass = !inProd && otp === '000000'
38
+
39
+ if (!isOtpValid && !(devBypass && !inProd))
40
+ throw new Error('Invalid OTP code')
41
+
42
+ const providerKey = phone ? 'phonePassword' : 'emailPassword'
43
+
44
+ await context.wabe.controllers.database.updateObject({
45
+ className: 'User',
46
+ id: realUser.id,
47
+ data: {
48
+ authentication: {
49
+ [providerKey]: {
50
+ ...(phone && {
51
+ phone: realUser.authentication?.phonePassword?.phone,
52
+ }),
53
+ ...(email && { email }),
54
+ password,
55
+ },
56
+ },
57
+ },
58
+ select: {},
59
+ context: contextWithRoot(context),
60
+ })
61
+ }
62
+
63
+ return true
64
+ }
@@ -0,0 +1,118 @@
1
+ import { describe, expect, it, mock } from 'bun:test'
2
+ import { sendEmailResolver } from './sendEmail'
3
+ import type { WabeContext } from '../../server/interface'
4
+
5
+ describe('SendEmail', () => {
6
+ it('should throw an error if email adapter is not defined', () => {
7
+ expect(() =>
8
+ sendEmailResolver(
9
+ undefined,
10
+ {
11
+ input: { from: 'from', to: ['to'], subject: 'subject', text: 'text' },
12
+ },
13
+ {
14
+ isRoot: false,
15
+ user: {
16
+ id: 'id',
17
+ },
18
+ wabe: {
19
+ controllers: {
20
+ email: undefined,
21
+ } as any,
22
+ },
23
+ } as WabeContext<any>,
24
+ ),
25
+ ).toThrow('Email adapter not defined')
26
+ })
27
+
28
+ it('should send email when user is connected', async () => {
29
+ const mockSend = mock(() => {}).mockResolvedValueOnce(true as never)
30
+
31
+ const res = await sendEmailResolver(
32
+ undefined,
33
+ {
34
+ input: { from: 'from', to: ['to'], subject: 'subject', text: 'text' },
35
+ },
36
+ {
37
+ isRoot: false,
38
+ user: {
39
+ id: 'id',
40
+ },
41
+ wabe: {
42
+ controllers: {
43
+ email: {
44
+ send: mockSend,
45
+ },
46
+ } as any,
47
+ },
48
+ } as WabeContext<any>,
49
+ )
50
+
51
+ expect(res).toBeTrue()
52
+
53
+ expect(mockSend).toHaveBeenCalledTimes(1)
54
+ expect(mockSend).toHaveBeenCalledWith({
55
+ from: 'from',
56
+ to: ['to'],
57
+ subject: 'subject',
58
+ text: 'text',
59
+ })
60
+ })
61
+
62
+ it('should send email when user is root', async () => {
63
+ const mockSend = mock(() => {}).mockResolvedValueOnce(true as never)
64
+
65
+ const res = await sendEmailResolver(
66
+ undefined,
67
+ {
68
+ input: { from: 'from', to: ['to'], subject: 'subject', text: 'text' },
69
+ },
70
+ {
71
+ isRoot: true,
72
+ wabe: {
73
+ controllers: {
74
+ email: {
75
+ send: mockSend,
76
+ },
77
+ } as any,
78
+ },
79
+ } as WabeContext<any>,
80
+ )
81
+
82
+ expect(res).toBeTrue()
83
+
84
+ expect(mockSend).toHaveBeenCalledTimes(1)
85
+ expect(mockSend).toHaveBeenCalledWith({
86
+ from: 'from',
87
+ to: ['to'],
88
+ subject: 'subject',
89
+ text: 'text',
90
+ })
91
+ })
92
+
93
+ it('should not send email when user is not connected and is not root', () => {
94
+ const mockSend = mock(() => {}).mockResolvedValueOnce(true as never)
95
+
96
+ expect(() =>
97
+ sendEmailResolver(
98
+ undefined,
99
+ {
100
+ input: { from: 'from', to: ['to'], subject: 'subject', text: 'text' },
101
+ },
102
+ {
103
+ isRoot: false,
104
+ user: undefined,
105
+ wabe: {
106
+ controllers: {
107
+ email: {
108
+ send: mockSend,
109
+ },
110
+ } as any,
111
+ },
112
+ } as WabeContext<any>,
113
+ ),
114
+ ).toThrow('Permission denied')
115
+
116
+ expect(mockSend).toHaveBeenCalledTimes(0)
117
+ })
118
+ })