Spaces:
Paused
Paused
File size: 8,211 Bytes
dd7a3f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | mod common;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
fn json_body(json: serde_json::Value) -> Body {
Body::from(json.to_string())
}
#[tokio::test]
async fn setup_creates_admin_and_returns_token() {
let (app, _state) = common::setup_test_app().await;
let req = Request::builder()
.method("POST")
.uri("/api/auth/setup")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "admin",
"password": "testpassword123"
})))
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let set_cookie = common::extract_set_cookie(res.headers());
assert!(set_cookie.is_some(), "should set refresh cookie");
let body = res.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert!(json["token"].is_string());
}
#[tokio::test]
async fn setup_fails_when_admin_exists() {
let (app, _state) = common::setup_test_app().await;
// First setup succeeds
common::setup_admin(&app).await;
// Second setup fails
let req = Request::builder()
.method("POST")
.uri("/api/auth/setup")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "admin2",
"password": "testpassword123"
})))
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::FORBIDDEN);
}
#[tokio::test]
async fn login_with_valid_credentials() {
let (app, _state) = common::setup_test_app().await;
common::setup_admin(&app).await;
let req = Request::builder()
.method("POST")
.uri("/api/auth/login")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "admin",
"password": "testpassword123"
})))
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let body = res.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert!(json["token"].is_string());
}
#[tokio::test]
async fn login_with_wrong_password() {
let (app, _state) = common::setup_test_app().await;
common::setup_admin(&app).await;
let req = Request::builder()
.method("POST")
.uri("/api/auth/login")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "admin",
"password": "wrongpassword"
})))
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn login_with_unknown_user() {
let (app, _state) = common::setup_test_app().await;
common::setup_admin(&app).await;
let req = Request::builder()
.method("POST")
.uri("/api/auth/login")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "nonexistent",
"password": "testpassword123"
})))
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn refresh_with_valid_cookie() {
let (app, _state) = common::setup_test_app().await;
// Setup to get refresh token
let req = Request::builder()
.method("POST")
.uri("/api/auth/setup")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "admin",
"password": "testpassword123"
})))
.unwrap();
let res = app.clone().oneshot(req).await.unwrap();
let set_cookie = common::extract_set_cookie(res.headers()).unwrap();
let refresh_token = common::extract_refresh_token(&set_cookie).unwrap();
// Use refresh token
let req = Request::builder()
.method("POST")
.uri("/api/auth/refresh")
.header("cookie", format!("refresh_token={refresh_token}"))
.body(Body::empty())
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
// Should get new tokens
let new_set_cookie = common::extract_set_cookie(res.headers());
assert!(new_set_cookie.is_some(), "should set new refresh cookie");
let body = res.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert!(json["token"].is_string());
}
#[tokio::test]
async fn refresh_token_rotation_invalidates_old() {
let (app, _state) = common::setup_test_app().await;
// Setup to get refresh token
let req = Request::builder()
.method("POST")
.uri("/api/auth/setup")
.header("content-type", "application/json")
.body(json_body(serde_json::json!({
"username": "admin",
"password": "testpassword123"
})))
.unwrap();
let res = app.clone().oneshot(req).await.unwrap();
let set_cookie = common::extract_set_cookie(res.headers()).unwrap();
let old_refresh = common::extract_refresh_token(&set_cookie).unwrap();
// Use refresh token (first time — should work)
let req = Request::builder()
.method("POST")
.uri("/api/auth/refresh")
.header("cookie", format!("refresh_token={old_refresh}"))
.body(Body::empty())
.unwrap();
let res = app.clone().oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
// Use same old refresh token again — should fail (rotated)
let req = Request::builder()
.method("POST")
.uri("/api/auth/refresh")
.header("cookie", format!("refresh_token={old_refresh}"))
.body(Body::empty())
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn refresh_without_cookie_returns_401() {
let (app, _state) = common::setup_test_app().await;
let req = Request::builder()
.method("POST")
.uri("/api/auth/refresh")
.body(Body::empty())
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
}
#[tokio::test]
async fn logout_clears_tokens() {
let (app, _state) = common::setup_test_app().await;
let token = common::setup_admin(&app).await;
let req = Request::builder()
.method("POST")
.uri("/api/auth/logout")
.header("authorization", format!("Bearer {token}"))
.body(Body::empty())
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let set_cookie = common::extract_set_cookie(res.headers()).unwrap();
assert!(set_cookie.contains("Max-Age=0"), "should clear cookie");
}
#[tokio::test]
async fn auth_status_setup_required_true() {
let (app, _state) = common::setup_test_app().await;
let req = Request::builder()
.uri("/api/auth/status")
.body(Body::empty())
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let body = res.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["setup_required"], true);
}
#[tokio::test]
async fn auth_status_setup_required_false() {
let (app, _state) = common::setup_test_app().await;
common::setup_admin(&app).await;
let req = Request::builder()
.uri("/api/auth/status")
.body(Body::empty())
.unwrap();
let res = app.oneshot(req).await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
let body = res.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["setup_required"], false);
}
|