From dd27263e759f34cebb853b1e36a0be67e59991af Mon Sep 17 00:00:00 2001 From: Martin Kudlej Date: Thu, 27 Apr 2023 22:25:19 +0200 Subject: [PATCH] changes for new version of API --- tests/integration/conftest.py | 20 ++++---- tests/integration/test_integration_cms.py | 59 +++++++++++++++++------ threescale_api/client.py | 6 --- threescale_api/resources.py | 35 +++++++++----- threescale_api/utils.py | 2 +- 5 files changed, 79 insertions(+), 43 deletions(-) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 77a4a33..e9c8716 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -500,9 +500,9 @@ def fields_definition(api, fields_definitions_params): @pytest.fixture(scope="module") -def cms_file_data(): +def cms_file_data(cms_section): """CMS file fixture data""" - return {"path": f"/path{get_suffix()}", "downloadable": True} + return {"path": f"/path{get_suffix()}", "downloadable": True, 'section_id': cms_section['id']} @pytest.fixture(scope="module") @@ -522,10 +522,9 @@ def cms_file(api, cms_file_data, cms_file_files): @pytest.fixture(scope="module") -def cms_section_params(cms_file): +def cms_section_params(): """CMS section fixture params""" - return {"title": f"title-{get_suffix()}", "public": True, "partial_path": f"/path-{get_suffix()}", - "cms_file_ids": [cms_file['id']]} + return {"title": f"title-{get_suffix()}", "public": True, "partial_path": f"/path-{get_suffix()}"} @pytest.fixture(scope="module") @@ -539,7 +538,7 @@ def cms_section(api, cms_section_params): @pytest.fixture(scope="module") def cms_partial_params(): """CMS partial fixture params""" - return {"type": "partial", "system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}"} + return {"system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}"} @pytest.fixture(scope="module") @@ -553,8 +552,8 @@ def cms_partial(api, cms_partial_params): @pytest.fixture(scope="module") def cms_layout_params(cms_section): """CMS layout fixture params""" - return {"type": "layout", "system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}", - "title": f"title-{get_suffix()}", "liquid_enabled": True} + return {"system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}", + "title": f"title-{get_suffix()}", "liquid_enabled": True, "section_id": cms_section['id']} @pytest.fixture(scope="module") def cms_layout(api, cms_layout_params): @@ -566,12 +565,11 @@ def cms_layout(api, cms_layout_params): @pytest.fixture(scope="module") def cms_page_params(cms_section, cms_layout): """CMS page fixture params""" - return {"type": "page", "system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}", + 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'], - "liquid_enabled": True, "handler": "markdown", "tag_list": [1,2,3,4,5], - "content_type": "text/html"} + "liquid_enabled": True, "handler": "markdown", "content_type": "text/html"} @pytest.fixture(scope="module") diff --git a/tests/integration/test_integration_cms.py b/tests/integration/test_integration_cms.py index 4b10bb8..2249bcf 100644 --- a/tests/integration/test_integration_cms.py +++ b/tests/integration/test_integration_cms.py @@ -1,4 +1,6 @@ +import pytest from tests.integration import asserts +from threescale_api import errors from .asserts import assert_resource, assert_resource_params @@ -33,6 +35,9 @@ def test_file_can_be_updated(cms_file_data, cms_file): """ Can be file object updated? """ updated_path = cms_file['path'] + 'up' cms_file['path'] = cms_file['path'] + 'up' + # TODO https://issues.redhat.com/browse/THREESCALE-9571 + for item in "created_at", "updated_at", "url", "title", "content_type": + cms_file.pop(item) cms_file.update() assert cms_file['path'] == updated_path updated = cms_file.read() @@ -41,20 +46,6 @@ def test_file_can_be_updated(cms_file_data, cms_file): # Sections -# builtin - -def test_builtin_section_list(api): - """ List all sections. """ - assert len(api.cms_builtin_sections.list()) >= 1 - - -def test_builtin_section_can_be_read(api): - """ It is possible to get section by ID? """ - cms_section = api.cms_builtin_sections.list()[-1] - read = api.cms_sections.read(cms_section.entity_id) - asserts.assert_resource(read) - -# user def test_section_list(api, cms_section): @@ -79,12 +70,26 @@ def test_section_can_be_updated(cms_section_params, cms_section): """ Can be section object updated? """ updated_title = cms_section['title'] + 'up' cms_section['title'] = cms_section['title'] + 'up' + # TODO https://issues.redhat.com/browse/THREESCALE-9571 + for item in "created_at", "updated_at": + cms_section.pop(item) cms_section.update() assert cms_section['title'] == updated_title updated = cms_section.read() assert updated['title'] == updated_title assert cms_section['title'] == updated_title + +# # bug!!! TODO https://issues.redhat.com/browse/THREESCALE-9572 +# def test_builtin_section_delete(api): +# """It is not possible to delete section partial.""" +# with pytest.raises(errors.ApiClientError) as exc_info: +# api.cms_sections.list()[0].delete() +# assert exc_info.value.code == 423 +# # TODO +# # assert exc_info.value.code == 400 + + # Partials # builtin @@ -100,6 +105,14 @@ def test_builtin_partial_can_be_read(api): read = api.cms_builtin_partials.read(cms_partial.entity_id) asserts.assert_resource(read) +def test_builtin_partial_delete(api): + """It is not possible to delete builtin partial.""" + with pytest.raises(errors.ApiClientError) as exc_info: + api.cms_builtin_partials.list()[0].delete() + assert exc_info.value.code == 423 + # TODO https://issues.redhat.com/browse/THREESCALE-9572 + # assert exc_info.value.code == 400 + # user @@ -125,6 +138,9 @@ def test_partial_can_be_updated(cms_partial_params, cms_partial): """ Can be partial object updated? """ updated_draft = cms_partial['draft'] + 'up' cms_partial['draft'] = cms_partial['draft'] + 'up' + # TODO https://issues.redhat.com/browse/THREESCALE-9571 + for item in "created_at", "updated_at", "published": + cms_partial.pop(item) cms_partial.update() assert cms_partial['draft'] == updated_draft updated = cms_partial.read() @@ -158,6 +174,15 @@ def test_builtin_page_can_be_read(api): asserts.assert_resource(read) +def test_builtin_page_delete(api): + """It is not possible to delete builtin page.""" + with pytest.raises(errors.ApiClientError) as exc_info: + api.cms_builtin_pages.list()[0].delete() + assert exc_info.value.code == 423 + # TODO https://issues.redhat.com/browse/THREESCALE-9572 + # assert exc_info.value.code == 400 + + # user @@ -183,6 +208,9 @@ def test_page_can_be_updated(cms_page_params, cms_page): """ Can be page object updated? """ updated_draft = cms_page['draft'] + 'up' cms_page['draft'] = cms_page['draft'] + 'up' + # TODO https://issues.redhat.com/browse/THREESCALE-9571 + for item in "created_at", "updated_at", "hidden", "published": + cms_page.pop(item) cms_page.update() assert cms_page['draft'] == updated_draft updated = cms_page.read() @@ -225,6 +253,9 @@ def test_layout_can_be_updated(cms_layout_params, cms_layout): """ Can be layout object updated? """ updated_draft = cms_layout['draft'] + 'up' cms_layout['draft'] = cms_layout['draft'] + 'up' + # TODO https://issues.redhat.com/browse/THREESCALE-9571 + for item in "created_at", "updated_at", "published": + cms_layout.pop(item) cms_layout.update() assert cms_layout['draft'] == updated_draft updated = cms_layout.read() diff --git a/threescale_api/client.py b/threescale_api/client.py index 6ccc5ea..6e40251 100644 --- a/threescale_api/client.py +++ b/threescale_api/client.py @@ -52,8 +52,6 @@ def __init__(self, url: str, token: str, resources.FieldsDefinitions(self, instance_klass=resources.FieldsDefinition) self._cms_files = resources.CmsFiles(self, instance_klass=resources.CmsFile) self._cms_sections = resources.CmsSections(self, instance_klass=resources.CmsSection) - self._cms_builtin_sections =\ - resources.CmsBuiltinSections(self, instance_klass=resources.CmsSection) self._cms_pages = resources.CmsPages(self, instance_klass=resources.CmsPage) self._cms_builtin_pages = resources.CmsBuiltinPages(self, instance_klass=resources.CmsPage) self._cms_layouts = resources.CmsLayouts(self, instance_klass=resources.CmsLayout) @@ -273,10 +271,6 @@ def cms_files(self) -> resources.CmsFiles: def cms_sections(self) -> resources.CmsSections: return self._cms_sections - @property - def cms_builtin_sections(self) -> resources.CmsBuiltinSections: - return self._cms_builtin_sections - @property def cms_pages(self) -> resources.CmsPages: return self._cms_pages diff --git a/threescale_api/resources.py b/threescale_api/resources.py index 1571fbe..6678f5a 100644 --- a/threescale_api/resources.py +++ b/threescale_api/resources.py @@ -919,6 +919,12 @@ class CmsClient(DefaultClient): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + def _extract_resource(self, response, collection) -> Union[List, Dict]: + extracted = response.json() + if self._entity_collection and self._entity_collection in extracted: + extracted = extracted.get(self._entity_collection) + return extracted + def _list(self, **kwargs): if "page" in kwargs.get("params", {}): return super()._list(**kwargs) @@ -948,7 +954,7 @@ def __iter__(self): class CmsFiles(CmsClient): """ Client for files. """ - def __init__(self, *args, entity_name='file', entity_collection='files', **kwargs): + def __init__(self, *args, entity_name='file', entity_collection='collection', **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -959,7 +965,7 @@ def url(self) -> str: class CmsSections(CmsClient): """ Client for sections. """ - def __init__(self, *args, entity_name='section', entity_collection='sections', **kwargs): + def __init__(self, *args, entity_name='section', entity_collection='collection', **kwargs): super().__init__(*args, entity_name=entity_name, entity_collection=entity_collection, **kwargs) @@ -968,17 +974,9 @@ def url(self) -> str: return self.threescale_client.admin_api_url + '/cms/sections' -class CmsBuiltinSections(CmsSections): - """ Client for builtin sections. """ - def __init__(self, *args, entity_name='builtin_section', entity_collection='sections', - **kwargs): - super().__init__(*args, entity_name=entity_name, - entity_collection=entity_collection, **kwargs) - - class CmsTemplates(CmsClient): """ Client for templates. """ - def __init__(self, *args, entity_collection='templates', **kwargs): + def __init__(self, *args, entity_collection='collection', **kwargs): super().__init__(*args, entity_collection=entity_collection, **kwargs) @property @@ -993,6 +991,21 @@ def publish(self, entity_id, **kwargs): instance = self._create_instance(response=response) return instance + def list(self, **kwargs) -> List['DefaultResource']: + """List all entities + Args: + **kwargs: Optional parameters + Returns(List['DefaultResource]): List of resources + """ + log.info(self._log_message("[LIST] List", args=kwargs)) + instance = self.select_by(type=self._entity_name, **kwargs) + return instance + + def create(self, params: dict = None, + *args, **kwargs) -> 'DefaultResource': + params.update({'type': self._entity_name}) + return super().create(params=params, **kwargs) + class CmsPages(CmsTemplates): """ Client for pages """ diff --git a/threescale_api/utils.py b/threescale_api/utils.py index 203326b..dba6648 100644 --- a/threescale_api/utils.py +++ b/threescale_api/utils.py @@ -23,7 +23,7 @@ def extract_response(response: requests.Response, entity: str = None, if collection and collection in extracted: extracted = extracted.get(collection) if isinstance(extracted, list): - return [value.get(entity) for value in extracted if entity in value] + return [value.get(entity) for value in extracted] if entity in extracted.keys(): return extracted.get(entity) return extracted