author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 15 Oct 2009 20:31:55 +0200 | |
branch | stable |
changeset 3694 | 33dbb1da1db9 |
parent 1977 | 606923dff11b |
child 2773 | b2530e3e0afb |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
2 |
"""Unit tests for url publishing service |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1654
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
import re |
|
11 |
||
12 |
from logilab.common.testlib import unittest_main |
|
13 |
||
14 |
from cubicweb.devtools.apptest import EnvBasedTC |
|
15 |
from cubicweb.devtools._apptest import FakeRequest |
|
16 |
||
17 |
from cubicweb.rset import ResultSet |
|
18 |
from cubicweb.web import NotFound, Redirect |
|
19 |
from cubicweb.web.views.urlrewrite import SimpleReqRewriter |
|
20 |
||
21 |
||
22 |
class URLPublisherTC(EnvBasedTC): |
|
23 |
"""test suite for QSPreProcessor""" |
|
24 |
||
25 |
def setup_database(self): |
|
26 |
self.create_user(u'ÿsaÿe') |
|
27 |
b = self.add_entity('BlogEntry', title=u'hell\'o', content=u'blabla') |
|
28 |
c = self.add_entity('Tag', name=u'yo') # take care: Tag's name normalized to lower case |
|
29 |
self.execute('SET C tags B WHERE C eid %(c)s, B eid %(b)s', {'c':c.eid, 'b':b.eid}, 'b') |
|
1654 | 30 |
|
0 | 31 |
def process(self, url): |
32 |
req = self.req = self.request() |
|
33 |
return self.env.app.url_resolver.process(req, url) |
|
1654 | 34 |
|
0 | 35 |
def test_raw_path(self): |
36 |
"""tests raw path resolution'""" |
|
37 |
self.assertEquals(self.process('view'), ('view', None)) |
|
38 |
self.assertEquals(self.process('edit'), ('edit', None)) |
|
39 |
self.assertRaises(NotFound, self.process, 'whatever') |
|
40 |
||
41 |
def test_eid_path(self): |
|
42 |
"""tests eid path resolution""" |
|
43 |
self.assertIsInstance(self.process('123')[1], ResultSet) |
|
44 |
self.assertEquals(len(self.process('123')[1]), 1) |
|
45 |
self.assertRaises(NotFound, self.process, '123/345') |
|
46 |
self.assertRaises(NotFound, self.process, 'not_eid') |
|
47 |
||
48 |
def test_rest_path(self): |
|
49 |
"""tests the rest path resolution""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
50 |
ctrl, rset = self.process('CWUser') |
0 | 51 |
self.assertEquals(ctrl, 'view') |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
52 |
self.assertEquals(rset.description[0][0], 'CWUser') |
0 | 53 |
self.assertEquals(rset.printable_rql(), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
54 |
"Any X,AA,AB,AC,AD ORDERBY AA WHERE X is CWUser, X login AA, X firstname AB, X surname AC, X modification_date AD") |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
55 |
ctrl, rset = self.process('CWUser/login/admin') |
0 | 56 |
self.assertEquals(ctrl, 'view') |
57 |
self.assertEquals(len(rset), 1) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
58 |
self.assertEquals(rset.description[0][0], 'CWUser') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
59 |
self.assertEquals(rset.printable_rql(), 'Any X WHERE X is CWUser, X login "admin"') |
1654 | 60 |
ctrl, rset = self.process('cwuser/admin') |
0 | 61 |
self.assertEquals(ctrl, 'view') |
62 |
self.assertEquals(len(rset), 1) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
63 |
self.assertEquals(rset.description[0][0], 'CWUser') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
64 |
self.assertEquals(rset.printable_rql(), 'Any X WHERE X is CWUser, X login "admin"') |
1654 | 65 |
ctrl, rset = self.process('cwuser/eid/%s'%rset[0][0]) |
0 | 66 |
self.assertEquals(ctrl, 'view') |
67 |
self.assertEquals(len(rset), 1) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
68 |
self.assertEquals(rset.description[0][0], 'CWUser') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
69 |
self.assertEquals(rset.printable_rql(), 'Any X WHERE X is CWUser, X eid 5') |
0 | 70 |
# test non-ascii paths |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
71 |
ctrl, rset = self.process('CWUser/login/%C3%BFsa%C3%BFe') |
0 | 72 |
self.assertEquals(ctrl, 'view') |
73 |
self.assertEquals(len(rset), 1) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
74 |
self.assertEquals(rset.description[0][0], 'CWUser') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
75 |
self.assertEquals(rset.printable_rql(), u'Any X WHERE X is CWUser, X login "ÿsaÿe"') |
0 | 76 |
# test quoted paths |
77 |
ctrl, rset = self.process('BlogEntry/title/hell%27o') |
|
78 |
self.assertEquals(ctrl, 'view') |
|
79 |
self.assertEquals(len(rset), 1) |
|
80 |
self.assertEquals(rset.description[0][0], 'BlogEntry') |
|
81 |
self.assertEquals(rset.printable_rql(), u'Any X WHERE X is BlogEntry, X title "hell\'o"') |
|
82 |
# errors |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
83 |
self.assertRaises(NotFound, self.process, 'CWUser/eid/30000') |
0 | 84 |
self.assertRaises(NotFound, self.process, 'Workcases') |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
85 |
self.assertRaises(NotFound, self.process, 'CWUser/inexistant_attribute/joe') |
1654 | 86 |
|
0 | 87 |
def test_action_path(self): |
88 |
"""tests the action path resolution""" |
|
89 |
self.assertRaises(Redirect, self.process, '1/edit') |
|
90 |
self.assertRaises(Redirect, self.process, 'Tag/name/yo/edit') |
|
91 |
self.assertRaises(Redirect, self.process, 'Tag/yo/edit') |
|
92 |
self.assertRaises(NotFound, self.process, 'view/edit') |
|
93 |
self.assertRaises(NotFound, self.process, '1/non_action') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
94 |
self.assertRaises(NotFound, self.process, 'CWUser/login/admin/non_action') |
0 | 95 |
|
96 |
||
97 |
def test_regexp_path(self): |
|
98 |
"""tests the regexp path resolution""" |
|
99 |
ctrl, rset = self.process('add/Task') |
|
100 |
self.assertEquals(ctrl, 'view') |
|
1654 | 101 |
self.assertEquals(rset, None) |
0 | 102 |
self.assertEquals(self.req.form, {'etype' : "Task", 'vid' : "creation"}) |
103 |
self.assertRaises(NotFound, self.process, 'add/foo/bar') |
|
104 |
||
105 |
||
106 |
def test_nonascii_path(self): |
|
107 |
oldrules = SimpleReqRewriter.rules |
|
108 |
SimpleReqRewriter.rules = [(re.compile('/\w+', re.U), dict(vid='foo')),] |
|
109 |
try: |
|
110 |
path = str(FakeRequest().url_quote(u'été')) |
|
111 |
ctrl, rset = self.process(path) |
|
1654 | 112 |
self.assertEquals(rset, None) |
0 | 113 |
self.assertEquals(self.req.form, {'vid' : "foo"}) |
114 |
finally: |
|
115 |
SimpleReqRewriter.rules = oldrules |
|
1654 | 116 |
|
0 | 117 |
|
118 |
if __name__ == '__main__': |
|
119 |
unittest_main() |