summaryrefslogtreecommitdiffstats
path: root/src/server/routes/users/get_user.rs
blob: 4079731c534742ca504c1c3a4d4075eb8a41cc75 (plain)
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
use activitypub_federation::{
    axum::json::FederationJson, config::Data, protocol::context::WithContext, traits::Object,
};
use axum::{debug_handler, extract::Path, http::StatusCode, response::IntoResponse};
use tracing::trace;

use crate::{error::AppError, state::AppHandle};

#[debug_handler]
pub async fn http_get_user(
    Path(name): Path<String>,
    data: Data<AppHandle>,
) -> Result<impl IntoResponse, AppError> {
    if let Some(a) = read_user(&name, &data).await? {
        let json_user = a.into_json(&data).await?;
        Ok((
            StatusCode::OK,
            FederationJson(WithContext::new_default(json_user)),
        )
            .into_response())
    } else {
        Ok((StatusCode::NOT_FOUND, "").into_response())
    }
}

pub async fn read_user(
    name: &str,
    data: &Data<AppHandle>,
) -> Result<Option<crate::entity::user::User>, AppError> {
    trace!(username = name, "getting user");
    let read = sqlx::query_as!(
        crate::entity::user::DbUser,
        "select * from account where username = $1 and local = $2",
        name,
        true
    )
    .fetch_optional(&data.services.postgres)
    .await?;

    let user = read.into_iter().find(|value| value.username.eq(&name));
    let user = match user {
        Some(user) => Some(crate::entity::user::User::try_from(user)?),
        None => None,
    };
    Ok(user)
}

#[cfg(test)]
mod tests {
    use axum::{
        body::Body,
        http::{Request, StatusCode},
    };
    use sqlx::PgPool;
    use stack_up::Services;
    use tower::ServiceExt;

    use crate::{
        server::{self, test_config},
        state::AppState,
    };

    #[sqlx::test]
    async fn get_user(pool: PgPool) {
        let services = Services { postgres: pool };
        let state = AppState::new(services, &test_config()).await.unwrap();
        let app = server::router(state);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/users/sellershut")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
    }

    #[sqlx::test]
    async fn get_user_not_found(pool: PgPool) {
        let services = Services { postgres: pool };
        let state = AppState::new(services, &test_config()).await.unwrap();
        let app = server::router(state);

        let response = app
            .oneshot(
                Request::builder()
                    .uri("/users/selut")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }
}