From 3ab83e0972078ee0dd88735070edca859590f963 Mon Sep 17 00:00:00 2001 From: joscasta Date: Mon, 27 Jan 2025 16:55:47 +0100 Subject: [PATCH 1/8] Adding Service Subscription and Service Plan resources and tests for Service Subscription --- tests/integration/conftest.py | 14 ++- .../test_integration_service_subscriptions.py | 39 ++++++++ threescale_api/resources.py | 89 +++++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 tests/integration/test_integration_service_subscriptions.py diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index d16a882..04ad00e 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -11,9 +11,9 @@ 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) load_dotenv() @@ -148,6 +148,16 @@ def account_user(account,account_user_params) -> AccountUser: yield user cleanup(user) +@pytest.fixture(scope='module') +def service_subscription_params(account) -> dict: + #suffix = get_suffix() + return dict(plan_id=account['id']) + +@pytest.fixture(scope='module') +def service_subscription(account, service_subscription_params) -> ServiceSubscription: + resource = account.service_subscriptions.create(params=service_subscription_params) + yield resource + @pytest.fixture(scope='module') def application_plan_params() -> dict: suffix = get_suffix() diff --git a/tests/integration/test_integration_service_subscriptions.py b/tests/integration/test_integration_service_subscriptions.py new file mode 100644 index 0000000..25252ec --- /dev/null +++ b/tests/integration/test_integration_service_subscriptions.py @@ -0,0 +1,39 @@ +import pytest +import backoff + +from tests.integration.conftest import account +from threescale_api.errors import ApiClientError + +from tests.integration import asserts + +def test_should_create_service_subscription(account, service_subscription, service_subscription_params): + + account.service_subscriptions.create(params=service_subscription_params) + resource = service_subscription.get(service_subscription_params) + asserts.assert_resource(resource) + asserts.assert_resource_params(account, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) + +def test_should_read_service_subscription(account, service_subscription, service_subscription_params): + resource = account.service_subscriptions.get(params=service_subscription_params) + asserts.assert_resource(resource) + asserts.assert_resource_params(resource, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) + +#def test_should_update_service_subscription(account, service_subscription, service_subscription_params): +# service_subscription_params['plan_id'] = 100 +# resource = account.service_subscriptions.update(service_subscription.entity_id, service_subscription_params) +# asserts.assert_resource(resource) +# asserts.assert_resource_params(resource, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) +# resource_updated = account.service_subscription.read(params=service_subscription_params) +# asserts.assert_resource(resource_updated) +# asserts.assert_resource_params(resource_updated, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) + +#def test_should_approve_service_subscription(account, service_subscription, service_subscription_params): +# resource = account.service_subscriptions.approve_service_subscription(params=service_subscription_params) +# asserts.assert_resource(resource) +# read = account.service_subscriptions.read(service_subscription.entity_id) +# asserts.assert_resource(read) + +def test_list_service_subscriptions(account): + resource = account.service_subscriptions.list() + assert len(resource) >= 1 + diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 1a9f015..5657a2d 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -3,6 +3,8 @@ from typing import Dict, Union, List, Iterable from urllib.parse import quote_plus +from dill.pointers import parent + from threescale_api import auth from threescale_api import utils from threescale_api import errors @@ -273,6 +275,59 @@ 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', + per_page=None, **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_service_subscription(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_service_subscription(self, entity_id: int, plan_id: int, **kwargs): + params = dict(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', per_page=None, **kwargs): + super().__init__(*args, entity_name=entity_name, + entity_collection=entity_collection, **kwargs) + + def url(self) -> str: + if type(self.parent) is Service: + return self.parent.url + '/service_plans' + return self.threescale_client.admin_api_url + '/service_plans' + + def service_plans_features_list(self, entity_id: int, **kwargs): + url = self.url + f"/{entity_id}/features.xml" + response = self.rest.get(url=url) + instance = utils.extract_response(response) + return instance + + def service_plan_add_feature(self, entity_id: int, feature_id: int, **kwargs): + params = dict(feature_id=feature_id) + url = self.url + f"/{entity_id}/features.xml" + response = self.rest.post(url=url, json=params, **kwargs) + instance = utils.extract_response(response=response) + return instance + + def service_plan_delete_feature(self, entity_id: int, id: int, **kwargs): + params = dict(id=id) + url = self.url + f"/{entity_id}/features/{id}.xml" + response = self.rest.delete(url=url, json=params, **kwargs) + instance = utils.extract_response(response=response) + return instance + class Applications(DefaultStateClient): def __init__(self, *args, entity_name='application', entity_collection='applications', @@ -1138,6 +1193,16 @@ def service(self) -> 'Service': def backend(self) -> 'Backend': return self.metric.parent +class ServiceSubscription(DefaultResource): + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def approve_service_subscription(self, entity_id: int, **kwargs): + return self.client.approve_service_subscription(entity_id=self.entity_id, **kwargs) + + def change_plan_service_subscription(self, entity_id: int, **kwargs): + return self.client.change_plan_service_subscription(entity_id=self.entity_id, **kwargs) + class Metric(DefaultResource): def __init__(self, entity_name='system_name', **kwargs): @@ -1249,6 +1314,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) @@ -1455,6 +1524,22 @@ def test_request(self, relpath=None, verify: bool = None): return client.get(relpath) +class ServicePlan(DefaultResource): + def __init__(self, **kwargs): + super().__init__(**kwargs) + + def service_plan_list(self, **kwargs): + return self.client.service_plan_list(self, **kwargs) + + def service_plan_features_list(self, entity_id: int, **kwargs): + return self.client.service_plan_features_list(entity_id=self.entity_id, **kwargs) + + def service_plan_add_feature(self, entity_id: int, id: int, **kwargs): + return self.client.service_plan_add_feature(entity_id=id, id=id, **kwargs) + + def service_plan_delete_feature(self, entity_id: int, id: int, **kwargs): + return self.client.service_plan_delete_feature(entity_id=entity_id, id=id, **kwargs) + class ApplicationKey(DefaultResource): def __init__(self, entity_name='', **kwargs): @@ -1473,6 +1558,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) From c11872a1d5636a68365cd5cbfd5e0ad1fa82f476 Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Mon, 27 Jan 2025 17:25:19 +0100 Subject: [PATCH 2/8] Changes made in response to PR comments. --- threescale_api/resources.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 5657a2d..5fef004 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -3,8 +3,6 @@ from typing import Dict, Union, List, Iterable from urllib.parse import quote_plus -from dill.pointers import parent - from threescale_api import auth from threescale_api import utils from threescale_api import errors @@ -276,7 +274,9 @@ 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', + + def __init__(self, *args, entity_name='service_subscription', + entity_collection='service_subscriptions', per_page=None, **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -299,7 +299,9 @@ def change_plan_service_subscription(self, entity_id: int, plan_id: int, **kwarg return instance class ServicePlans(DefaultClient): - def __init__(self, *args, entity_name='service_plan', entity_collection='service_plans', per_page=None, **kwargs): + + def __init__(self, *args, entity_name='service_plan', + entity_collection='service_plans', per_page=None, **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -1194,6 +1196,7 @@ def backend(self) -> 'Backend': return self.metric.parent class ServiceSubscription(DefaultResource): + def __init__(self, **kwargs): super().__init__(**kwargs) @@ -1525,6 +1528,7 @@ def test_request(self, relpath=None, verify: bool = None): return client.get(relpath) class ServicePlan(DefaultResource): + def __init__(self, **kwargs): super().__init__(**kwargs) From 8b9289dfe2cfb709dd9ec2ee2933e7f2a0babed9 Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Tue, 28 Jan 2025 17:05:29 +0100 Subject: [PATCH 3/8] Update tests for test_integration_service_subscriptions. Add Service Plan resources. --- tests/integration/conftest.py | 20 ++++++++-- .../test_integration_service_subscriptions.py | 19 +++++----- threescale_api/resources.py | 37 +------------------ 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 04ad00e..afe0e5b 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -13,7 +13,8 @@ Proxy, Backend, Metric, MappingRule, BackendMappingRule, Account, BackendUsage, ActiveDoc, Webhooks, InvoiceState, - ApplicationKey, ApplicationPlans, AccountUser, AccountUsers, ServiceSubscription) + ApplicationKey, ApplicationPlans, AccountUser, AccountUsers, ServiceSubscription, + ServicePlan) load_dotenv() @@ -149,9 +150,20 @@ def account_user(account,account_user_params) -> AccountUser: cleanup(user) @pytest.fixture(scope='module') -def service_subscription_params(account) -> dict: - #suffix = get_suffix() - return dict(plan_id=account['id']) +def service_plan_params() -> dict: + suffix = get_suffix() + return dict(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 + +@pytest.fixture(scope='module') +def service_subscription_params(service_plan) -> dict: + suffix = get_suffix() + return dict(plan_id=service_plan['id']) @pytest.fixture(scope='module') def service_subscription(account, service_subscription_params) -> ServiceSubscription: diff --git a/tests/integration/test_integration_service_subscriptions.py b/tests/integration/test_integration_service_subscriptions.py index 25252ec..02d8cf6 100644 --- a/tests/integration/test_integration_service_subscriptions.py +++ b/tests/integration/test_integration_service_subscriptions.py @@ -6,17 +6,21 @@ from tests.integration import asserts +def test_list_service_subscriptions(account, service_subscription): + resource = account.service_subscriptions.list() + assert len(resource) >= 1 + def test_should_create_service_subscription(account, service_subscription, service_subscription_params): - account.service_subscriptions.create(params=service_subscription_params) - resource = service_subscription.get(service_subscription_params) - asserts.assert_resource(resource) - asserts.assert_resource_params(account, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) + asserts.assert_resource(service_subscription) + asserts.assert_resource_params(service_subscription, service_subscription_params) def test_should_read_service_subscription(account, service_subscription, service_subscription_params): - resource = account.service_subscriptions.get(params=service_subscription_params) + + + resource = account.service_subscriptions.read(service_subscription.entity_id) asserts.assert_resource(resource) - asserts.assert_resource_params(resource, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) + asserts.assert_resource_params(service_subscription,service_subscription_params) #def test_should_update_service_subscription(account, service_subscription, service_subscription_params): # service_subscription_params['plan_id'] = 100 @@ -33,7 +37,4 @@ def test_should_read_service_subscription(account, service_subscription, service # read = account.service_subscriptions.read(service_subscription.entity_id) # asserts.assert_resource(read) -def test_list_service_subscriptions(account): - resource = account.service_subscriptions.list() - assert len(resource) >= 1 diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 5fef004..594864e 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -305,30 +305,10 @@ def __init__(self, *args, entity_name='service_plan', super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) + @property def url(self) -> str: - if type(self.parent) is Service: - return self.parent.url + '/service_plans' - return self.threescale_client.admin_api_url + '/service_plans' - - def service_plans_features_list(self, entity_id: int, **kwargs): - url = self.url + f"/{entity_id}/features.xml" - response = self.rest.get(url=url) - instance = utils.extract_response(response) - return instance - - def service_plan_add_feature(self, entity_id: int, feature_id: int, **kwargs): - params = dict(feature_id=feature_id) - url = self.url + f"/{entity_id}/features.xml" - response = self.rest.post(url=url, json=params, **kwargs) - instance = utils.extract_response(response=response) - return instance + return self.parent.url + '/service_plans' - def service_plan_delete_feature(self, entity_id: int, id: int, **kwargs): - params = dict(id=id) - url = self.url + f"/{entity_id}/features/{id}.xml" - response = self.rest.delete(url=url, json=params, **kwargs) - instance = utils.extract_response(response=response) - return instance class Applications(DefaultStateClient): @@ -1532,19 +1512,6 @@ class ServicePlan(DefaultResource): def __init__(self, **kwargs): super().__init__(**kwargs) - def service_plan_list(self, **kwargs): - return self.client.service_plan_list(self, **kwargs) - - def service_plan_features_list(self, entity_id: int, **kwargs): - return self.client.service_plan_features_list(entity_id=self.entity_id, **kwargs) - - def service_plan_add_feature(self, entity_id: int, id: int, **kwargs): - return self.client.service_plan_add_feature(entity_id=id, id=id, **kwargs) - - def service_plan_delete_feature(self, entity_id: int, id: int, **kwargs): - return self.client.service_plan_delete_feature(entity_id=entity_id, id=id, **kwargs) - - class ApplicationKey(DefaultResource): def __init__(self, entity_name='', **kwargs): super().__init__(entity_name=entity_name, **kwargs) From 405ad3ceb6fa5962083ecbd8a624cd228d419730 Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Thu, 30 Jan 2025 15:29:09 +0100 Subject: [PATCH 4/8] Adding resources for Service Plan and the equivalent unit tests --- tests/integration/conftest.py | 2 +- .../test_integration_service_plans.py | 36 +++++++++++++++++++ .../test_integration_service_subscriptions.py | 17 --------- threescale_api/resources.py | 13 +++++++ 4 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 tests/integration/test_integration_service_plans.py diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index afe0e5b..c07d8a3 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -150,7 +150,7 @@ def account_user(account,account_user_params) -> AccountUser: cleanup(user) @pytest.fixture(scope='module') -def service_plan_params() -> dict: +def service_plan_params(service) -> dict: suffix = get_suffix() return dict(name=f"test-{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..8ff6015 --- /dev/null +++ b/tests/integration/test_integration_service_plans.py @@ -0,0 +1,36 @@ +import pytest + + +from tests.integration.conftest import service +from threescale_api.errors import ApiClientError + +from tests.integration import asserts + +def test_list_service_plans(service, service_plan): + resource = service.service_plans.list() + assert len(resource) >= 1 + +def test_check_service_plan_creation(service, 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, 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 index 02d8cf6..e1d81de 100644 --- a/tests/integration/test_integration_service_subscriptions.py +++ b/tests/integration/test_integration_service_subscriptions.py @@ -11,30 +11,13 @@ def test_list_service_subscriptions(account, service_subscription): assert len(resource) >= 1 def test_should_create_service_subscription(account, service_subscription, service_subscription_params): - asserts.assert_resource(service_subscription) asserts.assert_resource_params(service_subscription, service_subscription_params) def test_should_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) -#def test_should_update_service_subscription(account, service_subscription, service_subscription_params): -# service_subscription_params['plan_id'] = 100 -# resource = account.service_subscriptions.update(service_subscription.entity_id, service_subscription_params) -# asserts.assert_resource(resource) -# asserts.assert_resource_params(resource, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) -# resource_updated = account.service_subscription.read(params=service_subscription_params) -# asserts.assert_resource(resource_updated) -# asserts.assert_resource_params(resource_updated, service_subscription_params, [param for param in service_subscription_params.keys() if param != 'id']) - -#def test_should_approve_service_subscription(account, service_subscription, service_subscription_params): -# resource = account.service_subscriptions.approve_service_subscription(params=service_subscription_params) -# asserts.assert_resource(resource) -# read = account.service_subscriptions.read(service_subscription.entity_id) -# asserts.assert_resource(read) diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 594864e..ea10692 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -273,6 +273,7 @@ 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', @@ -298,6 +299,7 @@ def change_plan_service_subscription(self, entity_id: int, plan_id: int, **kwarg instance = utils.extract_response(response=response) return instance + class ServicePlans(DefaultClient): def __init__(self, *args, entity_name='service_plan', @@ -309,6 +311,11 @@ def __init__(self, *args, entity_name='service_plan', 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): @@ -1175,6 +1182,7 @@ def service(self) -> 'Service': def backend(self) -> 'Backend': return self.metric.parent + class ServiceSubscription(DefaultResource): def __init__(self, **kwargs): @@ -1507,11 +1515,16 @@ 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) From 4468b33fc4b5b5bb8742e4f1de049e0f59563d5f Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Wed, 5 Feb 2025 15:27:24 +0100 Subject: [PATCH 5/8] Implement suggested changes from PR review. --- tests/integration/conftest.py | 3 ++- tests/integration/test_integration_service_plans.py | 10 +++------- .../test_integration_service_subscriptions.py | 13 +++---------- threescale_api/resources.py | 8 ++++---- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index c07d8a3..5faa692 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -156,9 +156,9 @@ def service_plan_params(service) -> dict: @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: @@ -169,6 +169,7 @@ def service_subscription_params(service_plan) -> dict: 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: diff --git a/tests/integration/test_integration_service_plans.py b/tests/integration/test_integration_service_plans.py index 8ff6015..7277061 100644 --- a/tests/integration/test_integration_service_plans.py +++ b/tests/integration/test_integration_service_plans.py @@ -1,16 +1,12 @@ -import pytest -from tests.integration.conftest import service -from threescale_api.errors import ApiClientError - from tests.integration import asserts -def test_list_service_plans(service, service_plan): +def test_list_service_plans(service): resource = service.service_plans.list() assert len(resource) >= 1 -def test_check_service_plan_creation(service, service_plan, service_plan_params): +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() @@ -29,7 +25,7 @@ def test_update_service_plans(service, service_plan, service_plan_params): asserts.assert_resource(resource) asserts.assert_resource_params(service_plan, service_plan_params) -def test_set_default_service_plan(service, 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) diff --git a/tests/integration/test_integration_service_subscriptions.py b/tests/integration/test_integration_service_subscriptions.py index e1d81de..e2c9a5a 100644 --- a/tests/integration/test_integration_service_subscriptions.py +++ b/tests/integration/test_integration_service_subscriptions.py @@ -1,23 +1,16 @@ -import pytest -import backoff -from tests.integration.conftest import account -from threescale_api.errors import ApiClientError from tests.integration import asserts -def test_list_service_subscriptions(account, service_subscription): +def test_list_service_subscriptions(account): resource = account.service_subscriptions.list() assert len(resource) >= 1 -def test_should_create_service_subscription(account, service_subscription, service_subscription_params): +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_should_read_service_subscription(account, 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 ea10692..5e63eb1 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -286,13 +286,13 @@ def __init__(self, *args, entity_name='service_subscription', def url(self) -> str: return self.parent.url + '/service_subscriptions' - def approve_service_subscription(self, entity_id: int, **kwargs): + 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_service_subscription(self, entity_id: int, plan_id: int, **kwargs): + def change_plan(self, entity_id: int, plan_id: int, **kwargs): params = dict(plan_id=plan_id) url = self.url + f"/{entity_id}/change_plan.json" response = self.rest.put(url=url, json=params, **kwargs) @@ -1188,10 +1188,10 @@ class ServiceSubscription(DefaultResource): def __init__(self, **kwargs): super().__init__(**kwargs) - def approve_service_subscription(self, entity_id: int, **kwargs): + def approve(self, entity_id: int, **kwargs): return self.client.approve_service_subscription(entity_id=self.entity_id, **kwargs) - def change_plan_service_subscription(self, entity_id: int, **kwargs): + def change_plan(self, entity_id: int, **kwargs): return self.client.change_plan_service_subscription(entity_id=self.entity_id, **kwargs) From c3741e4c33af021753fb507e261df4aac99c0385 Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Wed, 5 Feb 2025 15:30:52 +0100 Subject: [PATCH 6/8] Fix missing correct nammes in DefaultResoruce ServiceSubscription --- threescale_api/resources.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 5e63eb1..cc9bca4 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -1189,10 +1189,10 @@ def __init__(self, **kwargs): super().__init__(**kwargs) def approve(self, entity_id: int, **kwargs): - return self.client.approve_service_subscription(entity_id=self.entity_id, **kwargs) + return self.client.approve(entity_id=self.entity_id, **kwargs) def change_plan(self, entity_id: int, **kwargs): - return self.client.change_plan_service_subscription(entity_id=self.entity_id, **kwargs) + return self.client.change_plan(entity_id=self.entity_id, **kwargs) class Metric(DefaultResource): From fe1c07275cab965ff98c4f4ee8efcfe799efc7b5 Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Thu, 6 Feb 2025 09:49:46 +0100 Subject: [PATCH 7/8] Fix minor bugs in PR after review. --- tests/integration/conftest.py | 7 +++---- threescale_api/resources.py | 11 +++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5faa692..ebc2147 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -150,9 +150,9 @@ def account_user(account,account_user_params) -> AccountUser: cleanup(user) @pytest.fixture(scope='module') -def service_plan_params(service) -> dict: +def service_plan_params() -> dict: suffix = get_suffix() - return dict(name=f"test-{suffix}") + return {"name":f'test-{suffix}'} @pytest.fixture(scope='module') def service_plan(service, service_plan_params) -> ServicePlan: @@ -162,8 +162,7 @@ def service_plan(service, service_plan_params) -> ServicePlan: @pytest.fixture(scope='module') def service_subscription_params(service_plan) -> dict: - suffix = get_suffix() - return dict(plan_id=service_plan['id']) + return {"plan_id":service_plan['id']} @pytest.fixture(scope='module') def service_subscription(account, service_subscription_params) -> ServiceSubscription: diff --git a/threescale_api/resources.py b/threescale_api/resources.py index cc9bca4..d73cdf2 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -277,8 +277,7 @@ def pending(self, entity_id, **kwargs) -> 'Account': class ServiceSubscriptions(DefaultClient): def __init__(self, *args, entity_name='service_subscription', - entity_collection='service_subscriptions', - per_page=None, **kwargs): + entity_collection='service_subscriptions', **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -293,7 +292,7 @@ def approve(self, entity_id: int, **kwargs): return instance def change_plan(self, entity_id: int, plan_id: int, **kwargs): - params = dict(plan_id=plan_id) + 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) @@ -303,7 +302,7 @@ def change_plan(self, entity_id: int, plan_id: int, **kwargs): class ServicePlans(DefaultClient): def __init__(self, *args, entity_name='service_plan', - entity_collection='service_plans', per_page=None, **kwargs): + entity_collection='service_plans', **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -1188,10 +1187,10 @@ class ServiceSubscription(DefaultResource): def __init__(self, **kwargs): super().__init__(**kwargs) - def approve(self, entity_id: int, **kwargs): + def approve(self, **kwargs): return self.client.approve(entity_id=self.entity_id, **kwargs) - def change_plan(self, entity_id: int, **kwargs): + def change_plan(self, **kwargs): return self.client.change_plan(entity_id=self.entity_id, **kwargs) From bf57f21ca7cd44633f68915e9b07abcedb5f6b2c Mon Sep 17 00:00:00 2001 From: Jose Manuel Castano Date: Thu, 6 Feb 2025 10:51:45 +0100 Subject: [PATCH 8/8] Fix typo in resources.py --- threescale_api/resources.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/threescale_api/resources.py b/threescale_api/resources.py index d73cdf2..8306e0e 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -292,7 +292,7 @@ def approve(self, entity_id: int, **kwargs): return instance def change_plan(self, entity_id: int, plan_id: int, **kwargs): - params = {"plan_id":plan_id} + 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)