Skip to content

Commit

Permalink
fix assert_resource_params bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mkudlej committed Dec 16, 2024
1 parent 233e7f8 commit 261d21a
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 71 deletions.
9 changes: 6 additions & 3 deletions tests/integration/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def assert_errors_contains(resource: threescale_api.defaults.DefaultClient, fiel


def assert_resource_params(obj: threescale_api.defaults.DefaultResource, params: dict, allowed=None):
if allowed is None:
allowed = params.keys()
for (key, val) in params.items():
if allowed is not None and key in allowed:
assert obj[key] == val, f"Resource value for key \"{key}\" should be correct."
assert obj.entity[key] == val, "Entity value for key \"{key}\" should be correct."
if key in allowed:
assert obj[key] == val, f"Resource hasn't expected value for a \"{key}\". {obj[key]} == {val}"
assert obj.entity[key] == val, "Entity hasn't expected value for key \"{key}\". {obj.entity[key]} == {val}"



def assert_http_ok(response: requests.Response):
Expand Down
34 changes: 15 additions & 19 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def access_token(access_token_params, api):
def update_account_params():
suffix = secrets.token_urlsafe(8)
name = f"updated-{suffix}"
return dict(name=name, username=name, org_name=name)
return dict(org_name=name)


@pytest.fixture(scope='module')
def account_params():
suffix = get_suffix()
name = f"test-{suffix}"
return dict(name=name, username=name, org_name=name)
return dict(username=name, org_name=name)


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -206,24 +206,22 @@ def metric_params(service):
def backend_metric_params(backend):
suffix = get_suffix()
friendly_name = f'test-metric-{suffix}'
system_name = f'{friendly_name}'.replace('-', '_')
return dict(backend_id=backend['id'], friendly_name=friendly_name,
system_name=system_name, unit='count')
unit='count')

@pytest.fixture
def updated_metric_params(metric_params):
params = metric_params.copy()
suffix = get_suffix()
friendly_name = f'test-updated-metric-{suffix}'
metric_params['friendly_name'] = f'/anything/{friendly_name}'
metric_params['system_name'] = friendly_name.replace('-', '_')
return metric_params
params['friendly_name'] = f'/anything/{friendly_name}'
return params

@pytest.fixture
def backend_updated_metric_params(backend_metric_params):
suffix = get_suffix()
friendly_name = f'test-updated-metric-{suffix}'
backend_metric_params['friendly_name'] = f'/anything/{friendly_name}'
backend_metric_params['system_name'] = friendly_name.replace('-', '_')
return backend_metric_params


Expand All @@ -249,17 +247,14 @@ def backend_hits_metric(backend):
def method_params(service):
suffix = get_suffix()
friendly_name = f'test-method-{suffix}'
system_name = f'{friendly_name}'.replace('-', '_')
return dict(friendly_name=friendly_name, system_name=system_name,
unit='hits')
return dict(friendly_name=friendly_name)


@pytest.fixture
def updated_method_params(method_params):
suffix = get_suffix()
friendly_name = f'test-updated-method-{suffix}'
method_params['friendly_name'] = friendly_name
method_params['system_name'] = f'{friendly_name}'.replace('-', '_')
return method_params


Expand Down Expand Up @@ -404,11 +399,11 @@ def backend(backend_params, api) -> Backend:
cleanup(backend)

@pytest.fixture(scope='module')
def backend_metric(backend, metric_params, proxy) -> Metric:
def backend_metric(backend, backend_metric_params, proxy) -> Metric:
"""
Fixture for getting backend metric.
"""
resource = backend.metrics.create(params=metric_params)
resource = backend.metrics.create(params=backend_metric_params)
yield resource
for map_rule in backend.mapping_rules.select_by(metric_id=resource['id']):
map_rule.delete()
Expand Down Expand Up @@ -495,7 +490,7 @@ def provider_account_params():
name = f"test-{suffix}"
return dict(username=name,
email=f'{name}@example.com',
assword='123456')
password='123456')


