chorus/tests/common/mod.rs

102 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-25 11:33:50 +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
};
#[derive(Debug)]
pub 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,
}
// Set up a test by creating an Instance and a User. Reduces Test boilerplate.
pub 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(),
);
Ratelimiter overhaul (#144) * Rename limits and limit to have better names * Remove empty lines * Remove handle_request (moved to requestlimiter) * Start working on new ratelimiter * Make limits Option, add "limited?" to constructor * Add missing logic to send_request * Rename Limits * Create Ratelimits and Limit Struct * Define Limit * Import Ratelimits * Define get_rate_limits * Remove unused import * + check_rate_limits & limits_config_to_ratelimits * Remove Absolute Limits These limits are not meant to be tracked anyways. * add ratelimits is_exhausted * Add error handling and send request checking * change limits to option ratelimits * Add strum * Change Ratelimits to Hashmap * Remove ratelimits in favor of hashmap * Change code from struct to hashmap * start working on update rate limits * Remove wrong import * Rename ChorusLibError to ChorusError * Documented the chorus errors * Made error documentation docstring * Make ReceivedErrorCodeError have error string * Remove unneeded import * Match changes in errors.rs * Improve update_rate_limits and can_send_request * add ratelimits.to_hash_map() * use instances' client instead of new client * add LimitsConfiguration to instance * improve update_limits, change a method name * Fix un-updated errors * Get LimitConfiguration in a sane way * Move common.rs into ratelimiter::ChorusRequest * Delete common.rs * Make instance.rs use overhauled errors * Refactor to use new Rate limiting implementation * Refactor to use new Rate limiting implementation * Refactor to use new Rate limiting implementation * Refactor to use new Rate limiting implementation * Refactor to use new Rate limiting implementation * Refactor to use new Rate limiting implementation * update ratelimiter implementation across all files * Fix remaining errors post-refactor * Changed Enum case to be correct * Use result * Re-add missing body to request * Remove unneeded late initalization * Change visibility from pub to pub(crate) I feel like these core methods don't need to be exposed as public API. * Remove unnecessary import * Fix clippy warnings * Add docstring * Change Error names across all files * Update Cargo.toml Strum is not needed * Update ratelimits.rs * Update ratelimits.rs * Bug/discord instance info unavailable (#146) * Change text to be more ambigous * Use default Configuration instead of erroring out * Emit warning log if instance config cant be gotten * Remove import * Update src/instance.rs Co-authored-by: SpecificProtagonist <specificprotagonist@posteo.org> * Add missing closing bracket * Put limits and limits_configuration as one struct * Derive Hash * remove import * rename limits and limits_configuration * Save clone call * Change LimitsConfiguration to RateLimits `LimitsConfiguration` is in no way related to whether the instance has API rate limits enabled or not. Therefore, it has been replaced with what it should have been all along. * Add ensure_limit_in_map(), add `window` to `Limit` * Remove unneeded var * Remove import * Clean up unneeded things Dead code warnings have been supressed, but flagged as FIXME so they don't get forgotten. Anyone using tools like TODO Tree in VSCode can still see that they are there, however, they will not be shown as warnings anymore * Remove nested submodule `limit` * Add doc comments * Add more doc comments * Add some log messages to some methods --------- Co-authored-by: SpecificProtagonist <specificprotagonist@posteo.org>
2023-07-09 18:38:02 +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-06-25 11:33:50 +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-06-25 11:33:50 +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-05-27 22:12:07 +02:00
pub 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
}