Skip to content

Commit 3ab83e0

Browse files
author
joscasta
committed
Adding Service Subscription and Service Plan resources and tests for Service Subscription
1 parent b3ab1ee commit 3ab83e0

File tree

3 files changed

+140
-2
lines changed

3 files changed

+140
-2
lines changed

tests/integration/conftest.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import threescale_api
1212
from threescale_api.resources import (Service, ApplicationPlan, Application,
1313
Proxy, Backend, Metric, MappingRule,
14-
BackendMappingRule, BackendUsage,
14+
BackendMappingRule, Account, BackendUsage,
1515
ActiveDoc, Webhooks, InvoiceState,
16-
ApplicationKey, ApplicationPlans, AccountUser, AccountUsers)
16+
ApplicationKey, ApplicationPlans, AccountUser, AccountUsers, ServiceSubscription)
1717

1818
load_dotenv()
1919

@@ -148,6 +148,16 @@ def account_user(account,account_user_params) -> AccountUser:
148148
yield user
149149
cleanup(user)
150150

151+
@pytest.fixture(scope='module')
152+
def service_subscription_params(account) -> dict:
153+
#suffix = get_suffix()
154+
return dict(plan_id=account['id'])
155+
156+
@pytest.fixture(scope='module')
157+
def service_subscription(account, service_subscription_params) -> ServiceSubscription:
158+
resource = account.service_subscriptions.create(params=service_subscription_params)
159+
yield resource
160+
151161
@pytest.fixture(scope='module')
152162
def application_plan_params() -> dict:
153163
suffix = get_suffix()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pytest
2+
import backoff
3+
4+
from tests.integration.conftest import account
5+
from threescale_api.errors import ApiClientError
6+
7+
from tests.integration import asserts
8+
9+
def test_should_create_service_subscription(account, service_subscription, service_subscription_params):
10+
11+
account.service_subscriptions.create(params=service_subscription_params)
12+
resource = service_subscription.get(service_subscription_params)
13+
asserts.assert_resource(resource)
14+
asserts.assert_resource_params(account, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id'])
15+
16+
def test_should_read_service_subscription(account, service_subscription, service_subscription_params):
17+
resource = account.service_subscriptions.get(params=service_subscription_params)
18+
asserts.assert_resource(resource)
19+
asserts.assert_resource_params(resource, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id'])
20+
21+
#def test_should_update_service_subscription(account, service_subscription, service_subscription_params):
22+
# service_subscription_params['plan_id'] = 100
23+
# resource = account.service_subscriptions.update(service_subscription.entity_id, service_subscription_params)
24+
# asserts.assert_resource(resource)
25+
# asserts.assert_resource_params(resource, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id'])
26+
# resource_updated = account.service_subscription.read(params=service_subscription_params)
27+
# asserts.assert_resource(resource_updated)
28+
# asserts.assert_resource_params(resource_updated, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id'])
29+
30+
#def test_should_approve_service_subscription(account, service_subscription, service_subscription_params):
31+
# resource = account.service_subscriptions.approve_service_subscription(params=service_subscription_params)
32+
# asserts.assert_resource(resource)
33+
# read = account.service_subscriptions.read(service_subscription.entity_id)
34+
# asserts.assert_resource(read)
35+
36+
def test_list_service_subscriptions(account):
37+
resource = account.service_subscriptions.list()
38+
assert len(resource) >= 1
39+

threescale_api/resources.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from typing import Dict, Union, List, Iterable
44
from urllib.parse import quote_plus
55

6+
from dill.pointers import parent
7+
68
from threescale_api import auth
79
from threescale_api import utils
810
from threescale_api import errors
@@ -273,6 +275,59 @@ def pending(self, entity_id, **kwargs) -> 'Account':
273275
"""
274276
return self.set_state(entity_id=entity_id, state='make_pending', **kwargs)
275277

278+
class ServiceSubscriptions(DefaultClient):
279+
def __init__(self, *args, entity_name='service_subscription', entity_collection='service_subscriptions',
280+
per_page=None, **kwargs):
281+
super().__init__(*args, entity_name=entity_name,
282+
entity_collection=entity_collection, **kwargs)
283+
284+
@property
285+
def url(self) -> str:
286+
return self.parent.url + '/service_subscriptions'
287+
288+
def approve_service_subscription(self, entity_id: int, **kwargs):
289+
url = self.url + f"/{entity_id}/approve.json"
290+
response = self.rest.put(url=url, **kwargs)
291+
instance = utils.extract_response(response=response)
292+
return instance
293+
294+
def change_plan_service_subscription(self, entity_id: int, plan_id: int, **kwargs):
295+
params = dict(plan_id=plan_id)
296+
url = self.url + f"/{entity_id}/change_plan.json"
297+
response = self.rest.put(url=url, json=params, **kwargs)
298+
instance = utils.extract_response(response=response)
299+
return instance
300+
301+
class ServicePlans(DefaultClient):
302+
def __init__(self, *args, entity_name='service_plan', entity_collection='service_plans', per_page=None, **kwargs):
303+
super().__init__(*args, entity_name=entity_name,
304+
entity_collection=entity_collection, **kwargs)
305+
306+
def url(self) -> str:
307+
if type(self.parent) is Service:
308+
return self.parent.url + '/service_plans'
309+
return self.threescale_client.admin_api_url + '/service_plans'
310+
311+
def service_plans_features_list(self, entity_id: int, **kwargs):
312+
url = self.url + f"/{entity_id}/features.xml"
313+
response = self.rest.get(url=url)
314+
instance = utils.extract_response(response)
315+
return instance
316+
317+
def service_plan_add_feature(self, entity_id: int, feature_id: int, **kwargs):
318+
params = dict(feature_id=feature_id)
319+
url = self.url + f"/{entity_id}/features.xml"
320+
response = self.rest.post(url=url, json=params, **kwargs)
321+
instance = utils.extract_response(response=response)
322+
return instance
323+
324+
def service_plan_delete_feature(self, entity_id: int, id: int, **kwargs):
325+
params = dict(id=id)
326+
url = self.url + f"/{entity_id}/features/{id}.xml"
327+
response = self.rest.delete(url=url, json=params, **kwargs)
328+
instance = utils.extract_response(response=response)
329+
return instance
330+
276331

277332
class Applications(DefaultStateClient):
278333
def __init__(self, *args, entity_name='application', entity_collection='applications',
@@ -1138,6 +1193,16 @@ def service(self) -> 'Service':
11381193
def backend(self) -> 'Backend':
11391194
return self.metric.parent
11401195

1196+
class ServiceSubscription(DefaultResource):
1197+
def __init__(self, **kwargs):
1198+
super().__init__(**kwargs)
1199+
1200+
def approve_service_subscription(self, entity_id: int, **kwargs):
1201+
return self.client.approve_service_subscription(entity_id=self.entity_id, **kwargs)
1202+
1203+
def change_plan_service_subscription(self, entity_id: int, **kwargs):
1204+
return self.client.change_plan_service_subscription(entity_id=self.entity_id, **kwargs)
1205+
11411206

11421207
class Metric(DefaultResource):
11431208
def __init__(self, entity_name='system_name', **kwargs):
@@ -1249,6 +1314,10 @@ def app_plans(self) -> ApplicationPlans:
12491314
def metrics(self) -> Metrics:
12501315
return Metrics(parent=self, instance_klass=Metric)
12511316

1317+
@property
1318+
def service_plans(self) -> ServicePlans:
1319+
return ServicePlans(parent=self, instance_klass=ServicePlan)
1320+
12521321
@property
12531322
def proxy(self) -> 'Proxies':
12541323
return Proxies(parent=self, instance_klass=Proxy)
@@ -1455,6 +1524,22 @@ def test_request(self, relpath=None, verify: bool = None):
14551524

14561525
return client.get(relpath)
14571526

1527+
class ServicePlan(DefaultResource):
1528+
def __init__(self, **kwargs):
1529+
super().__init__(**kwargs)
1530+
1531+
def service_plan_list(self, **kwargs):
1532+
return self.client.service_plan_list(self, **kwargs)
1533+
1534+
def service_plan_features_list(self, entity_id: int, **kwargs):
1535+
return self.client.service_plan_features_list(entity_id=self.entity_id, **kwargs)
1536+
1537+
def service_plan_add_feature(self, entity_id: int, id: int, **kwargs):
1538+
return self.client.service_plan_add_feature(entity_id=id, id=id, **kwargs)
1539+
1540+
def service_plan_delete_feature(self, entity_id: int, id: int, **kwargs):
1541+
return self.client.service_plan_delete_feature(entity_id=entity_id, id=id, **kwargs)
1542+
14581543

14591544
class ApplicationKey(DefaultResource):
14601545
def __init__(self, entity_name='', **kwargs):
@@ -1473,6 +1558,10 @@ def applications(self) -> Applications:
14731558
def users(self) -> AccountUsers:
14741559
return AccountUsers(parent=self, instance_klass=AccountUser)
14751560

1561+
@property
1562+
def service_subscriptions(self) -> ServiceSubscriptions:
1563+
return ServiceSubscriptions(parent=self, instance_klass=ServiceSubscription)
1564+
14761565
def credit_card_set(self, params: dict = None, **kwargs):
14771566
url = self.url + "/credit_card"
14781567
response = self.client.rest.put(url=url, json=params, **kwargs)

0 commit comments

Comments
 (0)