1
1
import { Day , formatDate } from 'web-utility' ;
2
2
3
- import {
4
- AwardTarget ,
5
- Hackathon ,
6
- HackathonStatus ,
7
- Operation ,
8
- StaffType
9
- } from '../source/model' ;
10
- import { Award , HttpResponse , User } from './client' ;
3
+ import { Award , Hackathon , HttpResponse , Team , User } from './client' ;
11
4
import { client , GITHUB_PAT } from './shared' ;
12
5
6
+ const baseData = {
7
+ id : expect . any ( Number ) ,
8
+ createdAt : expect . any ( String ) ,
9
+ updatedAt : expect . any ( String )
10
+ } ;
13
11
let platformAdmin : User ,
14
12
hackathonCreator : User ,
15
13
testHackathon : Hackathon ,
16
14
teamLeader1 : User ,
17
- testAward : Award ;
15
+ testAward : Award ,
16
+ testTeam : Team ;
18
17
19
18
describe ( 'Main business logic' , ( ) => {
20
19
it ( 'should response 401 error with invalid token' , async ( ) => {
21
20
try {
22
21
await client . user . userControllerGetSession ( ) ;
22
+
23
+ fail ( 'it should have thrown a 403 error' ) ;
23
24
} catch ( error ) {
24
25
expect ( ( error as HttpResponse < unknown > ) . status ) . toBe ( 401 ) ;
25
26
}
@@ -101,22 +102,19 @@ describe('Main business logic', () => {
101
102
it ( 'should record 2 activities of a signed-up & edited User' , async ( ) => {
102
103
const UID = hackathonCreator . id ;
103
104
const activityLog = {
104
- id : expect . any ( Number ) ,
105
+ ... baseData ,
105
106
tableName : 'User' ,
106
107
recordId : UID ,
107
- record : expect . any ( Object ) ,
108
- createdAt : expect . any ( String ) ,
109
- createdBy : expect . any ( Object ) ,
110
- updatedAt : expect . any ( String )
108
+ record : expect . any ( Object )
111
109
} ,
112
110
{ data } =
113
111
await client . activityLog . activityLogControllerGetUserList ( UID ) ;
114
112
115
113
expect ( data ) . toMatchObject ( {
116
114
count : 2 ,
117
115
list : [
118
- { ...activityLog , operation : Operation . Create } ,
119
- { ...activityLog , operation : Operation . Update }
116
+ { ...activityLog , operation : 'create' } ,
117
+ { ...activityLog , operation : 'update' }
120
118
]
121
119
} ) ;
122
120
} ) ;
@@ -141,8 +139,8 @@ describe('Main business logic', () => {
141
139
} ) ;
142
140
143
141
it ( 'should create a new hackathon by every user' , async ( ) => {
144
- const eventStartedAt = formatDate ( new Date ( ) , 'YYYY-MM-DD' ) ,
145
- eventEndedAt = formatDate ( + new Date ( ) + Day , 'YYYY-MM-DD' ) ,
142
+ const eventStartedAt = formatDate ( Date . now ( ) - Day , 'YYYY-MM-DD' ) ,
143
+ eventEndedAt = formatDate ( Date . now ( ) + Day , 'YYYY-MM-DD' ) ,
146
144
hackathonMeta = {
147
145
name : 'test-hackathon' ,
148
146
displayName : 'Test Hackathon' ,
@@ -177,36 +175,30 @@ describe('Main business logic', () => {
177
175
}
178
176
) ;
179
177
expect ( hackathon ) . toMatchObject ( {
178
+ ...baseData ,
180
179
...hackathonMeta ,
181
180
autoApprove : true ,
182
181
readOnly : false ,
183
- status : HackathonStatus . Planning ,
184
- id : expect . any ( Number ) ,
185
- createdAt : expect . any ( String ) ,
186
- createdBy : expect . any ( Object ) ,
187
- updatedAt : expect . any ( String )
182
+ status : 'planning'
188
183
} ) ;
189
- // @ts -expect-error Enum compatibility
190
184
testHackathon = hackathon ;
191
185
} ) ;
192
186
193
187
it ( 'should auto set the creator as an admin of this hackathon' , async ( ) => {
194
188
const { data : staffList } =
195
189
await client . hackathon . staffControllerGetList (
196
190
testHackathon . name ,
197
- StaffType . Admin
191
+ 'admin'
198
192
) ;
199
193
expect ( staffList ) . toMatchObject ( {
200
194
count : 1 ,
201
195
list : [
202
196
{
203
- id : expect . any ( Number ) ,
204
- type : StaffType . Admin ,
197
+ ... baseData ,
198
+ type : 'admin' ,
205
199
user : expect . any ( Object ) ,
206
200
description : 'Hackathon Creator' ,
207
- hackathon : expect . any ( Object ) ,
208
- createdAt : expect . any ( String ) ,
209
- updatedAt : expect . any ( String )
201
+ hackathon : expect . any ( Object )
210
202
}
211
203
]
212
204
} ) ;
@@ -226,7 +218,6 @@ describe('Main business logic', () => {
226
218
isJudge : false ,
227
219
isEnrolled : false
228
220
} ) ;
229
- // @ts -expect-error Enum compatibility
230
221
testHackathon = { ...testHackathon , ...data } ;
231
222
delete testHackathon . roles ;
232
223
} ) ;
@@ -237,15 +228,13 @@ describe('Main business logic', () => {
237
228
238
229
const { data } = await client . hackathon . hackathonControllerUpdateOne (
239
230
testHackathon . name ,
240
- // @ts -expect-error Enum compatibility
241
231
testHackathon ,
242
232
{ headers : { Authorization : `Bearer ${ hackathonCreator . token } ` } }
243
233
) ;
244
234
expect ( data . detail ) . toBe ( testHackathon . detail ) ;
245
235
expect ( data . updatedAt ) . toStrictEqual ( expect . any ( String ) ) ;
246
236
expect ( data . updatedBy . id ) . toBe ( hackathonCreator . id ) ;
247
237
248
- // @ts -expect-error Enum compatibility
249
238
testHackathon = { ...testHackathon , ...data } ;
250
239
delete testHackathon . updatedBy ;
251
240
delete testHackathon . deletedAt ;
@@ -293,14 +282,12 @@ describe('Main business logic', () => {
293
282
expect ( status ) . toBe ( 201 ) ;
294
283
295
284
expect ( session ) . toMatchObject ( {
296
- id : expect . any ( Number ) ,
285
+ ... baseData ,
297
286
email : expect . any ( String ) ,
298
287
name : expect . any ( String ) ,
299
288
avatar : expect . any ( String ) ,
300
289
password : null ,
301
- token : expect . any ( String ) ,
302
- createdAt : expect . any ( String ) ,
303
- updatedAt : expect . any ( String )
290
+ token : expect . any ( String )
304
291
} ) ;
305
292
306
293
const { deletedAt, password, token, ...user } = session ;
@@ -330,7 +317,7 @@ describe('Main business logic', () => {
330
317
name : 'Best Innovation Award' ,
331
318
description : 'Award for the most innovative project' ,
332
319
quantity : 1 ,
333
- target : AwardTarget . Team ,
320
+ target : 'team' as const ,
334
321
pictures : [
335
322
{
336
323
name : 'award-image' ,
@@ -347,10 +334,8 @@ describe('Main business logic', () => {
347
334
testAward = award ;
348
335
349
336
expect ( award ) . toMatchObject ( {
337
+ ...baseData ,
350
338
...awardData ,
351
- id : expect . any ( Number ) ,
352
- createdAt : expect . any ( String ) ,
353
- updatedAt : expect . any ( String ) ,
354
339
hackathon : expect . any ( Object )
355
340
} ) ;
356
341
expect ( award . hackathon . id ) . toBe ( testHackathon . id ) ;
@@ -392,6 +377,24 @@ describe('Main business logic', () => {
392
377
expect ( award . id ) . toBe ( testAward . id ) ;
393
378
} ) ;
394
379
380
+ it ( 'should not allow unauthorized users to manage awards' , async ( ) => {
381
+ try {
382
+ await client . hackathon . awardControllerCreateOne (
383
+ testHackathon . name ,
384
+ {
385
+ name : 'Unauthorized Award' ,
386
+ description : 'Test award description' ,
387
+ quantity : 1 ,
388
+ target : 'team' as const ,
389
+ pictures : [ ]
390
+ }
391
+ ) ;
392
+ fail ( 'Should have thrown a 401 error' ) ;
393
+ } catch ( error ) {
394
+ expect ( ( error as HttpResponse < unknown > ) . status ) . toBe ( 401 ) ;
395
+ }
396
+ } ) ;
397
+
395
398
it ( 'should delete an award' , async ( ) => {
396
399
await client . hackathon . awardControllerDeleteOne (
397
400
testHackathon . name ,
@@ -405,29 +408,109 @@ describe('Main business logic', () => {
405
408
testAward . id
406
409
) ;
407
410
fail ( 'Should have thrown a 404 error' ) ;
408
- } catch ( error : unknown ) {
411
+ } catch ( error ) {
409
412
expect ( ( error as HttpResponse < unknown > ) . status ) . toBe ( 404 ) ;
410
413
}
411
414
} ) ;
412
415
413
- it ( 'should not allow unauthorized users to manage awards' , async ( ) => {
416
+ it ( 'should enroll a user in the hackathon' , async ( ) => {
417
+ const { data : enrollment } =
418
+ await client . hackathon . enrollmentControllerCreateOne (
419
+ testHackathon . name ,
420
+ { form : [ ] } ,
421
+ { headers : { Authorization : `Bearer ${ teamLeader1 . token } ` } }
422
+ ) ;
423
+ expect ( enrollment ) . toMatchObject ( {
424
+ ...baseData ,
425
+ hackathon : expect . any ( Object )
426
+ } ) ;
427
+ const { data : hackathon } =
428
+ await client . hackathon . hackathonControllerGetOne (
429
+ testHackathon . name
430
+ ) ;
431
+ expect ( hackathon . enrollment ) . toBe ( 1 ) ;
432
+ } ) ;
433
+
434
+ it ( 'should create a hackathon team by a participant' , async ( ) => {
435
+ const newTeam = {
436
+ displayName : 'New Team' ,
437
+ description : 'A new team for the hackathon'
438
+ } ;
439
+ const { data : team } = await client . hackathon . teamControllerCreateOne (
440
+ testHackathon . name ,
441
+ newTeam ,
442
+ {
443
+ headers : {
444
+ Authorization : `Bearer ${ teamLeader1 . token } `
445
+ }
446
+ }
447
+ ) ;
448
+ expect ( team ) . toMatchObject ( { ...baseData , ...newTeam } ) ;
449
+
450
+ testTeam = team ;
451
+ delete testTeam . deletedAt ;
452
+ } ) ;
453
+
454
+ it ( 'should get the created team by its Hackathon Name & ID' , async ( ) => {
455
+ const { data : team } = await client . hackathon . teamControllerGetOne (
456
+ testHackathon . name ,
457
+ testTeam . id
458
+ ) ;
459
+ expect ( team ) . toMatchObject ( {
460
+ ...baseData ,
461
+ ...testTeam ,
462
+ createdBy : expect . any ( Object ) ,
463
+ hackathon : expect . any ( Object )
464
+ } ) ;
465
+ } ) ;
466
+
467
+ it ( 'should update the team by its members' , async ( ) => {
468
+ const updateData = {
469
+ displayName : 'Updated Team Name' ,
470
+ description : 'Updated team description'
471
+ } ;
472
+ const { data : team } = await client . hackathon . teamControllerUpdateOne (
473
+ testHackathon . name ,
474
+ testTeam . id ,
475
+ updateData ,
476
+ { headers : { Authorization : `Bearer ${ teamLeader1 . token } ` } }
477
+ ) ;
478
+ expect ( team ) . toMatchObject ( {
479
+ ...baseData ,
480
+ ...updateData ,
481
+ updatedBy : expect . any ( Object )
482
+ } ) ;
483
+ } ) ;
484
+
485
+ it ( 'should not allow unauthorized users to update a team' , async ( ) => {
414
486
try {
415
- await client . hackathon . awardControllerCreateOne (
487
+ await client . hackathon . teamControllerUpdateOne (
416
488
testHackathon . name ,
489
+ testTeam . id ,
417
490
{
418
- name : 'Unauthorized Award' ,
419
- description : 'Test award description' ,
420
- quantity : 1 ,
421
- target : AwardTarget . Team ,
422
- pictures : [ ]
491
+ displayName : 'Unauthorized Update' ,
492
+ description : 'This should not be allowed'
493
+ } ,
494
+ {
495
+ headers : {
496
+ Authorization : `Bearer ${ hackathonCreator . token } `
497
+ }
423
498
}
424
499
) ;
425
- fail ( 'Should have thrown a 401 error' ) ;
426
- } catch ( error : unknown ) {
427
- expect ( ( error as HttpResponse < unknown > ) . status ) . toBe ( 401 ) ;
500
+ fail ( 'it should have thrown a 403 error' ) ;
501
+ } catch ( error ) {
502
+ expect ( ( error as HttpResponse < unknown > ) . status ) . toBe ( 403 ) ;
428
503
}
429
504
} ) ;
430
505
506
+ it ( 'should get the list of teams in the hackathon' , async ( ) => {
507
+ const { data : teamList } = await client . hackathon . teamControllerGetList (
508
+ testHackathon . name
509
+ ) ;
510
+ expect ( teamList . count ) . toBe ( 1 ) ;
511
+ expect ( teamList . list [ 0 ] . id ) . toBe ( testTeam . id ) ;
512
+ } ) ;
513
+
431
514
it ( 'should delete a hackathon by its admin' , async ( ) => {
432
515
const { name } = testHackathon ;
433
516
const { status, data } =
@@ -439,6 +522,8 @@ describe('Main business logic', () => {
439
522
440
523
try {
441
524
await client . hackathon . hackathonControllerGetOne ( name ) ;
525
+
526
+ fail ( 'it should have thrown a 403 error' ) ;
442
527
} catch ( error ) {
443
528
expect ( ( error as HttpResponse < unknown > ) . status ) . toBe ( 404 ) ;
444
529
}
0 commit comments