cubicweb/ext/test/unittest_rest.py
changeset 11057 0b59724cb3f2
parent 10960 9e64fddebc89
child 11357 ef53a59140e1
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 from six import PY3
       
    19 
       
    20 from logilab.common.testlib import unittest_main
       
    21 from cubicweb.devtools.testlib import CubicWebTC
       
    22 
       
    23 from cubicweb.ext.rest import rest_publish
       
    24 
       
    25 class RestTC(CubicWebTC):
       
    26 
       
    27     def context(self, req):
       
    28         return req.execute('CWUser X WHERE X login "admin"').get_entity(0, 0)
       
    29 
       
    30     def test_eid_role(self):
       
    31         with self.admin_access.web_request() as req:
       
    32             context = self.context(req)
       
    33             self.assertEqual(rest_publish(context, ':eid:`%s`' % context.eid),
       
    34                              '<p><a class="reference" href="http://testing.fr/cubicweb/cwuser/admin">'
       
    35                              '#%s</a></p>\n' % context.eid)
       
    36             self.assertEqual(rest_publish(context, ':eid:`%s:some text`' % context.eid),
       
    37                              '<p><a class="reference" href="http://testing.fr/cubicweb/cwuser/admin">'
       
    38                              'some text</a></p>\n')
       
    39 
       
    40     def test_bad_rest_no_crash(self):
       
    41         with self.admin_access.web_request() as req:
       
    42             rest_publish(self.context(req), '''
       
    43 | card | implication     |
       
    44 --------------------------
       
    45 | 1-1  | N1 = N2         |
       
    46 | 1-?  | N1 <= N2        |
       
    47 | 1-+  | N1 >= N2        |
       
    48 | 1-*  | N1>0 => N2>0    |
       
    49 --------------------------
       
    50 | ?-?  | N1 # N2         |
       
    51 | ?-+  | N1 >= N2        |
       
    52 | ?-*  | N1 #  N2        |
       
    53 --------------------------
       
    54 | +-+  | N1>0 => N2>0 et |
       
    55 |      | N2>0 => N1>0    |
       
    56 | +-*  | N1>+ => N2>0    |
       
    57 --------------------------
       
    58 | *-*  | N1#N2           |
       
    59 --------------------------
       
    60 
       
    61 ''')
       
    62 
       
    63     def test_disable_field_name_colspan(self):
       
    64         with self.admin_access.web_request() as req:
       
    65             context = self.context(req)
       
    66             value = rest_publish(context, '''my field list:
       
    67 
       
    68 :a long dumb param name: value
       
    69 ''')
       
    70             self.assertNotIn('colspan', value)
       
    71 
       
    72     def test_rql_role_with_vid(self):
       
    73         with self.admin_access.web_request() as req:
       
    74             context = self.context(req)
       
    75             out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:table`')
       
    76             self.assertTrue(out.endswith('<a href="http://testing.fr/cubicweb/cwuser/anon" '
       
    77                                          'title="">anon</a></td></tr>\n</tbody></table>'
       
    78                                          '</div></p>\n'))
       
    79 
       
    80     def test_rql_role_with_vid_empty_rset(self):
       
    81         with self.admin_access.web_request() as req:
       
    82             context = self.context(req)
       
    83             out = rest_publish(context, ':rql:`Any X WHERE X is CWUser, X login "nono":table`')
       
    84             self.assertTrue(out.endswith('<p><div class="searchMessage"><strong>'
       
    85                                          'No result matching query</strong></div>\n</p>\n'))
       
    86 
       
    87     def test_rql_role_with_unknown_vid(self):
       
    88         with self.admin_access.web_request() as req:
       
    89             context = self.context(req)
       
    90             out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:toto`')
       
    91             self.assertTrue(out.startswith("<p>an error occurred while interpreting this "
       
    92                                            "rql directive: ObjectNotFound(%s'toto',)</p>" %
       
    93                                            ('' if PY3 else 'u')),
       
    94                             out)
       
    95 
       
    96     def test_rql_role_without_vid(self):
       
    97         with self.admin_access.web_request() as req:
       
    98             context = self.context(req)
       
    99             out = rest_publish(context, ':rql:`Any X WHERE X is CWUser`')
       
   100             self.assertEqual(out, u'<p><h1>CWUser_plural</h1><div class="section">'
       
   101                              '<a href="http://testing.fr/cubicweb/cwuser/admin" title="">admin</a>'
       
   102                              '</div><div class="section">'
       
   103                              '<a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a>'
       
   104                              '</div></p>\n')
       
   105 
       
   106     def test_bookmark_role(self):
       
   107         with self.admin_access.web_request() as req:
       
   108             context = self.context(req)
       
   109             rset = req.execute('INSERT Bookmark X: X title "hello", X path '
       
   110                                '"/view?rql=Any X WHERE X is CWUser"')
       
   111             eid = rset[0][0]
       
   112             out = rest_publish(context, ':bookmark:`%s`' % eid)
       
   113             self.assertEqual(out, u'<p><h1>CWUser_plural</h1><div class="section">'
       
   114                              '<a href="http://testing.fr/cubicweb/cwuser/admin" title="">admin'
       
   115                              '</a></div><div class="section">'
       
   116                              '<a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon'
       
   117                              '</a></div></p>\n')
       
   118 
       
   119     def test_rqltable_nocontent(self):
       
   120         with self.admin_access.web_request() as req:
       
   121             context = self.context(req)
       
   122             out = rest_publish(context, """.. rql-table::""")
       
   123             self.assertIn("System Message: ERROR", out)
       
   124             self.assertIn("Content block expected for the &quot;rql-table&quot; "
       
   125                           "directive; none found" , out)
       
   126 
       
   127     def test_rqltable_norset(self):
       
   128         with self.admin_access.web_request() as req:
       
   129             context = self.context(req)
       
   130             rql = "Any X WHERE X is CWUser, X firstname 'franky'"
       
   131             out = rest_publish(
       
   132                 context, """\
       
   133 .. rql-table::
       
   134 
       
   135                 %(rql)s""" % {'rql': rql})
       
   136             self.assertIn("System Message: WARNING", out)
       
   137             self.assertIn("empty result set", out)
       
   138 
       
   139     def test_rqltable_nooptions(self):
       
   140         with self.admin_access.web_request() as req:
       
   141             rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   142             out = rest_publish(
       
   143                 self.context(req), """\
       
   144 .. rql-table::
       
   145 
       
   146    %(rql)s
       
   147                 """ % {'rql': rql})
       
   148             view = self.vreg['views'].select('table', req, rset=req.execute(rql))
       
   149             self.assertEqual(view.render(w=None)[49:], out[49:])
       
   150 
       
   151     def test_rqltable_vid(self):
       
   152         with self.admin_access.web_request() as req:
       
   153             rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   154             vid = 'mytable'
       
   155             out = rest_publish(
       
   156                 self.context(req), """\
       
   157 .. rql-table::
       
   158    :vid: %(vid)s
       
   159 
       
   160    %(rql)s
       
   161                 """ % {'rql': rql, 'vid': vid})
       
   162             view = self.vreg['views'].select(vid, req, rset=req.execute(rql))
       
   163             self.assertEqual(view.render(w=None)[49:], out[49:])
       
   164             self.assertIn(vid, out[:49])
       
   165 
       
   166     def test_rqltable_badvid(self):
       
   167         with self.admin_access.web_request() as req:
       
   168             rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   169             vid = 'mytabel'
       
   170             out = rest_publish(
       
   171                 self.context(req), """\
       
   172 .. rql-table::
       
   173    :vid: %(vid)s
       
   174 
       
   175    %(rql)s
       
   176                 """ % {'rql': rql, 'vid': vid})
       
   177             self.assertIn("fail to select '%s' view" % vid, out)
       
   178 
       
   179     def test_rqltable_headers(self):
       
   180         with self.admin_access.web_request() as req:
       
   181             rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   182             headers = ["nom", "prenom", "identifiant"]
       
   183             out = rest_publish(
       
   184                 self.context(req), """\
       
   185 .. rql-table::
       
   186    :headers: %(headers)s
       
   187 
       
   188    %(rql)s
       
   189                 """ % {'rql': rql, 'headers': ', '.join(headers)})
       
   190             view = self.vreg['views'].select('table', req, rset=req.execute(rql))
       
   191             view.headers = headers
       
   192             self.assertEqual(view.render(w=None)[49:], out[49:])
       
   193 
       
   194     def test_rqltable_headers_missing(self):
       
   195         with self.admin_access.web_request() as req:
       
   196             rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   197             headers = ["nom", "", "identifiant"]
       
   198             out = rest_publish(
       
   199                 self.context(req), """\
       
   200 .. rql-table::
       
   201    :headers: %(headers)s
       
   202 
       
   203    %(rql)s
       
   204                 """ % {'rql': rql, 'headers': ', '.join(headers)})
       
   205             view = self.vreg['views'].select('table', req, rset=req.execute(rql))
       
   206             view.headers = [headers[0], None, headers[2]]
       
   207             self.assertEqual(view.render(w=None)[49:], out[49:])
       
   208 
       
   209     def test_rqltable_headers_missing_edges(self):
       
   210         with self.admin_access.web_request() as req:
       
   211             rql = "Any S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   212             headers = [" ", "prenom", ""]
       
   213             out = rest_publish(
       
   214                 self.context(req), """\
       
   215 .. rql-table::
       
   216    :headers: %(headers)s
       
   217 
       
   218    %(rql)s
       
   219                 """ % {'rql': rql, 'headers': ', '.join(headers)})
       
   220             view = self.vreg['views'].select('table', req, rset=req.execute(rql))
       
   221             view.headers = [None, headers[1], None]
       
   222             self.assertEqual(view.render(w=None)[49:], out[49:])
       
   223 
       
   224     def test_rqltable_colvids(self):
       
   225         with self.admin_access.web_request() as req:
       
   226             rql = "Any X,S,F,L WHERE X is CWUser, X surname S, X firstname F, X login L"
       
   227             colvids = {0: "oneline"}
       
   228             out = rest_publish(
       
   229                 self.context(req), """\
       
   230 .. rql-table::
       
   231    :colvids: %(colvids)s
       
   232 
       
   233    %(rql)s
       
   234                 """ % {'rql': rql,
       
   235                        'colvids': ', '.join(["%d=%s" % (k, v)
       
   236                                              for k, v in colvids.items()])
       
   237                    })
       
   238             view = self.vreg['views'].select('table', req, rset=req.execute(rql))
       
   239             view.cellvids = colvids
       
   240             self.assertEqual(view.render(w=None)[49:], out[49:])
       
   241 
       
   242 
       
   243 if __name__ == '__main__':
       
   244     unittest_main()