chorus/tests/common/mod.rs

130 lines
3.9 KiB
Rust
Raw Normal View History

2023-07-28 17:33:23 +02:00
use chorus::gateway::Gateway;
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-07-28 17:33:23 +02:00
RoleCreateModifySchema, RoleObject,
2023-06-10 00:23:49 +02:00
},
2023-06-20 02:59:18 +02:00
UrlBundle,
2023-05-27 22:12:07 +02:00
};
2023-07-28 17:33:23 +02:00
#[allow(dead_code)]
2023-05-27 22:12:07 +02:00
#[derive(Debug)]
2023-07-28 17:33:23 +02:00
pub(crate) struct TestBundle {
2023-06-20 02:59:18 +02:00
pub urls: UrlBundle,
2023-05-27 22:12:07 +02:00
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,
}
2023-07-28 17:33:23 +02:00
#[allow(unused)]
impl TestBundle {
pub(crate) async fn create_user(&mut self, username: &str) -> UserMeta {
let register_schema = RegisterSchema {
username: username.to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..Default::default()
};
self.instance
.register_account(&register_schema)
.await
.unwrap()
}
pub(crate) async fn clone_user_without_gateway(&self) -> UserMeta {
UserMeta {
belongs_to: self.user.belongs_to.clone(),
token: self.user.token.clone(),
limits: self.user.limits.clone(),
settings: self.user.settings.clone(),
object: self.user.object.clone(),
gateway: Gateway::new(self.instance.urls.wss.clone()).await.unwrap(),
}
}
}
2023-05-27 22:12:07 +02:00
// Set up a test by creating an Instance and a User. Reduces Test boilerplate.
2023-07-28 17:33:23 +02:00
pub(crate) async fn setup() -> TestBundle {
2023-06-20 02:59:18 +02:00
let urls = UrlBundle::new(
2023-05-27 22:12:07 +02:00
"http://localhost:3001/api".to_string(),
"ws://localhost:3001".to_string(),
"http://localhost:3001".to_string(),
);
2023-07-28 17:33:23 +02:00
let mut instance = Instance::new(urls.clone(), true).await.unwrap();
2023-05-27 22:12:07 +02:00
// Requires the existance of the below user.
2023-07-28 17:33:23 +02:00
let reg = RegisterSchema {
username: "integrationtestuser".into(),
consent: true,
2023-06-19 10:27:32 +02:00
date_of_birth: Some("2000-01-01".to_string()),
2023-07-28 17:33:23 +02:00
..Default::default()
};
2023-05-27 22:12:07 +02:00
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-06-22 13:14:07 +02:00
let channel = Channel::create(&mut user, guild.id, channel_create_schema)
2023-05-29 23:57:23 +02:00
.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,
};
2023-06-22 13:14:07 +02:00
let role = chorus::types::RoleObject::create(&mut user, guild.id, role_create_schema)
2023-06-10 00:23:49 +02:00
.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.
2023-06-19 19:01:18 +02:00
#[allow(dead_code)]
2023-07-28 17:33:23 +02:00
pub(crate) async fn teardown(mut bundle: TestBundle) {
2023-06-22 13:14:07 +02:00
Guild::delete(&mut bundle.user, bundle.guild.id)
.await
.unwrap();
bundle.user.delete().await.unwrap()
2023-05-27 22:12:07 +02:00
}