Skip to content

Commit 9947c8d

Browse files
author
mkudlej
authored
Merge pull request #139 from mkudlej/cms_filters
changes for bug fixes and enhacements THREESCALE-9576, THREESCALE-9172, THREESCALE-9572, THREESCALE-9191
2 parents b65eafe + 6f609ab commit 9947c8d

File tree

3 files changed

+105
-32
lines changed

3 files changed

+105
-32
lines changed

tests/integration/conftest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,11 @@ def cms_file(api, cms_file_data, cms_file_files):
522522

523523

524524
@pytest.fixture(scope="module")
525-
def cms_section_params():
525+
def cms_section_params(api):
526526
"""CMS section fixture params"""
527-
return {"title": f"title-{get_suffix()}", "public": True, "partial_path": f"/path-{get_suffix()}"}
527+
parent_id = api.cms_sections.list()[0]['id']
528+
return {"title": f"title-{get_suffix()}", "public": True,
529+
"partial_path": f"/path-{get_suffix()}", "parent_id": parent_id}
528530

529531

530532
@pytest.fixture(scope="module")
@@ -550,10 +552,10 @@ def cms_partial(api, cms_partial_params):
550552

551553

552554
@pytest.fixture(scope="module")
553-
def cms_layout_params(cms_section):
555+
def cms_layout_params():
554556
"""CMS layout fixture params"""
555557
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']}
558+
"title": f"title-{get_suffix()}", "liquid_enabled": True}
557559

558560
@pytest.fixture(scope="module")
559561
def cms_layout(api, cms_layout_params):

tests/integration/test_integration_cms.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
""" Test CMS API """
2+
13
import pytest
24
from tests.integration import asserts
35
from threescale_api import errors
@@ -35,7 +37,7 @@ def test_file_can_be_updated(cms_file_data, cms_file):
3537
""" Can be file object updated? """
3638
updated_path = cms_file['path'] + 'up'
3739
cms_file['path'] = cms_file['path'] + 'up'
38-
# TODO https://issues.redhat.com/browse/THREESCALE-9571
40+
# https://issues.redhat.com/browse/THREESCALE-9571
3941
for item in "created_at", "updated_at", "url", "title", "content_type":
4042
cms_file.pop(item)
4143
cms_file.update()
@@ -70,7 +72,7 @@ def test_section_can_be_updated(cms_section_params, cms_section):
7072
""" Can be section object updated? """
7173
updated_title = cms_section['title'] + 'up'
7274
cms_section['title'] = cms_section['title'] + 'up'
73-
# TODO https://issues.redhat.com/browse/THREESCALE-9571
75+
# https://issues.redhat.com/browse/THREESCALE-9571
7476
for item in "created_at", "updated_at":
7577
cms_section.pop(item)
7678
cms_section.update()
@@ -80,14 +82,11 @@ def test_section_can_be_updated(cms_section_params, cms_section):
8082
assert cms_section['title'] == updated_title
8183

8284

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
85+
def test_builtin_section_delete(api):
86+
"""It is not possible to delete section partial."""
87+
with pytest.raises(errors.ApiClientError) as exc_info:
88+
api.cms_sections.list()[0].delete()
89+
assert exc_info.value.code == 422
9190

9291

9392
# Partials
@@ -109,16 +108,17 @@ def test_builtin_partial_delete(api):
109108
"""It is not possible to delete builtin partial."""
110109
with pytest.raises(errors.ApiClientError) as exc_info:
111110
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
111+
assert exc_info.value.code == 422
115112

116113
# user
117114

118115

119116
def test_partial_list(api, cms_partial):
120117
""" List all user defined partials. """
121-
assert len(api.cms_partials.list()) >= 1
118+
parts_list = api.cms_partials.list()
119+
assert len(parts_list) >= 1
120+
assert all('draft' in part.entity.keys() and 'published' in part.entity.keys()
121+
for part in parts_list)
122122

123123

