author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Thu, 28 May 2009 22:57:52 +0200 | |
changeset 1998 | 12040c090aa4 |
parent 1977 | 606923dff11b |
child 2439 | 77d8dd77acb3 |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1900
diff
changeset
|
1 |
""" |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1900
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1900
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1900
diff
changeset
|
4 |
: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:
1900
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1900
diff
changeset
|
6 |
: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:
1900
diff
changeset
|
7 |
""" |
0 | 8 |
from logilab.common.testlib import TestCase, unittest_main |
9 |
||
10 |
from cubicweb.devtools._apptest import FakeRequest |
|
11 |
from cubicweb.devtools.apptest import EnvBasedTC |
|
12 |
||
13 |
from cubicweb.web.views.urlrewrite import SimpleReqRewriter, SchemaBasedRewriter, rgx, rgx_action |
|
14 |
||
15 |
||
16 |
class UrlRewriteTC(TestCase): |
|
17 |
||
18 |
def test_auto_extend_rules(self): |
|
19 |
class Rewriter(SimpleReqRewriter): |
|
20 |
rules = [ |
|
21 |
('foo', dict(rql='Foo F')), |
|
22 |
('/index', dict(vid='index2')), |
|
23 |
] |
|
24 |
rules = [] |
|
25 |
for pattern, values in Rewriter.rules: |
|
26 |
if hasattr(pattern, 'pattern'): |
|
27 |
pattern = pattern.pattern |
|
28 |
rules.append((pattern, values)) |
|
29 |
self.assertListEquals(rules, [ |
|
30 |
('foo' , dict(rql='Foo F')), |
|
31 |
('/index' , dict(vid='index2')), |
|
32 |
('/schema', {'vid': 'schema'}), |
|
1900
8dd4bb69c73d
[tests] fix test after eproperties got renamed to cwproperties
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1802
diff
changeset
|
33 |
('/myprefs', dict(vid='propertiesform')), |
8dd4bb69c73d
[tests] fix test after eproperties got renamed to cwproperties
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1802
diff
changeset
|
34 |
('/siteconfig', dict(vid='systempropertiesform')), |
0 | 35 |
('/manage', dict(vid='manage')), |
36 |
('/notfound', {'vid': '404'}), |
|
37 |
('/error', {'vid': 'error'}), |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
38 |
('/schema/([^/]+?)/?$', {'rql': r'Any X WHERE X is CWEType, X name "\1"', 'vid': 'eschema'}), |
0 | 39 |
('/add/([^/]+?)/?$' , dict(vid='creation', etype=r'\1')), |
40 |
('/doc/images/(.+?)/?$', dict(fid='\\1', vid='wdocimages')), |
|
41 |
('/doc/?$', dict(fid='main', vid='wdoc')), |
|
42 |
('/doc/(.+?)/?$', dict(fid='\\1', vid='wdoc')), |
|
43 |
('/changelog/?$', dict(vid='changelog')), |
|
44 |
# now in SchemaBasedRewriter |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
45 |
#('/search/(.+)$', dict(rql=r'Any X WHERE X has_text "\1"')), |
0 | 46 |
]) |
47 |
||
48 |
||
49 |
def test_no_extend_rules(self): |
|
50 |
class Rewriter(SimpleReqRewriter): |
|
51 |
ignore_baseclass_rules = True |
|
52 |
rules = [ |
|
53 |
('foo', dict(rql='Foo F')), |
|
54 |
('/index', dict(vid='index2')), |
|
55 |
] |
|
56 |
self.assertListEquals(Rewriter.rules, [ |
|
57 |
('foo' , dict(rql='Foo F')), |
|
58 |
('/index' , dict(vid='index2')), |
|
59 |
]) |
|
60 |
||
61 |
def test_basic_transformation(self): |
|
62 |
"""test simple string-based rewrite""" |
|
63 |
rewriter = SimpleReqRewriter() |
|
64 |
req = FakeRequest() |
|
65 |
self.assertRaises(KeyError, rewriter.rewrite, req, '/view?vid=whatever') |
|
66 |
self.assertEquals(req.form, {}) |
|
67 |
rewriter.rewrite(req, '/index') |
|
68 |
self.assertEquals(req.form, {'vid' : "index"}) |
|
69 |
||
70 |
def test_regexp_transformation(self): |
|
71 |
"""test regexp-based rewrite""" |
|
72 |
rewriter = SimpleReqRewriter() |
|
73 |
req = FakeRequest() |
|
74 |
rewriter.rewrite(req, '/add/Task') |
|
75 |
self.assertEquals(req.form, {'vid' : "creation", 'etype' : "Task"}) |
|
76 |
req = FakeRequest() |
|
77 |
rewriter.rewrite(req, '/add/Task/') |
|
78 |
self.assertEquals(req.form, {'vid' : "creation", 'etype' : "Task"}) |
|
79 |
||
80 |
||
81 |
||
82 |
||
83 |
class RgxActionRewriteTC(EnvBasedTC): |
|
84 |
||
85 |
def setup_database(self): |
|
86 |
self.p1 = self.create_user(u'user1') |
|
87 |
self.p1.set_attributes(firstname=u'joe', surname=u'Dalton') |
|
88 |
self.p2 = self.create_user(u'user2') |
|
89 |
self.p2.set_attributes(firstname=u'jack', surname=u'Dalton') |
|
90 |
||
91 |
def test_rgx_action_with_transforms(self): |
|
92 |
class TestSchemaBasedRewriter(SchemaBasedRewriter): |
|
93 |
rules = [ |
|
94 |
(rgx('/(?P<sn>\w+)/(?P<fn>\w+)'), rgx_action(r'Any X WHERE X surname %(sn)s, X firstname %(fn)s', |
|
95 |
argsgroups=('sn', 'fn'), |
|
96 |
transforms={'sn' : unicode.capitalize, |
|
97 |
'fn' : unicode.lower,})), |
|
98 |
] |
|
99 |
rewriter = TestSchemaBasedRewriter() |
|
100 |
req = self.request() |
|
101 |
pmid, rset = rewriter.rewrite(req, u'/DaLToN/JoE') |
|
102 |
self.assertEquals(len(rset), 1) |
|
103 |
self.assertEquals(rset[0][0], self.p1.eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
104 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
105 |
|
0 | 106 |
|
107 |
if __name__ == '__main__': |
|
108 |
unittest_main() |