feat: add user following and followers endpoints, update user profile response structure

This commit is contained in:
2025-09-06 19:43:46 +02:00
parent c7cb3f537d
commit 8552858c8c
9 changed files with 141 additions and 19 deletions

View File

@@ -31,7 +31,7 @@ async fn test_api_key_flow() {
let body = response.into_body().collect().await.unwrap().to_bytes();
let v: Value = serde_json::from_slice(&body).unwrap();
let plaintext_key = v["plaintext_key"]
let plaintext_key = v["plaintextKey"]
.as_str()
.expect("Plaintext key not found")
.to_string();

View File

@@ -60,7 +60,7 @@ async fn test_feed_and_user_thoughts() {
let body = response.into_body().collect().await.unwrap().to_bytes();
let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v["thoughts"].as_array().unwrap().len(), 1);
assert_eq!(v["thoughts"][0]["author_username"], "user1");
assert_eq!(v["thoughts"][0]["authorUsername"], "user1");
assert_eq!(v["thoughts"][0]["content"], "A thought from user1");
// 3. user1 follows user2
@@ -79,8 +79,8 @@ async fn test_feed_and_user_thoughts() {
let body = response.into_body().collect().await.unwrap().to_bytes();
let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v["thoughts"].as_array().unwrap().len(), 2);
assert_eq!(v["thoughts"][0]["author_username"], "user2");
assert_eq!(v["thoughts"][0]["authorUsername"], "user2");
assert_eq!(v["thoughts"][0]["content"], "user2 was here");
assert_eq!(v["thoughts"][1]["author_username"], "user1");
assert_eq!(v["thoughts"][1]["authorUsername"], "user1");
assert_eq!(v["thoughts"][1]["content"], "A thought from user1");
}

View File

@@ -1,6 +1,10 @@
use crate::api::main::login_user;
use super::main::{create_user_with_password, setup};
use axum::http::StatusCode;
use utils::testing::make_jwt_request;
use http_body_util::BodyExt;
use serde_json::Value;
use utils::testing::{make_get_request, make_jwt_request};
#[tokio::test]
async fn test_follow_endpoints() {
@@ -67,3 +71,54 @@ async fn test_follow_endpoints() {
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[tokio::test]
async fn test_follow_lists() {
let app = setup().await;
let user_a = create_user_with_password(&app.db, "userA", "password123", "a@a.com").await;
let user_b = create_user_with_password(&app.db, "userB", "password123", "b@b.com").await;
let user_c = create_user_with_password(&app.db, "userC", "password123", "c@c.com").await;
// A follows B, C follows A
app::persistence::follow::follow_user(&app.db, user_a.id, user_b.id)
.await
.unwrap();
app::persistence::follow::follow_user(&app.db, user_c.id, user_a.id)
.await
.unwrap();
// 1. Check user A's lists
let response_following =
make_get_request(app.router.clone(), "/users/userA/following", None).await;
let body_following = response_following
.into_body()
.collect()
.await
.unwrap()
.to_bytes();
let v: Value = serde_json::from_slice(&body_following).unwrap();
assert_eq!(v["users"].as_array().unwrap().len(), 1);
assert_eq!(v["users"][0]["username"], "userB");
let response_followers =
make_get_request(app.router.clone(), "/users/userA/followers", None).await;
let body_followers = response_followers
.into_body()
.collect()
.await
.unwrap()
.to_bytes();
let v: Value = serde_json::from_slice(&body_followers).unwrap();
assert_eq!(v["users"].as_array().unwrap().len(), 1);
assert_eq!(v["users"][0]["username"], "userC");
// 2. Check user A's /me endpoint
let jwt_a = login_user(app.router.clone(), "userA", "password123").await;
let response_me = make_jwt_request(app.router.clone(), "/users/me", "GET", None, &jwt_a).await;
let body_me = response_me.into_body().collect().await.unwrap().to_bytes();
let v: Value = serde_json::from_slice(&body_me).unwrap();
assert_eq!(v["username"], "userA");
assert_eq!(v["following"].as_array().unwrap().len(), 1);
assert_eq!(v["following"][0]["username"], "userB");
}

View File

@@ -23,7 +23,7 @@ async fn test_thought_endpoints() {
let body = response.into_body().collect().await.unwrap().to_bytes();
let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v["content"], "My first thought!");
assert_eq!(v["author_username"], "user1");
assert_eq!(v["authorUsername"], "user1");
let thought_id = v["id"].as_str().unwrap().to_string();
// 2. Post a thought with invalid content
@@ -79,8 +79,8 @@ async fn test_thought_replies() {
let reply_thought: Value = serde_json::from_slice(&body).unwrap();
// 3. Verify the reply is linked correctly
assert_eq!(reply_thought["reply_to_id"], original_thought_id);
assert_eq!(reply_thought["author_username"], "user2");
assert_eq!(reply_thought["replyToId"], original_thought_id);
assert_eq!(reply_thought["authorUsername"], "user2");
}
#[tokio::test]

View File

@@ -22,7 +22,7 @@ async fn test_post_users() {
let v: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v["username"], "test");
assert!(v["display_name"].is_string());
assert!(v["displayName"].is_string());
}
#[tokio::test]
@@ -86,7 +86,7 @@ async fn test_me_endpoints() {
let v: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v["username"], "me_user");
assert!(v["bio"].is_null());
assert!(v["display_name"].is_string());
assert!(v["displayName"].is_string());
// 4. PUT /users/me to update the profile
let update_body = json!({
@@ -106,7 +106,7 @@ async fn test_me_endpoints() {
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let v_updated: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v_updated["display_name"], "Me User");
assert_eq!(v_updated["displayName"], "Me User");
assert_eq!(v_updated["bio"], "This is my updated bio.");
// 5. GET /users/me again to verify the update was persisted
@@ -114,7 +114,7 @@ async fn test_me_endpoints() {
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let v_verify: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v_verify["display_name"], "Me User");
assert_eq!(v_verify["displayName"], "Me User");
assert_eq!(v_verify["bio"], "This is my updated bio.");
}
@@ -241,7 +241,7 @@ async fn test_update_me_css_and_images() {
let body = response.into_body().collect().await.unwrap().to_bytes();
let v: Value = serde_json::from_slice(&body).unwrap();
assert_eq!(v["avatar_url"], "https://example.com/new-avatar.png");
assert_eq!(v["header_url"], "https://example.com/new-header.jpg");
assert_eq!(v["custom_css"], "body { color: blue; }");
assert_eq!(v["avatarUrl"], "https://example.com/new-avatar.png");
assert_eq!(v["headerUrl"], "https://example.com/new-header.jpg");
assert_eq!(v["customCss"], "body { color: blue; }");
}