Lines
100 %
Functions
Branches
use super::*;
use crate::model::User;
use axum::http::StatusCode;
fn user() -> User {
User {
id: uuid::Uuid::new_v4(),
name: "Test".to_string(),
email: "test@example.com".to_string(),
role: "user".to_string(),
photo: String::new(),
verified: true,
created_at: None,
updated_at: None,
}
fn config() -> Config {
Config {
site_url: String::new(),
redis_url: String::new(),
access_token_max_age: 15,
refresh_token_max_age: 60,
ssh: crate::config::SshConnectInfo {
hostname: String::new(),
port: 0,
host_fingerprint: String::new(),
},
fn auth(refreshed: bool) -> JWTAuthMiddleware {
JWTAuthMiddleware {
user: user(),
access_token_uuid: uuid::Uuid::new_v4(),
refreshed,
#[test]
fn a_wrong_password_is_a_bad_request() {
let err = password_change_error(
uuid::Uuid::new_v4(),
server::command::CmdError::Args("wrong".to_string()),
);
assert_eq!(err.status, StatusCode::BAD_REQUEST);
assert_eq!(err.message, "Invalid email or password");
fn a_storage_failure_is_not_reported_as_a_wrong_password() {
server::command::CmdError::Config(server::config::ConfigError::NoConfig("k".into())),
assert_eq!(err.status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(err.message, "Internal error");
#[tokio::test]
async fn mismatched_confirmation_is_refused_before_anything_is_written() {
let result = change_password_handler(
Extension(auth(false)),
Json(ChangePasswordSchema {
current_password: "old".to_string(),
new_password: "one".to_string(),
confirm_password: "two".to_string(),
}),
)
.await;
let err = result.err().expect("mismatch refused");
assert_eq!(err.message, "The new passwords do not match");
async fn refresh_without_a_cookie_is_unauthorized() {
let err = own_refresh_token(&CookieJar::default(), uuid::Uuid::new_v4())
.await
.expect_err("no cookie");
assert_eq!(err.status, StatusCode::UNAUTHORIZED);
fn session_cookies_hide_the_tokens_from_scripts() {
let [access, refresh] = session_cookies(&config(), "a".to_string(), "r".to_string());
assert!(access.starts_with("access_token=a") && access.contains("HttpOnly"));
assert!(refresh.starts_with("refresh_token=r") && refresh.contains("HttpOnly"));
fn ending_a_session_expires_the_cookies_and_redirects() {
let headers = session_end_headers("/login");
assert_eq!(headers.get("HX-Redirect").expect("redirect"), "/login");
assert_eq!(headers.get_all(header::SET_COOKIE).iter().count(), 2);