chorus/tests/common/mod.rs

100 lines
3.0 KiB
Rust
Raw Normal View History

2023-05-27 22:12:07 +02:00
use chorus::{
instance::{Instance, UserMeta},
2023-06-10 00:23:49 +02:00
types::{
Channel, ChannelCreateSchema, Guild, GuildCreateSchema, RegisterSchema,
2023-06-19 10:27:32 +02:00
RegisterSchemaOptions, RoleCreateModifySchema, RoleObject,
2023-06-10 00:23:49 +02:00
},
2023-05-27 22:12:07 +02:00
URLBundle,
};
#[derive(Debug)]
pub struct TestBundle {
pub urls: URLBundle,
pub user: UserMeta,
pub instance: Instance,
2023-05-29 16:51:41 +02:00
pub guild: Guild,
2023-06-10 00:23:49 +02:00
pub role: RoleObject,
2023-05-27 22:12:07 +02:00
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.
2023-06-19 10:27:32 +02:00
let reg = RegisterSchemaOptions {
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("integrationtestuser", true)
}
.build()
2023-05-27 22:12:07 +02:00
.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(&reg).await.unwrap();
2023-05-29 23:46:43 +02:00
let guild = Guild::create(&mut user, guild_create_schema).await.unwrap();
2023-05-29 23:57:23 +02:00
let channel = Channel::create(&mut user, &guild.id.to_string(), channel_create_schema)
.await
.unwrap();
2023-05-27 22:12:07 +02:00
2023-06-10 00:23:49 +02:00
let role_create_schema: chorus::types::RoleCreateModifySchema = RoleCreateModifySchema {
name: Some("Bundle role".to_string()),
permissions: Some("8".to_string()), // Administrator permissions
hoist: Some(true),
icon: None,
unicode_emoji: Some("".to_string()),
mentionable: Some(true),
position: None,
color: None,
};
let guild_id = guild.id.clone().to_string();
let role = chorus::types::RoleObject::create(&mut user, &guild_id, role_create_schema)
.await
.unwrap();
2023-05-27 22:12:07 +02:00
TestBundle {
urls,
user,
instance,
2023-05-29 16:51:41 +02:00
guild,
2023-06-10 00:23:49 +02:00
role,
2023-05-27 22:12:07 +02:00
channel,
}
}
// Teardown method to clean up after a test.
pub async fn teardown(mut bundle: TestBundle) {
2023-05-29 23:51:12 +02:00
Guild::delete(&mut bundle.user, &bundle.guild.id.to_string()).await;
2023-05-27 22:12:07 +02:00
bundle.user.delete().await;
}