@pytest.fixture(scope='module')
Expand All @@ -514,7 +509,7 @@ def webhook(api):
def account_plans_update_params():
suffix = secrets.token_urlsafe(8)
name = f"updated-{suffix}"
return dict(name=name, description=name)
return dict(name=name)


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -559,7 +554,7 @@ def fields_definitions_params():
return dict(name=f"name-{get_suffix()}",
label=f"label-{get_suffix()}",
target="Account",
required="false")
required=False)


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -637,10 +632,11 @@ def cms_layout(api, cms_layout_params):
@pytest.fixture(scope="module")
def cms_page_params(cms_section, cms_layout):
"""CMS page fixture params"""
# section_name or section_id can be used to identify section
# layout_name or layout_id can be used to identify section
return {"system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}",
"title": f"title-{get_suffix()}", "path": f"/path-{get_suffix()}",
"section_name": f"section-{get_suffix()}", "section_id": cms_section['id'],
"layout_name": f"layout-{get_suffix()}", "layout_id": cms_layout['id'],
"section_id": cms_section['id'], "layout_id": cms_layout['id'],
"liquid_enabled": True, "handler": "markdown", "content_type": "text/html"}


Expand Down
10 changes: 5 additions & 5 deletions tests/integration/test_integration_account_plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ def test_account_plan_can_be_created(api, account_plan, account_plans_params):
asserts.assert_resource(account_plan)
asserts.assert_resource_params(account_plan, account_plans_params)

def test_account_plan_can_be_read(api, account_plan, account_plans_params):
read = api.account_plans.read(account_plan.entity_id)
asserts.assert_resource(read)
asserts.assert_resource_params(read, account_plans_params)


def test_account_plan_update(account_plan, account_plans_update_params):
updated_account_plan = account_plan.update(params=account_plans_update_params)
asserts.assert_resource(updated_account_plan)
asserts.assert_resource_params(updated_account_plan, account_plans_update_params)


def test_account_plan_can_be_read(api, account_plan, account_plans_params):
read = api.account_plans.read(account_plan.entity_id)
asserts.assert_resource(read)
asserts.assert_resource_params(read, account_plans_params)
18 changes: 9 additions & 9 deletions tests/integration/test_integration_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@ def test_accounts_list(api, account):

def test_account_can_be_created(api, account, account_params):
asserts.assert_resource(account)
asserts.assert_resource_params(account, account_params)


def test_account_update(api,account,update_account_params):
updated_account = account.update(params=update_account_params)
asserts.assert_resource(updated_account)
asserts.assert_resource_params(updated_account,update_account_params)
asserts.assert_resource_params(account, account_params, ['org_name'])


def test_account_can_be_read(api, account, account_params):
read = api.accounts.read(account.entity_id)
asserts.assert_resource(read)
asserts.assert_resource_params(read, account_params)
asserts.assert_resource_params(read, account_params, ['org_name'])


def test_account_can_be_read_by_name(api, account, account_params):
account_name = account['org_name']
read = api.accounts[account_name]
asserts.assert_resource(read)
asserts.assert_resource_params(read, account_params)
asserts.assert_resource_params(read, account_params, ['org_name'])


def test_account_update(api,account, update_account_params):
updated_account = account.update(params=update_account_params)
asserts.assert_resource(updated_account)
asserts.assert_resource_params(updated_account, update_account_params)


def test_users_list(api, account):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_integration_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_application_update(application, update_application_params):

def test_application_key_can_be_created(app_key, app_key_params):
asserts.assert_resource(app_key)
asserts.assert_resource_params(app_key, app_key_params)
assert app_key["value"] == app_key_params["key"]


def test_application_key_list(application, app_key):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_integration_application_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@pytest.fixture(scope='module')
def update_params():
suffix = secrets.token_urlsafe(8)
return dict(name=f"updated-{suffix}", cost_per_month='12.0', setup_fee='50.0')
return dict(name=f"updated-{suffix}", cost_per_month=12.0, setup_fee=50.0)


