Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YanWQ-monad committed May 26, 2024
1 parent 1954374 commit b23781f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 49 deletions.
17 changes: 4 additions & 13 deletions tests/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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;

Expand Down
22 changes: 17 additions & 5 deletions tests/tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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]
Expand All @@ -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();
Expand All @@ -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();
Expand Down
24 changes: 5 additions & 19 deletions tests/tests/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -58,20 +58,6 @@ struct ChatsResponse {
pub chats: Vec<ChatResponse>,
}

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,
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
33 changes: 22 additions & 11 deletions tests/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 21 additions & 1 deletion tests/tests/moment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b23781f

Please sign in to comment.