httpd: Fix more routes that return the wrong code

These routes should return 404 if the entity was not found.
This commit is contained in:
cloudhead 2023-08-09 16:59:19 +02:00
parent fae518bca2
commit 8060181b9f
No known key found for this signature in database
5 changed files with 87 additions and 25 deletions

View File

@ -93,11 +93,19 @@ impl IntoResponse for Error {
}
Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
Error::Crypto(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
Error::Surf(radicle_surf::Error::Git(e)) if radicle::git::is_not_found_err(&e) => {
(StatusCode::NOT_FOUND, None)
}
Error::Surf(radicle_surf::Error::Directory(
radicle_surf::fs::error::Directory::PathNotFound(_),
)) => (StatusCode::NOT_FOUND, None),
Error::Git2(e) if radicle::git::is_not_found_err(&e) => (StatusCode::NOT_FOUND, None),
Error::Git2(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
Some(e.message().to_owned()),
),
Error::Storage(err) if err.is_not_found() => (StatusCode::NOT_FOUND, None),
Error::StorageRef(err) if err.is_not_found() => (StatusCode::NOT_FOUND, None),
Error::BadRequest(msg) => (StatusCode::BAD_REQUEST, Some(msg)),
other => {
tracing::error!("Error: {message}");

View File

@ -1298,6 +1298,19 @@ mod routes {
);
}
#[tokio::test]
async fn test_projects_commits_not_found() {
let tmp = tempfile::tempdir().unwrap();
let app = super::router(seed(tmp.path()));
let response = get(
&app,
format!("/projects/{RID}/commits/ffffffffffffffffffffffffffffffffffffffff"),
)
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_projects_tree() {
let tmp = tempfile::tempdir().unwrap();
@ -1389,6 +1402,21 @@ mod routes {
);
}
#[tokio::test]
async fn test_projects_tree_not_found() {
let tmp = tempfile::tempdir().unwrap();
let app = super::router(seed(tmp.path()));
let response = get(
&app,
format!("/projects/{RID}/tree/ffffffffffffffffffffffffffffffffffffffff"),
)
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let response = get(&app, format!("/projects/{RID}/tree/{HEAD}/unknown")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_projects_remotes_root() {
let tmp = tempfile::tempdir().unwrap();
@ -1433,6 +1461,19 @@ mod routes {
);
}
#[tokio::test]
async fn test_projects_remotes_not_found() {
let tmp = tempfile::tempdir().unwrap();
let app = super::router(seed(tmp.path()));
let response = get(
&app,
format!("/projects/{RID}/remotes/z6MksFqXN3Yhqk8pTJdUGLwATkRfQvwZXPqR2qMEhbS9wzpT"),
)
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_projects_blob() {
let tmp = tempfile::tempdir().unwrap();
@ -1468,6 +1509,15 @@ mod routes {
);
}
#[tokio::test]
async fn test_projects_blob_not_found() {
let tmp = tempfile::tempdir().unwrap();
let app = super::router(seed(tmp.path()));
let response = get(&app, format!("/projects/{RID}/blob/{HEAD}/unknown")).await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_projects_readme() {
let tmp = tempfile::tempdir().unwrap();

View File

@ -121,6 +121,31 @@ fn router(options: Options, profile: Profile) -> anyhow::Result<Router> {
Ok(app)
}
pub mod logger {
use tracing::dispatcher::Dispatch;
pub fn init() -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
tracing::dispatcher::set_global_default(Dispatch::new(subscriber()))
}
#[cfg(feature = "logfmt")]
pub fn subscriber() -> impl tracing::Subscriber {
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::EnvFilter;
tracing_subscriber::Registry::default()
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
.with(tracing_logfmt::layer())
}
#[cfg(not(feature = "logfmt"))]
pub fn subscriber() -> impl tracing::Subscriber {
tracing_subscriber::FmtSubscriber::builder()
.with_target(false)
.finish()
}
}
#[cfg(test)]
mod routes {
use std::collections::HashMap;

View File

@ -3,36 +3,13 @@ use std::{collections::HashMap, process};
use radicle::prelude::Id;
use radicle_httpd as httpd;
use tracing::dispatcher::Dispatch;
#[cfg(feature = "logfmt")]
mod logger {
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::EnvFilter;
pub fn subscriber() -> impl tracing::Subscriber {
tracing_subscriber::Registry::default()
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
.with(tracing_logfmt::layer())
}
}
#[cfg(not(feature = "logfmt"))]
mod logger {
pub fn subscriber() -> impl tracing::Subscriber {
tracing_subscriber::FmtSubscriber::builder()
.with_target(false)
.finish()
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let options = parse_options()?;
tracing::dispatcher::set_global_default(Dispatch::new(logger::subscriber()))
.expect("Global logger hasn't already been set");
// SAFETY: The logger is only initialized once.
httpd::logger::init().unwrap();
tracing::info!("version {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_HEAD"));
match httpd::run(options).await {

View File

@ -72,6 +72,8 @@ pub fn seed(dir: &Path) -> Context {
let profile = profile(home.as_path(), [0xff; 32]);
let signer = Box::new(MockSigner::from_seed([0xff; 32]));
crate::logger::init().ok();
seed_with_signer(dir, profile, &signer)
}