wabe 0.6.12 → 0.6.13

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 (147) hide show
  1. package/package.json +4 -1
  2. package/dev/index.ts +0 -215
  3. package/generated/schema.graphql +0 -1945
  4. package/generated/wabe.ts +0 -448
  5. package/src/authentication/OTP.test.ts +0 -69
  6. package/src/authentication/OTP.ts +0 -64
  7. package/src/authentication/Session.test.ts +0 -629
  8. package/src/authentication/Session.ts +0 -517
  9. package/src/authentication/cookies.ts +0 -10
  10. package/src/authentication/defaultAuthentication.ts +0 -209
  11. package/src/authentication/index.ts +0 -4
  12. package/src/authentication/interface.ts +0 -177
  13. package/src/authentication/oauth/GitHub.test.ts +0 -91
  14. package/src/authentication/oauth/GitHub.ts +0 -121
  15. package/src/authentication/oauth/Google.test.ts +0 -91
  16. package/src/authentication/oauth/Google.ts +0 -101
  17. package/src/authentication/oauth/Oauth2Client.test.ts +0 -219
  18. package/src/authentication/oauth/Oauth2Client.ts +0 -135
  19. package/src/authentication/oauth/index.ts +0 -2
  20. package/src/authentication/oauth/utils.test.ts +0 -33
  21. package/src/authentication/oauth/utils.ts +0 -27
  22. package/src/authentication/providers/EmailOTP.test.ts +0 -127
  23. package/src/authentication/providers/EmailOTP.ts +0 -95
  24. package/src/authentication/providers/EmailPassword.test.ts +0 -263
  25. package/src/authentication/providers/EmailPassword.ts +0 -138
  26. package/src/authentication/providers/EmailPasswordSRP.test.ts +0 -208
  27. package/src/authentication/providers/EmailPasswordSRP.ts +0 -191
  28. package/src/authentication/providers/GitHub.ts +0 -24
  29. package/src/authentication/providers/Google.ts +0 -24
  30. package/src/authentication/providers/OAuth.test.ts +0 -185
  31. package/src/authentication/providers/OAuth.ts +0 -106
  32. package/src/authentication/providers/PhonePassword.test.ts +0 -221
  33. package/src/authentication/providers/PhonePassword.ts +0 -136
  34. package/src/authentication/providers/QRCodeOTP.test.ts +0 -77
  35. package/src/authentication/providers/QRCodeOTP.ts +0 -69
  36. package/src/authentication/providers/index.ts +0 -6
  37. package/src/authentication/resolvers/refreshResolver.test.ts +0 -30
  38. package/src/authentication/resolvers/refreshResolver.ts +0 -19
  39. package/src/authentication/resolvers/signInWithResolver.inte.test.ts +0 -59
  40. package/src/authentication/resolvers/signInWithResolver.test.ts +0 -306
  41. package/src/authentication/resolvers/signInWithResolver.ts +0 -106
  42. package/src/authentication/resolvers/signOutResolver.test.ts +0 -38
  43. package/src/authentication/resolvers/signOutResolver.ts +0 -18
  44. package/src/authentication/resolvers/signUpWithResolver.test.ts +0 -180
  45. package/src/authentication/resolvers/signUpWithResolver.ts +0 -68
  46. package/src/authentication/resolvers/verifyChallenge.test.ts +0 -230
  47. package/src/authentication/resolvers/verifyChallenge.ts +0 -78
  48. package/src/authentication/roles.test.ts +0 -49
  49. package/src/authentication/roles.ts +0 -40
  50. package/src/authentication/security.ts +0 -278
  51. package/src/authentication/utils.test.ts +0 -97
  52. package/src/authentication/utils.ts +0 -39
  53. package/src/cache/InMemoryCache.test.ts +0 -62
  54. package/src/cache/InMemoryCache.ts +0 -45
  55. package/src/cron/index.test.ts +0 -17
  56. package/src/cron/index.ts +0 -43
  57. package/src/database/DatabaseController.test.ts +0 -613
  58. package/src/database/DatabaseController.ts +0 -1415
  59. package/src/database/index.test.ts +0 -1551
  60. package/src/database/index.ts +0 -9
  61. package/src/database/interface.ts +0 -308
  62. package/src/email/DevAdapter.ts +0 -7
  63. package/src/email/EmailController.test.ts +0 -29
  64. package/src/email/EmailController.ts +0 -13
  65. package/src/email/index.ts +0 -2
  66. package/src/email/interface.ts +0 -36
  67. package/src/email/templates/sendOtpCode.ts +0 -120
  68. package/src/file/FileController.ts +0 -28
  69. package/src/file/FileDevAdapter.ts +0 -51
  70. package/src/file/hookDeleteFile.ts +0 -25
  71. package/src/file/hookReadFile.ts +0 -66
  72. package/src/file/hookUploadFile.ts +0 -52
  73. package/src/file/index.test.ts +0 -1031
  74. package/src/file/index.ts +0 -2
  75. package/src/file/interface.ts +0 -63
  76. package/src/file/security.ts +0 -156
  77. package/src/graphql/GraphQLSchema.test.ts +0 -5099
  78. package/src/graphql/GraphQLSchema.ts +0 -886
  79. package/src/graphql/index.ts +0 -2
  80. package/src/graphql/parseGraphqlSchema.ts +0 -85
  81. package/src/graphql/parser.test.ts +0 -203
  82. package/src/graphql/parser.ts +0 -707
  83. package/src/graphql/pointerAndRelationFunction.ts +0 -191
  84. package/src/graphql/resolvers.ts +0 -464
  85. package/src/graphql/tests/aggregation.test.ts +0 -1115
  86. package/src/graphql/tests/e2e.test.ts +0 -590
  87. package/src/graphql/tests/scalars.test.ts +0 -250
  88. package/src/graphql/types.ts +0 -227
  89. package/src/hooks/HookObject.test.ts +0 -122
  90. package/src/hooks/HookObject.ts +0 -165
  91. package/src/hooks/authentication.ts +0 -67
  92. package/src/hooks/createUser.test.ts +0 -77
  93. package/src/hooks/createUser.ts +0 -10
  94. package/src/hooks/defaultFields.test.ts +0 -176
  95. package/src/hooks/defaultFields.ts +0 -32
  96. package/src/hooks/deleteSession.test.ts +0 -181
  97. package/src/hooks/deleteSession.ts +0 -20
  98. package/src/hooks/hashFieldHook.test.ts +0 -152
  99. package/src/hooks/hashFieldHook.ts +0 -89
  100. package/src/hooks/index.test.ts +0 -258
  101. package/src/hooks/index.ts +0 -420
  102. package/src/hooks/permissions.test.ts +0 -412
  103. package/src/hooks/permissions.ts +0 -93
  104. package/src/hooks/protected.test.ts +0 -551
  105. package/src/hooks/protected.ts +0 -74
  106. package/src/hooks/searchableFields.test.ts +0 -147
  107. package/src/hooks/searchableFields.ts +0 -86
  108. package/src/hooks/session.test.ts +0 -134
  109. package/src/hooks/session.ts +0 -76
  110. package/src/hooks/setEmail.test.ts +0 -216
  111. package/src/hooks/setEmail.ts +0 -33
  112. package/src/hooks/setupAcl.test.ts +0 -618
  113. package/src/hooks/setupAcl.ts +0 -25
  114. package/src/hooks/virtualFields.test.ts +0 -228
  115. package/src/hooks/virtualFields.ts +0 -48
  116. package/src/index.ts +0 -9
  117. package/src/schema/Schema.test.ts +0 -482
  118. package/src/schema/Schema.ts +0 -839
  119. package/src/schema/defaultResolvers.ts +0 -93
  120. package/src/schema/index.ts +0 -1
  121. package/src/schema/resolvers/meResolver.test.ts +0 -62
  122. package/src/schema/resolvers/meResolver.ts +0 -10
  123. package/src/schema/resolvers/resetPassword.test.ts +0 -341
  124. package/src/schema/resolvers/resetPassword.ts +0 -63
  125. package/src/schema/resolvers/sendEmail.test.ts +0 -118
  126. package/src/schema/resolvers/sendEmail.ts +0 -21
  127. package/src/schema/resolvers/sendOtpCode.test.ts +0 -141
  128. package/src/schema/resolvers/sendOtpCode.ts +0 -52
  129. package/src/security.test.ts +0 -4136
  130. package/src/server/defaultSessionHandler.test.ts +0 -62
  131. package/src/server/defaultSessionHandler.ts +0 -104
  132. package/src/server/generateCodegen.ts +0 -433
  133. package/src/server/index.test.ts +0 -843
  134. package/src/server/index.ts +0 -336
  135. package/src/server/interface.ts +0 -11
  136. package/src/server/routes/authHandler.ts +0 -171
  137. package/src/server/routes/index.ts +0 -48
  138. package/src/utils/crypto.test.ts +0 -41
  139. package/src/utils/crypto.ts +0 -105
  140. package/src/utils/database.ts +0 -8
  141. package/src/utils/export.ts +0 -12
  142. package/src/utils/helper.ts +0 -204
  143. package/src/utils/index.test.ts +0 -11
  144. package/src/utils/index.ts +0 -196
  145. package/src/utils/preload.ts +0 -8
  146. package/src/utils/testHelper.ts +0 -124
  147. package/tsconfig.json +0 -32
