From 24e2f3996a261d54b122f882aa0754bbdb1f1eb9 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 16 Apr 2021 13:57:25 +0200 Subject: [PATCH] client-api: Fix tests for unstable-pre-spec --- .../src/r0/session/get_login_types.rs | 67 +++++++++++-------- .../src/r0/session/sso_login_with_provider.rs | 4 +- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/ruma-client-api/src/r0/session/get_login_types.rs b/ruma-client-api/src/r0/session/get_login_types.rs index cc429036..3f0fa403 100644 --- a/ruma-client-api/src/r0/session/get_login_types.rs +++ b/ruma-client-api/src/r0/session/get_login_types.rs @@ -257,6 +257,7 @@ mod login_type_serde; #[cfg(test)] mod tests { + use matches::assert_matches; use serde::{Deserialize, Serialize}; #[cfg(feature = "unstable-pre-spec")] use serde_json::to_value as to_json_value; @@ -266,28 +267,29 @@ mod tests { use super::{IdentityProvider, IdentityProviderBrand, SsoLoginType, TokenLoginType}; use super::{LoginType, PasswordLoginType}; - #[derive(Debug, Eq, PartialEq, Deserialize, Serialize)] + #[derive(Debug, Deserialize, Serialize)] struct Foo { pub flows: Vec, } #[test] fn deserialize_password_login_type() { - assert_eq!( + assert_matches!( from_json_value::(json!({ "flows": [ { "type": "m.login.password" } ], - })) - .unwrap(), - Foo { flows: vec![LoginType::Password(PasswordLoginType {})] } + })), + Ok(Foo { flows }) + if flows.len() == 1 + && matches!(flows[0], LoginType::Password(PasswordLoginType {})) ); } #[test] #[cfg(feature = "unstable-pre-spec")] fn deserialize_sso_login_type() { - let foo = from_json_value::(json!({ + let mut foo = from_json_value::(json!({ "flows": [ { "type": "m.login.sso", @@ -308,27 +310,38 @@ mod tests { })) .unwrap(); - assert_eq!( - foo, - Foo { - flows: vec![LoginType::Sso(SsoLoginType { - identity_providers: vec![ - IdentityProvider { - id: "oidc-gitlab".into(), - name: "GitLab".into(), - icon: Some("mxc://localhost/gitlab-icon".into()), - brand: Some(IdentityProviderBrand::GitLab) - }, - IdentityProvider { - id: "custom".into(), - name: "Custom".into(), - icon: None, - brand: None - } - ] - })] - } - ) + let mut flow = foo.flows.pop(); + assert_matches!(foo.flows.as_slice(), []); + + let mut identity_providers = match flow { + Some(LoginType::Sso(SsoLoginType { identity_providers })) => identity_providers, + _ => panic!("unexpected enum variant: {:?}", flow), + }; + + let provider = identity_providers.pop(); + assert_matches!( + provider, + Some(IdentityProvider { + id, + name, + icon: None, + brand: None, + }) if id == "custom" + && name == "Custom" + ); + + let provider = identity_providers.pop(); + assert_matches!( + provider, + Some(IdentityProvider { + id, + name, + icon: Some(icon), + brand: Some(IdentityProviderBrand::GitLab), + }) if id == "oidc-gitlab" + && name == "GitLab" + && icon.to_string() == "mxc://localhost/gitlab-icon" + ); } #[test] diff --git a/ruma-client-api/src/r0/session/sso_login_with_provider.rs b/ruma-client-api/src/r0/session/sso_login_with_provider.rs index c7e26919..650fcabf 100644 --- a/ruma-client-api/src/r0/session/sso_login_with_provider.rs +++ b/ruma-client-api/src/r0/session/sso_login_with_provider.rs @@ -51,9 +51,9 @@ impl Response { } } -#[cfg(test)] +#[cfg(all(test, feature = "client"))] mod tests { - use ruma_api::OutgoingRequest; + use ruma_api::OutgoingRequest as _; use super::Request;