Skip to content

Commit dd27263

Browse files
author
Martin Kudlej
committed
changes for new version of API
1 parent b8c47fe commit dd27263

File tree

5 files changed

+79
-43
lines changed

5 files changed

+79
-43
lines changed

tests/integration/conftest.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,9 @@ def fields_definition(api, fields_definitions_params):
500500

501501

502502
@pytest.fixture(scope="module")
503-
def cms_file_data():
503+
def cms_file_data(cms_section):
504504
"""CMS file fixture data"""
505-
return {"path": f"/path{get_suffix()}", "downloadable": True}
505+
return {"path": f"/path{get_suffix()}", "downloadable": True, 'section_id': cms_section['id']}
506506

507507

508508
@pytest.fixture(scope="module")
@@ -522,10 +522,9 @@ def cms_file(api, cms_file_data, cms_file_files):
522522

523523

524524
@pytest.fixture(scope="module")
525-
def cms_section_params(cms_file):
525+
def cms_section_params():
526526
"""CMS section fixture params"""
527-
return {"title": f"title-{get_suffix()}", "public": True, "partial_path": f"/path-{get_suffix()}",
528-
"cms_file_ids": [cms_file['id']]}
527+
return {"title": f"title-{get_suffix()}", "public": True, "partial_path": f"/path-{get_suffix()}"}
529528

530529

531530
@pytest.fixture(scope="module")
@@ -539,7 +538,7 @@ def cms_section(api, cms_section_params):
539538
@pytest.fixture(scope="module")
540539
def cms_partial_params():
541540
"""CMS partial fixture params"""
542-
return {"type": "partial", "system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}"}
541+
return {"system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}"}
543542

544543

545544
@pytest.fixture(scope="module")
@@ -553,8 +552,8 @@ def cms_partial(api, cms_partial_params):
553552
@pytest.fixture(scope="module")
554553
def cms_layout_params(cms_section):
555554
"""CMS layout fixture params"""
556-
return {"type": "layout", "system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}",
557-
"title": f"title-{get_suffix()}", "liquid_enabled": True}
555+
return {"system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}",
556+
"title": f"title-{get_suffix()}", "liquid_enabled": True, "section_id": cms_section['id']}
558557

559558
@pytest.fixture(scope="module")
560559
def cms_layout(api, cms_layout_params):
@@ -566,12 +565,11 @@ def cms_layout(api, cms_layout_params):
566565
@pytest.fixture(scope="module")
567566
def cms_page_params(cms_section, cms_layout):
568567
"""CMS page fixture params"""
569-
return {"type": "page", "system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}",
568+
return {"system_name": f"sname-{get_suffix()}", "draft": f"draft-{get_suffix()}",
570569
"title": f"title-{get_suffix()}", "path": f"/path-{get_suffix()}",
571570
"section_name": f"section-{get_suffix()}", "section_id": cms_section['id'],
572571
"layout_name": f"layout-{get_suffix()}", "layout_id": cms_layout['id'],
573-
"liquid_enabled": True, "handler": "markdown", "tag_list": [1,2,3,4,5],
574-
"content_type": "text/html"}
572+
"liquid_enabled": True, "handler": "markdown", "content_type": "text/html"}
575573

576574

577575
@pytest.fixture(scope="module")

tests/integration/test_integration_cms.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import pytest
12
from tests.integration import asserts
3+
from threescale_api import errors
24
from .asserts import assert_resource, assert_resource_params
35

46

@@ -33,6 +35,9 @@ def test_file_can_be_updated(cms_file_data, cms_file):
3335
""" Can be file object updated? """
3436
updated_path = cms_file['path'] + 'up'
3537
cms_file['path'] = cms_file['path'] + 'up'
38+
# TODO https://issues.redhat.com/browse/THREESCALE-9571
39+
for item in "created_at", "updated_at", "url", "title", "content_type":
40+
cms_file.pop(item)
3641
cms_file.update()
3742
assert cms_file['path'] == updated_path
3843
updated = cms_file.read()
@@ -41,20 +46,6 @@ def test_file_can_be_updated(cms_file_data, cms_file):
4146

4247

4348
# Sections
44-
# builtin
45-
46-
def test_builtin_section_list(api):
47-
""" List all sections. """
48-
assert len(api.cms_builtin_sections.list()) >= 1
49-
50-
51-
def test_builtin_section_can_be_read(api):
52-
""" It is possible to get section by ID? """
53-
cms_section = api.cms_builtin_sections.list()[-1]
54-
read = api.cms_sections.read(cms_section.entity_id)
55-
asserts.assert_resource(read)
56-
57-
# user
5849

