Commit 35342ebf authored by Élodie Thiéblin's avatar Élodie Thiéblin
Browse files

feat(urlpublish): add empty_rset_raises_404 flag on rql rewrite urls

parent f104b7b55d13
......@@ -30,6 +30,7 @@ from cubicweb.devtools.fake import FakeRequest
from cubicweb.web import LogOut, Redirect, INTERNAL_FIELD_VALUE
from cubicweb.web.views.basecontrollers import ViewController
from cubicweb.web.application import anonymized_request
from cubicweb.web.views.urlrewrite import SimpleReqRewriter, rgx
class FakeMapping:
......@@ -760,18 +761,37 @@ class ApplicationTC(CubicWebTC):
def test_notfound_raised_on_empty_underlying_rset(self):
"""tests URLs that are rewritten with an underlying rqlquery.
If the rql query is empty, a NotFound error should be raised."""
If the rql query is empty and the flag empty_rset_raises_404 is True,
a NotFound error should be raised."""
oldrules = SimpleReqRewriter.rules
with self.new_access('anon').web_request('schema/CWUser') as req:
SimpleReqRewriter.rules = [(rgx(r'/mycwetypeurl/([^/]+)'),
dict(vid='primary',
rql=r'Any T WHERE T is CWEType, T name "\1"',
empty_rset_raises_404=True),)]
with self.new_access('anon').web_request('mycwetypeurl/CWUser') as req:
self.app_handle_request(req)
self.assertEqual(req.status_out, 200)
# There is an underlying rql to fetch the schema of cwetype.
# As there is no entity «pouet», it should raise NotFound.
with self.new_access('anon').web_request('schema/pouet') as req:
with self.new_access('anon').web_request('mycwetypeurl/pouet') as req:
self.app_handle_request(req)
self.assertEqual(req.status_out, 404)
SimpleReqRewriter.rules = [(rgx(r'/mycwetypeurl/([^/]+)'),
dict(vid='primary',
rql=r'Any T WHERE T is CWEType, T name "\1"'),)]
with self.new_access('anon').web_request('mycwetypeurl/CWUser') as req:
self.app_handle_request(req)
self.assertEqual(req.status_out, 200)
with self.new_access('anon').web_request('mycwetypeurl/pouet') as req:
self.app_handle_request(req)
self.assertEqual(req.status_out, 200)
SimpleReqRewriter.rules = oldrules
if __name__ == '__main__':
unittest_main()
......@@ -89,7 +89,11 @@ class ViewController(Controller):
:raise NotFound: if the rql has been rewritten (rewrite rules) and the
resulting rset is empty"""
view, rset = self._select_view_and_rset(rset)
if not rset and getattr(self._cw, '_rql_rewritten', False):
if (
not rset
and getattr(self._cw, '_rql_rewritten', False)
and self._cw.form.get("empty_rset_raises_404", False)
):
raise NotFound
view.set_http_cache_headers()
if self._cw.is_client_cache_valid():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment