From 47b5a0b1d3d3bb8ed3267ccc4e9546c090fab33a Mon Sep 17 00:00:00 2001 From: Andi Pieper Date: Tue, 20 Feb 2024 14:50:14 +0100 Subject: [PATCH] fix(plus): user subscription transitions (#417) * database migration, deprecations * implement sub state transition recording, test scaffolding * implement a helper function to encode webhook payloads * mores tests, moved fixtures around, amended docs for new set_token helper * tracking some master changes * Fix a problem with unknown capabilities getting passed in from the subscription change webhook * Skip creating a new vector, get the first element directly from the iterator. * Follow Clippy's hints --- src/db/fxa_webhook.rs | 13 ++++++++++--- tests/api/fxa_webhooks.rs | 7 +++++-- 2 files changed, 15 insertions(+), 5 deletions(-) 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;