Merge pull request #77 from polyphony-chat/refactor/test-restructuring

Make all integration tests completely reproducible
This commit is contained in:
Flori 2023-05-27 23:01:51 +02:00 committed by GitHub
commit 02eaddbcf6
12 changed files with 177 additions and 308 deletions

View File

@ -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(&reg).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();
}
}*/

View File

@ -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(&reg).await.unwrap().token;
}
}

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -178,10 +178,3 @@ impl Instance {
.await
}
}
#[cfg(test)]
mod test {
#[tokio::test]
async fn get_user() {}
}

View File

@ -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<String>,
pub undelete: Option<bool>,
pub captcha_key: Option<String>,
pub login_source: Option<String>,
@ -210,15 +210,12 @@ impl LoginSchema {
*/
pub fn new(
login: String,
password: String,
password: Option<String>,
undelete: Option<bool>,
captcha_key: Option<String>,
login_source: Option<String>,
gift_code_sku_id: Option<String>,
) -> Result<LoginSchema, FieldFormatError> {
let login = AuthUsername::new(login)?.username;
let password = AuthPassword::new(password)?.password;
Ok(LoginSchema {
login,
password,

23
tests/auth.rs Normal file
View File

@ -0,0 +1,23 @@
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(&reg).await.unwrap();
common::teardown(bundle).await;
}

23
tests/channel.rs Normal file
View File

@ -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
}

View File

@ -5,16 +5,16 @@ use chorus::{
};
#[derive(Debug)]
struct TestBundle {
urls: URLBundle,
user: UserMeta,
instance: Instance,
guild_id: String,
channel: Channel,
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.
async fn setup() -> TestBundle {
pub async fn setup() -> TestBundle {
let urls = URLBundle::new(
"http://localhost:3001/api".to_string(),
"ws://localhost:3001".to_string(),
@ -89,7 +89,7 @@ async fn setup() -> TestBundle {
}
// Teardown method to clean up after a test.
async fn teardown(mut bundle: TestBundle) {
pub async fn teardown(mut bundle: TestBundle) {
Guild::delete(
&mut bundle.user,
bundle.instance.urls.get_api(),
@ -98,55 +98,3 @@ async fn teardown(mut bundle: TestBundle) {
.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
}
}

29
tests/guild.rs Normal file
View File

@ -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
}

12
tests/instance.rs Normal file
View File

@ -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;
}

80
tests/message.rs Normal file
View File

@ -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
}