Skip to content

Commit 5509cb6

Browse files
authored
Merge pull request #165 from josemacassan/service_subscription
Adding Service Subscription and Service Plan resources and tests for Service Subscriptions
2 parents b3ab1ee + bf57f21 commit 5509cb6

File tree

4 files changed

+144
-2
lines changed

4 files changed

+144
-2
lines changed

tests/integration/conftest.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
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,
17+
ServicePlan)
1718

1819
load_dotenv()
1920

@@ -148,6 +149,27 @@ def account_user(account,account_user_params) -> AccountUser:
148149
yield user
149150
cleanup(user)
150151

152+
@pytest.fixture(scope='module')
153+
def service_plan_params() -> dict:
154+
suffix = get_suffix()
155+
return {"name":f'test-{suffix}'}
156+
157+
@pytest.fixture(scope='module')
158+
def service_plan(service, service_plan_params) -> ServicePlan:
159+
resource = service.service_plans.create(params=service_plan_params)
160+
yield resource
161+
cleanup(resource)
162+
163+
@pytest.fixture(scope='module')
164+
def service_subscription_params(service_plan) -> dict:
165+
return {"plan_id":service_plan['id']}
166+
167+
@pytest.fixture(scope='module')
168+
def service_subscription(account, service_subscription_params) -> ServiceSubscription:
169+
resource = account.service_subscriptions.create(params=service_subscription_params)
170+
yield resource
171+
cleanup(resource)
172+
151173
@pytest.fixture(scope='module')
152174
def application_plan_params() -> dict:
153175
suffix = get_suffix()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
from tests.integration import asserts
4+
5+
def test_list_service_plans(service):
6+
resource = service.service_plans.list()
7+
assert len(resource) >= 1
8+
9+
def test_check_service_plan_creation(service_plan, service_plan_params):
10+
asserts.assert_resource(service_plan)
11+
asserts.assert_resource_params(service_plan, service_plan_params)
12+
assert service_plan.exists()
13+
14+
def test_read_service_plans(service, service_plan, service_plan_params):
15+
asserts.assert_resource(service_plan)
16+
resource = service.service_plans.read(service_plan.entity_id)
17+
asserts.assert_resource(resource)
18+
asserts.assert_resource_params(service_plan,service_plan_params)
19+
20+
def test_update_service_plans(service, service_plan, service_plan_params):
21+
asserts.assert_resource(service_plan)
22+
service_plan['state'] = 'publish'
23+
service_plan['approval_required'] = True
24+
resource = service.service_plans.update(service_plan.entity_id, service_plan_params)
25+
asserts.assert_resource(resource)
26+
asserts.assert_resource_params(service_plan, service_plan_params)
27+
28+
def test_set_default_service_plan(service_plan, service_plan_params):
29+
asserts.assert_resource(service_plan)
30+
resource = service_plan.set_default()
31+
asserts.assert_resource(resource)
32+
asserts.assert_resource_params(service_plan, service_plan_params)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
from tests.integration import asserts
4+
5+
def test_list_service_subscriptions(account):
6+
resource = account.service_subscriptions.list()
7+
assert len(resource) >= 1
8+
9+
def test_create_service_subscription(service_subscription, service_subscription_params):
10+
asserts.assert_resource(service_subscription)
11+
asserts.assert_resource_params(service_subscription, service_subscription_params)
12+
13+
def test_read_service_subscription(account, service_subscription, service_subscription_params):
14+
resource = account.service_subscriptions.read(service_subscription.entity_id)
15+
asserts.assert_resource(resource)
16+
asserts.assert_resource_params(service_subscription,service_subscription_params)

threescale_api/resources.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,49 @@ def pending(self, entity_id, **kwargs) -> 'Account':
274274
return self.set_state(entity_id=entity_id, state='make_pending', **kwargs)
275275

276276

277+
class ServiceSubscriptions(DefaultClient):
278+
279+
def __init__(self, *args, entity_name='service_subscription',
280+
entity_collection='service_subscriptions', **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(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(self, entity_id: int, plan_id: int, **kwargs):
295+
params = {"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+
302+
class ServicePlans(DefaultClient):
303+
304+
def __init__(self, *args, entity_name='service_plan',
305+
entity_collection='service_plans', **kwargs):
306+
super().__init__(*args, entity_name=entity_name,
307+
entity_collection=entity_collection, **kwargs)
308+
309+
@property
310+
def url(self) -> str:
311+
return self.parent.url + '/service_plans'
312+
313+
def service_plan_set_default(self, entity_id: int, **kwargs):
314+
url = self.url + f"/{entity_id}/default"
315+
response = self.rest.put(url=url, **kwargs)
316+
instance = self._create_instance(response=response)
317+
return instance
318+
319+
277320
class Applications(DefaultStateClient):
278321
def __init__(self, *args, entity_name='application', entity_collection='applications',
279322
per_page=None, **kwargs):
@@ -1139,6 +1182,18 @@ def backend(self) -> 'Backend':
11391182
return self.metric.parent
11401183

11411184

1185+
class ServiceSubscription(DefaultResource):
1186+
1187+
def __init__(self, **kwargs):
1188+
super().__init__(**kwargs)
1189+
1190+
def approve(self, **kwargs):
1191+
return self.client.approve(entity_id=self.entity_id, **kwargs)
1192+
1193+
def change_plan(self, **kwargs):
1194+
return self.client.change_plan(entity_id=self.entity_id, **kwargs)
1195+
1196+
11421197
class Metric(DefaultResource):
11431198
def __init__(self, entity_name='system_name', **kwargs):
11441199
super().__init__(entity_name=entity_name, **kwargs)
@@ -1249,6 +1304,10 @@ def app_plans(self) -> ApplicationPlans:
12491304
def metrics(self) -> Metrics:
12501305
return Metrics(parent=self, instance_klass=Metric)
12511306

1307+
@property
1308+
def service_plans(self) -> ServicePlans:
1309+
return ServicePlans(parent=self, instance_klass=ServicePlan)
1310+
12521311
@property
12531312
def proxy(self) -> 'Proxies':
12541313
return Proxies(parent=self, instance_klass=Proxy)
@@ -1456,6 +1515,15 @@ def test_request(self, relpath=None, verify: bool = None):
14561515
return client.get(relpath)
14571516

14581517

1518+
class ServicePlan(DefaultResource):
1519+
1520+
def __init__(self, **kwargs):
1521+
super().__init__(**kwargs)
1522+
1523+
def set_default(self, **kwargs):
1524+
return self.client.service_plan_set_default(entity_id=self.entity_id, **kwargs)
1525+
1526+
14591527
class ApplicationKey(DefaultResource):
14601528
def __init__(self, entity_name='', **kwargs):
14611529
super().__init__(entity_name=entity_name, **kwargs)
@@ -1473,6 +1541,10 @@ def applications(self) -> Applications:
14731541
def users(self) -> AccountUsers:
14741542
return AccountUsers(parent=self, instance_klass=AccountUser)
14751543

1544+
@property
1545+
def service_subscriptions(self) -> ServiceSubscriptions:
1546+
return ServiceSubscriptions(parent=self, instance_klass=ServiceSubscription)
1547+
14761548
def credit_card_set(self, params: dict = None, **kwargs):
14771549
url = self.url + "/credit_card"
14781550
response = self.client.rest.put(url=url, json=params, **kwargs)

0 commit comments

Comments
 (0)