From b7aeebaede2d8d387dccbe56925ce43e54285d16 Mon Sep 17 00:00:00 2001 From: Martin Kudlej Date: Fri, 13 Dec 2024 15:43:58 +0100 Subject: [PATCH] fix assert_resource_params bug --- tests/integration/asserts.py | 9 +++-- tests/integration/conftest.py | 34 ++++++++----------- .../test_integration_account_plans.py | 10 +++--- .../integration/test_integration_accounts.py | 18 +++++----- .../test_integration_application.py | 2 +- .../test_integration_application_plan.py | 2 +- .../test_integration_backend_metrics.py | 11 +++--- .../test_integration_backend_usages.py | 4 +-- .../test_integration_fields_definitions.py | 2 +- .../integration/test_integration_invoices.py | 16 ++++----- tests/integration/test_integration_metrics.py | 23 +++++-------- ...test_integration_provider_account_users.py | 4 +-- 12 files changed, 64 insertions(+), 71 deletions(-) diff --git a/tests/integration/asserts.py b/tests/integration/asserts.py index ee74d1c..1599193 100644 --- a/tests/integration/asserts.py +++ b/tests/integration/asserts.py @@ -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 value for key \"{key}\" should be correct. {obj[key]} == {val}" + assert obj.entity[key] == val, "Entity value for key \"{key}\" should be correct. {obj.entity[key]} == val" + def assert_http_ok(response: requests.Response): diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index bfdb8ab..c79850a 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -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') @@ -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 @@ -249,9 +247,7 @@ 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 @@ -259,7 +255,6 @@ 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 @@ -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() @@ -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') @@ -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') @@ -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") @@ -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"} diff --git a/tests/integration/test_integration_account_plans.py b/tests/integration/test_integration_account_plans.py index 6f75f8f..3791714 100644 --- a/tests/integration/test_integration_account_plans.py +++ b/tests/integration/test_integration_account_plans.py @@ -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) diff --git a/tests/integration/test_integration_accounts.py b/tests/integration/test_integration_accounts.py index 4394bd8..8f96492 100644 --- a/tests/integration/test_integration_accounts.py +++ b/tests/integration/test_integration_accounts.py @@ -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): diff --git a/tests/integration/test_integration_application.py b/tests/integration/test_integration_application.py index 0cbdf35..24dacee 100644 --- a/tests/integration/test_integration_application.py +++ b/tests/integration/test_integration_application.py @@ -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): diff --git a/tests/integration/test_integration_application_plan.py b/tests/integration/test_integration_application_plan.py index 3d90fb2..9713524 100644 --- a/tests/integration/test_integration_application_plan.py +++ b/tests/integration/test_integration_application_plan.py @@ -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): diff --git a/tests/integration/test_integration_backend_metrics.py b/tests/integration/test_integration_backend_metrics.py index 56f631b..c5563d8 100644 --- a/tests/integration/test_integration_backend_metrics.py +++ b/tests/integration/test_integration_backend_metrics.py @@ -8,7 +8,7 @@ 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) @@ -16,8 +16,9 @@ def test_should_fields_be_required(backend): 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']) @@ -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): diff --git a/tests/integration/test_integration_backend_usages.py b/tests/integration/test_integration_backend_usages.py index fdf779e..a0f474e 100644 --- a/tests/integration/test_integration_backend_usages.py +++ b/tests/integration/test_integration_backend_usages.py @@ -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): @@ -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']) diff --git a/tests/integration/test_integration_fields_definitions.py b/tests/integration/test_integration_fields_definitions.py index a6771ef..115d143 100644 --- a/tests/integration/test_integration_fields_definitions.py +++ b/tests/integration/test_integration_fields_definitions.py @@ -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) diff --git a/tests/integration/test_integration_invoices.py b/tests/integration/test_integration_invoices.py index 15ec327..7e1ccfb 100644 --- a/tests/integration/test_integration_invoices.py +++ b/tests/integration/test_integration_invoices.py @@ -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 @@ -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) diff --git a/tests/integration/test_integration_metrics.py b/tests/integration/test_integration_metrics.py index 8e62672..87f0798 100644 --- a/tests/integration/test_integration_metrics.py +++ b/tests/integration/test_integration_metrics.py @@ -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): @@ -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']) @@ -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.""" @@ -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() diff --git a/tests/integration/test_integration_provider_account_users.py b/tests/integration/test_integration_provider_account_users.py index 597b506..91dea20 100644 --- a/tests/integration/test_integration_provider_account_users.py +++ b/tests/integration/test_integration_provider_account_users.py @@ -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): @@ -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):