-
Notifications
You must be signed in to change notification settings - Fork 198
/
nip96.test.ts
654 lines (523 loc) · 19.4 KB
/
nip96.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import { describe, expect, it } from 'bun:test'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import { FileServerPreference } from './kinds.ts'
import {
calculateFileHash,
checkFileProcessingStatus,
deleteFile,
generateDownloadUrl,
generateFSPEventTemplate,
readServerConfig,
uploadFile,
validateDelayedProcessingResponse,
validateFileUploadResponse,
validateServerConfiguration,
type DelayedProcessingResponse,
type FileUploadResponse,
type ServerConfiguration,
} from './nip96.ts'
describe('validateServerConfiguration', () => {
it("should return true if 'api_url' is valid URL", () => {
const config: ServerConfiguration = {
api_url: 'http://example.com',
}
expect(validateServerConfiguration(config)).toBe(true)
})
it("should return false if 'api_url' is empty", () => {
const config: ServerConfiguration = {
api_url: '',
}
expect(validateServerConfiguration(config)).toBe(false)
})
it("should return false if both 'api_url' and 'delegated_to_url' are provided", () => {
const config: ServerConfiguration = {
api_url: 'http://example.com',
delegated_to_url: 'http://example.com',
}
expect(validateServerConfiguration(config)).toBe(false)
})
})
describe('readServerConfig', () => {
it('should return a valid ServerConfiguration object', async () => {
// setup mock server
const HTTPROUTE = '/.well-known/nostr/nip96.json' as const
const validConfig: ServerConfiguration = {
api_url: 'http://example.com',
}
const handler = http.get(`http://example.com${HTTPROUTE}`, () => {
return HttpResponse.json(validConfig)
})
const server = setupServer(handler)
server.listen()
const result = await readServerConfig('http://example.com/')
expect(result).toEqual(validConfig)
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw an error if response is not valid', async () => {
// setup mock server
const HTTPROUTE = '/.well-known/nostr/nip96.json' as const
const invalidConfig = {
// missing api_url
}
const handler = http.get(`http://example.com${HTTPROUTE}`, () => {
return HttpResponse.json(invalidConfig)
})
const server = setupServer(handler)
server.listen()
expect(readServerConfig('http://example.com/')).rejects.toThrow()
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw an error if response is not proper json', async () => {
// setup mock server
const HTTPROUTE = '/.well-known/nostr/nip96.json' as const
const handler = http.get(`http://example.com${HTTPROUTE}`, () => {
return HttpResponse.json(null)
})
const server = setupServer(handler)
server.listen()
expect(readServerConfig('http://example.com/')).rejects.toThrow()
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw an error if response status is not 200', async () => {
// setup mock server
const HTTPROUTE = '/.well-known/nostr/nip96.json' as const
const handler = http.get(`http://example.com${HTTPROUTE}`, () => {
return new HttpResponse(null, { status: 400 })
})
const server = setupServer(handler)
server.listen()
expect(readServerConfig('http://example.com/')).rejects.toThrow()
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw an error if input url is not valid', async () => {
expect(readServerConfig('invalid-url')).rejects.toThrow()
})
})
describe('validateFileUploadResponse', () => {
it('should return true if response is valid', () => {
const mockResponse: FileUploadResponse = {
status: 'error',
message: 'File uploaded failed',
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(true)
})
it('should return false if status is undefined', () => {
const mockResponse: Omit<FileUploadResponse, 'status'> = {
// status: 'error',
message: 'File upload failed',
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if message is undefined', () => {
const mockResponse: Omit<FileUploadResponse, 'message'> = {
status: 'error',
// message: 'message',
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if status is not valid', () => {
const mockResponse = {
status: 'something else',
message: 'message',
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if "message" is not a string', () => {
const mockResponse = {
status: 'error',
message: 123,
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if status is "processing" and "processing_url" is undefined', () => {
const mockResponse = {
status: 'processing',
message: 'message',
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if status is "processing" and "processing_url" is not a string', () => {
const mockResponse = {
status: 'processing',
message: 'message',
processing_url: 123,
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if status is "success" and "nip94_event" is undefined', () => {
const mockResponse = {
status: 'success',
message: 'message',
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if "nip94_event" tags are invalid', () => {
const mockResponse = {
status: 'success',
message: 'message',
nip94_event: {
tags: [
// missing url
['ox', '719171db19525d9d08dd69cb716a18158a249b7b3b3ec4bbdec5698dca104b7b'],
],
},
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return false if "nip94_event" tags are empty', () => {
const mockResponse = {
status: 'success',
message: 'message',
nip94_event: {
tags: [],
},
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(false)
})
it('should return true if "nip94_event" tags are valid', () => {
const mockResponse = {
status: 'success',
message: 'message',
nip94_event: {
tags: [
['url', 'http://example.com'],
['ox', '719171db19525d9d08dd69cb716a18158a249b7b3b3ec4bbdec5698dca104b7b'],
],
},
}
const result = validateFileUploadResponse(mockResponse)
expect(result).toBe(true)
})
})
describe('uploadFile', () => {
it('should return a valid FileUploadResponse object', async () => {
// setup mock server
const validFileUploadResponse: FileUploadResponse = {
status: 'success',
message: 'message',
nip94_event: {
content: '',
tags: [
['url', 'http://example.com'],
['ox', '719171db19525d9d08dd69cb716a18158a249b7b3b3ec4bbdec5698dca104b7b'],
],
},
}
const handler = http.post('http://example.com/upload', () => {
return HttpResponse.json(validFileUploadResponse, { status: 200 })
})
const server = setupServer(handler)
server.listen()
const file = new File(['hello world'], 'hello.txt')
const serverUploadUrl = 'http://example.com/upload'
const nip98AuthorizationHeader = 'Nostr abcabc'
const result = await uploadFile(file, serverUploadUrl, nip98AuthorizationHeader)
expect(result).toEqual(validFileUploadResponse)
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw a proper error if response status is 413', async () => {
// setup mock server
const handler = http.post('http://example.com/upload', () => {
return new HttpResponse(null, { status: 413 })
})
const server = setupServer(handler)
server.listen()
const file = new File(['hello world'], 'hello.txt')
const serverUploadUrl = 'http://example.com/upload'
const nip98AuthorizationHeader = 'Nostr abcabc'
expect(uploadFile(file, serverUploadUrl, nip98AuthorizationHeader)).rejects.toThrow('File too large!')
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw a proper error if response status is 400', async () => {
// setup mock server
const handler = http.post('http://example.com/upload', () => {
return new HttpResponse(null, { status: 400 })
})
const server = setupServer(handler)
server.listen()
const file = new File(['hello world'], 'hello.txt')
const serverUploadUrl = 'http://example.com/upload'
const nip98AuthorizationHeader = 'Nostr abcabc'
expect(uploadFile(file, serverUploadUrl, nip98AuthorizationHeader)).rejects.toThrow(
'Bad request! Some fields are missing or invalid!',
)
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw a proper error if response status is 403', async () => {
// setup mock server
const handler = http.post('http://example.com/upload', () => {
return new HttpResponse(null, { status: 403 })
})
const server = setupServer(handler)
server.listen()
const file = new File(['hello world'], 'hello.txt')
const serverUploadUrl = 'http://example.com/upload'
const nip98AuthorizationHeader = 'Nostr abcabc'
expect(uploadFile(file, serverUploadUrl, nip98AuthorizationHeader)).rejects.toThrow(
'Forbidden! Payload tag does not match the requested file!',
)
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw a proper error if response status is 402', async () => {
// setup mock server
const handler = http.post('http://example.com/upload', () => {
return new HttpResponse(null, { status: 402 })
})
const server = setupServer(handler)
server.listen()
const file = new File(['hello world'], 'hello.txt')
const serverUploadUrl = 'http://example.com/upload'
const nip98AuthorizationHeader = 'Nostr abcabc'
expect(uploadFile(file, serverUploadUrl, nip98AuthorizationHeader)).rejects.toThrow('Payment required!')
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw a proper error if response status is not 200, 400, 402, 403, 413', async () => {
// setup mock server
const handler = http.post('http://example.com/upload', () => {
return new HttpResponse(null, { status: 500 })
})
const server = setupServer(handler)
server.listen()
const file = new File(['hello world'], 'hello.txt')
const serverUploadUrl = 'http://example.com/upload'
const nip98AuthorizationHeader = 'Nostr abcabc'
expect(uploadFile(file, serverUploadUrl, nip98AuthorizationHeader)).rejects.toThrow(
'Unknown error in uploading file!',
)
// cleanup mock server
server.resetHandlers()
server.close()
})
})
describe('generateDownloadUrl', () => {
it('should generate a download URL without file extension', () => {
const fileHash = 'abc123'
const serverDownloadUrl = 'http://example.com/download'
const expectedUrl = 'http://example.com/download/abc123'
const result = generateDownloadUrl(fileHash, serverDownloadUrl)
expect(result).toBe(expectedUrl)
})
it('should generate a download URL with file extension', () => {
const fileHash = 'abc123'
const serverDownloadUrl = 'http://example.com/download'
const fileExtension = '.jpg'
const expectedUrl = 'http://example.com/download/abc123.jpg'
const result = generateDownloadUrl(fileHash, serverDownloadUrl, fileExtension)
expect(result).toBe(expectedUrl)
})
})
describe('deleteFile', () => {
it('should return a basic json response for successful delete', async () => {
// setup mock server
const handler = http.delete('http://example.com/delete/abc123', () => {
return HttpResponse.json({ status: 'success', message: 'File deleted.' }, { status: 200 })
})
const server = setupServer(handler)
server.listen()
const fileHash = 'abc123'
const serverDeleteUrl = 'http://example.com/delete'
const nip98AuthorizationHeader = 'Nostr abcabc'
const result = await deleteFile(fileHash, serverDeleteUrl, nip98AuthorizationHeader)
expect(result).toEqual({ status: 'success', message: 'File deleted.' })
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw an error for unsuccessful delete', async () => {
// setup mock server
const handler = http.delete('http://example.com/delete/abc123', () => {
return new HttpResponse(null, { status: 400 })
})
const server = setupServer(handler)
server.listen()
const fileHash = 'abc123'
const serverDeleteUrl = 'http://example.com/delete'
const nip98AuthorizationHeader = 'Nostr abcabc'
expect(deleteFile(fileHash, serverDeleteUrl, nip98AuthorizationHeader)).rejects.toThrow()
// cleanup mock server
server.resetHandlers()
server.close()
})
})
describe('validateDelayedProcessingResponse', () => {
it('should return false for non-object input', () => {
expect(validateDelayedProcessingResponse('not an object')).toBe(false)
})
it('should return false for null input', () => {
expect(validateDelayedProcessingResponse(null)).toBe(false)
})
it('should return false for object missing required properties', () => {
const missingStatus: Omit<DelayedProcessingResponse, 'status'> = {
// missing status
message: 'test',
percentage: 50,
}
const missingMessage: Omit<DelayedProcessingResponse, 'message'> = {
status: 'processing',
// missing message
percentage: 50,
}
const missingPercentage: Omit<DelayedProcessingResponse, 'percentage'> = {
status: 'processing',
message: 'test',
// missing percentage
}
expect(validateDelayedProcessingResponse(missingStatus)).toBe(false)
expect(validateDelayedProcessingResponse(missingMessage)).toBe(false)
expect(validateDelayedProcessingResponse(missingPercentage)).toBe(false)
})
it('should return false for invalid status', () => {
expect(validateDelayedProcessingResponse({ status: 'invalid', message: 'test', percentage: 50 })).toBe(false)
})
it('should return false for non-string message', () => {
expect(validateDelayedProcessingResponse({ status: 'processing', message: 123, percentage: 50 })).toBe(false)
})
it('should return false for non-number percentage', () => {
expect(validateDelayedProcessingResponse({ status: 'processing', message: 'test', percentage: '50' })).toBe(false)
})
it('should return false for percentage out of range', () => {
expect(validateDelayedProcessingResponse({ status: 'processing', message: 'test', percentage: 150 })).toBe(false)
})
it('should return true for valid input', () => {
expect(validateDelayedProcessingResponse({ status: 'processing', message: 'test', percentage: 50 })).toBe(true)
})
})
describe('checkFileProcessingStatus', () => {
it('should throw an error if response is not ok', async () => {
// setup mock server
const handler = http.get('http://example.com/status/abc123', () => {
return new HttpResponse(null, { status: 400 })
})
const server = setupServer(handler)
server.listen()
const processingUrl = 'http://example.com/status/abc123'
expect(checkFileProcessingStatus(processingUrl)).rejects.toThrow()
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should throw an error if response is not a valid json', async () => {
// setup mock server
const handler = http.get('http://example.com/status/abc123', () => {
return HttpResponse.text('not a json', { status: 200 })
})
const server = setupServer(handler)
server.listen()
const processingUrl = 'http://example.com/status/abc123'
expect(checkFileProcessingStatus(processingUrl)).rejects.toThrow()
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should return a valid DelayedProcessingResponse object if response status is 200', async () => {
// setup mock server
const validDelayedProcessingResponse: DelayedProcessingResponse = {
status: 'processing',
message: 'test',
percentage: 50,
}
const handler = http.get('http://example.com/status/abc123', () => {
return HttpResponse.json(validDelayedProcessingResponse, { status: 200 })
})
const server = setupServer(handler)
server.listen()
const processingUrl = 'http://example.com/status/abc123'
const result = await checkFileProcessingStatus(processingUrl)
expect(result).toEqual(validDelayedProcessingResponse)
// cleanup mock server
server.resetHandlers()
server.close()
})
it('should return a valid FileUploadResponse object if response status is 201', async () => {
// setup mock server
const validFileUploadResponse: FileUploadResponse = {
status: 'success',
message: 'message',
nip94_event: {
content: '',
tags: [
['url', 'http://example.com'],
['ox', '719171db19525d9d08dd69cb716a18158a249b7b3b3ec4bbdec5698dca104b7b'],
],
},
}
const handler = http.get('http://example.com/status/abc123', () => {
return HttpResponse.json(validFileUploadResponse, { status: 201 })
})
const server = setupServer(handler)
server.listen()
const processingUrl = 'http://example.com/status/abc123'
const result = await checkFileProcessingStatus(processingUrl)
expect(result).toEqual(validFileUploadResponse)
// cleanup mock server
server.resetHandlers()
server.close()
})
})
describe('generateFSPEventTemplate', () => {
it('should generate FSP event template', () => {
const serverUrls = ['http://example.com', 'https://example.org']
const eventTemplate = generateFSPEventTemplate(serverUrls)
expect(eventTemplate.kind).toBe(FileServerPreference)
expect(eventTemplate.content).toBe('')
expect(eventTemplate.tags).toEqual([
['server', 'http://example.com'],
['server', 'https://example.org'],
])
expect(typeof eventTemplate.created_at).toBe('number')
})
it('should filter invalid server URLs', () => {
const serverUrls = ['http://example.com', 'invalid-url', 'https://example.org']
const eventTemplate = generateFSPEventTemplate(serverUrls)
expect(eventTemplate.tags).toEqual([
['server', 'http://example.com'],
['server', 'https://example.org'],
])
})
it('should handle empty server URLs', () => {
const serverUrls: string[] = []
const eventTemplate = generateFSPEventTemplate(serverUrls)
expect(eventTemplate.tags).toEqual([])
})
})
describe('calculateFileHash', () => {
it('should calculate file hash', async () => {
const file = new File(['hello world'], 'hello.txt')
const hash = await calculateFileHash(file)
expect(hash).toBe('b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9')
})
it('should calculate file hash with empty file', async () => {
const file = new File([], 'empty.txt')
const hash = await calculateFileHash(file)
expect(hash).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
})
})