From 97377a244248d413a4f123519334ce1d7c837826 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Tue, 10 Sep 2019 22:45:32 +0200 Subject: [PATCH] Implement a few tests --- backend/src/main.rs | 3 ++ backend/src/test/bitbucket.rs | 70 ++++++++++++++++++++++++++++++++++ backend/src/test/github.rs | 71 +++++++++++++++++++++++++++++++++++ backend/src/test/gitlab.rs | 71 +++++++++++++++++++++++++++++++++++ backend/src/test/mod.rs | 3 ++ 5 files changed, 218 insertions(+) create mode 100644 backend/src/test/bitbucket.rs create mode 100644 backend/src/test/github.rs create mode 100644 backend/src/test/gitlab.rs create mode 100644 backend/src/test/mod.rs diff --git a/backend/src/main.rs b/backend/src/main.rs index 6b202ba..5cf0ca9 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -16,6 +16,9 @@ mod error; mod service; mod statics; +#[cfg(test)] +mod test; + use crate::{ cdn::Cloudflare, data::{FilePath, State}, diff --git a/backend/src/test/bitbucket.rs b/backend/src/test/bitbucket.rs new file mode 100644 index 0000000..c8da5a0 --- /dev/null +++ b/backend/src/test/bitbucket.rs @@ -0,0 +1,70 @@ +use crate::{data::State, proxy_file, redirect, service::Bitbucket, REDIRECT_AGE}; +use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App}; +use awc::Client; +use std::sync::{Arc, RwLock}; +use time_cache::Cache; + +#[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::), + ), + ); + + 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()); +} + +#[test] +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::), + ), + ); + + 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()); +} + +#[test] +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::), + ), + ); + + 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()); +} diff --git a/backend/src/test/github.rs b/backend/src/test/github.rs new file mode 100644 index 0000000..bcf6382 --- /dev/null +++ b/backend/src/test/github.rs @@ -0,0 +1,71 @@ +use crate::{data::State, proxy_file, redirect, service::Github, REDIRECT_AGE}; +use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App}; +use awc::Client; +use std::sync::{Arc, RwLock}; +use time_cache::Cache; + +#[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( + "/github/{user}/{repo}/{commit}/{file:.*}", + web::get().to_async(redirect::), + ), + ); + + 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()); +} + +#[test] +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( + "/github/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), + ), + ); + // github + let req = test::TestRequest::with_uri( + "/github/vbrandl/yagcdn/f1b35e7c05b952be6de559051d7daad2ecf05369/Cargo.toml.invalid", + ) + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + + assert_eq!(StatusCode::NOT_FOUND, resp.status()); +} + +#[test] +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( + "/github/{user}/{repo}/{commit:[0-9a-fA-F]{40}}/{file:.*}", + web::get().to_async(proxy_file::), + ), + ); + // github + let req = test::TestRequest::with_uri( + "/github/vbrandl/yagcdn/f1b35e7c05b952be6de559051d7daad2ecf05369/backend/Cargo.toml", + ) + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + + assert_eq!(StatusCode::OK, resp.status()); +} diff --git a/backend/src/test/gitlab.rs b/backend/src/test/gitlab.rs new file mode 100644 index 0000000..a5a53d0 --- /dev/null +++ b/backend/src/test/gitlab.rs @@ -0,0 +1,71 @@ +use crate::{data::State, proxy_file, redirect, service::GitLab, REDIRECT_AGE}; +use actix_web::{dev::Service, http::StatusCode, middleware, test, web, App}; +use awc::Client; +use std::sync::{Arc, RwLock}; +use time_cache::Cache; + +#[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::), + ), + ); + + 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()); +} + +#[test] +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::), + ), + ); + + let req = test::TestRequest::with_uri( + "/gitlab/vbrandl/hoc/1223d429db877e46653260b15aa2bbd326bcd495/Cargo.toml.invalid", + ) + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + + assert_eq!(StatusCode::NOT_FOUND, resp.status()); +} + +#[test] +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::), + ), + ); + + let req = test::TestRequest::with_uri( + "/gitlab/vbrandl/hoc/1223d429db877e46653260b15aa2bbd326bcd495/Cargo.toml", + ) + .to_request(); + let resp = test::block_fn(|| app.call(req)).unwrap(); + + assert_eq!(StatusCode::OK, resp.status()); +} diff --git a/backend/src/test/mod.rs b/backend/src/test/mod.rs new file mode 100644 index 0000000..37005aa --- /dev/null +++ b/backend/src/test/mod.rs @@ -0,0 +1,3 @@ +mod bitbucket; +mod github; +mod gitlab;