author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 17 Aug 2009 18:37:16 +0200 | |
changeset 2883 | 57ff19806026 |
parent 2783 | 493f527aed1e |
child 3185 | bd0126d17e83 |
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 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2439
diff
changeset
|
10 |
from cubicweb.devtools.testlib import CubicWebTC |
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2439
diff
changeset
|
11 |
from cubicweb.devtools.fake import FakeRequest |
0 | 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')), |
|
2780
ad1dfc3855b0
B web/tests back to green
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2439
diff
changeset
|
32 |
('/_', dict(vid='manage')), |
ad1dfc3855b0
B web/tests back to green
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2439
diff
changeset
|
33 |
('/_registry', dict(vid='registry')), |
2439
77d8dd77acb3
[cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
34 |
('/schema', dict(vid='schema')), |
1900
8dd4bb69c73d
[tests] fix test after eproperties got renamed to cwproperties
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1802
diff
changeset
|
35 |
('/myprefs', dict(vid='propertiesform')), |
8dd4bb69c73d
[tests] fix test after eproperties got renamed to cwproperties
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1802
diff
changeset
|
36 |
('/siteconfig', dict(vid='systempropertiesform')), |
2439
77d8dd77acb3
[cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
37 |
('/siteinfo', dict(vid='info')), |
0 | 38 |
('/manage', dict(vid='manage')), |
2439
77d8dd77acb3
[cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
39 |
('/notfound', dict(vid='404')), |
77d8dd77acb3
[cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
40 |
('/error', dict(vid='error')), |
77d8dd77acb3
[cleanup] fix tests
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
41 |
('/sparql', dict(vid='sparql')), |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
42 |
('/schema/([^/]+?)/?$', {'rql': r'Any X WHERE X is CWEType, X name "\1"', 'vid': 'eschema'}), |
0 | 43 |
('/add/([^/]+?)/?$' , dict(vid='creation', etype=r'\1')), |
44 |
('/doc/images/(.+?)/?$', dict(fid='\\1', vid='wdocimages')), |
|
45 |
('/doc/?$', dict(fid='main', vid='wdoc')), |
|
46 |
('/doc/(.+?)/?$', dict(fid='\\1', vid='wdoc')), |
|
47 |
('/changelog/?$', dict(vid='changelog')), |
|
48 |
# now in SchemaBasedRewriter |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
49 |
#('/search/(.+)$', dict(rql=r'Any X WHERE X has_text "\1"')), |
0 | 50 |
]) |
51 |
||
52 |
||
53 |
def test_no_extend_rules(self): |
|
54 |
class Rewriter(SimpleReqRewriter): |
|
55 |
ignore_baseclass_rules = True |
|
56 |
rules = [ |
|
57 |
('foo', dict(rql='Foo F')), |
|
58 |
('/index', dict(vid='index2')), |
|
59 |
] |
|
60 |
self.assertListEquals(Rewriter.rules, [ |
|
61 |
('foo' , dict(rql='Foo F')), |
|
62 |
('/index' , dict(vid='index2')), |
|
63 |
]) |
|
64 |
||
65 |
def test_basic_transformation(self): |
|
66 |
"""test simple string-based rewrite""" |
|
67 |
rewriter = SimpleReqRewriter() |
|
68 |
req = FakeRequest() |
|
69 |
self.assertRaises(KeyError, rewriter.rewrite, req, '/view?vid=whatever') |
|
70 |
self.assertEquals(req.form, {}) |
|
71 |
rewriter.rewrite(req, '/index') |
|
72 |
self.assertEquals(req.form, {'vid' : "index"}) |
|
73 |
||
74 |
def test_regexp_transformation(self): |
|
75 |
"""test regexp-based rewrite""" |
|
76 |
rewriter = SimpleReqRewriter() |
|
77 |
req = FakeRequest() |
|
78 |
rewriter.rewrite(req, '/add/Task') |
|
79 |
self.assertEquals(req.form, {'vid' : "creation", 'etype' : "Task"}) |
|
80 |
req = FakeRequest() |
|
81 |
rewriter.rewrite(req, '/add/Task/') |
|
82 |
self.assertEquals(req.form, {'vid' : "creation", 'etype' : "Task"}) |
|
83 |
||
84 |
||
85 |
||
86 |
||
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2439
diff
changeset
|
87 |
class RgxActionRewriteTC(CubicWebTC): |
0 | 88 |
|
89 |
def setup_database(self): |
|
90 |
self.p1 = self.create_user(u'user1') |
|
91 |
self.p1.set_attributes(firstname=u'joe', surname=u'Dalton') |
|
92 |
self.p2 = self.create_user(u'user2') |
|
93 |
self.p2.set_attributes(firstname=u'jack', surname=u'Dalton') |
|
94 |
||
95 |
def test_rgx_action_with_transforms(self): |
|
96 |
class TestSchemaBasedRewriter(SchemaBasedRewriter): |
|
97 |
rules = [ |
|
98 |
(rgx('/(?P<sn>\w+)/(?P<fn>\w+)'), rgx_action(r'Any X WHERE X surname %(sn)s, X firstname %(fn)s', |
|
99 |
argsgroups=('sn', 'fn'), |
|
100 |
transforms={'sn' : unicode.capitalize, |
|
101 |
'fn' : unicode.lower,})), |
|
102 |
] |
|
103 |
rewriter = TestSchemaBasedRewriter() |
|
104 |
req = self.request() |
|
105 |
pmid, rset = rewriter.rewrite(req, u'/DaLToN/JoE') |
|
106 |
self.assertEquals(len(rset), 1) |
|
107 |
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
|
108 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
109 |
|
0 | 110 |
|
111 |
if __name__ == '__main__': |
|
112 |
unittest_main() |