@@ -18,8 +18,15 @@ import {
18
18
renameProject ,
19
19
restoreDeletedProject ,
20
20
softDeleteProject ,
21
+ updateGithubRepository ,
21
22
} from './project.server'
22
- import { AccessLevel , AccessRequestStatus } from '../types'
23
+ import {
24
+ AccessLevel ,
25
+ MaxGithubBranchNameLength ,
26
+ MaxGithubOwnerLength ,
27
+ MaxGithubRepositoryLength ,
28
+ AccessRequestStatus ,
29
+ } from '../types'
23
30
24
31
describe ( 'project model' , ( ) => {
25
32
afterEach ( async ( ) => {
@@ -419,4 +426,102 @@ describe('project model', () => {
419
426
expect ( got . collaborators [ 'four' ] . map ( ( c ) => c . id ) [ 1 ] ) . toBe ( 'carol' )
420
427
} )
421
428
} )
429
+
430
+ describe ( 'updateGithubRepository' , ( ) => {
431
+ beforeEach ( async ( ) => {
432
+ await createTestUser ( prisma , { id : 'bob' } )
433
+ await createTestUser ( prisma , { id : 'alice' } )
434
+ await createTestProject ( prisma , { id : 'one' , ownerId : 'bob' } )
435
+ await prisma . project . update ( {
436
+ where : { proj_id : 'one' } ,
437
+ data : { github_repository : 'something' } ,
438
+ } )
439
+ } )
440
+
441
+ it ( 'errors if the project is not found' , async ( ) => {
442
+ const fn = async ( ) =>
443
+ updateGithubRepository ( { projectId : 'unknown' , userId : 'bob' , repository : null } )
444
+ await expect ( fn ) . rejects . toThrow ( 'not found' )
445
+ } )
446
+
447
+ it ( 'errors if the user does not own the project' , async ( ) => {
448
+ const fn = async ( ) =>
449
+ updateGithubRepository ( { projectId : 'one' , userId : 'alice' , repository : null } )
450
+ await expect ( fn ) . rejects . toThrow ( 'not found' )
451
+ } )
452
+
453
+ it ( 'updates the repository string (null)' , async ( ) => {
454
+ await updateGithubRepository ( { projectId : 'one' , userId : 'bob' , repository : null } )
455
+ const project = await prisma . project . findUnique ( {
456
+ where : { proj_id : 'one' } ,
457
+ select : { github_repository : true } ,
458
+ } )
459
+ if ( project == null ) {
460
+ throw new Error ( 'expected project not to be null' )
461
+ }
462
+ expect ( project . github_repository ) . toBe ( null )
463
+ } )
464
+
465
+ it ( 'updates the repository string (without branch)' , async ( ) => {
466
+ await updateGithubRepository ( {
467
+ projectId : 'one' ,
468
+ userId : 'bob' ,
469
+ repository : { owner : 'foo' , repository : 'bar' , branch : null } ,
470
+ } )
471
+ const project = await prisma . project . findUnique ( {
472
+ where : { proj_id : 'one' } ,
473
+ select : { github_repository : true } ,
474
+ } )
475
+ if ( project == null ) {
476
+ throw new Error ( 'expected project not to be null' )
477
+ }
478
+ expect ( project . github_repository ) . toBe ( 'foo/bar' )
479
+ } )
480
+
481
+ it ( 'updates the repository string (with branch)' , async ( ) => {
482
+ await updateGithubRepository ( {
483
+ projectId : 'one' ,
484
+ userId : 'bob' ,
485
+ repository : { owner : 'foo' , repository : 'bar' , branch : 'baz' } ,
486
+ } )
487
+ const project = await prisma . project . findUnique ( {
488
+ where : { proj_id : 'one' } ,
489
+ select : { github_repository : true } ,
490
+ } )
491
+ if ( project == null ) {
492
+ throw new Error ( 'expected project not to be null' )
493
+ }
494
+ expect ( project . github_repository ) . toBe ( 'foo/bar:baz' )
495
+ } )
496
+
497
+ it ( 'updates the repository string (with branch), trimming to max lengths' , async ( ) => {
498
+ const repo = {
499
+ owner :
500
+ 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' ,
501
+ repository :
502
+ 'barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr' ,
503
+ branch :
504
+ 'bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' ,
505
+ }
506
+ await updateGithubRepository ( {
507
+ projectId : 'one' ,
508
+ userId : 'bob' ,
509
+ repository : repo ,
510
+ } )
511
+ const project = await prisma . project . findUnique ( {
512
+ where : { proj_id : 'one' } ,
513
+ select : { github_repository : true } ,
514
+ } )
515
+ if ( project == null ) {
516
+ throw new Error ( 'expected project not to be null' )
517
+ }
518
+ expect ( project . github_repository ) . toBe (
519
+ repo . owner . slice ( 0 , MaxGithubOwnerLength ) +
520
+ '/' +
521
+ repo . repository . slice ( 0 , MaxGithubRepositoryLength ) +
522
+ ':' +
523
+ repo . branch . slice ( 0 , MaxGithubBranchNameLength ) ,
524
+ )
525
+ } )
526
+ } )
422
527
} )
0 commit comments