5950

6051
def test_section_list(api, cms_section):
@@ -79,12 +70,26 @@ def test_section_can_be_updated(cms_section_params, cms_section):
7970
""" Can be section object updated? """
8071
updated_title = cms_section['title'] + 'up'
8172
cms_section['title'] = cms_section['title'] + 'up'
73+
# TODO https://issues.redhat.com/browse/THREESCALE-9571
74+
for item in "created_at", "updated_at":
75+
cms_section.pop(item)
8276
cms_section.update()
8377
assert cms_section['title'] == updated_title
8478
updated = cms_section.read()
8579
assert updated['title'] == updated_title
8680
assert cms_section['title'] == updated_title
8781

82+
83+
# # bug!!! TODO https://issues.redhat.com/browse/THREESCALE-9572
84+
# def test_builtin_section_delete(api):
85+
# """It is not possible to delete section partial."""
86+
# with pytest.raises(errors.ApiClientError) as exc_info:
87+
# api.cms_sections.list()[0].delete()
88+
# assert exc_info.value.code == 423
89+
# # TODO
90+
# # assert exc_info.value.code == 400
91+
92+
8893
# Partials
8994
# builtin
9095

@@ -100,6 +105,14 @@ def test_builtin_partial_can_be_read(api):
100105
read = api.cms_builtin_partials.read(cms_partial.entity_id)
101106
asserts.assert_resource(read)
102107

108+
def test_builtin_partial_delete(api):
109+
"""It is not possible to delete builtin partial."""
110+
with pytest.raises(errors.ApiClientError) as exc_info:
111+
api.cms_builtin_partials.list()[0].delete()
112+
assert exc_info.value.code == 423
113+
# TODO https://issues.redhat.com/browse/THREESCALE-9572
114+
# assert exc_info.value.code == 400
115+
103116
# user
104117

105118

@@ -125,6 +138,9 @@ def test_partial_can_be_updated(cms_partial_params, cms_partial):
125138
""" Can be partial object updated? """
126139
updated_draft = cms_partial['draft'] + 'up'
127140
cms_partial['draft'] = cms_partial['draft'] + 'up'
141+
# TODO https://issues.redhat.com/browse/THREESCALE-9571
142+
for item in "created_at", "updated_at", "published":
143+
cms_partial.pop(item)
128144
cms_partial.update()
129145
assert cms_partial['draft'] == updated_draft
130146
updated = cms_partial.read()
@@ -158,6 +174,15 @@ def test_builtin_page_can_be_read(api):
158174
asserts.assert_resource(read)
159175

160176

177+
def test_builtin_page_delete(api):
178+
"""It is not possible to delete builtin page."""
179+
with pytest.raises(errors.ApiClientError) as exc_info:
180+
api.cms_builtin_pages.list()[0].delete()
181+
assert exc_info.value.code == 423
182+
# TODO https://issues.redhat.com/browse/THREESCALE-9572
183+
# assert exc_info.value.code == 400
184+
185+
161186
# user
162187

163188

@@ -183,6 +208,9 @@ def test_page_can_be_updated(cms_page_params, cms_page):
183208
""" Can be page object updated? """
184209
updated_draft = cms_page['draft'] + 'up'
185210
cms_page['draft'] = cms_page['draft'] + 'up'
211+
# TODO https://issues.redhat.com/browse/THREESCALE-9571
212+
for item in "created_at", "updated_at", "hidden", "published":
213+
cms_page.pop(item)
186214
cms_page.update()
187215
assert cms_page['draft'] == updated_draft
188216
updated = cms_page.read()
@@ -225,6 +253,9 @@ def test_layout_can_be_updated(cms_layout_params, cms_layout):
225253
""" Can be layout object updated? """
226254
updated_draft = cms_layout['draft'] + 'up'
227255
cms_layout['draft'] = cms_layout['draft'] + 'up'
256+
# TODO https://issues.redhat.com/browse/THREESCALE-9571
257+
for item in "created_at", "updated_at", "published":
258+
cms_layout.pop(item)
228259
cms_layout.update()
229260
assert cms_layout['draft'] == updated_draft
230261
updated = cms_layout.read()

