From 556976890db34d28e48f71d7dc27112844b1080e Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 20 May 2023 12:22:54 +0200 Subject: [PATCH 1/7] Begin working on setup fn in tests/integration.rs --- tests/integration.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/integration.rs diff --git a/tests/integration.rs b/tests/integration.rs new file mode 100644 index 0000000..7b6841e --- /dev/null +++ b/tests/integration.rs @@ -0,0 +1,18 @@ +use chorus::{instance::Instance, limit::LimitedRequester, URLBundle}; + +struct TestBundle { + urls: URLBundle, + instance: Instance, +} + +async fn setup() -> TestBundle { + let urls = URLBundle::new( + "http://localhost:3001/api".to_string(), + "ws://localhost:3001".to_string(), + "http://localhost:3001".to_string(), + ); + let requester = LimitedRequester::new().await; + let instance = Instance::new(urls.clone(), requester).await.unwrap(); + + TestBundle { urls, instance } +} From b5b032c37ebb632b2d54d24031cb6cfcea68110b Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 20 May 2023 21:04:03 +0200 Subject: [PATCH 2/7] Create integration.rs, is broken right now --- Cargo.toml | 5 +++- src/instance.rs | 3 +-- tests/integration.rs | 55 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f346a21..d0312a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,7 @@ custom_error = "1.9.2" native-tls = "0.2.11" tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]} futures-util = "0.3.28" -http = "0.2.9" \ No newline at end of file +http = "0.2.9" + +[dev-dependencies] +lazy_static = "1.4.0" \ No newline at end of file diff --git a/src/instance.rs b/src/instance.rs index ee33961..dfb9e2f 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -1,10 +1,9 @@ use crate::api::limits::Limits; -use crate::api::types::{InstancePolicies}; +use crate::api::types::InstancePolicies; use crate::errors::{FieldFormatError, InstanceServerError}; use crate::limit::LimitedRequester; use crate::URLBundle; - use std::fmt; #[derive(Debug)] diff --git a/tests/integration.rs b/tests/integration.rs index 7b6841e..8b4990b 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,18 +1,59 @@ -use chorus::{instance::Instance, limit::LimitedRequester, URLBundle}; +use std::sync::Mutex; -struct TestBundle { - urls: URLBundle, - instance: Instance, +use chorus::{ + api::{AuthUsername, LoginSchema, User}, + instance::Instance, + limit::LimitedRequester, + URLBundle, +}; + +use lazy_static::lazy_static; + +lazy_static! { + static ref INSTANCE: Mutex = + tokio::runtime::Runtime::new().unwrap().block_on(async { + Mutex::new( + Instance::new( + URLBundle::new( + "http://localhost:3001/api".to_string(), + "ws://localhost:3001".to_string(), + "http://localhost:3001".to_string(), + ), + LimitedRequester::new().await, + ) + .await + .unwrap(), + ) + }); } -async fn setup() -> TestBundle { +struct TestBundle<'a> { + urls: URLBundle, + user: User<'a>, +} + +async fn setup<'a>() -> TestBundle<'a> { let urls = URLBundle::new( "http://localhost:3001/api".to_string(), "ws://localhost:3001".to_string(), "http://localhost:3001".to_string(), ); let requester = LimitedRequester::new().await; - let instance = Instance::new(urls.clone(), requester).await.unwrap(); + let mut instance = Instance::new(urls.clone(), requester).await.unwrap(); + // Requires the existance of the below user. + let login_schema: LoginSchema = LoginSchema::new( + AuthUsername::new("user@test.xyz".to_string()).unwrap(), + "transrights".to_string(), + None, + None, + None, + None, + ) + .unwrap(); + let user = instance.login_account(&login_schema).await.unwrap(); - TestBundle { urls, instance } + TestBundle { + urls: urls, + user: user, + } } From 8cb26929ceec432cfba0dcc7cc32b080e0557a9f Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 21 May 2023 00:47:40 +0200 Subject: [PATCH 3/7] Create teardown() --- tests/integration.rs | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index 8b4990b..dd5abbc 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -7,39 +7,18 @@ use chorus::{ URLBundle, }; -use lazy_static::lazy_static; - -lazy_static! { - static ref INSTANCE: Mutex = - tokio::runtime::Runtime::new().unwrap().block_on(async { - Mutex::new( - Instance::new( - URLBundle::new( - "http://localhost:3001/api".to_string(), - "ws://localhost:3001".to_string(), - "http://localhost:3001".to_string(), - ), - LimitedRequester::new().await, - ) - .await - .unwrap(), - ) - }); -} - -struct TestBundle<'a> { +struct TestBundle { urls: URLBundle, - user: User<'a>, + user: User, } -async fn setup<'a>() -> TestBundle<'a> { +async fn setup() -> TestBundle { let urls = URLBundle::new( "http://localhost:3001/api".to_string(), "ws://localhost:3001".to_string(), "http://localhost:3001".to_string(), ); - let requester = LimitedRequester::new().await; - let mut instance = Instance::new(urls.clone(), requester).await.unwrap(); + let mut instance = Instance::new(urls.clone()).await.unwrap(); // Requires the existance of the below user. let login_schema: LoginSchema = LoginSchema::new( AuthUsername::new("user@test.xyz".to_string()).unwrap(), @@ -57,3 +36,5 @@ async fn setup<'a>() -> TestBundle<'a> { user: user, } } + +async fn teardown() {} From 849acf798ee6e0233292b113b46d80412530392a Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 21 May 2023 15:05:02 +0200 Subject: [PATCH 4/7] Add delete() to User --- src/api/users/users.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index bc80bca..cf052aa 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -125,6 +125,35 @@ impl User { ); Ok(user_updated) } + + /// Sends a request to the server which deletes the user from the Instance. + /// + /// # Arguments + /// + /// * `self` - The `User` object to delete. + /// + /// # Returns + /// + /// Returns `None` if the user was successfully deleted, or an `InstanceServerError` if an error occurred. + pub async fn delete(mut self) -> Option { + let mut belongs_to = self.belongs_to.borrow_mut(); + let request = Client::new() + .post(format!("{}/users/@me/delete/", belongs_to.urls.get_api())) + .bearer_auth(self.token); + match LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Global, + &mut belongs_to.limits, + &mut self.limits, + ) + .await + { + Ok(_) => None, + Err(e) => Some(e), + } + } } impl Instance { From dd6cc2f45ebd3ee1ccc2e045c34a7fa2454538a8 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 21 May 2023 15:49:52 +0200 Subject: [PATCH 5/7] Register testuser on setup(), delete on teardown() --- tests/integration.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index dd5abbc..57f567a 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,9 +1,6 @@ -use std::sync::Mutex; - use chorus::{ - api::{AuthUsername, LoginSchema, User}, + api::{AuthUsername, RegisterSchema, User}, instance::Instance, - limit::LimitedRequester, URLBundle, }; @@ -12,6 +9,7 @@ struct TestBundle { user: User, } +// Set up a test by creating an Instance and a User. async fn setup() -> TestBundle { let urls = URLBundle::new( "http://localhost:3001/api".to_string(), @@ -20,21 +18,25 @@ async fn setup() -> TestBundle { ); let mut instance = Instance::new(urls.clone()).await.unwrap(); // Requires the existance of the below user. - let login_schema: LoginSchema = LoginSchema::new( - AuthUsername::new("user@test.xyz".to_string()).unwrap(), - "transrights".to_string(), + let reg = RegisterSchema::new( + AuthUsername::new("integrationtestuser".to_string()).unwrap(), None, + true, + None, + None, + None, + Some("2000-01-01".to_string()), None, None, None, ) .unwrap(); - let user = instance.login_account(&login_schema).await.unwrap(); + let user = instance.register_account(®).await.unwrap(); - TestBundle { - urls: urls, - user: user, - } + TestBundle { urls, user } } -async fn teardown() {} +// Teardown method to clean up after a test. +async fn teardown(bundle: TestBundle) { + bundle.user.delete().await; +} From 31ff7b924367ddfd38e19e12fb9c7c5bb5966c71 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 21 May 2023 15:51:24 +0200 Subject: [PATCH 6/7] Derive Debug, add comment --- tests/integration.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/integration.rs b/tests/integration.rs index 57f567a..e790c5d 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -4,12 +4,13 @@ use chorus::{ URLBundle, }; +#[derive(Debug)] struct TestBundle { urls: URLBundle, user: User, } -// Set up a test by creating an Instance and a User. +// Set up a test by creating an Instance and a User. Reduces Test boilerplate. async fn setup() -> TestBundle { let urls = URLBundle::new( "http://localhost:3001/api".to_string(), From 301ac320c681f66ebcb22876dba02d03bb00cd1c Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 21 May 2023 16:02:54 +0200 Subject: [PATCH 7/7] move integration test from guild to integration.rs --- src/api/guilds/guilds.rs | 50 ---------------------------------------- tests/integration.rs | 31 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 50 deletions(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 0c0137f..5bbbd23 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -114,53 +114,3 @@ impl<'a> types::Guild { } } } - -#[cfg(test)] -mod test { - use crate::api::schemas; - use crate::api::types; - use crate::instance::Instance; - - #[tokio::test] - async fn guild_creation_deletion() { - let mut instance = Instance::new(crate::URLBundle { - api: "http://localhost:3001/api".to_string(), - wss: "ws://localhost:3001/".to_string(), - cdn: "http://localhost:3001".to_string(), - }) - .await - .unwrap(); - let login_schema: schemas::LoginSchema = schemas::LoginSchema::new( - schemas::AuthUsername::new("user@test.xyz".to_string()).unwrap(), - "transrights".to_string(), - None, - None, - None, - None, - ) - .unwrap(); - let mut user = instance.login_account(&login_schema).await.unwrap(); - - let guild_create_schema = schemas::GuildCreateSchema { - name: Some("test".to_string()), - region: None, - icon: None, - channels: None, - guild_template_code: None, - system_channel_id: None, - rules_channel_id: None, - }; - - let guild = - types::Guild::create(&mut user, "http://localhost:3001/api", guild_create_schema) - .await - .unwrap(); - - println!("{}", guild); - - match types::Guild::delete(&mut user, "http://localhost:3001/api", guild).await { - None => assert!(true), - Some(_) => assert!(false), - } - } -} diff --git a/tests/integration.rs b/tests/integration.rs index e790c5d..e004496 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -41,3 +41,34 @@ async fn setup() -> TestBundle { async fn teardown(bundle: TestBundle) { bundle.user.delete().await; } + +mod guild { + use chorus::api::{schemas, types}; + + #[tokio::test] + async fn guild_creation_deletion() { + let mut bundle = crate::setup().await; + + let guild_create_schema = schemas::GuildCreateSchema { + name: Some("test".to_string()), + region: None, + icon: None, + channels: None, + guild_template_code: None, + system_channel_id: None, + rules_channel_id: None, + }; + + let guild = + types::Guild::create(&mut bundle.user, bundle.urls.get_api(), guild_create_schema) + .await + .unwrap(); + + println!("{}", guild); + + match types::Guild::delete(&mut bundle.user, bundle.urls.get_api(), guild).await { + None => assert!(true), + Some(_) => assert!(false), + } + } +}