Skip to content

Commit

Permalink
changes for new version of API
Browse files Browse the repository at this point in the history
  • Loading branch information
mkudlej committed Apr 28, 2023
1 parent b8c47fe commit 51d3956
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 43 deletions.
20 changes: 9 additions & 11 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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):
Expand All @@ -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")
Expand Down
59 changes: 45 additions & 14 deletions tests/integration/test_integration_cms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from tests.integration import asserts
from threescale_api import errors
from .asserts import assert_resource, assert_resource_params


Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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

Expand All @@ -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


Expand All @@ -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()
Expand Down Expand Up @@ -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


Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 0 additions & 6 deletions threescale_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
36 changes: 25 additions & 11 deletions threescale_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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
Expand All @@ -993,6 +991,22 @@ 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 """
Expand Down
2 changes: 1 addition & 1 deletion threescale_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 51d3956

Please sign in to comment.