diff --git a/backend/src/test/bitbucket.rs b/backend/src/test/bitbucket.rs index c8da5a0..d2a1884 100644 --- a/backend/src/test/bitbucket.rs +++ b/backend/src/test/bitbucket.rs @@ -1,25 +1,30 @@ -use crate::{data::State, proxy_file, redirect, service::Bitbucket, REDIRECT_AGE}; -use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App}; +use crate::{ + data::{Key, Service, State}, + proxy_file, purge_local_cache, redirect, + service::Bitbucket, + REDIRECT_AGE, +}; +use actix_web::{dev::Service as _, http::StatusCode, middleware, test, web, App}; use awc::Client; use std::sync::{Arc, RwLock}; -use time_cache::Cache; +use time_cache::{Cache, CacheResult}; #[test] fn requesting_branch_redirects() { let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); let mut app = test::init_service( App::new() - .data(Client::new()) - .data(state) - .wrap(middleware::NormalizePath) - .route( - "/bitbucket/{user}/{repo}/{commit}/{file:.*}", - web::get().to_async(redirect::), + .data(Client::new()) + .data(state) + .wrap(middleware::NormalizePath) + .route( + "/bitbucket/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(redirect::), ), - ); + ); - let req = test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/master/README.md") - .to_request(); + let req = + test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/master/README.md").to_request(); let resp = test::block_fn(|| app.call(req)).unwrap(); assert_eq!(StatusCode::SEE_OTHER, resp.status()); @@ -30,18 +35,19 @@ fn invalid_file_404() { let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); let mut app = test::init_service( App::new() - .data(Client::new()) - .data(state) - .wrap(middleware::NormalizePath) - .route( - "/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", - web::get().to_async(proxy_file::), + .data(Client::new()) + .data(state) + .wrap(middleware::NormalizePath) + .route( + "/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ), - ); + ); - let req = - test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md.invalid") - .to_request(); + let req = test::TestRequest::with_uri( + "/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md.invalid", + ) + .to_request(); let resp = test::block_fn(|| app.call(req)).unwrap(); assert_eq!(StatusCode::NOT_FOUND, resp.status()); @@ -52,19 +58,68 @@ fn valid_file_200() { let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); let mut app = test::init_service( App::new() - .data(Client::new()) - .data(state) - .wrap(middleware::NormalizePath) - .route( - "/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", - web::get().to_async(proxy_file::), + .data(Client::new()) + .data(state) + .wrap(middleware::NormalizePath) + .route( + "/bitbucket/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ), - ); + ); - let req = - test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md") - .to_request(); + let req = test::TestRequest::with_uri( + "/bitbucket/vbrandl/vbrandl.net/369c392927a6d75f16c5dc38e2577276b94676bd/README.md", + ) + .to_request(); let resp = test::block_fn(|| app.call(req)).unwrap(); assert_eq!(StatusCode::OK, resp.status()); } + +#[test] +fn redirect_cache() { + let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); + let mut app = test::init_service( + App::new() + .data(Client::new()) + .data(Arc::clone(&state)) + .wrap(middleware::NormalizePath) + .route( + "/bitbucket/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(redirect::), + ) + .route( + "/bitbucket/{user}/{repo}/{commit}/{file:.*}", + web::delete().to_async(purge_local_cache::), + ), + ); + + let req = + test::TestRequest::with_uri("/bitbucket/vbrandl/vbrandl.net/master/README.md").to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + assert_eq!(StatusCode::SEE_OTHER, resp.status()); + + let key = Key::new( + Service::Bitbucket, + Arc::new("vbrandl".to_string()), + Arc::new("vbrandl.net".to_string()), + Arc::new("master".to_string()), + ); + { + let cache = state.read().unwrap(); + let res = cache.get(&key); + assert_ne!(CacheResult::Empty, res); + assert_ne!(CacheResult::Invalid, res); + } // release the lock + + let req = test::TestRequest::delete() + .uri("/bitbucket/vbrandl/vbrandl.net/master/README.md") + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + assert_eq!(StatusCode::OK, resp.status()); + + { + let cache = state.read().unwrap(); + assert_eq!(CacheResult::Empty, cache.get(&key)); + } // release the lock +} diff --git a/backend/src/test/github.rs b/backend/src/test/github.rs index bcf6382..1a6b8cd 100644 --- a/backend/src/test/github.rs +++ b/backend/src/test/github.rs @@ -1,8 +1,13 @@ -use crate::{data::State, proxy_file, redirect, service::Github, REDIRECT_AGE}; -use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App}; +use crate::{ + data::{Key, Service, State}, + proxy_file, purge_local_cache, redirect, + service::Github, + REDIRECT_AGE, +}; +use actix_web::{dev::Service as _, http::StatusCode, middleware, test, web, App}; use awc::Client; use std::sync::{Arc, RwLock}; -use time_cache::Cache; +use time_cache::{Cache, CacheResult}; #[test] fn requesting_branch_redirects() { @@ -37,7 +42,7 @@ fn invalid_file_404() { web::get().to_async(proxy_file::), ), ); - // github + let req = test::TestRequest::with_uri( "/github/vbrandl/yagcdn/f1b35e7c05b952be6de559051d7daad2ecf05369/Cargo.toml.invalid", ) @@ -69,3 +74,50 @@ fn valid_file_200() { assert_eq!(StatusCode::OK, resp.status()); } + +#[test] +fn redirect_cache() { + let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); + let mut app = test::init_service( + App::new() + .data(Client::new()) + .data(Arc::clone(&state)) + .wrap(middleware::NormalizePath) + .route( + "/github/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(redirect::), + ) + .route( + "/github/{user}/{repo}/{commit}/{file:.*}", + web::delete().to_async(purge_local_cache::), + ), + ); + + let req = test::TestRequest::with_uri("/github/vbrandl/yagcdn/master/Cargo.toml").to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + assert_eq!(StatusCode::SEE_OTHER, resp.status()); + + let key = Key::new( + Service::GitHub, + Arc::new("vbrandl".to_string()), + Arc::new("yagcdn".to_string()), + Arc::new("master".to_string()), + ); + { + let cache = state.read().unwrap(); + let res = cache.get(&key); + assert_ne!(CacheResult::Empty, res); + assert_ne!(CacheResult::Invalid, res); + } // release the lock + + let req = test::TestRequest::delete() + .uri("/github/vbrandl/yagcdn/master/Cargo.toml") + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + assert_eq!(StatusCode::OK, resp.status()); + + { + let cache = state.read().unwrap(); + assert_eq!(CacheResult::Empty, cache.get(&key)); + } // release the lock +} diff --git a/backend/src/test/gitlab.rs b/backend/src/test/gitlab.rs index a5a53d0..71d4815 100644 --- a/backend/src/test/gitlab.rs +++ b/backend/src/test/gitlab.rs @@ -1,22 +1,27 @@ -use crate::{data::State, proxy_file, redirect, service::GitLab, REDIRECT_AGE}; -use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App}; +use crate::{ + data::{Key, Service, State}, + proxy_file, purge_local_cache, redirect, + service::GitLab, + REDIRECT_AGE, +}; +use actix_web::{dev::Service as _, http::StatusCode, middleware, test, web, App}; use awc::Client; use std::sync::{Arc, RwLock}; -use time_cache::Cache; +use time_cache::{Cache, CacheResult}; #[test] fn requesting_branch_redirects() { let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); let mut app = test::init_service( App::new() - .data(Client::new()) - .data(state) - .wrap(middleware::NormalizePath) - .route( - "/gitlab/{user}/{repo}/{commit}/{file:.*}", - web::get().to_async(redirect::), + .data(Client::new()) + .data(state) + .wrap(middleware::NormalizePath) + .route( + "/gitlab/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(redirect::), ), - ); + ); let req = test::TestRequest::with_uri("/gitlab/vbrandl/hoc/master/Cargo.toml").to_request(); let resp = test::block_fn(|| app.call(req)).unwrap(); @@ -29,19 +34,19 @@ fn invalid_file_404() { let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); let mut app = test::init_service( App::new() - .data(Client::new()) - .data(state) - .wrap(middleware::NormalizePath) - .route( - "/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", - web::get().to_async(proxy_file::), + .data(Client::new()) + .data(state) + .wrap(middleware::NormalizePath) + .route( + "/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ), - ); + ); let req = test::TestRequest::with_uri( "/gitlab/vbrandl/hoc/1223d429db877e46653260b15aa2bbd326bcd495/Cargo.toml.invalid", - ) - .to_request(); + ) + .to_request(); let resp = test::block_fn(|| app.call(req)).unwrap(); assert_eq!(StatusCode::NOT_FOUND, resp.status()); @@ -52,20 +57,67 @@ fn valid_file_200() { let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); let mut app = test::init_service( App::new() - .data(Client::new()) - .data(state) - .wrap(middleware::NormalizePath) - .route( - "/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", - web::get().to_async(proxy_file::), + .data(Client::new()) + .data(state) + .wrap(middleware::NormalizePath) + .route( + "/gitlab/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), ), - ); + ); let req = test::TestRequest::with_uri( "/gitlab/vbrandl/hoc/1223d429db877e46653260b15aa2bbd326bcd495/Cargo.toml", - ) - .to_request(); + ) + .to_request(); let resp = test::block_fn(|| app.call(req)).unwrap(); assert_eq!(StatusCode::OK, resp.status()); } + +#[test] +fn redirect_cache() { + let state: State = Arc::new(RwLock::new(Cache::new(REDIRECT_AGE))); + let mut app = test::init_service( + App::new() + .data(Client::new()) + .data(Arc::clone(&state)) + .wrap(middleware::NormalizePath) + .route( + "/gitlab/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(redirect::), + ) + .route( + "/gitlab/{user}/{repo}/{commit}/{file:.*}", + web::delete().to_async(purge_local_cache::), + ), + ); + + let req = test::TestRequest::with_uri("/gitlab/vbrandl/hoc/master/Cargo.toml").to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + assert_eq!(StatusCode::SEE_OTHER, resp.status()); + + let key = Key::new( + Service::GitLab, + Arc::new("vbrandl".to_string()), + Arc::new("hoc".to_string()), + Arc::new("master".to_string()), + ); + { + let cache = state.read().unwrap(); + let res = cache.get(&key); + assert_ne!(CacheResult::Empty, res); + assert_ne!(CacheResult::Invalid, res); + } // release the lock + + let req = test::TestRequest::delete() + .uri("/gitlab/vbrandl/hoc/master/Cargo.toml") + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + assert_eq!(StatusCode::OK, resp.status()); + + { + let cache = state.read().unwrap(); + assert_eq!(CacheResult::Empty, cache.get(&key)); + } // release the lock +}