Skip to content

Commit

Permalink
Merge pull request #165 from josemacassan/service_subscription
Browse files Browse the repository at this point in the history
Adding Service Subscription and Service Plan resources and tests for Service Subscriptions
  • Loading branch information
dhlavac authored Feb 7, 2025
2 parents b3ab1ee + bf57f21 commit 5509cb6
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
26 changes: 24 additions & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/test_integration_service_plans.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions tests/integration/test_integration_service_subscriptions.py
Original file line number Diff line number Diff line change
@@ -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)
72 changes: 72 additions & 0 deletions threescale_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5509cb6

Please sign in to comment.