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:
parent
fae518bca2
commit
8060181b9f
|
|
@ -93,11 +93,19 @@ impl IntoResponse for Error {
|
||||||
}
|
}
|
||||||
Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
|
Error::Auth(msg) => (StatusCode::BAD_REQUEST, Some(msg.to_string())),
|
||||||
Error::Crypto(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) => (
|
Error::Git2(e) => (
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Some(e.message().to_owned()),
|
Some(e.message().to_owned()),
|
||||||
),
|
),
|
||||||
Error::Storage(err) if err.is_not_found() => (StatusCode::NOT_FOUND, None),
|
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)),
|
Error::BadRequest(msg) => (StatusCode::BAD_REQUEST, Some(msg)),
|
||||||
other => {
|
other => {
|
||||||
tracing::error!("Error: {message}");
|
tracing::error!("Error: {message}");
|
||||||
|
|
|
||||||
|
|
@ -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]
|
#[tokio::test]
|
||||||
async fn test_projects_tree() {
|
async fn test_projects_tree() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
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]
|
#[tokio::test]
|
||||||
async fn test_projects_remotes_root() {
|
async fn test_projects_remotes_root() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
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]
|
#[tokio::test]
|
||||||
async fn test_projects_blob() {
|
async fn test_projects_blob() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
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]
|
#[tokio::test]
|
||||||
async fn test_projects_readme() {
|
async fn test_projects_readme() {
|
||||||
let tmp = tempfile::tempdir().unwrap();
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,31 @@ fn router(options: Options, profile: Profile) -> anyhow::Result<Router> {
|
||||||
Ok(app)
|
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)]
|
#[cfg(test)]
|
||||||
mod routes {
|
mod routes {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
|
||||||
|
|
@ -3,36 +3,13 @@ use std::{collections::HashMap, process};
|
||||||
|
|
||||||
use radicle::prelude::Id;
|
use radicle::prelude::Id;
|
||||||
use radicle_httpd as httpd;
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
let options = parse_options()?;
|
let options = parse_options()?;
|
||||||
|
|
||||||
tracing::dispatcher::set_global_default(Dispatch::new(logger::subscriber()))
|
// SAFETY: The logger is only initialized once.
|
||||||
.expect("Global logger hasn't already been set");
|
httpd::logger::init().unwrap();
|
||||||
|
|
||||||
tracing::info!("version {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_HEAD"));
|
tracing::info!("version {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_HEAD"));
|
||||||
|
|
||||||
match httpd::run(options).await {
|
match httpd::run(options).await {
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,8 @@ pub fn seed(dir: &Path) -> Context {
|
||||||
let profile = profile(home.as_path(), [0xff; 32]);
|
let profile = profile(home.as_path(), [0xff; 32]);
|
||||||
let signer = Box::new(MockSigner::from_seed([0xff; 32]));
|
let signer = Box::new(MockSigner::from_seed([0xff; 32]));
|
||||||
|
|
||||||
|
crate::logger::init().ok();
|
||||||
|
|
||||||
seed_with_signer(dir, profile, &signer)
|
seed_with_signer(dir, profile, &signer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue