use axum::extract::{Json, State}; use super::post; use crate::{setup::app, test::fixtures}; #[tokio::test] async fn fresh_instance() { // Set up the environment let app = fixtures::scratch_app().await; // Call the endpoint let identity = fixtures::cookie::not_logged_in(); let (name, password) = fixtures::login::propose(); let request = post::Request { name: name.clone(), password: password.clone(), }; let (identity, Json(response)) = post::handler(State(app.clone()), fixtures::now(), identity, Json(request)) .await .expect("setup in a fresh app succeeds"); // Verify the response assert_eq!(name, response.name); // Verify that the issued token is valid let secret = identity .secret() .expect("newly-issued identity has a token secret"); let (_, login) = app .tokens() .validate(&secret, &fixtures::now()) .await .expect("newly-issued identity cookie is valid"); assert_eq!(response, login); // Verify that the given credentials can log in let (login, _) = app .tokens() .login(&name, &password, &fixtures::now()) .await .expect("credentials given on signup are valid"); assert_eq!(response, login); } #[tokio::test] async fn login_exists() { // Set up the environment let app = fixtures::scratch_app().await; fixtures::login::create(&app, &fixtures::now()).await; // Call the endpoint let identity = fixtures::cookie::not_logged_in(); let (name, password) = fixtures::login::propose(); let request = post::Request { name, password }; let post::Error(error) = post::handler(State(app.clone()), fixtures::now(), identity, Json(request)) .await .expect_err("setup in a populated app fails"); // Verify the response assert!(matches!(error, app::Error::SetupCompleted)); }