chorus/tests/relationships.rs

141 lines
5.0 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use chorus::types::{self, Relationship, RelationshipType};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
mod common;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_get_mutual_relationships() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let username = user.object.read().unwrap().username.clone();
let discriminator = user.object.read().unwrap().discriminator.clone();
let other_user_id: types::Snowflake = other_user.object.read().unwrap().id;
let friend_request_schema = types::FriendRequestSendSchema {
username,
discriminator: Some(discriminator),
};
other_user
.send_friend_request(friend_request_schema)
.await
.unwrap();
let relationships = user.get_mutual_relationships(other_user_id).await.unwrap();
println!("{:?}", relationships);
common::teardown(bundle).await
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_get_relationships() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let username = user.object.read().unwrap().username.clone();
let discriminator = user.object.read().unwrap().discriminator.clone();
let friend_request_schema = types::FriendRequestSendSchema {
username,
discriminator: Some(discriminator),
};
other_user
.send_friend_request(friend_request_schema)
.await
.unwrap();
let relationships = user.get_relationships().await.unwrap();
assert_eq!(
relationships.first().unwrap().id,
other_user.object.read().unwrap().id
);
common::teardown(bundle).await
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_modify_relationship_friends() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let user_id: types::Snowflake = user.object.read().unwrap().id;
let other_user_id: types::Snowflake = other_user.object.read().unwrap().id;
other_user
.modify_user_relationship(user_id, types::RelationshipType::Friends)
.await
.unwrap();
let relationships = user.get_relationships().await.unwrap();
assert_eq!(
relationships.first().unwrap().id,
other_user.object.read().unwrap().id
);
assert_eq!(
relationships.first().unwrap().relationship_type,
RelationshipType::Incoming
);
let relationships = other_user.get_relationships().await.unwrap();
assert_eq!(
relationships.first().unwrap().id,
user.object.read().unwrap().id
);
assert_eq!(
relationships.first().unwrap().relationship_type,
RelationshipType::Outgoing
);
let _ = user
.modify_user_relationship(other_user_id, RelationshipType::Friends)
.await;
assert_eq!(
other_user
.get_relationships()
.await
.unwrap()
.first()
.unwrap()
.relationship_type,
RelationshipType::Friends
);
let _ = user.remove_relationship(other_user_id).await;
assert_eq!(
other_user.get_relationships().await.unwrap(),
Vec::<Relationship>::new()
);
common::teardown(bundle).await
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_modify_relationship_block() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser2").await;
let user = &mut bundle.user;
let user_id: types::Snowflake = user.object.read().unwrap().id;
other_user
.modify_user_relationship(user_id, types::RelationshipType::Blocked)
.await
.unwrap();
let relationships = user.get_relationships().await.unwrap();
assert_eq!(relationships, Vec::<Relationship>::new());
let relationships = other_user.get_relationships().await.unwrap();
assert_eq!(
relationships.first().unwrap().id,
user.object.read().unwrap().id
);
assert_eq!(
relationships.first().unwrap().relationship_type,
RelationshipType::Blocked
);
other_user.remove_relationship(user_id).await.unwrap();
assert_eq!(
other_user.get_relationships().await.unwrap(),
Vec::<Relationship>::new()
);
common::teardown(bundle).await
}