|
| 1 | +use eyre::Result; |
| 2 | +use liwan::app::models::UserRole; |
| 3 | +use poem::http::{header, status::StatusCode}; |
| 4 | +use serde_json::json; |
| 5 | + |
1 | 6 | mod common; |
2 | 7 |
|
3 | 8 | #[tokio::test] |
4 | | -async fn it_adds_two() { |
| 9 | +async fn test_login() -> Result<()> { |
| 10 | + let app = common::app(); |
| 11 | + let (tx, _rx) = common::events(); |
| 12 | + let router = common::router(app.clone(), tx); |
| 13 | + let client = poem::test::TestClient::new(router); |
| 14 | + |
| 15 | + app.users.create("test", "test", UserRole::User, &[])?; |
| 16 | + |
| 17 | + // login |
| 18 | + let login = &json!({ "username": "test", "password": "test" }); |
| 19 | + let res = client.post("/api/dashboard/auth/login").body_json(login).send().await; |
| 20 | + |
| 21 | + res.assert_status_is_ok(); |
| 22 | + let cookies = common::cookies(&res); |
| 23 | + |
| 24 | + // user info |
| 25 | + let res = client.get("/api/dashboard/auth/me").header(header::COOKIE, common::cookie_header(&cookies)).send().await; |
| 26 | + res.assert_status_is_ok(); |
| 27 | + res.assert_json(json!({ "username": "test", "role": "user" })).await; |
| 28 | + |
| 29 | + // logout |
| 30 | + let res = |
| 31 | + client.post("/api/dashboard/auth/logout").header(header::COOKIE, common::cookie_header(&cookies)).send().await; |
| 32 | + res.assert_status_is_ok(); |
| 33 | + |
| 34 | + // test that the user is logged out |
| 35 | + let res = client.get("/api/dashboard/auth/me").header(header::COOKIE, common::cookie_header(&cookies)).send().await; |
| 36 | + |
| 37 | + res.assert_status(StatusCode::UNAUTHORIZED); |
| 38 | + Ok(()) |
| 39 | +} |
| 40 | + |
| 41 | +#[tokio::test] |
| 42 | +async fn test_setup() -> Result<()> { |
5 | 43 | let app = common::app(); |
6 | 44 | let (tx, _rx) = common::events(); |
7 | | - let router = common::router(app, tx); |
| 45 | + let router = common::router(app.clone(), tx); |
8 | 46 | let client = poem::test::TestClient::new(router); |
9 | 47 |
|
10 | | - client.get("/").send().await; |
| 48 | + let token = app.onboarding.token().unwrap().expect("onboarding should exist"); |
| 49 | + |
| 50 | + // Invalid token should return 401 |
| 51 | + let setup = &json!({ "token": "invalid_token", "username": "admin2", "password": "admin2" }); |
| 52 | + let res = client.post("/api/dashboard/auth/setup").body_json(setup).send().await; |
| 53 | + res.assert_status(StatusCode::UNAUTHORIZED); |
| 54 | + |
| 55 | + // Valid token should return 200 |
| 56 | + let setup = &json!({ "token": token, "username": "admin", "password": "admin" }); |
| 57 | + let res = client.post("/api/dashboard/auth/setup").body_json(setup).send().await; |
| 58 | + res.assert_status_is_ok(); |
| 59 | + |
| 60 | + // Check that the user is created |
| 61 | + let login = &json!({ "username": "admin", "password": "admin" }); |
| 62 | + let res = client.post("/api/dashboard/auth/login").body_json(login).send().await; |
| 63 | + res.assert_status_is_ok(); |
| 64 | + |
| 65 | + // Check that the onboarding is cleared |
| 66 | + assert_eq!(app.onboarding.token().unwrap(), None, "onboarding should be cleared"); |
| 67 | + let setup = &json!({ "token": token, "username": "admin", "password": "admin" }); |
| 68 | + let res = client.post("/api/dashboard/auth/setup").body_json(setup).send().await; |
| 69 | + res.assert_status(StatusCode::UNAUTHORIZED); |
| 70 | + |
| 71 | + let setup = &json!({ "token": token, "username": "admin2", "password": "admin2" }); |
| 72 | + let res = client.post("/api/dashboard/auth/setup").body_json(setup).send().await; |
| 73 | + res.assert_status(StatusCode::UNAUTHORIZED); |
| 74 | + |
| 75 | + Ok(()) |
11 | 76 | } |
0 commit comments