ext/test/unittest_rest.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 14 Oct 2011 13:51:54 +0200
changeset 7958 646d44232b5a
parent 7732 5430d0db52ab
child 8255 25a00e4a8722
permissions -rw-r--r--
oops
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1643
diff changeset
    18
"""
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1643
diff changeset
    19
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1643
diff changeset
    20
"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
from logilab.common.testlib import unittest_main
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    22
from cubicweb.devtools.testlib import CubicWebTC
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    24
from cubicweb.ext.rest import rest_publish
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    25
2773
b2530e3e0afb [testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    26
class RestTC(CubicWebTC):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
    def context(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    28
        return self.execute('CWUser X WHERE X login "admin"').get_entity(0, 0)
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    29
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    def test_eid_role(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
        context = self.context()
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    32
        self.assertEqual(rest_publish(context, ':eid:`%s`' % context.eid),
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    33
                          '<p><a class="reference" href="http://testing.fr/cubicweb/cwuser/admin">#%s</a></p>\n' % context.eid)
6340
470d8e828fda [test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    34
        self.assertEqual(rest_publish(context, ':eid:`%s:some text`' %  context.eid),
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    35
                          '<p><a class="reference" href="http://testing.fr/cubicweb/cwuser/admin">some text</a></p>\n')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    def test_bad_rest_no_crash(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        data = rest_publish(self.context(), '''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
| card | implication     |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
| 1-1  | N1 = N2         |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
| 1-?  | N1 <= N2        |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
| 1-+  | N1 >= N2        |
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    44
| 1-*  | N1>0 => N2>0    |
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
| ?-?  | N1 # N2         |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
| ?-+  | N1 >= N2        |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
| ?-*  | N1 #  N2        |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
| +-+  | N1>0 => N2>0 et |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
|      | N2>0 => N1>0    |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
| +-*  | N1>+ => N2>0    |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
| *-*  | N1#N2           |
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
--------------------------
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
''')
1643
59c5cec05bbd move card role to cubes.card
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    58
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    59
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    60
    def test_rql_role_with_vid(self):
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    61
        context = self.context()
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    62
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:table`')
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    63
        self.assert_(out.endswith('<a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a>'
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    64
                                  '</td></tr></tbody></table></div>\n</div>\n</p>\n'))
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    65
7732
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    66
    def test_rql_role_with_vid_empty_rset(self):
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    67
        context = self.context()
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    68
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser, X login "nono":table`')
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    69
        self.assert_(out.endswith('<p><div class="searchMessage"><strong>No result matching query</strong></div>\n</p>\n'))
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    70
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    71
    def test_rql_role_with_unknown_vid(self):
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    72
        context = self.context()
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    73
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser:toto`')
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    74
        self.assert_(out.startswith("<p>an error occured while interpreting this rql directive: ObjectNotFound(u'toto',)</p>"))
5430d0db52ab [web] rql directive breaks if explicit vid and empty rset (closes #1893433)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6938
diff changeset
    75
6938
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    76
    def test_rql_role_without_vid(self):
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    77
        context = self.context()
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    78
        out = rest_publish(context, ':rql:`Any X WHERE X is CWUser`')
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    79
        self.assertEqual(out, u'<p><h1>cwuser_plural</h1><div class="section"><a href="http://testing.fr/cubicweb/cwuser/admin" title="">admin</a></div><div class="section"><a href="http://testing.fr/cubicweb/cwuser/anon" title="">anon</a></div></p>\n')
6c1a960735f5 add a rql directive to restructuredtext
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6340
diff changeset
    80
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
if __name__ == '__main__':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    unittest_main()