Skip to content

Test login endpoint #8

@MrSheerluck

Description

@MrSheerluck

Test POST /api/auth/login returns 200 with correct password and 401 with wrong password.

before picking the issue, comment below so that i can assign this to you

Add this at the bottom of api/src/auth.rs:

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{Json, body::Body, http::Request, routing::post};
    use serde_json::json;
    use tower::ServiceExt;

    fn setup_env() {
        // Use a well-known hash for "password123"
        std::env::set_var(
            "ADMIN_PASSWORD_HASH",
            "$2b$12$LJ3m4ys3Lk0TSwHlOR7cY.VUK9E2J1OZ0R5qJ0fKj6F3bQ1cJ2qK",
        );
        std::env::set_var("JWT_SECRET", "test-secret");
    }

    #[tokio::test]
    async fn test_login_success() {
        setup_env();
        let jar = CookieJar::new();
        let body = Json(json!({"password": "password123"}));
        let (_, response) = login(jar, body).await.unwrap();
        assert_eq!(response.0.get("success"), Some(&json!(true)));
    }

    #[tokio::test]
    async fn test_login_wrong_password() {
        setup_env();
        let jar = CookieJar::new();
        let body = Json(json!({"password": "wrongpass"}));
        let result = login(jar, body).await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), StatusCode::UNAUTHORIZED);
    }
}

Running Tests

# Backend tests
cd api && cargo test

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions