wabe 0.6.9 → 0.6.11

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 (162) hide show
  1. package/README.md +156 -50
  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/cron/index.d.ts +0 -1
  7. package/dist/database/DatabaseController.d.ts +41 -13
  8. package/dist/database/interface.d.ts +1 -0
  9. package/dist/email/DevAdapter.d.ts +0 -1
  10. package/dist/email/interface.d.ts +1 -1
  11. package/dist/graphql/resolvers.d.ts +4 -2
  12. package/dist/hooks/index.d.ts +8 -2
  13. package/dist/index.d.ts +0 -1
  14. package/dist/index.js +32144 -32058
  15. package/dist/schema/Schema.d.ts +2 -1
  16. package/dist/server/index.d.ts +4 -2
  17. package/dist/utils/crypto.d.ts +7 -0
  18. package/dist/utils/helper.d.ts +5 -1
  19. package/generated/schema.graphql +22 -14
  20. package/generated/wabe.ts +4 -4
  21. package/package.json +23 -23
  22. package/src/authentication/OTP.test.ts +69 -0
  23. package/src/authentication/OTP.ts +64 -0
  24. package/src/authentication/Session.test.ts +629 -0
  25. package/src/authentication/Session.ts +493 -0
  26. package/src/authentication/defaultAuthentication.ts +209 -0
  27. package/src/authentication/index.ts +3 -0
  28. package/src/authentication/interface.ts +155 -0
  29. package/src/authentication/oauth/GitHub.test.ts +91 -0
  30. package/src/authentication/oauth/GitHub.ts +121 -0
  31. package/src/authentication/oauth/Google.test.ts +91 -0
  32. package/src/authentication/oauth/Google.ts +101 -0
  33. package/src/authentication/oauth/Oauth2Client.test.ts +219 -0
  34. package/src/authentication/oauth/Oauth2Client.ts +135 -0
  35. package/src/authentication/oauth/index.ts +2 -0
  36. package/src/authentication/oauth/utils.test.ts +33 -0
  37. package/src/authentication/oauth/utils.ts +27 -0
  38. package/src/authentication/providers/EmailOTP.test.ts +127 -0
  39. package/src/authentication/providers/EmailOTP.ts +84 -0
  40. package/src/authentication/providers/EmailPassword.test.ts +176 -0
  41. package/src/authentication/providers/EmailPassword.ts +116 -0
  42. package/src/authentication/providers/EmailPasswordSRP.test.ts +208 -0
  43. package/src/authentication/providers/EmailPasswordSRP.ts +179 -0
  44. package/src/authentication/providers/GitHub.ts +24 -0
  45. package/src/authentication/providers/Google.ts +24 -0
  46. package/src/authentication/providers/OAuth.test.ts +185 -0
  47. package/src/authentication/providers/OAuth.ts +106 -0
  48. package/src/authentication/providers/PhonePassword.test.ts +176 -0
  49. package/src/authentication/providers/PhonePassword.ts +115 -0
  50. package/src/authentication/providers/QRCodeOTP.test.ts +77 -0
  51. package/src/authentication/providers/QRCodeOTP.ts +58 -0
  52. package/src/authentication/providers/index.ts +6 -0
  53. package/src/authentication/resolvers/refreshResolver.test.ts +30 -0
  54. package/src/authentication/resolvers/refreshResolver.ts +19 -0
  55. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +59 -0
  56. package/src/authentication/resolvers/signInWithResolver.test.ts +293 -0
  57. package/src/authentication/resolvers/signInWithResolver.ts +92 -0
  58. package/src/authentication/resolvers/signOutResolver.test.ts +38 -0
  59. package/src/authentication/resolvers/signOutResolver.ts +18 -0
  60. package/src/authentication/resolvers/signUpWithResolver.test.ts +180 -0
  61. package/src/authentication/resolvers/signUpWithResolver.ts +65 -0
  62. package/src/authentication/resolvers/verifyChallenge.test.ts +133 -0
  63. package/src/authentication/resolvers/verifyChallenge.ts +62 -0
  64. package/src/authentication/roles.test.ts +49 -0
  65. package/src/authentication/roles.ts +40 -0
  66. package/src/authentication/utils.test.ts +97 -0
  67. package/src/authentication/utils.ts +39 -0
  68. package/src/cache/InMemoryCache.test.ts +62 -0
  69. package/src/cache/InMemoryCache.ts +45 -0
  70. package/src/cron/index.test.ts +17 -0
  71. package/src/cron/index.ts +43 -0
  72. package/src/database/DatabaseController.test.ts +613 -0
  73. package/src/database/DatabaseController.ts +1007 -0
  74. package/src/database/index.test.ts +1372 -0
  75. package/src/database/index.ts +9 -0
  76. package/src/database/interface.ts +302 -0
  77. package/src/email/DevAdapter.ts +7 -0
  78. package/src/email/EmailController.test.ts +29 -0
  79. package/src/email/EmailController.ts +13 -0
  80. package/src/email/index.ts +2 -0
  81. package/src/email/interface.ts +36 -0
  82. package/src/email/templates/sendOtpCode.ts +120 -0
  83. package/src/file/FileController.ts +28 -0
  84. package/src/file/FileDevAdapter.ts +51 -0
  85. package/src/file/hookDeleteFile.ts +25 -0
  86. package/src/file/hookReadFile.ts +66 -0
  87. package/src/file/hookUploadFile.ts +50 -0
  88. package/src/file/index.test.ts +932 -0
  89. package/src/file/index.ts +2 -0
  90. package/src/file/interface.ts +39 -0
  91. package/src/graphql/GraphQLSchema.test.ts +4408 -0
  92. package/src/graphql/GraphQLSchema.ts +880 -0
  93. package/src/graphql/index.ts +2 -0
  94. package/src/graphql/parseGraphqlSchema.ts +85 -0
  95. package/src/graphql/parser.test.ts +203 -0
  96. package/src/graphql/parser.ts +542 -0
  97. package/src/graphql/pointerAndRelationFunction.ts +191 -0
  98. package/src/graphql/resolvers.ts +442 -0
  99. package/src/graphql/tests/aggregation.test.ts +1115 -0
  100. package/src/graphql/tests/e2e.test.ts +590 -0
  101. package/src/graphql/tests/scalars.test.ts +250 -0
  102. package/src/graphql/types.ts +227 -0
  103. package/src/hooks/HookObject.test.ts +122 -0
  104. package/src/hooks/HookObject.ts +165 -0
  105. package/src/hooks/authentication.ts +67 -0
  106. package/src/hooks/createUser.test.ts +77 -0
  107. package/src/hooks/createUser.ts +10 -0
  108. package/src/hooks/defaultFields.test.ts +176 -0
  109. package/src/hooks/defaultFields.ts +32 -0
  110. package/src/hooks/deleteSession.test.ts +181 -0
  111. package/src/hooks/deleteSession.ts +20 -0
  112. package/src/hooks/hashFieldHook.test.ts +152 -0
  113. package/src/hooks/hashFieldHook.ts +89 -0
  114. package/src/hooks/index.test.ts +258 -0
  115. package/src/hooks/index.ts +414 -0
  116. package/src/hooks/permissions.test.ts +412 -0
  117. package/src/hooks/permissions.ts +93 -0
  118. package/src/hooks/protected.test.ts +551 -0
  119. package/src/hooks/protected.ts +60 -0
  120. package/src/hooks/searchableFields.test.ts +147 -0
  121. package/src/hooks/searchableFields.ts +86 -0
  122. package/src/hooks/session.test.ts +134 -0
  123. package/src/hooks/session.ts +76 -0
  124. package/src/hooks/setEmail.test.ts +216 -0
  125. package/src/hooks/setEmail.ts +33 -0
  126. package/src/hooks/setupAcl.test.ts +618 -0
  127. package/src/hooks/setupAcl.ts +25 -0
  128. package/src/index.ts +9 -0
  129. package/src/schema/Schema.test.ts +482 -0
  130. package/src/schema/Schema.ts +757 -0
  131. package/src/schema/defaultResolvers.ts +93 -0
  132. package/src/schema/index.ts +1 -0
  133. package/src/schema/resolvers/meResolver.test.ts +62 -0
  134. package/src/schema/resolvers/meResolver.ts +10 -0
  135. package/src/schema/resolvers/resetPassword.test.ts +341 -0
  136. package/src/schema/resolvers/resetPassword.ts +63 -0
  137. package/src/schema/resolvers/sendEmail.test.ts +118 -0
  138. package/src/schema/resolvers/sendEmail.ts +21 -0
  139. package/src/schema/resolvers/sendOtpCode.test.ts +141 -0
  140. package/src/schema/resolvers/sendOtpCode.ts +52 -0
  141. package/src/security.test.ts +3434 -0
  142. package/src/server/defaultSessionHandler.test.ts +62 -0
  143. package/src/server/defaultSessionHandler.ts +105 -0
  144. package/src/server/generateCodegen.ts +433 -0
  145. package/src/server/index.test.ts +532 -0
  146. package/src/server/index.ts +334 -0
  147. package/src/server/interface.ts +11 -0
  148. package/src/server/routes/authHandler.ts +169 -0
  149. package/src/server/routes/index.ts +39 -0
  150. package/src/utils/crypto.test.ts +41 -0
  151. package/src/utils/crypto.ts +105 -0
  152. package/src/utils/export.ts +11 -0
  153. package/src/utils/helper.ts +204 -0
  154. package/src/utils/index.test.ts +11 -0
  155. package/src/utils/index.ts +189 -0
  156. package/src/utils/preload.ts +8 -0
  157. package/src/utils/testHelper.ts +116 -0
  158. package/tsconfig.json +32 -0
  159. package/bunfig.toml +0 -4
  160. package/dist/ai/index.d.ts +0 -1
  161. package/dist/ai/interface.d.ts +0 -9
  162. /package/dist/server/{defaultHandlers.d.ts → defaultSessionHandler.d.ts} +0 -0
