diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index d16a882..ebc2147 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -11,9 +11,10 @@ import threescale_api from threescale_api.resources import (Service, ApplicationPlan, Application, Proxy, Backend, Metric, MappingRule, - BackendMappingRule, BackendUsage, + BackendMappingRule, Account, BackendUsage, ActiveDoc, Webhooks, InvoiceState, - ApplicationKey, ApplicationPlans, AccountUser, AccountUsers) + ApplicationKey, ApplicationPlans, AccountUser, AccountUsers, ServiceSubscription, + ServicePlan) load_dotenv() @@ -148,6 +149,27 @@ def account_user(account,account_user_params) -> AccountUser: yield user cleanup(user) +@pytest.fixture(scope='module') +def service_plan_params() -> dict: + suffix = get_suffix() + return {"name":f'test-{suffix}'} + +@pytest.fixture(scope='module') +def service_plan(service, service_plan_params) -> ServicePlan: + resource = service.service_plans.create(params=service_plan_params) + yield resource + cleanup(resource) + +@pytest.fixture(scope='module') +def service_subscription_params(service_plan) -> dict: + return {"plan_id":service_plan['id']} + +@pytest.fixture(scope='module') +def service_subscription(account, service_subscription_params) -> ServiceSubscription: + resource = account.service_subscriptions.create(params=service_subscription_params) + yield resource + cleanup(resource) + @pytest.fixture(scope='module') def application_plan_params() -> dict: suffix = get_suffix() diff --git a/tests/integration/test_integration_service_plans.py b/tests/integration/test_integration_service_plans.py new file mode 100644 index 0000000..7277061 --- /dev/null +++ b/tests/integration/test_integration_service_plans.py @@ -0,0 +1,32 @@ + + +from tests.integration import asserts + +def test_list_service_plans(service): + resource = service.service_plans.list() + assert len(resource) >= 1 + +def test_check_service_plan_creation(service_plan, service_plan_params): + asserts.assert_resource(service_plan) + asserts.assert_resource_params(service_plan, service_plan_params) + assert service_plan.exists() + +def test_read_service_plans(service, service_plan, service_plan_params): + asserts.assert_resource(service_plan) + resource = service.service_plans.read(service_plan.entity_id) + asserts.assert_resource(resource) + asserts.assert_resource_params(service_plan,service_plan_params) + +def test_update_service_plans(service, service_plan, service_plan_params): + asserts.assert_resource(service_plan) + service_plan['state'] = 'publish' + service_plan['approval_required'] = True + resource = service.service_plans.update(service_plan.entity_id, service_plan_params) + asserts.assert_resource(resource) + asserts.assert_resource_params(service_plan, service_plan_params) + +def test_set_default_service_plan(service_plan, service_plan_params): + asserts.assert_resource(service_plan) + resource = service_plan.set_default() + asserts.assert_resource(resource) + asserts.assert_resource_params(service_plan, service_plan_params) diff --git a/tests/integration/test_integration_service_subscriptions.py b/tests/integration/test_integration_service_subscriptions.py new file mode 100644 index 0000000..e2c9a5a --- /dev/null +++ b/tests/integration/test_integration_service_subscriptions.py @@ -0,0 +1,16 @@ + + +from tests.integration import asserts + +def test_list_service_subscriptions(account): + resource = account.service_subscriptions.list() + assert len(resource) >= 1 + +def test_create_service_subscription(service_subscription, service_subscription_params): + asserts.assert_resource(service_subscription) + asserts.assert_resource_params(service_subscription, service_subscription_params) + +def test_read_service_subscription(account, service_subscription, service_subscription_params): + resource = account.service_subscriptions.read(service_subscription.entity_id) + asserts.assert_resource(resource) + asserts.assert_resource_params(service_subscription,service_subscription_params) diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 1a9f015..8306e0e 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -274,6 +274,49 @@ def pending(self, entity_id, **kwargs) -> 'Account': return self.set_state(entity_id=entity_id, state='make_pending', **kwargs) +class ServiceSubscriptions(DefaultClient): + + def __init__(self, *args, entity_name='service_subscription', + entity_collection='service_subscriptions', **kwargs): + super().__init__(*args, entity_name=entity_name, + entity_collection=entity_collection, **kwargs) + + @property + def url(self) -> str: + return self.parent.url + '/service_subscriptions' + + def approve(self, entity_id: int, **kwargs): + url = self.url + f"/{entity_id}/approve.json" + response = self.rest.put(url=url, **kwargs) + instance = utils.extract_response(response=response) + return instance + + def change_plan(self, entity_id: int, plan_id: int, **kwargs): + params = {"plan_id": plan_id} + url = self.url + f"/{entity_id}/change_plan.json" + response = self.rest.put(url=url, json=params, **kwargs) + instance = utils.extract_response(response=response) + return instance + + +class ServicePlans(DefaultClient): + + def __init__(self, *args, entity_name='service_plan', + entity_collection='service_plans', **kwargs): + super().__init__(*args, entity_name=entity_name, + entity_collection=entity_collection, **kwargs) + + @property + def url(self) -> str: + return self.parent.url + '/service_plans' + + def service_plan_set_default(self, entity_id: int, **kwargs): + url = self.url + f"/{entity_id}/default" + response = self.rest.put(url=url, **kwargs) + instance = self._create_instance(response=response) + return instance + + class Applications(DefaultStateClient): def __init__(self, *args, entity_name='application', entity_collection='applications', per_page=None, **kwargs): @@ -1139,6 +1182,18 @@ def backend(self) -> 'Backend': return self.metric.parent +class ServiceSubscription(DefaultResource): + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def approve(self, **kwargs): + return self.client.approve(entity_id=self.entity_id, **kwargs) + + def change_plan(self, **kwargs): + return self.client.change_plan(entity_id=self.entity_id, **kwargs) + + class Metric(DefaultResource): def __init__(self, entity_name='system_name', **kwargs): super().__init__(entity_name=entity_name, **kwargs) @@ -1249,6 +1304,10 @@ def app_plans(self) -> ApplicationPlans: def metrics(self) -> Metrics: return Metrics(parent=self, instance_klass=Metric) + @property + def service_plans(self) -> ServicePlans: + return ServicePlans(parent=self, instance_klass=ServicePlan) + @property def proxy(self) -> 'Proxies': return Proxies(parent=self, instance_klass=Proxy) @@ -1456,6 +1515,15 @@ def test_request(self, relpath=None, verify: bool = None): return client.get(relpath) +class ServicePlan(DefaultResource): + + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def set_default(self, **kwargs): + return self.client.service_plan_set_default(entity_id=self.entity_id, **kwargs) + + class ApplicationKey(DefaultResource): def __init__(self, entity_name='', **kwargs): super().__init__(entity_name=entity_name, **kwargs) @@ -1473,6 +1541,10 @@ def applications(self) -> Applications: def users(self) -> AccountUsers: return AccountUsers(parent=self, instance_klass=AccountUser) + @property + def service_subscriptions(self) -> ServiceSubscriptions: + return ServiceSubscriptions(parent=self, instance_klass=ServiceSubscription) + def credit_card_set(self, params: dict = None, **kwargs): url = self.url + "/credit_card" response = self.client.rest.put(url=url, json=params, **kwargs)