diff --git a/src/db/fxa_webhook.rs b/src/db/fxa_webhook.rs index 8a4f6293..99fba3d5 100644 --- a/src/db/fxa_webhook.rs +++ b/src/db/fxa_webhook.rs @@ -8,7 +8,7 @@ use crate::db::settings::create_or_update_settings; use crate::db::types::FxaEvent; use crate::db::users::get_user_opt; use crate::db::{schema, Pool}; -use crate::fxa::LoginManager; +use crate::fxa::{self, LoginManager}; use actix_rt::ArbiterHandle; use actix_web::web; use basket::Basket; @@ -185,9 +185,16 @@ pub async fn update_subscription_state_from_webhook( .values(fxa_event) .returning(schema::webhook_events::id) .get_result::(&mut conn)?; - let subscription: Subscription = match (update.is_active, update.capabilities.first()) { + // Filter out any unknown subscription types we get passed in + // before we try to get the interesting first element of the + // capabilities array. + let capability = update + .capabilities + .into_iter() + .find(|&c| c != fxa::types::Subscription::Unknown); + let subscription: Subscription = match (update.is_active, capability) { (false, _) => Subscription::Core, - (true, Some(c)) => Subscription::from(*c), + (true, Some(c)) => Subscription::from(c), (true, None) => Subscription::Core, }; if subscription == Subscription::Core { diff --git a/tests/api/fxa_webhooks.rs b/tests/api/fxa_webhooks.rs index 2cce4765..7f3387fe 100644 --- a/tests/api/fxa_webhooks.rs +++ b/tests/api/fxa_webhooks.rs @@ -414,17 +414,20 @@ async fn record_subscription_state_transitions_test() -> Result<(), Error> { ); } - // Now create a later transition to 10m and check the table again + // Now create a later transition to 10m and check the table again. { let json_str = std::fs::read_to_string( "tests/data/set_tokens/set_token_subscription_state_change_to_10m.json", ) .unwrap(); let mut claim: Value = serde_json::from_str(&json_str).unwrap(); - // add time to the event to be sure it is after the previous event + // Add some time to the event to be sure it is after the previous event. claim["iat"] = json!(1654425317000i64 + 300000); claim["events"]["https://schemas.accounts.firefox.com/event/subscription-state-change"] ["changeTime"] = json!(1654425317000i64 + 300000); + // We also add some unknown capability to the event to check that they are ignored correctly. + claim["events"]["https://schemas.accounts.firefox.com/event/subscription-state-change"] + ["capabilities"] = json!(["something_unknown", "mdn_plus_10m"]); let set_token = token_from_claim(&claim); let res = logged_in_client.trigger_webhook(&set_token).await;