From b23781f5d4d5aff5e7cf989acd45a4ddf7d6408d Mon Sep 17 00:00:00 2001 From: YanWQ-monad Date: Sun, 26 May 2024 22:26:29 +0800 Subject: [PATCH] test: add more tests --- tests/tests/account.rs | 17 ++++------------- tests/tests/auth.rs | 22 +++++++++++++++++----- tests/tests/chat.rs | 24 +++++------------------- tests/tests/event.rs | 33 ++++++++++++++++++++++----------- tests/tests/moment.rs | 22 +++++++++++++++++++++- 5 files changed, 69 insertions(+), 49 deletions(-) diff --git a/tests/tests/account.rs b/tests/tests/account.rs index 40276b1..7887d98 100644 --- a/tests/tests/account.rs +++ b/tests/tests/account.rs @@ -5,7 +5,7 @@ use uuid::Uuid; use crate::common::{create_app, TestApp}; use crate::tests::auth::{ - create_account, create_default_account, RegisterForm, TEST_DEFAULT_ACCOUNT_FORM, + create_default_account, create_default_account_pair, TEST_DEFAULT_ACCOUNT_1, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)] @@ -63,8 +63,8 @@ async fn test_profile() { let profile: AccountProfile = get_profile(&app, account.account_id).await; assert_eq!(profile.id, account.account_id); - assert_eq!(profile.sustech_id, TEST_DEFAULT_ACCOUNT_FORM.sustech_id); - assert_eq!(profile.name, TEST_DEFAULT_ACCOUNT_FORM.name); + assert_eq!(profile.sustech_id, TEST_DEFAULT_ACCOUNT_1.sustech_id); + assert_eq!(profile.name, TEST_DEFAULT_ACCOUNT_1.name); } #[actix_web::test] @@ -92,16 +92,7 @@ async fn test_profile_update() { #[actix_web::test] async fn test_profile_no_update_other() { let app = create_app().await; - let account = create_default_account(&app).await; - let another = create_account( - &app, - &RegisterForm { - sustech_id: 12345678, - name: "another", - password: "password", - }, - ) - .await; + let (account, another) = create_default_account_pair(&app).await; let old_profile: AccountProfile = get_profile(&app, account.account_id).await; diff --git a/tests/tests/auth.rs b/tests/tests/auth.rs index e2fcb2e..e017227 100644 --- a/tests/tests/auth.rs +++ b/tests/tests/auth.rs @@ -38,12 +38,18 @@ pub struct CasSimulateCallback<'a> { pub ticket: &'a str, } -pub const TEST_DEFAULT_ACCOUNT_FORM: RegisterForm<'_> = RegisterForm { +pub const TEST_DEFAULT_ACCOUNT_1: RegisterForm<'_> = RegisterForm { sustech_id: 11111111, name: "test", password: "password", }; +pub const TEST_DEFAULT_ACCOUNT_2: RegisterForm<'_> = RegisterForm { + sustech_id: 22222222, + name: "test2", + password: "password2", +}; + pub async fn create_account(app: impl TestApp, form: &RegisterForm<'_>) -> AccountInfo { let req = test::TestRequest::post() .uri("/api/auth/register") @@ -60,7 +66,13 @@ pub async fn create_account(app: impl TestApp, form: &RegisterForm<'_>) -> Accou } pub async fn create_default_account(app: impl TestApp) -> AccountInfo { - create_account(app, &TEST_DEFAULT_ACCOUNT_FORM).await + create_account(app, &TEST_DEFAULT_ACCOUNT_1).await +} + +pub async fn create_default_account_pair(app: impl TestApp) -> (AccountInfo, AccountInfo) { + let account_1 = create_account(&app, &TEST_DEFAULT_ACCOUNT_1).await; + let account_2 = create_account(&app, &TEST_DEFAULT_ACCOUNT_2).await; + (account_1, account_2) } #[actix_web::test] @@ -78,8 +90,8 @@ async fn test_login() { let req = test::TestRequest::post() .uri("/api/auth/login") .set_json(&LoginForm { - sustech_id: TEST_DEFAULT_ACCOUNT_FORM.sustech_id, - password: TEST_DEFAULT_ACCOUNT_FORM.password, + sustech_id: TEST_DEFAULT_ACCOUNT_1.sustech_id, + password: TEST_DEFAULT_ACCOUNT_1.password, }) .to_request(); let resp = app.call(req).await.unwrap(); @@ -97,7 +109,7 @@ async fn test_login_incorrect_password() { let req = test::TestRequest::post() .uri("/api/auth/login") .set_json(&LoginForm { - sustech_id: TEST_DEFAULT_ACCOUNT_FORM.sustech_id, + sustech_id: TEST_DEFAULT_ACCOUNT_1.sustech_id, password: "something_wrong", }) .to_request(); diff --git a/tests/tests/chat.rs b/tests/tests/chat.rs index 34e8978..fa7ca31 100644 --- a/tests/tests/chat.rs +++ b/tests/tests/chat.rs @@ -6,7 +6,7 @@ use std::collections::HashSet; use crate::common::{create_app, TestApp}; use crate::tests::account::AccountCard; -use crate::tests::auth::{create_account, create_default_account, AccountInfo, RegisterForm}; +use crate::tests::auth::{create_default_account, create_default_account_pair, AccountInfo}; use crate::tests::misc::Page; #[derive(Debug, Deserialize)] @@ -58,20 +58,6 @@ struct ChatsResponse { pub chats: Vec, } -async fn create_account_pair(app: impl TestApp) -> (AccountInfo, AccountInfo) { - let account_1 = create_default_account(&app).await; - let account_2 = create_account( - &app, - &RegisterForm { - sustech_id: 12345678, - name: "User2", - password: "password", - }, - ) - .await; - (account_1, account_2) -} - async fn get_chat( app: impl TestApp, primary_account: &AccountInfo, @@ -95,7 +81,7 @@ async fn get_chat( #[actix_web::test] async fn test_chat_commutative() { let app = create_app().await; - let (account_1, account_2) = create_account_pair(&app).await; + let (account_1, account_2) = create_default_account_pair(&app).await; let id_lhs = get_chat(&app, &account_1, &account_2).await; let id_rhs = get_chat(&app, &account_2, &account_1).await; @@ -119,7 +105,7 @@ async fn test_chat_no_reflexive() { #[actix_web::test] async fn test_chat_message() { let app = create_app().await; - let (account_1, account_2) = create_account_pair(&app).await; + let (account_1, account_2) = create_default_account_pair(&app).await; let chat_id = get_chat(&app, &account_1, &account_2).await; let form = NewMessageForm { @@ -150,7 +136,7 @@ async fn test_chat_message() { #[actix_web::test] async fn test_chat_member() { let app = create_app().await; - let (account_1, account_2) = create_account_pair(&app).await; + let (account_1, account_2) = create_default_account_pair(&app).await; let chat_id = get_chat(&app, &account_1, &account_2).await; let req = test::TestRequest::get() @@ -176,7 +162,7 @@ async fn test_chat_member() { #[actix_web::test] async fn test_chat_list() { let app = create_app().await; - let (account_1, account_2) = create_account_pair(&app).await; + let (account_1, account_2) = create_default_account_pair(&app).await; let chat_id = get_chat(&app, &account_1, &account_2).await; let req = test::TestRequest::get() diff --git a/tests/tests/event.rs b/tests/tests/event.rs index 2da137a..39a0bc9 100644 --- a/tests/tests/event.rs +++ b/tests/tests/event.rs @@ -8,7 +8,7 @@ use uuid::Uuid; use crate::common::{create_app, TestApp}; use crate::tests::account::AccountCard; -use crate::tests::auth::{create_account, create_default_account, AccountInfo, RegisterForm}; +use crate::tests::auth::{create_default_account, create_default_account_pair, AccountInfo}; use crate::tests::misc::Page; use crate::tests::misc::Place; @@ -210,16 +210,7 @@ async fn test_event_create_and_get() { async fn test_event_list() { let app = create_app().await; - let account_1 = create_default_account(&app).await; - let account_2 = create_account( - &app, - &RegisterForm { - sustech_id: 12345678, - name: "User2", - password: "password", - }, - ) - .await; + let (account_1, account_2) = create_default_account_pair(&app).await; let event_id_1 = create_event(&app, &account_1, &DEFAULT_EVENT_1).await; let event_id_2 = create_event(&app, &account_2, &DEFAULT_EVENT_2).await; @@ -406,6 +397,26 @@ async fn test_event_delete() { assert_eq!(resp.status(), StatusCode::NOT_FOUND); } +#[actix_web::test] +async fn test_event_no_delete_others() { + let app = create_app().await; + let (account, another) = create_default_account_pair(&app).await; + let event_id = create_default_event(&app, &account).await; + + let req = test::TestRequest::delete() + .uri(&format!("/api/event/{event_id}")) + .insert_header(another.to_header_pair()) + .to_request(); + let resp = app.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::UNAUTHORIZED); + + let req = test::TestRequest::get() + .uri(&format!("/api/event/{event_id}")) + .to_request(); + let resp = app.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::OK); +} + #[actix_web::test] async fn test_event_participate() { let app = create_app().await; diff --git a/tests/tests/moment.rs b/tests/tests/moment.rs index 0fd3c09..73be6bf 100644 --- a/tests/tests/moment.rs +++ b/tests/tests/moment.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use crate::common::{create_app, TestApp}; use crate::tests::account::AccountCard; -use crate::tests::auth::{create_default_account, AccountInfo}; +use crate::tests::auth::{create_default_account, create_default_account_pair, AccountInfo}; use crate::tests::misc::Page; #[derive(Debug, Serialize)] @@ -161,6 +161,26 @@ async fn test_moment_delete() { assert_eq!(resp.status(), StatusCode::NOT_FOUND); } +#[actix_web::test] +async fn test_moment_no_delete_others() { + let app = create_app().await; + let (account, another) = create_default_account_pair(&app).await; + let moment_id = create_default_moment(&app, &account).await; + + let req = test::TestRequest::delete() + .uri(&format!("/api/moment/{moment_id}")) + .insert_header(another.to_header_pair()) + .to_request(); + let resp = app.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::UNAUTHORIZED); + + let req = test::TestRequest::get() + .uri(&format!("/api/moment/{moment_id}")) + .to_request(); + let resp = app.call(req).await.unwrap(); + assert_eq!(resp.status(), StatusCode::OK); +} + #[actix_web::test] async fn test_moment_comment() { let app = create_app().await;