author | sylvain.thenault@logilab.fr |
Wed, 29 Apr 2009 10:46:08 +0200 | |
branch | tls-sprint |
changeset 1534 | 7edc063e8a91 |
parent 1398 | 5fe84a5f7035 |
child 1654 | 57b9fd2462b8 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
"""Unit tests for url publishing service""" |
|
3 |
||
4 |
import re |
|
5 |
||
6 |
from logilab.common.testlib import unittest_main |
|
7 |
||
8 |
from cubicweb.devtools.apptest import EnvBasedTC |
|
9 |
from cubicweb.devtools._apptest import FakeRequest |
|
10 |
||
11 |
from cubicweb.rset import ResultSet |
|
12 |
from cubicweb.web import NotFound, Redirect |
|
13 |
from cubicweb.web.views.urlrewrite import SimpleReqRewriter |
|
14 |
||
15 |
||
16 |
class URLPublisherTC(EnvBasedTC): |
|
17 |
"""test suite for QSPreProcessor""" |
|
18 |
||
19 |
def setup_database(self): |
|
20 |
self.create_user(u'ÿsaÿe') |
|
21 |
b = self.add_entity('BlogEntry', title=u'hell\'o', content=u'blabla') |
|
22 |
c = self.add_entity('Tag', name=u'yo') # take care: Tag's name normalized to lower case |
|
23 |
self.execute('SET C tags B WHERE C eid %(c)s, B eid %(b)s', {'c':c.eid, 'b':b.eid}, 'b') |
|
24 |
||
25 |
def process(self, url): |
|
26 |
req = self.req = self.request() |
|
27 |
return self.env.app.url_resolver.process(req, url) |
|
28 |
||
29 |
def test_raw_path(self): |
|
30 |
"""tests raw path resolution'""" |
|
31 |
self.assertEquals(self.process('view'), ('view', None)) |
|
32 |
self.assertEquals(self.process('edit'), ('edit', None)) |
|
33 |
self.assertRaises(NotFound, self.process, 'whatever') |
|
34 |
||
35 |
def test_eid_path(self): |
|
36 |
"""tests eid path resolution""" |
|
37 |
self.assertIsInstance(self.process('123')[1], ResultSet) |
|
38 |
self.assertEquals(len(self.process('123')[1]), 1) |
|
39 |
self.assertRaises(NotFound, self.process, '123/345') |
|
40 |
self.assertRaises(NotFound, self.process, 'not_eid') |
|
41 |
||
42 |
def test_rest_path(self): |
|
43 |
"""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
|
44 |
ctrl, rset = self.process('CWUser') |
0 | 45 |
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
|
46 |
self.assertEquals(rset.description[0][0], 'CWUser') |
0 | 47 |
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
|
48 |
"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
|
49 |
ctrl, rset = self.process('CWUser/login/admin') |
0 | 50 |
self.assertEquals(ctrl, 'view') |
51 |
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
|
52 |
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
|
53 |
self.assertEquals(rset.printable_rql(), 'Any X WHERE X is CWUser, X login "admin"') |
0 | 54 |
ctrl, rset = self.process('euser/admin') |
55 |
self.assertEquals(ctrl, 'view') |
|
56 |
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
|
57 |
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
|
58 |
self.assertEquals(rset.printable_rql(), 'Any X WHERE X is CWUser, X login "admin"') |
0 | 59 |
ctrl, rset = self.process('euser/eid/%s'%rset[0][0]) |
60 |
self.assertEquals(ctrl, 'view') |
|
61 |
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
|
62 |
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
|
63 |
self.assertEquals(rset.printable_rql(), 'Any X WHERE X is CWUser, X eid 5') |
0 | 64 |
# 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
|
65 |
ctrl, rset = self.process('CWUser/login/%C3%BFsa%C3%BFe') |
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(), u'Any X WHERE X is CWUser, X login "ÿsaÿe"') |
0 | 70 |
# test quoted paths |
71 |
ctrl, rset = self.process('BlogEntry/title/hell%27o') |
|
72 |
self.assertEquals(ctrl, 'view') |
|
73 |
self.assertEquals(len(rset), 1) |
|
74 |
self.assertEquals(rset.description[0][0], 'BlogEntry') |
|
75 |
self.assertEquals(rset.printable_rql(), u'Any X WHERE X is BlogEntry, X title "hell\'o"') |
|
76 |
# errors |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
77 |
self.assertRaises(NotFound, self.process, 'CWUser/eid/30000') |
0 | 78 |
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
|
79 |
self.assertRaises(NotFound, self.process, 'CWUser/inexistant_attribute/joe') |
0 | 80 |
|
81 |
def test_action_path(self): |
|
82 |
"""tests the action path resolution""" |
|
83 |
self.assertRaises(Redirect, self.process, '1/edit') |
|
84 |
self.assertRaises(Redirect, self.process, 'Tag/name/yo/edit') |
|
85 |
self.assertRaises(Redirect, self.process, 'Tag/yo/edit') |
|
86 |
self.assertRaises(NotFound, self.process, 'view/edit') |
|
87 |
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
|
88 |
self.assertRaises(NotFound, self.process, 'CWUser/login/admin/non_action') |
0 | 89 |
|
90 |
||
91 |
def test_regexp_path(self): |
|
92 |
"""tests the regexp path resolution""" |
|
93 |
ctrl, rset = self.process('add/Task') |
|
94 |
self.assertEquals(ctrl, 'view') |
|
95 |
self.assertEquals(rset, None) |
|
96 |
self.assertEquals(self.req.form, {'etype' : "Task", 'vid' : "creation"}) |
|
97 |
self.assertRaises(NotFound, self.process, 'add/foo/bar') |
|
98 |
||
99 |
||
100 |
def test_nonascii_path(self): |
|
101 |
oldrules = SimpleReqRewriter.rules |
|
102 |
SimpleReqRewriter.rules = [(re.compile('/\w+', re.U), dict(vid='foo')),] |
|
103 |
try: |
|
104 |
path = str(FakeRequest().url_quote(u'été')) |
|
105 |
ctrl, rset = self.process(path) |
|
106 |
self.assertEquals(rset, None) |
|
107 |
self.assertEquals(self.req.form, {'vid' : "foo"}) |
|
108 |
finally: |
|
109 |
SimpleReqRewriter.rules = oldrules |
|
110 |
||
111 |
||
112 |
if __name__ == '__main__': |
|
113 |
unittest_main() |