@@ -0,0 +1,147 @@
1
+ import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
2
+ import {
3
+ defaultSearchableFieldsBeforeCreate,
4
+ defaultSearchableFieldsBeforeUpdate,
5
+ stringExtraction,
6
+ } from './searchableFields'
7
+ import { HookObject } from './HookObject'
8
+ import type { DevWabeTypes } from '../utils/helper'
9
+ import { setupTests, closeTests } from '../utils/testHelper'
10
+ import { OperationType } from '.'
11
+ import type { Wabe } from '../server'
12
+
13
+ describe('searchablesFields', () => {
14
+ let wabe: Wabe<DevWabeTypes>
15
+
16
+ beforeAll(async () => {
17
+ const setup = await setupTests()
18
+ wabe = setup.wabe
19
+ })
20
+
21
+ afterAll(async () => {
22
+ await closeTests(wabe)
23
+ })
24
+
25
+ it('should extract searchable fields from email (searchableFields hook should be after emails hook)', async () => {
26
+ await wabe.controllers.database.createObject({
27
+ className: 'User',
28
+ context: {
29
+ isRoot: true,
30
+ wabe,
31
+ },
32
+ data: {
33
+ authentication: {
34
+ emailPassword: {
35
+ email: 'admin@wabe.dev',
36
+ password: 'admin',
37
+ },
38
+ },
39
+ },
40
+ select: {},
41
+ })
42
+
43
+ const res = await wabe.controllers.database.getObjects({
44
+ className: 'User',
45
+ select: { search: true },
46
+ context: {
47
+ isRoot: true,
48
+ wabe,
49
+ },
50
+ })
51
+
52
+ expect(res[0]?.search).toEqual([
53
+ 'a',
54
+ 'ad',
55
+ 'adm',
56
+ 'admi',
57
+ 'admin',
58
+ 'w',
59
+ 'wa',
60
+ 'wab',
61
+ 'wabe',
62
+ 'd',
63
+ 'de',
64
+ 'dev',
65
+ ])
66
+ })
67
+
68
+ it('should extract string correctly', () => {
69
+ expect(stringExtraction('test')).toEqual(['t', 'te', 'tes', 'test'])
70
+
71
+ expect(
72
+ stringExtraction({
73
+ test: 0,
74
+ tata: 'ceci',
75
+ }),
76
+ ).toEqual(['0', 'c', 'ce', 'cec', 'ceci'])
77
+
78
+ expect(stringExtraction('es pace')).toEqual(['e', 'es', 'p', 'pa', 'pac', 'pace'])
79
+ })
80
+
81
+ it('should complete the search fields of each object on insert', () => {
82
+ const hookObject = new HookObject<DevWabeTypes, 'User'>({
83
+ className: 'User',
84
+ operationType: OperationType.BeforeCreate,
85
+ newData: {
86
+ email: 'email@test.fr',
87
+ name: 'Test',
88
+ age: 20,
89
+ } as any,
90
+ context: {
91
+ wabe: {
92
+ config: {
93
+ schema: {
94
+ classes: [
95
+ {
96
+ name: 'User',
97
+ searchableFields: ['name', 'age'],
98
+ },
99
+ ],
100
+ },
101
+ },
102
+ },
103
+ } as any,
104
+ object: {} as any,
105
+ select: {},
106
+ })
107
+
108
+ defaultSearchableFieldsBeforeCreate(hookObject)
109
+
110
+ expect(hookObject.getNewData().search).toEqual(['t', 'te', 'tes', 'test', '20'])
111
+ })
112
+
113
+ it('should complete the search fields of each object on update', () => {
114
+ const hookObject = new HookObject<DevWabeTypes, 'User'>({
115
+ className: 'User',
116
+ operationType: OperationType.BeforeUpdate,
117
+ newData: {
118
+ email: 'email@test.fr',
119
+ name: 'tata',
120
+ age: 20,
121
+ } as any,
122
+ context: {
123
+ wabe: {
124
+ config: {
125
+ schema: {
126
+ classes: [
127
+ {
128
+ name: 'User',
129
+ searchableFields: ['name', 'age'],
130
+ },
131
+ ],
132
+ },
133
+ },
134
+ },
135
+ } as any,
136
+ object: {
137
+ name: 'test',
138
+ search: ['t', 'te', 'tes', 'test'],
139
+ } as any,
140
+ select: {},
141
+ })
142
+
143
+ defaultSearchableFieldsBeforeUpdate(hookObject)
144
+
145
+ expect(hookObject.getNewData().search).toEqual(['t', 'ta', 'tat', 'tata', '20'])
146
+ })
147
+ })
@@ -0,0 +1,86 @@
1
+ import { tokenize } from '../utils'
2
+ import { notEmpty } from '../utils/export'
3
+ import type { HookObject } from './HookObject'
4
+
5
+ export const stringExtraction = (value: any): Array<string> => {
6
+ if (value === undefined || value === null) return []
7
+
8
+ if (typeof value === 'object') return Object.values(value).flatMap((v) => stringExtraction(v))
9
+
10
+ if (typeof value === 'string')
11
+ return tokenize(value)
12
+ .trim()
13
+ .split(' ')
14
+ .map((word: string) => word.trim())
15
+ .flatMap((word: string) => {
16
+ const arrayOfString = []
17
+
18
+ for (let i = 1; i <= word.length; i += 1)
19
+ arrayOfString.push(word.substring(0, i).toLowerCase())
20
+
21
+ return arrayOfString
22
+ })
23
+
24
+ if (typeof value === 'number') return [String(value)]
25
+
26
+ return []
27
+ }
28
+
29
+ export const defaultSearchableFieldsBeforeCreate = (object: HookObject<any, any>) => {
30
+ const searchablesFields = object.context.wabe.config.schema?.classes?.find(
31
+ (currentClass) => currentClass.name === object.className,
32
+ )?.searchableFields
33
+
34
+ if (!searchablesFields || searchablesFields.length === 0) return
35
+
36
+ const newData = object.getNewData()
37
+
38
+ const extractedSearchField = Object.entries(newData)
39
+ .flatMap(([key, value]) => {
40
+ if (!searchablesFields.includes(key)) return undefined
41
+
42
+ return stringExtraction(value)
43
+ })
44
+ .filter(notEmpty)
45
+
46
+ object.upsertNewData('search', extractedSearchField)
47
+ }
48
+
49
+ export const defaultSearchableFieldsBeforeUpdate = (object: HookObject<any, any>) => {
50
+ const searchablesFields = object.context.wabe.config.schema?.classes?.find(
51
+ (currentClass) => currentClass.name === object.className,
52
+ )?.searchableFields
53
+
54
+ if (!searchablesFields || searchablesFields.length === 0) return
55
+
56
+ const newData = object.getNewData()
57
+
58
+ const newExtractedSearchField = Object.entries(newData)
59
+ .flatMap(([key, value]) => {
60
+ if (!searchablesFields.includes(key)) return undefined
61
+
62
+ return stringExtraction(value)
63
+ })
64
+ .filter(notEmpty)
65
+
66
+ const oldExtractedSearcFieldForUpdateFields = Object.entries(object.object || {})
67
+ .flatMap(([key, value]) => {
68
+ // If the data is not a searchable field or don't change
69
+ if (!searchablesFields.includes(key) || !Object.keys(newData).includes(key)) return undefined
70
+
71
+ return stringExtraction(value)
72
+ })
73
+ .filter(notEmpty)
74
+
75
+ const actualSearch = (object.object?.search || []) as string[]
76
+
77
+ // Actual search fields minus old search data for same field + new extracted data for the field
78
+ const extractedSearchFields = [
79
+ ...actualSearch.filter(
80
+ (element: string) => !oldExtractedSearcFieldForUpdateFields.includes(element),
81
+ ),
82
+ ...newExtractedSearchField,
83
+ ]
84
+
85
+ object.upsertNewData('search', extractedSearchFields)
86
+ }
@@ -0,0 +1,134 @@
1
+ import { describe, beforeAll, afterAll, expect, it, beforeEach } from 'bun:test'
2
+ import { gql } from 'graphql-request'
3
+ import type { Wabe } from '../server'
4
+ import { type DevWabeTypes, getAnonymousClient, getUserClient } from '../utils/helper'
5
+ import { setupTests, closeTests } from '../utils/testHelper'
6
+
7
+ describe('hooks/session', () => {
8
+ let wabe: Wabe<DevWabeTypes>
9
+ let port: number
10
+
11
+ beforeAll(async () => {
12
+ const setup = await setupTests()
13
+ wabe = setup.wabe
14
+ port = setup.port
15
+ })
16
+
17
+ beforeEach(async () => {
18
+ await wabe.controllers.database.clearDatabase()
19
+ })
20
+
21
+ afterAll(async () => {
22
+ await closeTests(wabe)
23
+ })
24
+
25
+ it('should link a new session to user relation after creation', async () => {
26
+ const anonymousClient = getAnonymousClient(port)
27
+
28
+ await anonymousClient.request<any>(graphql.signUpWith, {
29
+ input: {
30
+ authentication: {
31
+ emailPassword: {
32
+ email: 'email@test.fr',
33
+ password: 'password',
34
+ },
35
+ },
36
+ },
37
+ })
38
+
39
+ const res = await wabe.controllers.database.getObjects({
40
+ className: 'User',
41
+ context: {
42
+ wabe,
43
+ isRoot: true,
44
+ },
45
+ select: {
46
+ id: true,
47
+ sessions: true,
48
+ },
49
+ })
50
+
51
+ expect(res[0]?.sessions?.length).toEqual(1)
52
+ })
53
+
54
+ it('should unlink a session from user relation after deletion', async () => {
55
+ const anonymousClient = getAnonymousClient(port)
56
+
57
+ const res = await anonymousClient.request<any>(graphql.signUpWith, {
58
+ input: {
59
+ authentication: {
60
+ emailPassword: {
61
+ email: 'email2@test.fr',
62
+ password: 'password',
63
+ },
64
+ },
65
+ },
66
+ })
67
+
68
+ const userClient = getUserClient(port, {
69
+ accessToken: res.signUpWith.accessToken,
70
+ })
71
+
72
+ await userClient.request<any>(graphql.signInWith, {
73
+ input: {
74
+ authentication: {
75
+ emailPassword: {
76
+ email: 'email2@test.fr',
77
+ password: 'password',
78
+ },
79
+ },
80
+ },
81
+ })
82
+
83
+ const res2 = await wabe.controllers.database.getObjects({
84
+ className: 'User',
85
+ context: {
86
+ wabe,
87
+ isRoot: true,
88
+ },
89
+ select: {
90
+ id: true,
91
+ sessions: true,
92
+ },
93
+ })
94
+
95
+ await wabe.controllers.database.deleteObjects({
96
+ className: '_Session',
97
+ where: {
98
+ id: { equalTo: res2[0]?.sessions?.[0]?.id },
99
+ },
100
+ context: {
101
+ wabe,
102
+ isRoot: true,
103
+ },
104
+ select: {},
105
+ })
106
+
107
+ const res3 = await wabe.controllers.database.getObjects({
108
+ className: 'User',
109
+ context: {
110
+ wabe,
111
+ isRoot: true,
112
+ },
113
+ })
114
+
115
+ expect(res3[0]?.sessions?.length).toEqual(1)
116
+ })
117
+ })
118
+
119
+ const graphql = {
120
+ signUpWith: gql`
121
+ mutation signUpWith($input: SignUpWithInput!) {
122
+ signUpWith(input: $input) {
123
+ accessToken
124
+ }
125
+ }
126
+ `,
127
+ signInWith: gql`
128
+ mutation signInWith($input: SignInWithInput!) {
129
+ signInWith(input: $input) {
130
+ accessToken
131
+ }
132
+ }
133
+ `,
134
+ }
@@ -0,0 +1,76 @@
1
+ import type { DevWabeTypes } from '../utils/helper'
2
+ import { notEmpty } from '../utils/export'
3
+ import type { HookObject } from './HookObject'
4
+
5
+ export const defaultAfterCreateSession = async (
6
+ hookObject: HookObject<DevWabeTypes, '_Session'>,
7
+ ) => {
8
+ const object = hookObject.object
9
+ // @ts-expect-error
10
+ const userId = object?.user as string
11
+
12
+ if (!userId) return
13
+
14
+ const databaseController = hookObject.context.wabe.controllers.database
15
+
16
+ const user = await databaseController.getObject({
17
+ className: 'User',
18
+ id: userId,
19
+ select: {
20
+ sessions: true,
21
+ },
22
+ context: hookObject.context,
23
+ })
24
+
25
+ const sessionsId = user?.sessions?.map((session) => session.id) || []
26
+
27
+ await databaseController.updateObject({
28
+ className: 'User',
29
+ id: userId,
30
+ context: hookObject.context,
31
+ data: {
32
+ sessions: [...sessionsId, object?.id].filter(notEmpty),
33
+ },
34
+ })
35
+ }
36
+
37
+ export const defaultAfterDeleteSession = async (
38
+ hookObject: HookObject<DevWabeTypes, '_Session'>,
39
+ ) => {
40
+ const object = hookObject.object
41
+ // @ts-expect-error
42
+ const userId = object?.user as string
43
+
44
+ if (!userId) return
45
+
46
+ const databaseController = hookObject.context.wabe.controllers.database
47
+
48
+ const user = await databaseController.getObject({
49
+ className: 'User',
50
+ id: userId,
51
+ select: {
52
+ sessions: true,
53
+ },
54
+ context: hookObject.context,
55
+ })
56
+
57
+ const newSessionsId = user?.sessions
58
+ ?.filter((session) => session.id !== object?.id)
59
+ .map((session) => session.id)
60
+
61
+ await databaseController.updateObject({
62
+ className: 'User',
63
+ id: userId,
64
+ context: hookObject.context,
65
+ data: {
66
+ sessions: newSessionsId,
67
+ },
68
+ })
69
+ }
70
+
71
+ export const defaultBeforeUpdateSessionOnUser = (hookObject: HookObject<DevWabeTypes, 'User'>) => {
72
+ if (hookObject.context.isRoot) return
73
+
74
+ if (hookObject.isFieldUpdated('sessions'))
75
+ throw new Error('Not authorized to update user sessions')
76
+ }
@@ -0,0 +1,216 @@
1
+ import { describe, beforeAll, afterAll, expect, it, afterEach } from 'bun:test'
2
+ import { getGraphqlClient, type DevWabeTypes } from '../utils/helper'
3
+ import { setupTests, closeTests } from '../utils/testHelper'
4
+ import type { Wabe } from '../server'
5
+ import { gql, type GraphQLClient } from 'graphql-request'
6
+
7
+ describe('setEmail', () => {
8
+ let wabe: Wabe<DevWabeTypes>
9
+ let port: number
10
+ let client: GraphQLClient
11
+
12
+ beforeAll(async () => {
13
+ const setup = await setupTests()
14
+ wabe = setup.wabe
15
+ port = setup.port
16
+ client = getGraphqlClient(port)
17
+ })
18
+
19
+ afterAll(async () => {
20
+ await closeTests(wabe)
21
+ })
22
+
23
+ afterEach(async () => {
24
+ await client.request<any>(graphql.deleteUsers)
25
+ })
26
+
27
+ it("should set email if it doesn't exist", async () => {
28
+ const {
29
+ signUpWith: { id },
30
+ } = await client.request<any>(graphql.signUpWith, {
31
+ input: {
32
+ authentication: {
33
+ emailPassword: {
34
+ email: 'email@test.fr',
35
+ password: 'password',
36
+ },
37
+ },
38
+ },
39
+ })
40
+
41
+ const { user } = await client.request<any>(graphql.user, {
42
+ id,
43
+ })
44
+
45
+ expect(user.email).toEqual('email@test.fr')
46
+ expect(user.provider).toEqual('emailPassword')
47
+ })
48
+
49
+ it('should not set email if it is provided during the creation', async () => {
50
+ const {
51
+ createUser: { user },
52
+ } = await client.request<any>(graphql.createUser, {
53
+ input: {
54
+ fields: {
55
+ name: 'John',
56
+ age: 23,
57
+ email: 'email@test.fr',
58
+ },
59
+ },
60
+ })
61
+
62
+ expect(user.email).toEqual('email@test.fr')
63
+ expect(user.provider).toEqual(null)
64
+ })
65
+
66
+ it('should update email if it change during update', async () => {
67
+ const {
68
+ createUser: { user },
69
+ } = await client.request<any>(graphql.createUser, {
70
+ input: {
71
+ fields: {
72
+ name: 'John',
73
+ age: 23,
74
+ email: 'email@test.fr',
75
+ },
76
+ },
77
+ })
78
+
79
+ const userId = user.id
80
+
81
+ const {
82
+ updateUser: { user: updatedUser },
83
+ } = await client.request<any>(graphql.updateUser, {
84
+ input: {
85
+ id: userId,
86
+ fields: {
87
+ authentication: {
88
+ emailPassword: {
89
+ email: 'updated@test.fr',
90
+ },
91
+ },
92
+ },
93
+ },
94
+ })
95
+
96
+ expect(updatedUser.email).toEqual('updated@test.fr')
97
+ expect(updatedUser.provider).toEqual('emailPassword')
98
+ })
99
+
100
+ it('should not update email if email is provided', async () => {
101
+ const {
102
+ createUser: { user },
103
+ } = await client.request<any>(graphql.createUser, {
104
+ input: {
105
+ fields: {
106
+ name: 'John',
107
+ age: 23,
108
+ email: 'email@test.fr',
109
+ },
110
+ },
111
+ })
112
+
113
+ const userId = user.id
114
+
115
+ const {
116
+ updateUser: { user: updatedUser },
117
+ } = await client.request<any>(graphql.updateUser, {
118
+ input: {
119
+ id: userId,
120
+ fields: {
121
+ authentication: {
122
+ emailPassword: {
123
+ email: 'updated@test.fr',
124
+ },
125
+ },
126
+ email: 'updated2@test.fr',
127
+ },
128
+ },
129
+ })
130
+
131
+ expect(updatedUser.email).toEqual('updated2@test.fr')
132
+ expect(updatedUser.provider).toEqual(null)
133
+ })
134
+
135
+ it('should not update email if email is provided but not authentication field', async () => {
136
+ const {
137
+ createUser: { user },
138
+ } = await client.request<any>(graphql.createUser, {
139
+ input: {
140
+ fields: {
141
+ name: 'John',
142
+ age: 23,
143
+ email: 'email@test.fr',
144
+ },
145
+ },
146
+ })
147
+
148
+ const userId = user.id
149
+
150
+ const {
151
+ updateUser: { user: updatedUser },
152
+ } = await client.request<any>(graphql.updateUser, {
153
+ input: {
154
+ id: userId,
155
+ fields: {
156
+ email: 'updated2@test.fr',
157
+ },
158
+ },
159
+ })
160
+
161
+ expect(updatedUser.email).toEqual('updated2@test.fr')
162
+ expect(updatedUser.provider).toEqual(null)
163
+ })
164
+ })
165
+
166
+ const graphql = {
167
+ signUpWith: gql`
168
+ mutation signUpWith($input: SignUpWithInput!) {
169
+ signUpWith(input: $input) {
170
+ id
171
+ }
172
+ }
173
+ `,
174
+ user: gql`
175
+ query user($id: ID!) {
176
+ user(id: $id) {
177
+ id
178
+ email
179
+ provider
180
+ }
181
+ }
182
+ `,
183
+ createUser: gql`
184
+ mutation createUser($input: CreateUserInput!) {
185
+ createUser(input: $input) {
186
+ user {
187
+ id
188
+ email
189
+ provider
190
+ }
191
+ }
192
+ }
193
+ `,
194
+ updateUser: gql`
195
+ mutation updateUser($input: UpdateUserInput!) {
196
+ updateUser(input: $input) {
197
+ user {
198
+ id
199
+ email
200
+ provider
201
+ }
202
+ }
203
+ }
204
+ `,
205
+ deleteUsers: gql`
206
+ mutation deleteUsers {
207
+ deleteUsers(input: {}) {
208
+ edges {
209
+ node {
210
+ id
211
+ }
212
+ }
213
+ }
214
+ }
215
+ `,
216
+ }
@@ -0,0 +1,33 @@
1
+ import type { DevWabeTypes } from '../utils/helper'
2
+ import type { HookObject } from './HookObject'
3
+
4
+ const updateEmail = (object: HookObject<DevWabeTypes, 'User'>) => {
5
+ const authentication = object.getNewData().authentication
6
+
7
+ if (!authentication) return
8
+
9
+ // Considering that we only have one authentication provider (for double auth maybe need an adjustment)
10
+ const provider = Object.keys(authentication)[0] || ''
11
+
12
+ const emailToSave = authentication[provider].email
13
+
14
+ if (!emailToSave) return
15
+
16
+ object.upsertNewData('email', emailToSave)
17
+
18
+ if (provider) object.upsertNewData('provider', provider)
19
+ }
20
+
21
+ // This hook works for official authentication provider that store email in "email" field
22
+ // Maybe need custom hook for custom authentication provider
23
+ export const defaultSetEmail = (object: HookObject<DevWabeTypes, 'User'>) => {
24
+ if (object.isFieldUpdated('email')) return
25
+
26
+ updateEmail(object)
27
+ }
28
+
29
+ export const defaultSetEmailOnUpdate = (object: HookObject<DevWabeTypes, 'User'>) => {
30
+ if (object.isFieldUpdated('email')) return
31
+
32
+ updateEmail(object)
33
+ }