@@ -1,1031 +0,0 @@
1
- import { afterAll, afterEach, beforeAll, describe, expect, it, mock, spyOn } from 'bun:test'
2
- import { FileDevAdapter, type Wabe } from '..'
3
- import { type DevWabeTypes, getAnonymousClient } from '../utils/helper'
4
- import { setupTests, closeTests } from '../utils/testHelper'
5
- import { gql } from 'graphql-request'
6
-
7
- describe('File upload', () => {
8
- let wabe: Wabe<DevWabeTypes>
9
- let port: number
10
-
11
- const spyFileDevAdapterUploadFile = spyOn(FileDevAdapter.prototype, 'uploadFile')
12
- const spyFileDevAdapterReadFile = spyOn(FileDevAdapter.prototype, 'readFile')
13
-
14
- const mockBeforeUpload = mock()
15
-
16
- beforeAll(async () => {
17
- const setup = await setupTests([
18
- {
19
- name: 'Test3',
20
- fields: {
21
- file: { type: 'File' },
22
- },
23
- permissions: {
24
- read: {
25
- requireAuthentication: false,
26
- },
27
- create: {
28
- requireAuthentication: false,
29
- },
30
- update: {
31
- requireAuthentication: false,
32
- },
33
- delete: {
34
- requireAuthentication: false,
35
- },
36
- },
37
- },
38
- ])
39
- wabe = setup.wabe
40
- port = setup.port
41
-
42
- spyFileDevAdapterReadFile.mockClear()
43
- spyFileDevAdapterUploadFile.mockClear()
44
-
45
- const fileConfig = wabe.config.file
46
-
47
- if (fileConfig) fileConfig.beforeUpload = mockBeforeUpload
48
- })
49
-
50
- afterAll(async () => {
51
- await closeTests(wabe)
52
- })
53
-
54
- afterEach(async () => {
55
- spyFileDevAdapterUploadFile.mockClear()
56
- spyFileDevAdapterReadFile.mockClear()
57
- mockBeforeUpload.mockClear()
58
-
59
- await wabe.controllers.database.deleteObjects({
60
- // @ts-expect-error
61
- className: 'Test3',
62
- context: {
63
- isRoot: true,
64
- wabe,
65
- },
66
- where: {},
67
- select: {},
68
- })
69
- })
70
-
71
- it('should call beforeUpload if specified in the file config', async () => {
72
- await wabe.controllers.database.createObject({
73
- // @ts-expect-error
74
- className: 'Test3',
75
- context: {
76
- isRoot: true,
77
- wabe,
78
- },
79
- data: {
80
- // @ts-expect-error
81
- file: {
82
- file: new File(['a'], 'a', { type: 'text/plain' }),
83
- },
84
- },
85
- select: {},
86
- })
87
-
88
- expect(mockBeforeUpload).toHaveBeenCalledTimes(1)
89
- const fileArg = mockBeforeUpload.mock.calls[0]?.[0]
90
- expect(fileArg?.name).toEqual('a')
91
- expect(await fileArg?.text()).toEqual('a')
92
-
93
- // should return the same file if no file is returned by beforeUpload
94
- expect(spyFileDevAdapterUploadFile).toHaveBeenCalledTimes(1)
95
- const fileArg2 = spyFileDevAdapterUploadFile.mock.calls[0]?.[0]
96
- expect(fileArg2?.name).toEqual('a')
97
- expect(await fileArg2?.text()).toEqual('a')
98
- })
99
-
100
- it('should call beforeUpload and return the file returned by beforeUpload', async () => {
101
- mockBeforeUpload.mockImplementationOnce(() => new File(['b'], 'b.txt', { type: 'text/plain' }))
102
-
103
- await wabe.controllers.database.createObject({
104
- // @ts-expect-error
105
- className: 'Test3',
106
- context: {
107
- isRoot: true,
108
- wabe,
109
- },
110
- data: {
111
- // @ts-expect-error
112
- file: {
113
- file: new File(['a'], 'a', { type: 'text/plain' }),
114
- },
115
- },
116
- select: {},
117
- })
118
-
119
- expect(mockBeforeUpload).toHaveBeenCalledTimes(1)
120
- const fileArg = mockBeforeUpload.mock.calls[0]?.[0]
121
- expect(fileArg?.name).toEqual('a')
122
- expect(await fileArg?.text()).toEqual('a')
123
-
124
- // should return the same file if no file is returned by beforeUpload
125
- expect(spyFileDevAdapterUploadFile).toHaveBeenCalledTimes(1)
126
- const fileArg2 = spyFileDevAdapterUploadFile.mock.calls[0]?.[0]
127
- expect(fileArg2?.name).toEqual('b.txt')
128
- expect(await fileArg2?.text()).toEqual('b')
129
- })
130
-
131
- it('should not crash when there is no extension for the uploaded file', async () => {
132
- await wabe.controllers.database.createObject({
133
- // @ts-expect-error
134
- className: 'Test3',
135
- context: {
136
- isRoot: true,
137
- wabe,
138
- },
139
- data: {
140
- // @ts-expect-error
141
- file: {
142
- file: new File(['a'], 'a', { type: 'text/plain' }),
143
- },
144
- },
145
- select: {},
146
- })
147
-
148
- const result = await wabe.controllers.database.getObjects({
149
- // @ts-expect-error
150
- className: 'Test3',
151
- context: {
152
- isRoot: true,
153
- wabe,
154
- },
155
- where: {},
156
- // @ts-expect-error
157
- select: { file: true, id: true },
158
- })
159
-
160
- // @ts-expect-error
161
- expect(result[0].file.name).toEqual('a')
162
- // @ts-expect-error
163
- expect(result[0].file.url).toEqual(`http://127.0.0.1:${port}/bucket/a`)
164
- })
165
-
166
- it('should throw an error if no file adapter is provided', async () => {
167
- const previousFileController = wabe.controllers.file
168
- // @ts-expect-error
169
- wabe.controllers.file = null
170
-
171
- const formData = new FormData()
172
-
173
- formData.append(
174
- 'operations',
175
- JSON.stringify({
176
- query:
177
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file {name, isPresignedUrl}}}}',
178
- variables: { file: null },
179
- }),
180
- )
181
-
182
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
183
-
184
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
185
-
186
- const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
187
- method: 'POST',
188
- body: formData,
189
- })
190
-
191
- expect(await res.text()).toContain('No file adapter found')
192
-
193
- wabe.controllers.file = previousFileController
194
- })
195
-
196
- it("should upload a file with the database controller's method", async () => {
197
- await wabe.controllers.database.createObject({
198
- // @ts-expect-error
199
- className: 'Test3',
200
- context: {
201
- isRoot: true,
202
- wabe,
203
- },
204
- data: {
205
- // @ts-expect-error
206
- file: {
207
- file: new File(['a'], 'a.text', { type: 'text/plain' }),
208
- },
209
- },
210
- select: {},
211
- })
212
-
213
- const result = await wabe.controllers.database.getObjects({
214
- // @ts-expect-error
215
- className: 'Test3',
216
- context: {
217
- isRoot: true,
218
- wabe,
219
- },
220
- where: {},
221
- // @ts-expect-error
222
- select: { file: true, id: true },
223
- })
224
-
225
- // @ts-expect-error
226
- expect(result[0].file.name).toEqual('a.text')
227
- // @ts-expect-error
228
- expect(result[0].file.url).toEqual(`http://127.0.0.1:${port}/bucket/a.text`)
229
-
230
- const res = await wabe.controllers.database.updateObject({
231
- // @ts-expect-error
232
- className: 'Test3',
233
- context: {
234
- isRoot: true,
235
- wabe,
236
- },
237
- where: {},
238
- // @ts-expect-error
239
- select: { file: true, id: true },
240
- data: {
241
- // @ts-expect-error
242
- file: {
243
- url: 'https://palixir.github.io/wabe//assets/logo.png',
244
- },
245
- },
246
- id: result?.[0]?.id || '',
247
- })
248
-
249
- // @ts-expect-error
250
- expect(res.file.url).toEqual('https://palixir.github.io/wabe//assets/logo.png')
251
- // @ts-expect-error
252
- expect(res.file.isPresignedUrl).toEqual(false)
253
- })
254
-
255
- it('should upload multiple objects with the same file', async () => {
256
- const formData = new FormData()
257
-
258
- formData.append(
259
- 'operations',
260
- JSON.stringify({
261
- query: gql`
262
- mutation ($file: File!, $file2: File!) {
263
- createTest3s(input: { fields: [{ file: { file: $file } }, { file: { file: $file2 } }] }) {
264
- edges {
265
- node {
266
- id
267
- file {
268
- name
269
- }
270
- }
271
- }
272
- }
273
- }
274
- `,
275
- variables: { file: null },
276
- }),
277
- )
278
-
279
- formData.append('map', JSON.stringify({ 0: ['variables.file'], 1: ['variables.file2'] }))
280
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
281
- formData.append('1', new File(['b'], 'b.text', { type: 'text/plain' }))
282
-
283
- const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
284
- method: 'POST',
285
- body: formData,
286
- })
287
-
288
- const jsonRes = await res.json()
289
-
290
- // Return the url in dev adapter it's the file name
291
- expect(jsonRes.data.createTest3s.edges[0].node.file.name).toEqual('a.text')
292
- expect(jsonRes.data.createTest3s.edges[1].node.file.name).toEqual('b.text')
293
-
294
- expect(spyFileDevAdapterUploadFile).toHaveBeenCalledTimes(2)
295
- const fileArg = spyFileDevAdapterUploadFile.mock.calls[0]?.[0]
296
- expect(fileArg?.name).toEqual('a.text')
297
- expect(await fileArg?.text()).toEqual('a')
298
-
299
- const fileArg2 = spyFileDevAdapterUploadFile.mock.calls[1]?.[0]
300
- expect(fileArg2?.name).toEqual('b.text')
301
- expect(await fileArg2?.text()).toEqual('b')
302
- })
303
-
304
- it('should upload a file on request on type File on create request', async () => {
305
- const formData = new FormData()
306
-
307
- formData.append(
308
- 'operations',
309
- JSON.stringify({
310
- query:
311
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file {name, isPresignedUrl}}}}',
312
- variables: { file: null },
313
- }),
314
- )
315
-
316
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
317
-
318
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
319
-
320
- const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
321
- method: 'POST',
322
- body: formData,
323
- })
324
-
325
- const jsonRes = await res.json()
326
-
327
- // Return the url in dev adapter it's the file name
328
- expect(jsonRes.data.createTest3.test3.file.name).toEqual('a.text')
329
- expect(jsonRes.data.createTest3.test3.file.isPresignedUrl).toEqual(true)
330
-
331
- expect(spyFileDevAdapterUploadFile).toHaveBeenCalledTimes(1)
332
- const fileArg = spyFileDevAdapterUploadFile.mock.calls[0]?.[0]
333
- expect(fileArg?.name).toEqual('a.text')
334
- expect(await fileArg?.text()).toEqual('a')
335
- })
336
-
337
- it('should upload a file on request on type File on update request', async () => {
338
- const formData = new FormData()
339
-
340
- formData.append(
341
- 'operations',
342
- JSON.stringify({
343
- query:
344
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name }}}}',
345
- variables: { file: null },
346
- }),
347
- )
348
-
349
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
350
-
351
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
352
-
353
- const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
354
- method: 'POST',
355
- body: formData,
356
- })
357
-
358
- const jsonRes = await res.json()
359
-
360
- const idOfCreatedObject = jsonRes.data.createTest3.test3.id
361
-
362
- const formData2 = new FormData()
363
-
364
- formData2.append(
365
- 'operations',
366
- JSON.stringify({
367
- query: `mutation ($file: File!) {updateTest3(input: {id: "${idOfCreatedObject}",fields: {file: {file:$file}}}){test3{id, file { name }}}}`,
368
- variables: { file: null },
369
- }),
370
- )
371
-
372
- formData2.append('map', JSON.stringify({ 0: ['variables.file'] }))
373
-
374
- formData2.append('0', new File(['b'], 'b.text', { type: 'text/plain' }))
375
-
376
- const updatedRes = await fetch(`http://127.0.0.1:${port}/graphql`, {
377
- method: 'POST',
378
- body: formData2,
379
- })
380
-
381
- const jsonUpdatedRes = await updatedRes.json()
382
-
383
- // Return the url in dev adapter it's the file name
384
- expect(jsonUpdatedRes.data.updateTest3.test3.file.name).toEqual('b.text')
385
-
386
- // 2 for create and update
387
- expect(spyFileDevAdapterUploadFile).toHaveBeenCalledTimes(2)
388
- const fileArg = spyFileDevAdapterUploadFile.mock.calls[1]?.[0]
389
- expect(fileArg?.name).toEqual('b.text')
390
- expect(await fileArg?.text()).toEqual('b')
391
- })
392
-
393
- it('should return the url of the file on after read request', async () => {
394
- const formData = new FormData()
395
-
396
- formData.append(
397
- 'operations',
398
- JSON.stringify({
399
- query:
400
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
401
- variables: { file: null },
402
- }),
403
- )
404
-
405
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
406
-
407
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
408
-
409
- await fetch(`http://127.0.0.1:${port}/graphql`, {
410
- method: 'POST',
411
- body: formData,
412
- })
413
-
414
- const anonymousClient = getAnonymousClient(port)
415
-
416
- const { test3s } = await anonymousClient.request<any>(gql`
417
- query {
418
- test3s {
419
- edges {
420
- node {
421
- id
422
- file {
423
- name
424
- url
425
- urlGeneratedAt
426
- }
427
- }
428
- }
429
- }
430
- }
431
- `)
432
-
433
- expect(test3s.edges[0].node.file.name).toEqual('a.text')
434
- expect(test3s.edges[0].node.file.url).toEqual(`http://127.0.0.1:${port}/bucket/a.text`)
435
- expect(new Date(test3s.edges[0].node.file.urlGeneratedAt)).toBeDate()
436
- })
437
-
438
- it('should return the url of the file on after read request for multiple objects', async () => {
439
- const upload = async (fileName: string, content: string) => {
440
- const formData = new FormData()
441
-
442
- formData.append(
443
- 'operations',
444
- JSON.stringify({
445
- query:
446
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
447
- variables: { file: null },
448
- }),
449
- )
450
-
451
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
452
-
453
- formData.append('0', new File([content], fileName, { type: 'text/plain' }))
454
-
455
- await fetch(`http://127.0.0.1:${port}/graphql`, {
456
- method: 'POST',
457
- body: formData,
458
- })
459
- }
460
-
461
- await upload('a.text', 'a')
462
- await upload('b.text', 'b')
463
-
464
- const anonymousClient = getAnonymousClient(port)
465
-
466
- const { test3s } = await anonymousClient.request<any>(gql`
467
- query {
468
- test3s {
469
- edges {
470
- node {
471
- id
472
- file {
473
- name
474
- url
475
- urlGeneratedAt
476
- }
477
- }
478
- }
479
- }
480
- }
481
- `)
482
-
483
- const files = test3s.edges.map((edge: any) => edge.node.file)
484
-
485
- expect(files).toHaveLength(2)
486
- expect(files.map((f: any) => f.name)).toEqual(expect.arrayContaining(['a.text', 'b.text']))
487
-
488
- files.forEach((file: any) => {
489
- expect(file.url).toEqual(`http://127.0.0.1:${port}/bucket/${file.name}`)
490
- expect(new Date(file.urlGeneratedAt)).toBeDate()
491
- })
492
- })
493
-
494
- it('should not read the file again in the bucket if the cache is not expired', async () => {
495
- const formData = new FormData()
496
-
497
- formData.append(
498
- 'operations',
499
- JSON.stringify({
500
- query:
501
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
502
- variables: { file: null },
503
- }),
504
- )
505
-
506
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
507
-
508
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
509
-
510
- await fetch(`http://127.0.0.1:${port}/graphql`, {
511
- method: 'POST',
512
- body: formData,
513
- })
514
-
515
- const anonymousClient = getAnonymousClient(port)
516
-
517
- const { test3s } = await anonymousClient.request<any>(gql`
518
- query {
519
- test3s {
520
- edges {
521
- node {
522
- id
523
- file {
524
- name
525
- url
526
- urlGeneratedAt
527
- }
528
- }
529
- }
530
- }
531
- }
532
- `)
533
-
534
- expect(test3s.edges[0].node.file.name).toEqual('a.text')
535
- expect(test3s.edges[0].node.file.url).toEqual(`http://127.0.0.1:${port}/bucket/a.text`)
536
- expect(new Date(test3s.edges[0].node.file.urlGeneratedAt)).toBeDate()
537
-
538
- expect(spyFileDevAdapterReadFile).toHaveBeenCalledTimes(1)
539
-
540
- await anonymousClient.request<any>(gql`
541
- query {
542
- test3s {
543
- edges {
544
- node {
545
- id
546
- file {
547
- name
548
- url
549
- urlGeneratedAt
550
- }
551
- }
552
- }
553
- }
554
- }
555
- `)
556
-
557
- // Again once because the cache is not expired
558
- expect(spyFileDevAdapterReadFile).toHaveBeenCalledTimes(1)
559
- })
560
-
561
- it('should reset the cache if the file is updated', async () => {
562
- const formData = new FormData()
563
-
564
- formData.append(
565
- 'operations',
566
- JSON.stringify({
567
- query:
568
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
569
- variables: { file: null },
570
- }),
571
- )
572
-
573
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
574
-
575
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
576
-
577
- await fetch(`http://127.0.0.1:${port}/graphql`, {
578
- method: 'POST',
579
- body: formData,
580
- })
581
-
582
- const anonymousClient = getAnonymousClient(port)
583
-
584
- const { test3s } = await anonymousClient.request<any>(gql`
585
- query {
586
- test3s {
587
- edges {
588
- node {
589
- id
590
- file {
591
- name
592
- url
593
- urlGeneratedAt
594
- }
595
- }
596
- }
597
- }
598
- }
599
- `)
600
-
601
- expect(test3s.edges[0].node.file.name).toEqual('a.text')
602
- expect(test3s.edges[0].node.file.url).toEqual(`http://127.0.0.1:${port}/bucket/a.text`)
603
- expect(new Date(test3s.edges[0].node.file.urlGeneratedAt)).toBeDate()
604
-
605
- expect(spyFileDevAdapterReadFile).toHaveBeenCalledTimes(1)
606
-
607
- const idOfCreatedObject = test3s.edges[0].node.id
608
-
609
- const formData2 = new FormData()
610
-
611
- formData2.append(
612
- 'operations',
613
- JSON.stringify({
614
- query: `mutation ($file: File!) {updateTest3(input: {id: "${idOfCreatedObject}",fields: {file: {file:$file}}}){test3{id, file { name }}}}`,
615
- variables: { file: null },
616
- }),
617
- )
618
-
619
- formData2.append('map', JSON.stringify({ 0: ['variables.file'] }))
620
-
621
- formData2.append('0', new File(['b'], 'b.text', { type: 'text/plain' }))
622
-
623
- // We update the file
624
- await fetch(`http://127.0.0.1:${port}/graphql`, {
625
- method: 'POST',
626
- body: formData2,
627
- })
628
-
629
- await anonymousClient.request<any>(gql`
630
- query {
631
- test3s {
632
- edges {
633
- node {
634
- id
635
- file {
636
- name
637
- url
638
- urlGeneratedAt
639
- }
640
- }
641
- }
642
- }
643
- }
644
- `)
645
-
646
- // Again once because the file was updated
647
- expect(spyFileDevAdapterReadFile).toHaveBeenCalledTimes(2)
648
- })
649
-
650
- it('should reset the cache if the url is updated', async () => {
651
- const formData = new FormData()
652
-
653
- formData.append(
654
- 'operations',
655
- JSON.stringify({
656
- query:
657
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
658
- variables: { file: null },
659
- }),
660
- )
661
-
662
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
663
-
664
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
665
-
666
- await fetch(`http://127.0.0.1:${port}/graphql`, {
667
- method: 'POST',
668
- body: formData,
669
- })
670
-
671
- const anonymousClient = getAnonymousClient(port)
672
-
673
- const { test3s } = await anonymousClient.request<any>(gql`
674
- query {
675
- test3s {
676
- edges {
677
- node {
678
- id
679
- file {
680
- name
681
- url
682
- urlGeneratedAt
683
- }
684
- }
685
- }
686
- }
687
- }
688
- `)
689
-
690
- expect(test3s.edges[0].node.file.name).toEqual('a.text')
691
- expect(test3s.edges[0].node.file.url).toEqual(`http://127.0.0.1:${port}/bucket/a.text`)
692
- expect(new Date(test3s.edges[0].node.file.urlGeneratedAt)).toBeDate()
693
-
694
- expect(spyFileDevAdapterReadFile).toHaveBeenCalledTimes(1)
695
-
696
- const idOfCreatedObject = test3s.edges[0].node.id
697
-
698
- await wabe.controllers.database.updateObject({
699
- // @ts-expect-error
700
- className: 'Test3',
701
- context: {
702
- isRoot: true,
703
- wabe,
704
- },
705
- where: {},
706
- // @ts-expect-error
707
- select: { file: true, id: true },
708
- data: {
709
- // @ts-expect-error
710
- file: {
711
- url: 'https://palixir.github.io/wabe//assets/logo.png',
712
- },
713
- },
714
- id: idOfCreatedObject,
715
- })
716
-
717
- await anonymousClient.request<any>(gql`
718
- query {
719
- test3s {
720
- edges {
721
- node {
722
- id
723
- file {
724
- name
725
- url
726
- urlGeneratedAt
727
- }
728
- }
729
- }
730
- }
731
- }
732
- `)
733
-
734
- expect(spyFileDevAdapterReadFile).toHaveBeenCalledTimes(1)
735
- })
736
-
737
- it('should delete the file on the bucket after delete the object', async () => {
738
- const formData = new FormData()
739
-
740
- formData.append(
741
- 'operations',
742
- JSON.stringify({
743
- query:
744
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
745
- variables: { file: null },
746
- }),
747
- )
748
-
749
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
750
-
751
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
752
-
753
- const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
754
- method: 'POST',
755
- body: formData,
756
- })
757
-
758
- const jsonRes = await res.json()
759
-
760
- const id = jsonRes.data.createTest3.test3.id
761
-
762
- const url = await wabe.config.file?.adapter.readFile('a.text')
763
- expect(url).not.toBeNull()
764
-
765
- const anonymousClient = getAnonymousClient(port)
766
-
767
- await anonymousClient.request<any>(
768
- gql`
769
- mutation {
770
- deleteTest3(input: {id: "${id}"}) {
771
- test3 {
772
- id
773
- }
774
- }
775
- }
776
- `,
777
- )
778
-
779
- const { test3s } = await anonymousClient.request<any>(gql`
780
- query {
781
- test3s {
782
- edges {
783
- node {
784
- id
785
- file {
786
- name
787
- url
788
- urlGeneratedAt
789
- }
790
- }
791
- }
792
- }
793
- }
794
- `)
795
-
796
- expect(test3s.edges.length).toEqual(0)
797
-
798
- const url2 = await wabe.config.file?.adapter.readFile('a.text')
799
- expect(url2).toBeNull()
800
- })
801
-
802
- it('should not delete a file if the file not exists', async () => {
803
- const formData = new FormData()
804
-
805
- formData.append(
806
- 'operations',
807
- JSON.stringify({
808
- query:
809
- 'mutation ($file: File!) {createTest3(input: {fields: {file: {file:$file}}}){test3{id, file { name}}}}',
810
- variables: { file: null },
811
- }),
812
- )
813
-
814
- formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
815
-
816
- formData.append('0', new File(['a'], 'a.text', { type: 'text/plain' }))
817
-
818
- const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
819
- method: 'POST',
820
- body: formData,
821
- })
822
-
823
- const jsonRes = await res.json()
824
-
825
- const id = jsonRes.data.createTest3.test3.id
826
-
827
- const url = await wabe.config.file?.adapter.readFile('a.text')
828
- expect(url).not.toBeNull()
829
-
830
- await wabe.config.file?.adapter.deleteFile('a.text')
831
-
832
- const anonymousClient = getAnonymousClient(port)
833
-
834
- expect(
835
- anonymousClient.request<any>(
836
- gql`
837
- mutation {
838
- deleteTest3(input: {id: "${id}"}) {
839
- test3 {
840
- id
841
- }
842
- }
843
- }
844
- `,
845
- ),
846
- ).resolves.toEqual(expect.anything())
847
- })
848
-
849
- it('should upload a file providing an url without File scalar', async () => {
850
- const anonymousClient = getAnonymousClient(port)
851
-
852
- await anonymousClient.request<any>(gql`
853
- mutation {
854
- createTest3(
855
- input: { fields: { file: { url: "https://palixir.github.io/wabe//assets/logo.png" } } }
856
- ) {
857
- test3 {
858
- id
859
- file {
860
- name
861
- url
862
- urlGeneratedAt
863
- }
864
- }
865
- }
866
- }
867
- `)
868
-
869
- const { test3s } = await anonymousClient.request<any>(gql`
870
- query {
871
- test3s {
872
- edges {
873
- node {
874
- id
875
- file {
876
- name
877
- url
878
- urlGeneratedAt
879
- }
880
- }
881
- }
882
- }
883
- }
884
- `)
885
-
886
- expect(test3s.edges[0].node.file.url).toEqual('https://palixir.github.io/wabe//assets/logo.png')
887
- })
888
-
889
- it('should upload a file and access to it with the local url provided by upload directory', async () => {
890
- await wabe.controllers.database.createObject({
891
- // @ts-expect-error
892
- className: 'Test3',
893
- context: {
894
- isRoot: true,
895
- wabe,
896
- },
897
- data: {
898
- // @ts-expect-error
899
- file: {
900
- file: new File(['this is the content'], 'a.txt', {
901
- type: 'text/plain',
902
- }),
903
- },
904
- },
905
- select: {},
906
- })
907
-
908
- const result = await wabe.controllers.database.getObjects({
909
- // @ts-expect-error
910
- className: 'Test3',
911
- context: {
912
- isRoot: true,
913
- wabe,
914
- },
915
- where: {},
916
- // @ts-expect-error
917
- select: { file: true, id: true },
918
- })
919
-
920
- // @ts-expect-error
921
- expect(result[0].file.name).toEqual('a.txt')
922
- // @ts-expect-error
923
- expect(result[0].file.url).toEqual(`http://127.0.0.1:${port}/bucket/a.txt`)
924
-
925
- // @ts-expect-error
926
- const url = result?.[0]?.file?.url
927
-
928
- const res = await fetch(url)
929
-
930
- expect(await res.text()).toEqual('this is the content')
931
- })
932
- })
933
-
934
- describe('File upload security in production', () => {
935
- let wabe: Wabe<DevWabeTypes>
936
-
937
- beforeAll(async () => {
938
- const setup = await setupTests(
939
- [
940
- {
941
- name: 'TestSecurityFile',
942
- fields: {
943
- file: { type: 'File' },
944
- },
945
- permissions: {
946
- read: { requireAuthentication: false },
947
- create: { requireAuthentication: false },
948
- update: { requireAuthentication: false },
949
- delete: { requireAuthentication: false },
950
- },
951
- },
952
- ],
953
- { isProduction: true },
954
- )
955
-
956
- wabe = setup.wabe
957
- })
958
-
959
- afterAll(async () => {
960
- await closeTests(wabe)
961
- })
962
-
963
- afterEach(async () => {
964
- await wabe.controllers.database.deleteObjects({
965
- // @ts-expect-error
966
- className: 'TestSecurityFile',
967
- context: {
968
- isRoot: true,
969
- wabe,
970
- },
971
- where: {},
972
- select: {},
973
- })
974
- })
975
-
976
- it('should randomize uploaded file name in production', async () => {
977
- await wabe.controllers.database.createObject({
978
- // @ts-expect-error
979
- className: 'TestSecurityFile',
980
- context: {
981
- isRoot: true,
982
- wabe,
983
- },
984
- data: {
985
- // @ts-expect-error
986
- file: {
987
- file: new File(['hello'], 'report.txt', { type: 'text/plain' }),
988
- },
989
- },
990
- select: {},
991
- })
992
-
993
- const result = await wabe.controllers.database.getObjects({
994
- // @ts-expect-error
995
- className: 'TestSecurityFile',
996
- context: {
997
- isRoot: true,
998
- wabe,
999
- },
1000
- where: {},
1001
- // @ts-expect-error
1002
- select: { file: true, id: true },
1003
- })
1004
-
1005
- const storedName = (result[0] as any)?.file?.name as string
1006
-
1007
- expect(storedName).toBeString()
1008
- expect(storedName).not.toEqual('report.txt')
1009
- expect(storedName.endsWith('.txt')).toBe(true)
1010
- })
1011
-
1012
- it('should reject file type not allowed in production', async () => {
1013
- expect(
1014
- wabe.controllers.database.createObject({
1015
- // @ts-expect-error
1016
- className: 'TestSecurityFile',
1017
- context: {
1018
- isRoot: true,
1019
- wabe,
1020
- },
1021
- data: {
1022
- // @ts-expect-error
1023
- file: {
1024
- file: new File(['alert(1)'], 'script.js', { type: 'application/javascript' }),
1025
- },
1026
- },
1027
- select: {},
1028
- }),
1029
- ).rejects.toThrow('File extension is not allowed')
1030
- })
1031
- })