From cba6a86b04cbbadbc38460f8ba383b802f37539d Mon Sep 17 00:00:00 2001 From: K0nnyaku Date: Sun, 1 Dec 2024 19:39:04 +0800 Subject: [PATCH] test: support organization --- Cargo.lock | 2 +- tests/organization.rs | 100 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/organization.rs diff --git a/Cargo.lock b/Cargo.lock index 1304a80..52d0167 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,7 +71,7 @@ dependencies = [ [[package]] name = "algohub-server" -version = "0.1.6" +version = "0.1.7" dependencies = [ "anyhow", "chrono", diff --git a/tests/organization.rs b/tests/organization.rs new file mode 100644 index 0000000..0acbc40 --- /dev/null +++ b/tests/organization.rs @@ -0,0 +1,100 @@ +use std::path::Path; + +use algohub_server::{ + models::{ + account::Register, + organization::CreateOrganization, + response::{Empty, Response}, + OwnedCredentials, Token, + }, + routes::organization::{CreateOrgResponse, OrgData}, +}; +use anyhow::Result; +use rocket::local::asynchronous::Client; + +#[rocket::async_test] +async fn test_organization() -> Result<()> { + let rocket = algohub_server::rocket().await; + + let client = Client::tracked(rocket).await?; + + println!("Testing organization..."); + let response = client + .post("/account/create") + .json(&Register { + username: "fu050409".to_string(), + password: "password".to_string(), + email: "email@example.com".to_string(), + }) + .dispatch() + .await; + + assert_eq!(response.status().code, 200); + + let Response { + success, + message: _, + data, + } = response.into_json().await.unwrap(); + let data: OwnedCredentials = data.unwrap(); + + let id = data.id.clone(); + let token = data.token.clone(); + + assert!(success); + + let response = client + .post("/organization/create") + .json(&OrgData { + id: &id, + token: &token, + org: CreateOrganization { + name: "test_organization".to_string(), + display_name: None, + description: None, + }, + }) + .dispatch() + .await; + + assert_eq!(response.status().code, 200); + + let Response { + success, + message: _, + data, + } = response.into_json().await.unwrap(); + let data: CreateOrgResponse = data.unwrap(); + + assert!(success); + println!("Created organization: {}", data.id); + + let response = client + .post(format!("/organization/delete/{}", id)) + .json(&OrgData { + id: &id, + token: &token, + org: CreateOrganization { + name: "test_organization".to_string(), + display_name: None, + description: None, + }, + }) + .dispatch() + .await; + + response.into_json::>().await.unwrap(); + + assert!(!Path::new("content").join(id.clone()).exists()); + + client + .post(format!("/account/delete/{}", id)) + .json(&Token { token: &token }) + .dispatch() + .await + .into_json::>() + .await + .unwrap(); + + Ok(()) +}