|
| 1 | +use super::cache::CachePolicy; |
| 2 | +use crate::{ |
| 3 | + db::Pool, |
| 4 | + utils::spawn_blocking, |
| 5 | + web::{error::AxumResult, match_version_axum}, |
| 6 | +}; |
| 7 | +use axum::{ |
| 8 | + extract::{Extension, Path}, |
| 9 | + http::header::ACCESS_CONTROL_ALLOW_ORIGIN, |
| 10 | + response::IntoResponse, |
| 11 | + Json, |
| 12 | +}; |
| 13 | + |
| 14 | +pub(crate) async fn status_handler( |
| 15 | + Path((name, req_version)): Path<(String, String)>, |
| 16 | + Extension(pool): Extension<Pool>, |
| 17 | +) -> AxumResult<impl IntoResponse> { |
| 18 | + let (_, id) = match_version_axum(&pool, &name, Some(&req_version)) |
| 19 | + .await? |
| 20 | + .assume_exact()? |
| 21 | + .assume_exact()?; |
| 22 | + |
| 23 | + let rustdoc_status: bool = spawn_blocking({ |
| 24 | + move || { |
| 25 | + Ok(pool |
| 26 | + .get()? |
| 27 | + .query_one( |
| 28 | + "SELECT releases.rustdoc_status |
| 29 | + FROM releases |
| 30 | + WHERE releases.id = $1 |
| 31 | + ", |
| 32 | + &[&id], |
| 33 | + )? |
| 34 | + .get("rustdoc_status")) |
| 35 | + } |
| 36 | + }) |
| 37 | + .await?; |
| 38 | + |
| 39 | + Ok(( |
| 40 | + Extension(CachePolicy::NoStoreMustRevalidate), |
| 41 | + [(ACCESS_CONTROL_ALLOW_ORIGIN, "*")], |
| 42 | + Json(serde_json::json!({ "doc_status": rustdoc_status })), |
| 43 | + )) |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use crate::{ |
| 49 | + test::{assert_cache_control, wrapper}, |
| 50 | + web::cache::CachePolicy, |
| 51 | + }; |
| 52 | + use reqwest::StatusCode; |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn success() { |
| 56 | + wrapper(|env| { |
| 57 | + env.fake_release().name("foo").version("0.1.0").create()?; |
| 58 | + |
| 59 | + let response = env.frontend().get("/crate/foo/0.1.0/status.json").send()?; |
| 60 | + assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config()); |
| 61 | + assert_eq!(response.headers()["access-control-allow-origin"], "*"); |
| 62 | + let value: serde_json::Value = serde_json::from_str(&response.text()?)?; |
| 63 | + |
| 64 | + assert_eq!(value, serde_json::json!({"doc_status": true})); |
| 65 | + |
| 66 | + Ok(()) |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn failure() { |
| 72 | + wrapper(|env| { |
| 73 | + env.fake_release() |
| 74 | + .name("foo") |
| 75 | + .version("0.1.0") |
| 76 | + .build_result_failed() |
| 77 | + .create()?; |
| 78 | + |
| 79 | + let response = env.frontend().get("/crate/foo/0.1.0/status.json").send()?; |
| 80 | + assert_cache_control(&response, CachePolicy::NoStoreMustRevalidate, &env.config()); |
| 81 | + assert_eq!(response.headers()["access-control-allow-origin"], "*"); |
| 82 | + let value: serde_json::Value = serde_json::from_str(&response.text()?)?; |
| 83 | + |
| 84 | + assert_eq!(value, serde_json::json!({"doc_status": false})); |
| 85 | + |
| 86 | + Ok(()) |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn crate_version_not_found() { |
| 92 | + wrapper(|env| { |
| 93 | + env.fake_release().name("foo").version("0.1.0").create()?; |
| 94 | + |
| 95 | + let response = env.frontend().get("/crate/foo/0.2.0/status.json").send()?; |
| 96 | + assert!(response |
| 97 | + .url() |
| 98 | + .as_str() |
| 99 | + .ends_with("/crate/foo/0.2.0/status.json")); |
| 100 | + assert_eq!(response.status(), StatusCode::NOT_FOUND); |
| 101 | + Ok(()) |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn invalid_semver() { |
| 107 | + wrapper(|env| { |
| 108 | + env.fake_release().name("foo").version("0.1.0").create()?; |
| 109 | + |
| 110 | + let response = env.frontend().get("/crate/foo/0,1,0/status.json").send()?; |
| 111 | + assert!(response |
| 112 | + .url() |
| 113 | + .as_str() |
| 114 | + .ends_with("/crate/foo/0,1,0/status.json")); |
| 115 | + assert_eq!(response.status(), StatusCode::NOT_FOUND); |
| 116 | + Ok(()) |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + /// We only support asking for the status of exact versions |
| 121 | + #[test] |
| 122 | + fn no_semver() { |
| 123 | + wrapper(|env| { |
| 124 | + env.fake_release().name("foo").version("0.1.0").create()?; |
| 125 | + |
| 126 | + let response = env.frontend().get("/crate/foo/latest/status.json").send()?; |
| 127 | + assert!(response |
| 128 | + .url() |
| 129 | + .as_str() |
| 130 | + .ends_with("/crate/foo/latest/status.json")); |
| 131 | + assert_eq!(response.status(), StatusCode::NOT_FOUND); |
| 132 | + |
| 133 | + let response = env.frontend().get("/crate/foo/0.1/status.json").send()?; |
| 134 | + assert!(response |
| 135 | + .url() |
| 136 | + .as_str() |
| 137 | + .ends_with("/crate/foo/0.1/status.json")); |
| 138 | + assert_eq!(response.status(), StatusCode::NOT_FOUND); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | + }); |
| 142 | + } |
| 143 | +} |
0 commit comments