Merge pull request #63 from polyphony-chat/refactor/restructuring-of-tests

Refactor/restructuring of tests
This commit is contained in:
Flori 2023-05-21 21:20:28 +02:00 committed by GitHub
commit f599a3f229
4 changed files with 107 additions and 51 deletions

View File

@ -16,4 +16,7 @@ custom_error = "1.9.2"
native-tls = "0.2.11" native-tls = "0.2.11"
tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]} tokio-tungstenite = {version = "0.18.0", features = ["native-tls"]}
futures-util = "0.3.28" futures-util = "0.3.28"
http = "0.2.9" http = "0.2.9"
[dev-dependencies]
lazy_static = "1.4.0"

View File

@ -114,53 +114,3 @@ impl<'a> types::Guild {
} }
} }
} }
#[cfg(test)]
mod test {
use crate::api::schemas;
use crate::api::types;
use crate::instance::Instance;
#[tokio::test]
async fn guild_creation_deletion() {
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: schemas::LoginSchema = schemas::LoginSchema::new(
schemas::AuthUsername::new("user@test.xyz".to_string()).unwrap(),
"transrights".to_string(),
None,
None,
None,
None,
)
.unwrap();
let mut user = instance.login_account(&login_schema).await.unwrap();
let guild_create_schema = schemas::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 =
types::Guild::create(&mut user, "http://localhost:3001/api", guild_create_schema)
.await
.unwrap();
println!("{}", guild);
match types::Guild::delete(&mut user, "http://localhost:3001/api", guild).await {
None => assert!(true),
Some(_) => assert!(false),
}
}
}

View File

@ -125,6 +125,35 @@ impl User {
); );
Ok(user_updated) Ok(user_updated)
} }
/// Sends a request to the server which deletes the user from the Instance.
///
/// # Arguments
///
/// * `self` - The `User` object to delete.
///
/// # Returns
///
/// Returns `None` if the user was successfully deleted, or an `InstanceServerError` if an error occurred.
pub async fn delete(mut self) -> Option<InstanceServerError> {
let mut belongs_to = self.belongs_to.borrow_mut();
let request = Client::new()
.post(format!("{}/users/@me/delete/", belongs_to.urls.get_api()))
.bearer_auth(self.token);
match LimitedRequester::new()
.await
.send_request(
request,
crate::api::limits::LimitType::Global,
&mut belongs_to.limits,
&mut self.limits,
)
.await
{
Ok(_) => None,
Err(e) => Some(e),
}
}
} }
impl Instance { impl Instance {

74
tests/integration.rs Normal file
View File

@ -0,0 +1,74 @@
use chorus::{
api::{AuthUsername, RegisterSchema, User},
instance::Instance,
URLBundle,
};
#[derive(Debug)]
struct TestBundle {
urls: URLBundle,
user: User,
}
// 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(
AuthUsername::new("integrationtestuser".to_string()).unwrap(),
None,
true,
None,
None,
None,
Some("2000-01-01".to_string()),
None,
None,
None,
)
.unwrap();
let user = instance.register_account(&reg).await.unwrap();
TestBundle { urls, user }
}
// Teardown method to clean up after a test.
async fn teardown(bundle: TestBundle) {
bundle.user.delete().await;
}
mod guild {
use chorus::api::{schemas, types};
#[tokio::test]
async fn guild_creation_deletion() {
let mut bundle = crate::setup().await;
let guild_create_schema = schemas::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 =
types::Guild::create(&mut bundle.user, bundle.urls.get_api(), guild_create_schema)
.await
.unwrap();
println!("{}", guild);
match types::Guild::delete(&mut bundle.user, bundle.urls.get_api(), guild).await {
None => assert!(true),
Some(_) => assert!(false),
}
}
}