-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add support for cancelling subscriptions #46
Open
sjmiller609
wants to merge
2
commits into
MaterializeInc:main
Choose a base branch
from
sjmiller609:cancel-subscription
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,12 +42,13 @@ use tracing::info; | |
|
||
use orb_billing::{ | ||
AddIncrementCreditLedgerEntryRequestParams, AddVoidCreditLedgerEntryRequestParams, Address, | ||
AddressRequest, AmendEventRequest, Client, ClientConfig, CostViewMode, CreateCustomerRequest, | ||
CreateSubscriptionRequest, Customer, CustomerCostParams, CustomerCostPriceBlockPrice, | ||
CustomerId, CustomerPaymentProviderRequest, Error, Event, EventPropertyValue, | ||
EventSearchParams, IngestEventRequest, IngestionMode, InvoiceListParams, LedgerEntry, | ||
LedgerEntryRequest, ListParams, PaymentProvider, SubscriptionListParams, TaxId, TaxIdRequest, | ||
UpdateCustomerRequest, VoidReason, | ||
AddressRequest, AmendEventRequest, CancelOption, CancelSubscriptionParams, Client, | ||
ClientConfig, CostViewMode, CreateCustomerRequest, CreateSubscriptionRequest, Customer, | ||
CustomerCostParams, CustomerCostPriceBlockPrice, CustomerId, CustomerPaymentProviderRequest, | ||
Error, Event, EventPropertyValue, EventSearchParams, IngestEventRequest, IngestionMode, | ||
InvoiceListParams, LedgerEntry, LedgerEntryRequest, ListParams, PaymentProvider, | ||
SubscriptionListParams, SubscriptionStatus, TaxId, TaxIdRequest, UpdateCustomerRequest, | ||
VoidReason, | ||
}; | ||
|
||
/// The API key to authenticate with. | ||
|
@@ -186,7 +187,7 @@ async fn test_customers() { | |
.try_collect() | ||
.await | ||
.unwrap(); | ||
assert_eq!(balance.get(0).unwrap().balance, inc_res.ledger.amount); | ||
assert_eq!(balance.first().unwrap().balance, inc_res.ledger.amount); | ||
let ledger_res = client | ||
.create_ledger_entry( | ||
&customer.id, | ||
|
@@ -541,7 +542,7 @@ async fn test_events() { | |
.try_collect() | ||
.await | ||
.unwrap(); | ||
if events.get(0).map(|e| e.event_name.clone()) != Some("new test".into()) { | ||
if events.first().map(|e| e.event_name.clone()) != Some("new test".into()) { | ||
info!(" events list not updated after {iteration} attempts."); | ||
if iteration < MAX_LIST_RETRIES { | ||
continue; | ||
|
@@ -661,6 +662,67 @@ async fn test_subscriptions() { | |
.await | ||
.unwrap(); | ||
assert_eq!(fetched_subscriptions, &[subscriptions.remove(0)]); | ||
|
||
// Test immediate cancellation | ||
let subscription_to_cancel_immediately = subscriptions.pop().unwrap(); | ||
let immediate_cancel_params = CancelSubscriptionParams { | ||
cancel_option: CancelOption::Immediate, | ||
cancellation_date: None, | ||
}; | ||
let immediately_cancelled_subscription = client | ||
.cancel_subscription( | ||
&subscription_to_cancel_immediately.id, | ||
&immediate_cancel_params, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
immediately_cancelled_subscription.id, | ||
subscription_to_cancel_immediately.id | ||
); | ||
assert!(immediately_cancelled_subscription.end_date.is_some()); | ||
// The Orb API does not immediately update the status of a cancelled subscription. | ||
// But the end date is set to now when cancelled immediately. | ||
// assert_eq!(immediately_cancelled_subscription.status, Some(SubscriptionStatus::Ended)); | ||
Comment on lines
+685
to
+687
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, there are some frustrating latencies contained in the API. Depending on what you've seen, we'd be open to a sleep (as we do in |
||
|
||
// Test cancellation with a requested date | ||
let subscription_to_cancel_with_date = subscriptions.pop().unwrap(); | ||
let requested_date = OffsetDateTime::now_utc() + Duration::from_secs(60 * 60 * 24 * 7); | ||
let date_cancel_params = CancelSubscriptionParams { | ||
cancel_option: CancelOption::RequestedDate, | ||
cancellation_date: Some(requested_date), | ||
}; | ||
let date_cancelled_subscription = client | ||
.cancel_subscription(&subscription_to_cancel_with_date.id, &date_cancel_params) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
date_cancelled_subscription.id, | ||
subscription_to_cancel_with_date.id | ||
); | ||
assert!(date_cancelled_subscription.end_date.is_some()); | ||
let end_date = date_cancelled_subscription.end_date.unwrap(); | ||
assert!( | ||
end_date - requested_date < Duration::from_secs(60), | ||
"End date {:?} should be within 1 minute of requested date {:?}", | ||
end_date, | ||
requested_date | ||
); | ||
assert_eq!( | ||
date_cancelled_subscription.status, | ||
Some(SubscriptionStatus::Active) | ||
); | ||
|
||
// Test that cancelling an already cancelled subscription returns an error | ||
let result = client | ||
.cancel_subscription( | ||
&subscription_to_cancel_immediately.id, | ||
&immediate_cancel_params, | ||
) | ||
.await; | ||
assert_error_with_status_code(result, StatusCode::BAD_REQUEST); | ||
} | ||
|
||
#[test(tokio::test)] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For things that map to a POST body, we prefer to use "request"