From fe8e23752657231c461ff3b45bf4a840a460650a Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:11:35 +0200 Subject: [PATCH 01/12] Rename integration.rs to guild.rs --- tests/guild.rs | 29 +++++++++ tests/integration.rs | 152 ------------------------------------------- 2 files changed, 29 insertions(+), 152 deletions(-) create mode 100644 tests/guild.rs delete mode 100644 tests/integration.rs diff --git a/tests/guild.rs b/tests/guild.rs new file mode 100644 index 0000000..6b50a94 --- /dev/null +++ b/tests/guild.rs @@ -0,0 +1,29 @@ +mod common; +use chorus::types::{Guild, GuildCreateSchema}; + +#[tokio::test] +async fn guild_creation_deletion() { + let mut bundle = common::setup().await; + + let guild_create_schema = 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 = Guild::create(&mut bundle.user, bundle.urls.get_api(), guild_create_schema) + .await + .unwrap(); + + println!("{}", guild); + + match Guild::delete(&mut bundle.user, bundle.urls.get_api(), guild).await { + None => assert!(true), + Some(_) => assert!(false), + } + common::teardown(bundle).await +} diff --git a/tests/integration.rs b/tests/integration.rs deleted file mode 100644 index 116e89f..0000000 --- a/tests/integration.rs +++ /dev/null @@ -1,152 +0,0 @@ -use chorus::{ - instance::{Instance, UserMeta}, - types::{Channel, ChannelCreateSchema, Guild, GuildCreateSchema, RegisterSchema}, - URLBundle, -}; - -#[derive(Debug)] -struct TestBundle { - urls: URLBundle, - user: UserMeta, - instance: Instance, - guild_id: String, - channel: Channel, -} - -// 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(), - "ws://localhost:3001".to_string(), - "http://localhost:3001".to_string(), - ); - let mut instance = Instance::new(urls.clone()).await.unwrap(); - // Requires the existance of the below user. - let reg = RegisterSchema::new( - "integrationtestuser".to_string(), - None, - true, - None, - None, - None, - Some("2000-01-01".to_string()), - None, - None, - None, - ) - .unwrap(); - let guild_create_schema = GuildCreateSchema { - name: Some("Test-Guild!".to_string()), - region: None, - icon: None, - channels: None, - guild_template_code: None, - system_channel_id: None, - rules_channel_id: None, - }; - let channel_create_schema = ChannelCreateSchema { - name: "testchannel".to_string(), - channel_type: Some(0), - topic: None, - icon: None, - bitrate: None, - user_limit: None, - rate_limit_per_user: None, - position: None, - permission_overwrites: None, - parent_id: None, - id: None, - nsfw: Some(false), - rtc_region: None, - default_auto_archive_duration: None, - default_reaction_emoji: None, - flags: Some(0), - default_thread_rate_limit_per_user: Some(0), - video_quality_mode: None, - }; - let mut user = instance.register_account(®).await.unwrap(); - let guild_id = Guild::create(&mut user, urls.get_api(), guild_create_schema) - .await - .unwrap(); - let channel = Channel::create( - &user.token, - urls.get_api(), - guild_id.as_str(), - channel_create_schema, - &mut user.limits, - &mut instance.limits, - ) - .await - .unwrap(); - - TestBundle { - urls, - user, - instance, - guild_id, - channel, - } -} - -// Teardown method to clean up after a test. -async fn teardown(mut bundle: TestBundle) { - Guild::delete( - &mut bundle.user, - bundle.instance.urls.get_api(), - bundle.guild_id, - ) - .await; - bundle.user.delete().await; -} - -mod guild { - use chorus::types::{Channel, Guild, GuildCreateSchema}; - - #[tokio::test] - async fn guild_creation_deletion() { - let mut bundle = crate::setup().await; - - let guild_create_schema = 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 = Guild::create(&mut bundle.user, bundle.urls.get_api(), guild_create_schema) - .await - .unwrap(); - - println!("{}", guild); - - match Guild::delete(&mut bundle.user, bundle.urls.get_api(), guild).await { - None => assert!(true), - Some(_) => assert!(false), - } - crate::teardown(bundle).await - } - - #[tokio::test] - async fn get_channel() { - let mut bundle = crate::setup().await; - let bundle_channel = bundle.channel.clone(); - let bundle_user = &mut bundle.user; - - assert_eq!( - bundle_channel, - Channel::get( - bundle_user.token.as_str(), - bundle.instance.urls.get_api(), - &bundle_channel.id.to_string(), - &mut bundle_user.limits, - &mut bundle.instance.limits - ) - .await - .unwrap() - ); - crate::teardown(bundle).await - } -} From 1b17904403bd9262b617f994081db6a5c4b7bb98 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:12:07 +0200 Subject: [PATCH 02/12] Move common code into common::<> --- tests/common/mod.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/common/mod.rs diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..fd58140 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,101 @@ + +use chorus::{ + instance::{Instance, UserMeta}, + types::{Channel, ChannelCreateSchema, Guild, GuildCreateSchema, RegisterSchema}, + URLBundle, +}; + +#[derive(Debug)] +pub struct TestBundle { + pub urls: URLBundle, + pub user: UserMeta, + pub instance: Instance, + pub guild_id: String, + pub channel: Channel, +} + +// Set up a test by creating an Instance and a User. Reduces Test boilerplate. +pub 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 mut instance = Instance::new(urls.clone()).await.unwrap(); + // Requires the existance of the below user. + let reg = RegisterSchema::new( + "integrationtestuser".to_string(), + None, + true, + None, + None, + None, + Some("2000-01-01".to_string()), + None, + None, + None, + ) + .unwrap(); + let guild_create_schema = GuildCreateSchema { + name: Some("Test-Guild!".to_string()), + region: None, + icon: None, + channels: None, + guild_template_code: None, + system_channel_id: None, + rules_channel_id: None, + }; + let channel_create_schema = ChannelCreateSchema { + name: "testchannel".to_string(), + channel_type: Some(0), + topic: None, + icon: None, + bitrate: None, + user_limit: None, + rate_limit_per_user: None, + position: None, + permission_overwrites: None, + parent_id: None, + id: None, + nsfw: Some(false), + rtc_region: None, + default_auto_archive_duration: None, + default_reaction_emoji: None, + flags: Some(0), + default_thread_rate_limit_per_user: Some(0), + video_quality_mode: None, + }; + let mut user = instance.register_account(®).await.unwrap(); + let guild_id = Guild::create(&mut user, urls.get_api(), guild_create_schema) + .await + .unwrap(); + let channel = Channel::create( + &user.token, + urls.get_api(), + guild_id.as_str(), + channel_create_schema, + &mut user.limits, + &mut instance.limits, + ) + .await + .unwrap(); + + TestBundle { + urls, + user, + instance, + guild_id, + channel, + } +} + +// Teardown method to clean up after a test. +pub async fn teardown(mut bundle: TestBundle) { + Guild::delete( + &mut bundle.user, + bundle.instance.urls.get_api(), + bundle.guild_id, + ) + .await; + bundle.user.delete().await; +} From 28663efb09eaa23ac9894dbdf3b6e29f489fe33a Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:12:15 +0200 Subject: [PATCH 03/12] move channel test to channel --- tests/channel.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/channel.rs diff --git a/tests/channel.rs b/tests/channel.rs new file mode 100644 index 0000000..4e0db28 --- /dev/null +++ b/tests/channel.rs @@ -0,0 +1,23 @@ +mod common; +use chorus::types::Channel; + +#[tokio::test] +async fn get_channel() { + let mut bundle = common::setup().await; + let bundle_channel = bundle.channel.clone(); + let bundle_user = &mut bundle.user; + + assert_eq!( + bundle_channel, + Channel::get( + bundle_user.token.as_str(), + bundle.instance.urls.get_api(), + &bundle_channel.id.to_string(), + &mut bundle_user.limits, + &mut bundle.instance.limits + ) + .await + .unwrap() + ); + common::teardown(bundle).await +} From a3c800c3d90e4d4e5be7805ec995852c88968ca9 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:46:27 +0200 Subject: [PATCH 04/12] Move tests to tests/-dir --- src/api/auth/login.rs | 51 ------------- src/api/auth/register.rs | 33 --------- src/api/channels/messages.rs | 134 ----------------------------------- 3 files changed, 218 deletions(-) diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index 8589b39..7ecc4de 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -70,54 +70,3 @@ pub mod login { } } } - -/*#[cfg(test)] -mod test { - use crate::api::schemas::schemas::{ - AuthEmail, AuthPassword, AuthUsername, LoginSchema, RegisterSchema, - }; - use crate::instance::Instance; - use crate::limit::LimitedRequester; - use crate::URLBundle; - - #[tokio::test] - async fn test_login() { - let urls = URLBundle::new( - "http://localhost:3001/api".to_string(), - "http://localhost:3001".to_string(), - "http://localhost:3001".to_string(), - ); - let limited_requester = LimitedRequester::new(urls.get_api().to_string()).await; - let mut test_instance = Instance::new(urls.clone(), limited_requester) - .await - .unwrap(); - let reg = RegisterSchema::new( - AuthUsername::new("TestAccount".to_string()).unwrap(), - Some(AuthPassword::new("transrights".to_string()).unwrap()), - true, - Some(AuthEmail::new("apiauthlogin1@testlogin.xyz".to_string()).unwrap()), - None, - None, - Some("2000-01-01".to_string()), - None, - None, - None, - ) - .unwrap(); - test_instance.register_account(®).await.unwrap().token; - - let login_schema = LoginSchema::new( - AuthUsername::new("apiauthlogin1@testlogin.xyz".to_string()).unwrap(), - "transrights".to_string(), - Some(false), - None, - None, - None, - ); - - let login_result = test_instance - .login_account(&login_schema.unwrap()) - .await - .unwrap(); - } -}*/ diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index dc5bf61..d318916 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -78,36 +78,3 @@ pub mod register { } } } - -#[cfg(test)] -mod test { - use crate::instance::Instance; - use crate::limit::LimitedRequester; - use crate::types::RegisterSchema; - use crate::URLBundle; - - #[tokio::test] - async fn test_registration() { - let urls = URLBundle::new( - "http://localhost:3001/api".to_string(), - "http://localhost:3001".to_string(), - "http://localhost:3001".to_string(), - ); - let _limited_requester = LimitedRequester::new().await; - let mut test_instance = Instance::new(urls.clone()).await.unwrap(); - let reg = RegisterSchema::new( - "Hiiii".to_string(), - None, - true, - None, - None, - None, - Some("2000-01-01".to_string()), - None, - None, - None, - ) - .unwrap(); - let _ = test_instance.register_account(®).await.unwrap().token; - } -} diff --git a/src/api/channels/messages.rs b/src/api/channels/messages.rs index 9a68eaa..91e148a 100644 --- a/src/api/channels/messages.rs +++ b/src/api/channels/messages.rs @@ -114,137 +114,3 @@ pub mod messages { } } } - -#[cfg(test)] -mod test { - use crate::instance::UserMeta; - use crate::types::{LoginSchema, MessageSendSchema, PartialDiscordFileAttachment}; - use crate::{instance::Instance}; - - use std::io::Read; - use std::{cell::RefCell, fs::File}; - use std::{io::BufReader, rc::Rc}; - - #[tokio::test] - async fn send_message() { - let channel_id = "1106954414356168802".to_string(); - let mut message = MessageSendSchema::new( - None, - Some("A Message!".to_string()), - None, - None, - None, - None, - None, - None, - None, - None, - ); - 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: LoginSchema = LoginSchema::new( - "user@test.xyz".to_string(), - "transrights".to_string(), - None, - None, - None, - None, - ) - .unwrap(); - let login_result = instance.login_account(&login_schema).await.unwrap(); - let token = login_result.token; - println!("TOKEN: {}", token); - let settings = login_result.settings; - let limits = instance.limits.clone(); - let mut user = UserMeta::new( - Rc::new(RefCell::new(instance)), - token, - limits, - settings, - None, - ); - let _ = user - .send_message(&mut message, channel_id, None) - .await - .unwrap(); - } - - #[tokio::test] - async fn send_message_attachment() { - let channel_id = "1106954414356168802".to_string(); - - let f = File::open("./README.md").unwrap(); - let mut reader = BufReader::new(f); - let mut buffer = Vec::new(); - - reader.read_to_end(&mut buffer).unwrap(); - - let attachment = PartialDiscordFileAttachment { - id: None, - filename: "README.md".to_string(), - description: None, - content_type: None, - size: None, - url: None, - proxy_url: None, - width: None, - height: None, - ephemeral: None, - duration_secs: None, - waveform: None, - content: buffer, - }; - - let mut message = MessageSendSchema::new( - None, - Some("trans rights now".to_string()), - None, - None, - None, - None, - None, - None, - None, - Some(vec![attachment.clone()]), - ); - 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: LoginSchema = LoginSchema::new( - "user@test.xyz".to_string(), - "transrights".to_string(), - None, - None, - None, - None, - ) - .unwrap(); - let login_result = instance.login_account(&login_schema).await.unwrap(); - let token = login_result.token; - let settings = login_result.settings; - let limits = instance.limits.clone(); - let mut user = UserMeta::new( - Rc::new(RefCell::new(instance)), - token, - limits, - settings, - None, - ); - let vec_attach = vec![attachment.clone()]; - let _arg = Some(&vec_attach); - let response = user - .send_message(&mut message, channel_id, Some(vec![attachment.clone()])) - .await - .unwrap(); - println!("[Response:] {}", response.text().await.unwrap()); - } -} From efe101675c626871a8eff9a19e4e4beb88b92bdd Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:46:43 +0200 Subject: [PATCH 05/12] Make password optional --- src/types/schema/auth.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/types/schema/auth.rs b/src/types/schema/auth.rs index 073d8d4..68c4546 100644 --- a/src/types/schema/auth.rs +++ b/src/types/schema/auth.rs @@ -187,7 +187,7 @@ You will receive a [`FieldFormatError`], if: #[serde(rename_all = "snake_case")] pub struct LoginSchema { pub login: String, - pub password: String, + pub password: Option, pub undelete: Option, pub captcha_key: Option, pub login_source: Option, @@ -210,15 +210,12 @@ impl LoginSchema { */ pub fn new( login: String, - password: String, + password: Option, undelete: Option, captcha_key: Option, login_source: Option, gift_code_sku_id: Option, ) -> Result { - let login = AuthUsername::new(login)?.username; - let password = AuthPassword::new(password)?.password; - Ok(LoginSchema { login, password, From 4e1024b34a3b9468adcf4874c2151781e28d077b Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:46:50 +0200 Subject: [PATCH 06/12] Add auth tests --- tests/auth.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/auth.rs diff --git a/tests/auth.rs b/tests/auth.rs new file mode 100644 index 0000000..2b52cea --- /dev/null +++ b/tests/auth.rs @@ -0,0 +1,42 @@ +mod common; + +use chorus::types; + +#[tokio::test] +async fn test_registration() { + let mut bundle = common::setup().await; + let reg = types::RegisterSchema::new( + "Hiiii".to_string(), + None, + true, + None, + None, + None, + Some("2000-01-01".to_string()), + None, + None, + None, + ) + .unwrap(); + bundle.instance.register_account(®).await.unwrap(); + common::teardown(bundle).await; +} + +#[tokio::test] +async fn test_login() { + let mut bundle = common::setup().await; + let login_schema = types::LoginSchema::new( + "integrationtestuser".to_string(), + None, + Some(false), + None, + None, + None, + ); + bundle + .instance + .login_account(&login_schema.unwrap()) + .await + .unwrap(); + common::teardown(bundle).await; +} From 606b62d6f5c833106aee4c24eae8d975a7ec9f95 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:46:57 +0200 Subject: [PATCH 07/12] Add message tests --- tests/message.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/message.rs diff --git a/tests/message.rs b/tests/message.rs new file mode 100644 index 0000000..6ccda0b --- /dev/null +++ b/tests/message.rs @@ -0,0 +1,80 @@ +mod common; + +use chorus::types; +use std::fs::File; +use std::io::{BufReader, Read}; + +#[tokio::test] +async fn send_message() { + let mut bundle = common::setup().await; + let mut message = types::MessageSendSchema::new( + None, + Some("A Message!".to_string()), + None, + None, + None, + None, + None, + None, + None, + None, + ); + let _ = bundle + .user + .send_message(&mut message, bundle.channel.id.to_string(), None) + .await + .unwrap(); + common::teardown(bundle).await +} + +#[tokio::test] +async fn send_message_attachment() { + let f = File::open("./README.md").unwrap(); + let mut reader = BufReader::new(f); + let mut buffer = Vec::new(); + let mut bundle = common::setup().await; + + reader.read_to_end(&mut buffer).unwrap(); + + let attachment = types::PartialDiscordFileAttachment { + id: None, + filename: "README.md".to_string(), + description: None, + content_type: None, + size: None, + url: None, + proxy_url: None, + width: None, + height: None, + ephemeral: None, + duration_secs: None, + waveform: None, + content: buffer, + }; + + let mut message = types::MessageSendSchema::new( + None, + Some("trans rights now".to_string()), + None, + None, + None, + None, + None, + None, + None, + Some(vec![attachment.clone()]), + ); + + let vec_attach = vec![attachment.clone()]; + let _arg = Some(&vec_attach); + bundle + .user + .send_message( + &mut message, + bundle.channel.id.to_string(), + Some(vec![attachment.clone()]), + ) + .await + .unwrap(); + common::teardown(bundle).await +} From 915ab010eda2509c7e6d2030d985ebab8cb28195 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:47:14 +0200 Subject: [PATCH 08/12] Remove newline at beginning of file --- tests/common/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index fd58140..3fdb42b 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,4 +1,3 @@ - use chorus::{ instance::{Instance, UserMeta}, types::{Channel, ChannelCreateSchema, Guild, GuildCreateSchema, RegisterSchema}, From b1760af9d2e3ab643345cfdf118016ad8daba81f Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:47:46 +0200 Subject: [PATCH 09/12] Remove empty test --- src/api/users/users.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index 4113c6b..098a92f 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -178,10 +178,3 @@ impl Instance { .await } } - -#[cfg(test)] -mod test { - - #[tokio::test] - async fn get_user() {} -} From b83f90f8dc1e8c0d44b59deaaae0ec861d7c2373 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:51:43 +0200 Subject: [PATCH 10/12] Move test to tests-dir --- src/api/policies/instance/instance.rs | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index 20d7d6f..f183e11 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -37,21 +37,3 @@ impl Instance { Ok(instance_policies_schema) } } - -#[cfg(test)] -mod instance_policies_schema_test { - use crate::{instance::Instance, limit::LimitedRequester, URLBundle}; - - #[tokio::test] - async fn generate_instance_policies_schema() { - let urls = URLBundle::new( - "http://localhost:3001/api".to_string(), - "http://localhost:3001".to_string(), - "http://localhost:3001".to_string(), - ); - let _limited_requester = LimitedRequester::new().await; - let test_instance = Instance::new(urls.clone()).await.unwrap(); - - let _schema = test_instance.general_configuration_schema().await.unwrap(); - } -} From 02a41b415f09c54968867e8730d4604a56c3c884 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:51:57 +0200 Subject: [PATCH 11/12] Add instance tests --- tests/instance.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/instance.rs diff --git a/tests/instance.rs b/tests/instance.rs new file mode 100644 index 0000000..d3cd5f0 --- /dev/null +++ b/tests/instance.rs @@ -0,0 +1,12 @@ +mod common; + +#[tokio::test] +async fn generate_general_configuration_schema() { + let bundle = common::setup().await; + bundle + .instance + .general_configuration_schema() + .await + .unwrap(); + common::teardown(bundle).await; +} From e83b901e78a8da5f13a4189d5b5295eb437bd760 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sat, 27 May 2023 22:53:40 +0200 Subject: [PATCH 12/12] remove login test --- tests/auth.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/auth.rs b/tests/auth.rs index 2b52cea..f90e229 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -21,22 +21,3 @@ async fn test_registration() { bundle.instance.register_account(®).await.unwrap(); common::teardown(bundle).await; } - -#[tokio::test] -async fn test_login() { - let mut bundle = common::setup().await; - let login_schema = types::LoginSchema::new( - "integrationtestuser".to_string(), - None, - Some(false), - None, - None, - None, - ); - bundle - .instance - .login_account(&login_schema.unwrap()) - .await - .unwrap(); - common::teardown(bundle).await; -}