Files
thoughts/thoughts-backend/tests/api/thought.rs

51 lines
2.0 KiB
Rust

use crate::api::main::create_user_with_password;
use super::main::setup;
use axum::http::StatusCode;
use http_body_util::BodyExt;
use sea_orm::prelude::Uuid;
use serde_json::json;
use utils::testing::{make_delete_request, make_post_request};
#[tokio::test]
async fn test_thought_endpoints() {
let app = setup().await;
let user1 =
create_user_with_password(&app.db, "user1", "password123", "user1@example.com").await; // AuthUser is ID 1
let _user2 =
create_user_with_password(&app.db, "user2", "password123", "user2@example.com").await; // Other user is ID 2
// 1. Post a new thought as user 1
let body = json!({ "content": "My first thought!" }).to_string();
let response = make_post_request(app.router.clone(), "/thoughts", body, Some(user1.id)).await;
assert_eq!(response.status(), StatusCode::CREATED);
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");
let thought_id = v["id"].as_str().unwrap().to_string();
// 2. Post a thought with invalid content
let body = json!({ "content": "" }).to_string(); // Too short
let response = make_post_request(app.router.clone(), "/thoughts", body, Some(user1.id)).await;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
// 3. Attempt to delete another user's thought (user1 tries to delete a non-existent thought, but let's pretend it's user2's)
let response = make_delete_request(
app.router.clone(),
&format!("/thoughts/{}", Uuid::new_v4()),
Some(user1.id),
)
.await;
assert_eq!(response.status(), StatusCode::NOT_FOUND);
// 4. Delete the thought created in step 1
let response = make_delete_request(
app.router.clone(),
&format!("/thoughts/{}", thought_id),
Some(user1.id),
)
.await;
assert_eq!(response.status(), StatusCode::NO_CONTENT);
}