Skip to content

Commit 1611611

Browse files
committed
feat: handle payment error on trial
1 parent 4745854 commit 1611611

File tree

7 files changed

+770
-299
lines changed

7 files changed

+770
-299
lines changed

enterprise_access/apps/api_client/license_manager_client.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,61 @@ class LicenseManagerApiClient(BaseOAuthClient):
2828
subscription_provisioning_endpoint = api_base_url + 'provisioning-admins/subscriptions/'
2929
subscription_plan_renewal_provisioning_endpoint = api_base_url + 'provisioning-admins/subscription-plan-renewals/'
3030

31+
def list_subscriptions(self, enterprise_customer_uuid):
32+
"""
33+
List subscription plans for an enterprise.
34+
35+
Returns a paginated DRF list response: { count, next, previous, results: [...] }
36+
"""
37+
try:
38+
params = {
39+
'enterprise_customer_uuid': enterprise_customer_uuid,
40+
}
41+
42+
response = self.client.get(
43+
self.subscriptions_endpoint,
44+
params=params,
45+
timeout=settings.LICENSE_MANAGER_CLIENT_TIMEOUT,
46+
)
47+
response.raise_for_status()
48+
return response.json()
49+
except requests.exceptions.HTTPError as exc:
50+
logger.exception(
51+
'Failed to list subscriptions for enterprise %s, response: %s, exc: %s',
52+
enterprise_customer_uuid, safe_error_response_content(exc), exc,
53+
)
54+
raise
55+
56+
def update_subscription_plan(self, subscription_uuid, **payload):
57+
"""
58+
Partially update a subscription plan via the provisioning-admins endpoint.
59+
60+
Args:
61+
subscription_uuid (str): Subscription plan UUID.
62+
**payload: Fields to patch on the subscription (e.g. is_active, change_reason).
63+
64+
Returns:
65+
dict: JSON response from license-manager.
66+
"""
67+
if not payload:
68+
raise ValueError('Must supply payload to update subscription plan')
69+
70+
endpoint = f"{self.subscription_provisioning_endpoint}{subscription_uuid}/"
71+
try:
72+
response = self.client.patch(
73+
endpoint,
74+
json=payload,
75+
timeout=settings.LICENSE_MANAGER_CLIENT_TIMEOUT,
76+
)
77+
response.raise_for_status()
78+
return response.json()
79+
except requests.exceptions.HTTPError as exc:
80+
logger.exception(
81+
'Failed to update subscription %s, payload=%s, response: %s, exc: %s',
82+
subscription_uuid, payload, safe_error_response_content(exc), exc,
83+
)
84+
raise
85+
3186
def get_subscription_overview(self, subscription_uuid):
3287
"""
3388
Call license-manager API for data about a SubscriptionPlan.

enterprise_access/apps/api_client/tests/test_license_manager_client.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,49 @@ def test_create_customer_agreement(self, mock_oauth_client):
8888
json=expected_payload,
8989
)
9090

91+
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient', autospec=True)
92+
def test_list_subscriptions_params(self, mock_oauth_client):
93+
mock_get = mock_oauth_client.return_value.get
94+
mock_get.return_value.json.return_value = {'results': []}
95+
96+
lm_client = LicenseManagerApiClient()
97+
enterprise_uuid = 'ec-uuid-123'
98+
99+
# Should only set enterprise_customer_uuid parameter
100+
result = lm_client.list_subscriptions(enterprise_uuid)
101+
self.assertEqual(result, {'results': []})
102+
103+
# Verify URL and params
104+
expected_url = (
105+
'http://license-manager.example.com'
106+
'/api/v1/subscriptions/'
107+
)
108+
mock_get.assert_called_with(
109+
expected_url,
110+
params={'enterprise_customer_uuid': enterprise_uuid},
111+
timeout=settings.LICENSE_MANAGER_CLIENT_TIMEOUT,
112+
)
113+
114+
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient', autospec=True)
115+
def test_update_subscription_plan_patch(self, mock_oauth_client):
116+
mock_patch = mock_oauth_client.return_value.patch
117+
mock_patch.return_value.json.return_value = {'uuid': 'plan-uuid', 'is_active': False}
118+
119+
lm_client = LicenseManagerApiClient()
120+
payload = {'is_active': False, 'change_reason': 'delayed_payment'}
121+
result = lm_client.update_subscription_plan('plan-uuid', **payload)
122+
123+
self.assertEqual(result, mock_patch.return_value.json.return_value)
124+
expected_url = (
125+
'http://license-manager.example.com'
126+
'/api/v1/provisioning-admins/subscriptions/plan-uuid/'
127+
)
128+
mock_patch.assert_called_once_with(
129+
expected_url,
130+
json=payload,
131+
timeout=settings.LICENSE_MANAGER_CLIENT_TIMEOUT,
132+
)
133+
91134
@mock.patch('enterprise_access.apps.api_client.base_oauth.OAuthAPIClient', autospec=True)
92135
def test_create_subscription_plan(self, mock_oauth_client):
93136
mock_post = mock_oauth_client.return_value.post

0 commit comments

Comments
 (0)