Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plus): user subscription transitions #417

Merged
merged 10 commits into from
Feb 20, 2024
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
Loading