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