124124
def test_partial_can_be_created(cms_partial_params, cms_partial):
@@ -138,7 +138,7 @@ def test_partial_can_be_updated(cms_partial_params, cms_partial):
138138
""" Can be partial object updated? """
139139
updated_draft = cms_partial['draft'] + 'up'
140140
cms_partial['draft'] = cms_partial['draft'] + 'up'
141-
# TODO https://issues.redhat.com/browse/THREESCALE-9571
141+
# https://issues.redhat.com/browse/THREESCALE-9571
142142
for item in "created_at", "updated_at", "published":
143143
cms_partial.pop(item)
144144
cms_partial.update()
@@ -153,8 +153,7 @@ def test_partial_publish(cms_partial):
153153
assert cms_partial.entity.get('published', None) is None
154154
draft = cms_partial['draft']
155155
cms_partial = cms_partial.publish()
156-
# assert draft == cms_partial['draft'] bug
157-
# assert cms_partial['published'] == cms_partial['draft'] bug
156+
assert cms_partial['draft'] == None
158157
assert draft == cms_partial['published']
159158

160159

@@ -178,9 +177,7 @@ def test_builtin_page_delete(api):
178177
"""It is not possible to delete builtin page."""
179178
with pytest.raises(errors.ApiClientError) as exc_info:
180179
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
180+
assert exc_info.value.code == 422
184181

185182

186183
# user
@@ -208,7 +205,7 @@ def test_page_can_be_updated(cms_page_params, cms_page):
208205
""" Can be page object updated? """
209206
updated_draft = cms_page['draft'] + 'up'
210207
cms_page['draft'] = cms_page['draft'] + 'up'
211-
# TODO https://issues.redhat.com/browse/THREESCALE-9571
208+
# https://issues.redhat.com/browse/THREESCALE-9571
212209
for item in "created_at", "updated_at", "hidden", "published":
213210
cms_page.pop(item)
214211
cms_page.update()
@@ -223,8 +220,6 @@ def test_page_publish(cms_page):
223220
assert cms_page.entity.get('published', None) is None
224221
draft = cms_page['draft']
225222
cms_page = cms_page.publish()
226-
# assert draft == cms_page['draft'] bug
227-
# assert cms_page['published'] == cms_page['draft'] bug
228223
assert draft == cms_page['published']
229224

230225

@@ -253,7 +248,7 @@ def test_layout_can_be_updated(cms_layout_params, cms_layout):
253248
""" Can be layout object updated? """
254249
updated_draft = cms_layout['draft'] + 'up'
255250
cms_layout['draft'] = cms_layout['draft'] + 'up'
256-
# TODO https://issues.redhat.com/browse/THREESCALE-9571
251+
# https://issues.redhat.com/browse/THREESCALE-9571
257252
for item in "created_at", "updated_at", "published":
258253
cms_layout.pop(item)
259254
cms_layout.update()
@@ -268,6 +263,32 @@ def test_layout_publish(cms_layout):
268263
assert cms_layout.entity.get('published', None) is None
269264
draft = cms_layout['draft']
270265
cms_layout = cms_layout.publish()
271-
# assert draft == cms_layout['draft'] bug
272-
# assert cms_layout['published'] == cms_layout['draft'] bug
273266
assert draft == cms_layout['published']
267+
268+
# filters
269+
def test_section_filter(api, cms_section):
270+
""" Test section filtering """
271+
assert all(sec['parent_id'] == cms_section['parent_id']
272+
for sec in api.cms_sections.select_by(parent_id=cms_section['parent_id']))
273+
assert api.cms_sections.select_by(title=cms_section['title'])[0] == cms_section
274+
275+
def test_files_filter(api, cms_file, cms_section):
276+
""" Test files filtering """
277+
assert api.cms_files.select_by(section_id=cms_section['id'])[0] == cms_file
278+
assert api.cms_files.select_by(path=cms_file['path'])[0] == cms_file
279+
280+
# https://issues.redhat.com/browse/THREESCALE-9191?focusedId=22406548&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-22406548
281+
#def test_partial_filter(api, cms_partial, cms_section):
282+
# """ Test partial filtering """
283+
# assert api.cms_partials.select_by(section_id=cms_section['id'])[0] == cms_partial
284+
# assert api.cms_partials.select_by(system_name=cms_partial['system_name'])[0] == cms_partial
285+
286+
#def test_layout_filter(api, cms_layout, cms_section):
287+
# """ Test layout filtering """
288+
# assert api.cms_layouts.select_by(section_id=cms_section['id'])[0] == cms_layout
289+
# assert api.cms_layouts.select_by(title=cms_layout['title'])[0] == cms_layout
290+
291+
def test_page_filter(api, cms_section, cms_page):
292+
""" Test page filtering """
293+
assert api.cms_pages.select_by(section_id=cms_section['id'])[0] == cms_page
294+
assert api.cms_pages.select_by(title=cms_page['title'])[0] == cms_page

