Compare commits

...

2 Commits

Author SHA1 Message Date
kozabrada123 5fc1457f9c
Merge 53f08ff1f9 into ec9541f38e 2024-08-21 18:29:51 +02:00
kozabrada123 53f08ff1f9 add some tests, minor connection updates
- Document that create_domain_connection is unimplemented on Spacebar

- Add Discord connection type

- Change ConnectionType::array() into ConnectionType::vector() - returning a fixed size array is dubious, since it'll likely be expanded. Returning a vector is easier keep up to variable length

- Add ConnectionType::discord_vector() and ConnectionType::spacebar_vector() to return a vector ConnectionTypes available on the respective server backends

- add tests test_modify_user_profile, test_disable_user, test_get_user_note, test_set_user_note, test_get_user_affinities, test_get_guild_affinities, test_get_connections

Note: connections are hard to test, since they require secrets / an external service's account
2024-08-21 18:26:11 +02:00
3 changed files with 233 additions and 10 deletions

View File

@ -133,6 +133,8 @@ impl ChorusUser {
/// To create normal connection types, see [Self::authorize_connection] and /// To create normal connection types, see [Self::authorize_connection] and
/// [Self::create_connection_callback] /// [Self::create_connection_callback]
/// ///
/// As of 2024/08/21, Spacebar does not yet implement this endpoint.
///
/// # Examples /// # Examples
/// ```no_run /// ```no_run
/// let domain = "example.com".to_string(); /// let domain = "example.com".to_string();

View File

@ -91,6 +91,8 @@ impl From<Connection> for PublicConnection {
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
/// A type of connection; the service the connection is for /// A type of connection; the service the connection is for
/// ///
/// Note: this is subject to change, and the enum is likely non-exhaustive
///
/// # Reference /// # Reference
/// See <https://docs.discord.sex/resources/user#connection-type> /// See <https://docs.discord.sex/resources/user#connection-type>
pub enum ConnectionType { pub enum ConnectionType {
@ -105,6 +107,8 @@ pub enum ConnectionType {
/// (Not returned in Get User Profile or when fetching connections) /// (Not returned in Get User Profile or when fetching connections)
Contacts, Contacts,
Crunchyroll, Crunchyroll,
/// Note: spacebar only
Discord,
Domain, Domain,
Ebay, Ebay,
EpicGames, EpicGames,
@ -151,9 +155,42 @@ impl Display for ConnectionType {
} }
impl ConnectionType { impl ConnectionType {
/// Returns an array of all the connections /// Returns an vector of all the connection types
pub fn array() -> [ConnectionType; 25] { // API note: this could be an array, but it is subject to change.
[ pub fn vector() -> Vec<ConnectionType> {
vec![
ConnectionType::AmazonMusic,
ConnectionType::BattleNet,
ConnectionType::Bungie,
ConnectionType::Contacts,
ConnectionType::Crunchyroll,
ConnectionType::Discord,
ConnectionType::Domain,
ConnectionType::Ebay,
ConnectionType::EpicGames,
ConnectionType::Facebook,
ConnectionType::GitHub,
ConnectionType::Instagram,
ConnectionType::LeagueOfLegends,
ConnectionType::PayPal,
ConnectionType::Playstation,
ConnectionType::Reddit,
ConnectionType::RiotGames,
ConnectionType::Samsung,
ConnectionType::Spotify,
ConnectionType::Skype,
ConnectionType::Steam,
ConnectionType::TikTok,
ConnectionType::Twitch,
ConnectionType::Twitter,
ConnectionType::Xbox,
ConnectionType::YouTube,
]
}
/// Returns an vector of all the connection types available on discord
pub fn discord_vector() -> Vec<ConnectionType> {
vec![
ConnectionType::AmazonMusic, ConnectionType::AmazonMusic,
ConnectionType::BattleNet, ConnectionType::BattleNet,
ConnectionType::Bungie, ConnectionType::Bungie,
@ -181,6 +218,23 @@ impl ConnectionType {
ConnectionType::YouTube, ConnectionType::YouTube,
] ]
} }
/// Returns an vector of all the connection types available on spacebar
pub fn spacebar_vector() -> Vec<ConnectionType> {
vec![
ConnectionType::BattleNet,
ConnectionType::Discord,
ConnectionType::EpicGames,
ConnectionType::Facebook,
ConnectionType::GitHub,
ConnectionType::Reddit,
ConnectionType::Spotify,
ConnectionType::Twitch,
ConnectionType::Twitter,
ConnectionType::Xbox,
ConnectionType::YouTube,
]
}
} }
#[derive( #[derive(

View File

@ -2,7 +2,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // 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/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use chorus::types::{PublicUser, Snowflake, User}; use chorus::{
errors::ChorusError,
types::{
ConnectionType, DeleteDisableUserSchema, PublicUser, Snowflake, User,
UserModifyProfileSchema, UserNote,
},
};
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*; use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
@ -43,3 +49,164 @@ async fn test_get_user_profile() {
common::teardown(bundle).await; 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_user_profile() {
let mut bundle = common::setup().await;
let bio = Some(String::from("A user."));
let pronouns = Some(String::from("they/them"));
let modify = UserModifyProfileSchema {
bio: bio.clone(),
pronouns: pronouns.clone(),
..Default::default()
};
bundle.user.modify_profile(modify).await.unwrap();
let user_id = bundle.user.object.read().unwrap().id;
let user_profile = bundle
.user
.get_user_profile(user_id, chorus::types::GetUserProfileSchema::default())
.await
.unwrap();
assert_eq!(user_profile.profile_metadata.bio, bio);
assert_eq!(user_profile.profile_metadata.pronouns, pronouns.unwrap());
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_disable_user() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser4").await;
other_user
.disable(DeleteDisableUserSchema { password: None })
.await
.unwrap();
common::teardown(bundle).await;
}
// Note: these two tests are currently broken.
// FIXME: readd them once bitfl0wer/server#2 is merged
/*
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_get_user_note() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser3").await;
let user_id = bundle.user.object.read().unwrap().id;
let other_user_id = other_user.object.read().unwrap().id;
let result = bundle.user.get_user_note(other_user_id).await;
assert!(matches!(
result.err().unwrap(),
ChorusError::NotFound { .. }
));
bundle
.user
.set_user_note(other_user_id, Some(String::from("A note.")))
.await
.unwrap();
assert!(false);
let result = bundle.user.get_user_note(other_user_id).await;
assert_eq!(
result,
Ok(UserNote {
user_id,
note_user_id: other_user_id,
note: String::from("A note.")
})
);
other_user
.delete(DeleteDisableUserSchema { password: None })
.await
.unwrap();
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_set_user_note() {
let mut bundle = common::setup().await;
let mut other_user = bundle.create_user("integrationtestuser3").await;
let user_id = bundle.user.object.read().unwrap().id;
let other_user_id = other_user.object.read().unwrap().id;
bundle
.user
.set_user_note(other_user_id, Some(String::from("A note.")))
.await
.unwrap();
let result = bundle.user.get_user_note(other_user_id).await;
assert_eq!(
result,
Ok(UserNote {
user_id,
note_user_id: other_user_id,
note: String::from("A note.")
})
);
other_user
.delete(DeleteDisableUserSchema { password: None })
.await
.unwrap();
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_user_affinities() {
let mut bundle = common::setup().await;
let result = bundle.user.get_user_affinities().await.unwrap();
assert!(result.user_affinities.is_empty());
assert!(result.inverse_user_affinities.is_empty());
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_guild_affinities() {
let mut bundle = common::setup().await;
let result = bundle.user.get_guild_affinities().await.unwrap();
assert!(result.guild_affinities.is_empty());
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_connections() {
let mut bundle = common::setup().await;
let result = bundle.user.get_connections().await.unwrap();
// We can't *really* test creating or getting connections...
// TODO: Find a way?
assert!(result.is_empty());
common::teardown(bundle).await;
}