@@ -258,9 +258,13 @@ fn is_token_active(metadata: &ResponseTokenMetadata, current_time: u128) -> bool
258
258
259
259
#[ cfg( test) ]
260
260
mod tests {
261
+ use std:: backtrace:: Backtrace ;
262
+
263
+ use async_trait:: async_trait;
264
+ use reqwest:: { Method , Response } ;
261
265
use tempfile:: tempdir;
262
266
use turbopath:: AbsoluteSystemPathBuf ;
263
- use turborepo_vercel_api:: token:: Scope ;
267
+ use turborepo_vercel_api:: { token:: Scope , CachingStatus , CachingStatusResponse } ;
264
268
265
269
use super :: * ;
266
270
@@ -372,4 +376,149 @@ mod tests {
372
376
373
377
assert ! ( matches!( result, Err ( Error :: TokenNotFound ) ) ) ;
374
378
}
379
+
380
+ enum MockErrorType {
381
+ Error ,
382
+ Forbidden ,
383
+ }
384
+ enum MockCachingResponse {
385
+ CachingStatus ( bool ) ,
386
+ Error ( MockErrorType ) ,
387
+ }
388
+
389
+ struct MockCacheClient {
390
+ pub response : MockCachingResponse ,
391
+ }
392
+
393
+ #[ async_trait]
394
+ impl CacheClient for MockCacheClient {
395
+ async fn get_artifact (
396
+ & self ,
397
+ _hash : & str ,
398
+ _token : & str ,
399
+ _team_id : Option < & str > ,
400
+ _team_slug : Option < & str > ,
401
+ _method : Method ,
402
+ ) -> Result < Option < Response > , turborepo_api_client:: Error > {
403
+ unimplemented ! ( )
404
+ }
405
+
406
+ async fn fetch_artifact (
407
+ & self ,
408
+ _hash : & str ,
409
+ _token : & str ,
410
+ _team_id : Option < & str > ,
411
+ _team_slug : Option < & str > ,
412
+ ) -> Result < Option < Response > , turborepo_api_client:: Error > {
413
+ unimplemented ! ( )
414
+ }
415
+
416
+ async fn put_artifact (
417
+ & self ,
418
+ _hash : & str ,
419
+ _artifact_body : & [ u8 ] ,
420
+ _duration : u64 ,
421
+ _tag : Option < & str > ,
422
+ _token : & str ,
423
+ _team_id : Option < & str > ,
424
+ _team_slug : Option < & str > ,
425
+ ) -> Result < ( ) , turborepo_api_client:: Error > {
426
+ unimplemented ! ( )
427
+ }
428
+
429
+ async fn artifact_exists (
430
+ & self ,
431
+ _hash : & str ,
432
+ _token : & str ,
433
+ _team_id : Option < & str > ,
434
+ _team_slug : Option < & str > ,
435
+ ) -> Result < Option < Response > , turborepo_api_client:: Error > {
436
+ unimplemented ! ( )
437
+ }
438
+
439
+ async fn get_caching_status (
440
+ & self ,
441
+ _token : & str ,
442
+ _team_id : Option < & str > ,
443
+ _team_slug : Option < & str > ,
444
+ ) -> Result < CachingStatusResponse , turborepo_api_client:: Error > {
445
+ match self . response {
446
+ MockCachingResponse :: CachingStatus ( status) => {
447
+ let caching_status = if status {
448
+ CachingStatus :: Enabled
449
+ } else {
450
+ CachingStatus :: Disabled
451
+ } ;
452
+ Ok ( CachingStatusResponse {
453
+ status : caching_status,
454
+ } )
455
+ }
456
+ MockCachingResponse :: Error ( MockErrorType :: Error ) => {
457
+ Err ( turborepo_api_client:: Error :: UnknownStatus {
458
+ code : "error" . to_string ( ) ,
459
+ message : "Error fetching caching status" . to_string ( ) ,
460
+ backtrace : Backtrace :: capture ( ) ,
461
+ } )
462
+ }
463
+ MockCachingResponse :: Error ( MockErrorType :: Forbidden ) => {
464
+ Err ( turborepo_api_client:: Error :: UnknownStatus {
465
+ code : "forbidden" . to_string ( ) ,
466
+ message : "Forbidden from accessing cache" . to_string ( ) ,
467
+ backtrace : Backtrace :: capture ( ) ,
468
+ } )
469
+ }
470
+ }
471
+ }
472
+ }
473
+
474
+ #[ tokio:: test]
475
+ async fn test_has_cache_access_granted ( ) {
476
+ let mock = MockCacheClient {
477
+ response : MockCachingResponse :: CachingStatus ( true ) ,
478
+ } ;
479
+
480
+ let token = Token :: Existing ( "existing_token" . to_string ( ) ) ;
481
+ let team_info = Some ( TeamInfo {
482
+ id : "team_id" ,
483
+ slug : "team_slug" ,
484
+ } ) ;
485
+
486
+ let result = token. has_cache_access ( & mock, team_info) . await ;
487
+ assert ! ( result. is_ok( ) ) ;
488
+ assert ! ( result. unwrap( ) ) ;
489
+ }
490
+
491
+ #[ tokio:: test]
492
+ async fn test_cache_access_denied ( ) {
493
+ let mock = MockCacheClient {
494
+ response : MockCachingResponse :: Error ( MockErrorType :: Forbidden ) ,
495
+ } ;
496
+
497
+ let token = Token :: Existing ( "existing_token" . to_string ( ) ) ;
498
+ let team_info = Some ( TeamInfo {
499
+ id : "team_id" ,
500
+ slug : "team_slug" ,
501
+ } ) ;
502
+
503
+ let result = token. has_cache_access ( & mock, team_info) . await ;
504
+ assert ! ( result. is_ok( ) ) ;
505
+ assert ! ( !result. unwrap( ) ) ;
506
+ }
507
+
508
+ #[ tokio:: test]
509
+ async fn test_caching_status_errored ( ) {
510
+ let mock = MockCacheClient {
511
+ response : MockCachingResponse :: Error ( MockErrorType :: Error ) ,
512
+ } ;
513
+
514
+ let token = Token :: Existing ( "existing_token" . to_string ( ) ) ;
515
+ let team_info = Some ( TeamInfo {
516
+ id : "team_id" ,
517
+ slug : "team_slug" ,
518
+ } ) ;
519
+
520
+ let result = token. has_cache_access ( & mock, team_info) . await ;
521
+ assert ! ( result. is_err( ) ) ;
522
+ assert ! ( matches!( result. unwrap_err( ) , Error :: APIError ( _) ) ) ;
523
+ }
375
524
}
0 commit comments