threescale_api/resources.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,39 @@ def _extract_resource(self, response, collection) -> Union[List, Dict]:
910910
extracted = extracted.get(self._entity_collection)
911911
return extracted
912912

913+
def select_by(self, **params):
914+
"""Select by params - logical "and" Usage example: select_by(role='admin')
915+
Filtering by some params can be done on the backend.
916+
Filters for each class are stored in class variable FILTERS.
917+
Filters are removed because they are not part of function "predicate".
918+
-------------------------------------
919+
| Endpoint | Filters |
920+
-------------------------------------
921+
| Sections #index | parent_id |
922+
| Files #index | section_id |
923+
| Templates #index | type, section_id |
924+
-------------------------------------
925+
Args:
926+
**params: params used for selection
927+
Returns: List of resources
928+
"""
929+
log.debug("[SELECT] By params: %s", params)
930+
931+
filters = {fil: params.pop(fil) for fil in self.FILTERS if fil in params}
932+
933+
def predicate(item):
934+
for (key, val) in params.items():
935+
if item[key] != val:
936+
return False
937+
return True
938+
if filters:
939+
return self.select(predicate=predicate, params=filters)
940+
return self.select(predicate=predicate)
941+
913942

914943
class CmsFiles(CmsClient):
944+
FILTERS = ['parent_id']
945+
915946
""" Client for files. """
916947
def __init__(self, *args, entity_name='file', entity_collection='collection', **kwargs):
917948
super().__init__(*args, entity_name=entity_name,
@@ -923,6 +954,8 @@ def url(self) -> str:
923954

924955

925956
class CmsSections(CmsClient):
957+
FILTERS = ['section_id']
958+
926959
""" Client for sections. """
927960
def __init__(self, *args, entity_name='section', entity_collection='collection', **kwargs):
928961
super().__init__(*args, entity_name=entity_name,
@@ -934,6 +967,8 @@ def url(self) -> str:
934967

935968

936969
class CmsTemplates(CmsClient):
970+
FILTERS = ['type'] # , 'section_id']
971+
937972
""" Client for templates. """
938973
def __init__(self, *args, entity_collection='collection', **kwargs):
939974
super().__init__(*args, entity_collection=entity_collection, **kwargs)
@@ -957,13 +992,28 @@ def list(self, **kwargs) -> List['DefaultResource']:
957992
Returns(List['DefaultResource]): List of resources
958993
"""
959994
log.info(self._log_message("[LIST] List", args=kwargs))
960-
instance = self.select_by(type=self._entity_name, **kwargs)
995+
kwargs.setdefault("params", {})
996+
kwargs["params"].setdefault("content", "true")
997+
kwargs["params"].setdefault("type", self._entity_name)
998+
instance = self._list(**kwargs)
961999
return instance
9621000

1001+
def select(self, predicate, **kwargs) -> List['DefaultResource']:
1002+
"""Select resource s based on the predicate
1003+
Args:
1004+
predicate: Predicate
1005+
**kwargs: Optional args
1006+
Returns: List of resources
1007+
"""
1008+
kwargs.setdefault("params", {})
1009+
kwargs["params"].setdefault("content", "true")
1010+
kwargs["params"].setdefault("type", self._entity_name)
1011+
return [item for item in self._list(**kwargs) if predicate(item)]
1012+
9631013
def create(self, params: dict = None,
9641014
*args, **kwargs) -> 'DefaultResource':
9651015
params.update({'type': self._entity_name})
966-
return super().create(params=params, **kwargs)
1016+
return super().create(params=params, *args, **kwargs)
9671017

9681018

9691019
class CmsPages(CmsTemplates):

0 commit comments

Comments
 (0)