Skip to content

Commit

Permalink
fix(plus): user subscription transitions (#417)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
argl authored Feb 20, 2024
1 parent 604b0e7 commit 47b5a0b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/db/fxa_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -185,9 +185,16 @@ pub async fn update_subscription_state_from_webhook(
.values(fxa_event)
.returning(schema::webhook_events::id)
.get_result::<i64>(&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 {
Expand Down
7 changes: 5 additions & 2 deletions tests/api/fxa_webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 47b5a0b

Please sign in to comment.