def test_application_plan_can_be_created(api, application_plan_params, application_plan):
Expand Down
11 changes: 6 additions & 5 deletions tests/integration/test_integration_backend_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

def test_should_create_metric(backend_metric, backend_metric_params):
asserts.assert_resource(backend_metric)
asserts.assert_resource_params(backend_metric, backend_metric_params)
asserts.assert_resource_params(backend_metric, backend_metric_params, [param for param in backend_metric_params.keys() if param != 'backend_id'])

def test_should_fields_be_required(backend):
resource = backend.metrics.create(params={}, throws=False)
asserts.assert_errors_contains(resource, ['friendly_name', 'unit'])


def test_should_system_name_be_invalid(backend, backend_metric_params):
backend_metric_params['system_name'] = 'invalid name whitespaces'
resource = backend.metrics.create(params=backend_metric_params, throws=False)
params = backend_metric_params.copy()
params['system_name'] = 'invalid name whitespaces'
resource = backend.metrics.create(params=params, throws=False)
asserts.assert_errors_contains(resource, ['system_name'])


Expand All @@ -29,13 +30,13 @@ def test_should_raise_exception(backend):
def test_should_read_metric(backend_metric, backend_metric_params):
resource = backend_metric.read()
asserts.assert_resource(resource)
asserts.assert_resource_params(resource, backend_metric_params)
asserts.assert_resource_params(resource, backend_metric_params, [param for param in backend_metric_params.keys() if param != 'backend_id'])


def test_should_update_metric(backend_metric, backend_updated_metric_params):
resource = backend_metric.update(params=backend_updated_metric_params)
asserts.assert_resource(resource)
asserts.assert_resource_params(resource, backend_updated_metric_params)
asserts.assert_resource_params(resource, backend_updated_metric_params, [param for param in backend_updated_metric_params.keys() if param != 'backend_id'])


def test_should_delete_metric(backend, backend_updated_metric_params):
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_integration_backend_usages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_backend_usage_can_be_created(backend_usage, backend_usage_params):
asserts.assert_resource(backend_usage)
asserts.assert_resource_params(backend_usage, backend_usage_params)
asserts.assert_resource_params(backend_usage, backend_usage_params, [param for param in backend_usage_params.keys() if param != 'backend_api_id'])


def test_backend_usages_list(api, backend_usage, backend):
Expand All @@ -14,4 +14,4 @@ def test_backend_usages_list(api, backend_usage, backend):
def test_backend_usage_update(backend_usage, backend, backend_usage_update_params):
updated_backend_usage = backend_usage.update(backend_usage_update_params)
asserts.assert_resource(updated_backend_usage)
asserts.assert_resource_params(updated_backend_usage, backend_usage_update_params)
asserts.assert_resource_params(updated_backend_usage, backend_usage_update_params, [param for param in backend_usage_update_params.keys() if param != 'backend_api_id'])
2 changes: 1 addition & 1 deletion tests/integration/test_integration_fields_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_fields_definitions_read(api):

def test_fields_definitions_update(api, fields_definition):
update_params = dict(target="Cinstance", label="something_else",
hidden="true", read_only="true", position=1)
hidden=True, read_only=True, position=1)
updated = fields_definition.update(update_params)
assert_resource_params(updated, update_params)

Expand Down
16 changes: 8 additions & 8 deletions tests/integration/test_integration_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ def test_invoice_list(invoice, api):
assert len(invoices) >= 1


def test_invoice_update(invoice, api):
assert invoice['state'] == 'open'
update = api.invoices.update(invoice.entity_id, dict(friendly_id='1111-11111111'))
assert update['friendly_id'] == '1111-11111111'
read = api.invoices.read(invoice.entity_id)
assert read['friendly_id'] == '1111-11111111'


def test_invoice_list_by_account(api, account, invoice):
invoices = api.invoices.list_by_account(account.entity_id)
assert len(invoices) == 1
Expand All @@ -47,6 +39,14 @@ def test_invoice_read_by_account(api, account, invoice):
assert_resource_params(read, invoice)


