-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
free sku integration for reconciliation worker
- Loading branch information
Showing
5 changed files
with
171 additions
and
36 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,7 @@ def lookup_subscription(self, webCustomerId, skuId): | |
if now_ms < end_date: | ||
logger.debug("subscription found for %s", str(skuId)) | ||
valid_subscriptions.append(subscription) | ||
return valid_subscriptions | ||
return valid_subscriptions if len(valid_subscriptions) > 0 else None | ||
return None | ||
|
||
def extend_subscription(self, subscription_id, endDate): | ||
|
@@ -171,6 +171,7 @@ def create_entitlement(self, customerId, sku): | |
"webCustomerId": customerId, | ||
} | ||
logger.debug("Created entitlement") | ||
|
||
try: | ||
r = requests.request( | ||
method="post", | ||
|
@@ -187,6 +188,30 @@ def create_entitlement(self, customerId, sku): | |
|
||
return r.status_code | ||
|
||
def remove_entitlement(self, subscription_id): | ||
""" | ||
Removes subscription from user given subscription_id | ||
""" | ||
request_url = ( | ||
f"{self.marketplace_endpoint}/subscription/v5/terminateSubscription/{subscription_id}" | ||
) | ||
request_headers = {"Content-Type": "application/json"} | ||
|
||
logger.debug("Terminating subscription with id %s", subscription_id) | ||
try: | ||
r = requests.request( | ||
method="post", | ||
url=request_url, | ||
cert=self.cert, | ||
headers=request_headers, | ||
verify=True, | ||
timeout=REQUEST_TIMEOUT, | ||
) | ||
except requests.exceptions.ReadTimeout: | ||
logger.info("request to %s timed out", self.marketplace_endpoint) | ||
return 408 | ||
return r.status_code | ||
|
||
def get_subscription_details(self, subscription_id): | ||
""" | ||
Return the sku and expiration date for a specific subscription | ||
|
@@ -269,7 +294,6 @@ def get_list_of_subscriptions( | |
|
||
# Mocked classes for unit tests | ||
|
||
|
||
TEST_USER = { | ||
"account_number": 12345, | ||
"email": "[email protected]", | ||
|
@@ -358,6 +382,45 @@ def get_list_of_subscriptions( | |
"email": "[email protected]", | ||
"username": "free_user", | ||
} | ||
PAID_USER = { | ||
"account_number": 34567, | ||
"email": "[email protected]", | ||
"username": "paid_user", | ||
"subscriptions": [ | ||
{ | ||
"id": 12345678, | ||
"masterEndSystemName": "Quay", | ||
"createdEndSystemName": "SUBSCRIPTION", | ||
"createdDate": 1675957362000, | ||
"lastUpdateEndSystemName": "SUBSCRIPTION", | ||
"lastUpdateDate": 1675957362000, | ||
"installBaseStartDate": 1707368400000, | ||
"installBaseEndDate": 1707368399000, | ||
"webCustomerId": 123456, | ||
"subscriptionNumber": "12399889", | ||
"quantity": 1, | ||
"effectiveStartDate": 1707368400000, | ||
"effectiveEndDate": 3813177600000, | ||
} | ||
], | ||
"free_sku": [ | ||
{ | ||
"id": 56781234, | ||
"masterEndSystemName": "Quay", | ||
"createdEndSystemName": "SUBSCRIPTION", | ||
"createdDate": 1675957362000, | ||
"lastUpdateEndSystemName": "SUBSCRIPTION", | ||
"lastUpdateDate": 1675957362000, | ||
"installBaseStartDate": 1707368400000, | ||
"installBaseEndDate": 1707368399000, | ||
"webCustomerId": 123456, | ||
"subscriptionNumber": "12399889", | ||
"quantity": 1, | ||
"effectiveStartDate": 1707368400000, | ||
"effectiveEndDate": 3813177600000, | ||
} | ||
], | ||
} | ||
|
||
|
||
class FakeUserApi(RedHatUserApi): | ||
|
@@ -368,6 +431,8 @@ class FakeUserApi(RedHatUserApi): | |
def lookup_customer_id(self, email): | ||
if email == TEST_USER["email"]: | ||
return [TEST_USER["account_number"]] | ||
if email == PAID_USER["email"]: | ||
return [PAID_USER["account_number"]] | ||
if email == FREE_USER["email"]: | ||
return [FREE_USER["account_number"]] | ||
if email == STRIPE_USER["email"]: | ||
|
@@ -391,11 +456,20 @@ def lookup_subscription(self, customer_id, sku_id): | |
return [TEST_USER["private_subscription"]] | ||
elif customer_id == TEST_USER["account_number"] and sku_id == "MW00584MO": | ||
return [TEST_USER["reconciled_subscription"]] | ||
elif customer_id == PAID_USER["account_number"] and sku_id == "MW02701": | ||
return PAID_USER["subscriptions"] | ||
elif customer_id == PAID_USER["account_number"] and sku_id == "MW04192": | ||
return PAID_USER["free_sku"] | ||
elif customer_id == FREE_USER["account_number"]: | ||
return [] | ||
return None | ||
|
||
def create_entitlement(self, customer_id, sku_id): | ||
self.subscription_created = True | ||
|
||
def remove_entitlement(self, subscription_id): | ||
pass | ||
|
||
def extend_subscription(self, subscription_id, end_date): | ||
self.subscription_extended = True | ||
|
||
|
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 |
---|---|---|
|
@@ -19,7 +19,18 @@ def test_skip_free_user(initialized_db): | |
with patch.object(marketplace_subscriptions, "create_entitlement") as mock: | ||
worker._perform_reconciliation(marketplace_users, marketplace_subscriptions) | ||
|
||
mock.assert_not_called() | ||
# adding the free tier | ||
mock.assert_called_with(23456, "MW04192") | ||
|
||
|
||
def test_remove_free_tier(initialized_db): | ||
# if a user has a sku and also has a free tier, the free tier should be removed | ||
paid_user = model.user.create_user("paid_user", "password", "[email protected]") | ||
paid_user.save() | ||
marketplace_subscriptions.create_entitlement(12345, "MW04192") | ||
with patch.object(marketplace_subscriptions, "remove_entitlement") as mock: | ||
worker._perform_reconciliation(marketplace_users, marketplace_subscriptions) | ||
mock.assert_called_with(56781234) # fake "free" tier subscription id mocked in marketplace.py | ||
|
||
|
||
def test_reconcile_org_user(initialized_db): | ||
|
@@ -77,12 +88,12 @@ def test_reconcile_different_ids(initialized_db): | |
test_user = model.user.create_user("stripe_user", "password", "[email protected]") | ||
test_user.stripe_id = "cus_" + "".join(random.choices(string.ascii_lowercase, k=14)) | ||
test_user.save() | ||
model.entitlements.save_web_customer_id(test_user, 12345) | ||
model.entitlements.save_web_customer_id(test_user, 55555) | ||
|
||
worker._perform_reconciliation(marketplace_users, marketplace_subscriptions) | ||
|
||
new_id = model.entitlements.get_web_customer_ids(test_user.id) | ||
assert new_id != [12345] | ||
assert new_id != [55555] | ||
assert new_id == marketplace_users.lookup_customer_id(test_user.email) | ||
|
||
# make sure it will remove account numbers from db that do not belong | ||
|