threescale_api/client.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ def __init__(self, url: str, token: str,
5252
resources.FieldsDefinitions(self, instance_klass=resources.FieldsDefinition)
5353
self._cms_files = resources.CmsFiles(self, instance_klass=resources.CmsFile)
5454
self._cms_sections = resources.CmsSections(self, instance_klass=resources.CmsSection)
55-
self._cms_builtin_sections =\
56-
resources.CmsBuiltinSections(self, instance_klass=resources.CmsSection)
5755
self._cms_pages = resources.CmsPages(self, instance_klass=resources.CmsPage)
5856
self._cms_builtin_pages = resources.CmsBuiltinPages(self, instance_klass=resources.CmsPage)
5957
self._cms_layouts = resources.CmsLayouts(self, instance_klass=resources.CmsLayout)
@@ -273,10 +271,6 @@ def cms_files(self) -> resources.CmsFiles:
273271
def cms_sections(self) -> resources.CmsSections:
274272
return self._cms_sections
275273

276-
@property
277-
def cms_builtin_sections(self) -> resources.CmsBuiltinSections:
278-
return self._cms_builtin_sections
279-
280274
@property
281275
def cms_pages(self) -> resources.CmsPages:
282276
return self._cms_pages

threescale_api/resources.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,12 @@ class CmsClient(DefaultClient):
919919
def __init__(self, *args, **kwargs):
920920
super().__init__(*args, **kwargs)
921921

922+
def _extract_resource(self, response, collection) -> Union[List, Dict]:
923+
extracted = response.json()
924+
if self._entity_collection and self._entity_collection in extracted:
925+
extracted = extracted.get(self._entity_collection)
926+
return extracted
927+
922928
def _list(self, **kwargs):
923929
if "page" in kwargs.get("params", {}):
924930
return super()._list(**kwargs)
@@ -948,7 +954,7 @@ def __iter__(self):
948954

949955
class CmsFiles(CmsClient):
950956
""" Client for files. """
951-
def __init__(self, *args, entity_name='file', entity_collection='files', **kwargs):
957+
def __init__(self, *args, entity_name='file', entity_collection='collection', **kwargs):
952958
super().__init__(*args, entity_name=entity_name,
953959
entity_collection=entity_collection, **kwargs)
954960

@@ -959,7 +965,7 @@ def url(self) -> str:
959965

960966
class CmsSections(CmsClient):
961967
""" Client for sections. """
962-
def __init__(self, *args, entity_name='section', entity_collection='sections', **kwargs):
968+
def __init__(self, *args, entity_name='section', entity_collection='collection', **kwargs):
963969
super().__init__(*args, entity_name=entity_name,
964970
entity_collection=entity_collection, **kwargs)
965971

@@ -968,17 +974,9 @@ def url(self) -> str:
968974
return self.threescale_client.admin_api_url + '/cms/sections'
969975

970976

971-
class CmsBuiltinSections(CmsSections):
972-
""" Client for builtin sections. """
973-
def __init__(self, *args, entity_name='builtin_section', entity_collection='sections',
974-
**kwargs):
975-
super().__init__(*args, entity_name=entity_name,
976-
entity_collection=entity_collection, **kwargs)
977-
978-
979977
class CmsTemplates(CmsClient):
980978
""" Client for templates. """
981-
def __init__(self, *args, entity_collection='templates', **kwargs):
979+
def __init__(self, *args, entity_collection='collection', **kwargs):
982980
super().__init__(*args, entity_collection=entity_collection, **kwargs)
983981

984982
@property
@@ -993,6 +991,21 @@ def publish(self, entity_id, **kwargs):
993991
instance = self._create_instance(response=response)
994992
return instance
995993

994+
def list(self, **kwargs) -> List['DefaultResource']:
995+
"""List all entities
996+
Args:
997+
**kwargs: Optional parameters
998+
Returns(List['DefaultResource]): List of resources
999+
"""
1000+
log.info(self._log_message("[LIST] List", args=kwargs))
1001+
instance = self.select_by(type=self._entity_name, **kwargs)
1002+
return instance
1003+
1004+
def create(self, params: dict = None,
1005+
*args, **kwargs) -> 'DefaultResource':
1006+
params.update({'type': self._entity_name})
1007+
return super().create(params=params, **kwargs)
1008+
9961009

9971010
class CmsPages(CmsTemplates):
9981011
""" Client for pages """

threescale_api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def extract_response(response: requests.Response, entity: str = None,
2323
if collection and collection in extracted:
2424
extracted = extracted.get(collection)
2525
if isinstance(extracted, list):
26-
return [value.get(entity) for value in extracted if entity in value]
26+
return [value.get(entity) for value in extracted]
2727
if entity in extracted.keys():
2828
return extracted.get(entity)
2929
return extracted

0 commit comments

Comments
 (0)