def test_invoice_update(invoice, api):
assert invoice['state'] == 'open'
update = api.invoices.update(invoice.entity_id, dict(friendly_id='1111-11111111'))
assert update['friendly_id'] == '1111-11111111'
read = api.invoices.read(invoice.entity_id)
assert read['friendly_id'] == '1111-11111111'


def test_invoice_update_state(invoice_to_update, api):
assert invoice_to_update['state'] == 'open'
update = api.invoices.state_update(invoice_to_update.entity_id, InvoiceState.PENDING)
Expand Down
23 changes: 8 additions & 15 deletions tests/integration/test_integration_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_list_metrics(service, metric):

def test_should_create_metric(metric, metric_params):
asserts.assert_resource(metric)
asserts.assert_resource_params(metric, metric_params)
asserts.assert_resource_params(metric, metric_params, [param for param in metric_params.keys() if param != 'service_id'])


def test_should_fields_be_required(service):
Expand All @@ -19,8 +19,9 @@ def test_should_fields_be_required(service):


def test_should_system_name_be_invalid(service, metric_params):
metric_params['system_name'] = 'invalid name whitespaces'
resource = service.metrics.create(params=metric_params, throws=False)
params = metric_params.copy()
params['system_name'] = 'invalid name whitespaces'
resource = service.metrics.create(params=params, throws=False)
asserts.assert_errors_contains(resource, ['system_name'])


Expand All @@ -32,29 +33,22 @@ def test_should_raise_exception(service):
def test_should_read_metric(metric, metric_params):
resource = metric.read()
asserts.assert_resource(resource)
asserts.assert_resource_params(resource, metric_params)
asserts.assert_resource_params(resource, metric_params, [param for param in metric_params.keys() if param != 'service_id'])


def test_should_update_metric(metric, updated_metric_params):
resource = metric.update(params=updated_metric_params)
asserts.assert_resource(resource)
asserts.assert_resource_params(resource, updated_metric_params)
asserts.assert_resource_params(resource, updated_metric_params, [param for param in updated_metric_params.keys() if param != 'service_id'])


def test_should_delete_metric(service, updated_metric_params):
resource = service.metrics.create(params=updated_metric_params)
assert resource.exists()
resource.delete()
assert not resource.exists()


def test_should_list_metrics(service):
def test_should_list_metrics(service, metric):
resources = service.metrics.list()
assert len(resources) > 1


def test_should_apicast_return_403_when_metric_is_disabled(
service, metric_params, create_mapping_rule,
service, metric, create_mapping_rule,
account, ssl_verify, backend_usage):
"""Metric is disabled when its limit is set to 0."""

Expand All @@ -64,7 +58,6 @@ def test_should_apicast_return_403_when_metric_is_disabled(
description='metric disabled')
app = account.applications.create(params=application_params)

metric = service.metrics.create(params=metric_params)
plan.limits(metric).create(params=dict(period='month', value=0))

rules = proxy.mapping_rules.list()
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_integration_provider_account_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def test_provider_user_can_be_created(provider_account_user, provider_account_params):
asserts.assert_resource(provider_account_user)
asserts.assert_resource_params(provider_account_user, provider_account_params)
asserts.assert_resource_params(provider_account_user, provider_account_params, [param for param in provider_account_params.keys() if param != 'password'])


def test_provider_user_list(api):
Expand All @@ -14,7 +14,7 @@ def test_provider_user_list(api):
def test_provider_user_can_be_read(api, provider_account_user, provider_account_params):
account = api.provider_account_users.read(provider_account_user.entity_id)
asserts.assert_resource(account)
asserts.assert_resource_params(account, provider_account_params)
asserts.assert_resource_params(account, provider_account_params, [param for param in provider_account_params.keys() if param != 'password'])


def test_resource_role_change(provider_account_user):
Expand Down

0 comments on commit 261d21a

Please sign in to comment.