Skip to content

Commit 4b0415d

Browse files
authored
fix: add more caching status tests (#7574)
1 parent ce591fd commit 4b0415d

File tree

1 file changed

+150
-1
lines changed
  • crates/turborepo-auth/src

1 file changed

+150
-1
lines changed

crates/turborepo-auth/src/lib.rs

+150-1
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,13 @@ fn is_token_active(metadata: &ResponseTokenMetadata, current_time: u128) -> bool
258258

259259
#[cfg(test)]
260260
mod tests {
261+
use std::backtrace::Backtrace;
262+
263+
use async_trait::async_trait;
264+
use reqwest::{Method, Response};
261265
use tempfile::tempdir;
262266
use turbopath::AbsoluteSystemPathBuf;
263-
use turborepo_vercel_api::token::Scope;
267+
use turborepo_vercel_api::{token::Scope, CachingStatus, CachingStatusResponse};
264268

265269
use super::*;
266270

@@ -372,4 +376,149 @@ mod tests {
372376

373377
assert!(matches!(result, Err(Error::TokenNotFound)));
374378
}
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+
}
375524
}

0 commit comments

Comments
 (0)