author | Christophe de Vienne <christophe@unlish.com> |
Tue, 17 Jun 2014 11:14:20 +0200 | |
changeset 9930 | d20c2b262f55 |
parent 9864 | f60a80592224 |
child 10219 | eacb8ea38bf5 |
permissions | -rw-r--r-- |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
1 |
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5390
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6776
eebe86b89285
[tests] use TextTestRunner from unittest, no more need to use SkipAwareTextRunner from lgc.testlib (which doesn't exist anymore in recent releases
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6341
diff
changeset
|
18 |
"""unittests for cw.devtools.testlib module""" |
0 | 19 |
|
20 |
from cStringIO import StringIO |
|
21 |
||
6776
eebe86b89285
[tests] use TextTestRunner from unittest, no more need to use SkipAwareTextRunner from lgc.testlib (which doesn't exist anymore in recent releases
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6341
diff
changeset
|
22 |
from unittest import TextTestRunner |
9212
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
23 |
|
6776
eebe86b89285
[tests] use TextTestRunner from unittest, no more need to use SkipAwareTextRunner from lgc.testlib (which doesn't exist anymore in recent releases
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6341
diff
changeset
|
24 |
from logilab.common.testlib import TestSuite, TestCase, unittest_main |
9212
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
25 |
from logilab.common.registry import yes |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
26 |
|
0 | 27 |
from cubicweb.devtools import htmlparser |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
28 |
from cubicweb.devtools.testlib import CubicWebTC |
4766
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4473
diff
changeset
|
29 |
from cubicweb.pytestconf import clean_repo_test_cls |
0 | 30 |
|
6776
eebe86b89285
[tests] use TextTestRunner from unittest, no more need to use SkipAwareTextRunner from lgc.testlib (which doesn't exist anymore in recent releases
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6341
diff
changeset
|
31 |
|
0 | 32 |
class WebTestTC(TestCase): |
33 |
||
34 |
def setUp(self): |
|
35 |
output = StringIO() |
|
6776
eebe86b89285
[tests] use TextTestRunner from unittest, no more need to use SkipAwareTextRunner from lgc.testlib (which doesn't exist anymore in recent releases
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6341
diff
changeset
|
36 |
self.runner = TextTestRunner(stream=output) |
5390
a40ffdce549d
cleanup debugging trace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5389
diff
changeset
|
37 |
|
0 | 38 |
def test_error_raised(self): |
2773
b2530e3e0afb
[testlib] #345052 and #344207: major test lib refactoring/cleanup + update usage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
39 |
class MyWebTest(CubicWebTC): |
0 | 40 |
|
41 |
def test_error_view(self): |
|
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
42 |
with self.admin_access.web_request() as req: |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
43 |
req.create_entity('Bug', title=u"bt") |
9864
f60a80592224
[webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9857
diff
changeset
|
44 |
self.view('raising', req.execute('Bug B'), template=None, req=req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
45 |
|
0 | 46 |
def test_correct_view(self): |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
47 |
with self.admin_access.web_request() as req: |
9864
f60a80592224
[webtests] finish to give all self.view(....) a req=req parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9857
diff
changeset
|
48 |
self.view('primary', req.execute('CWUser U'), template=None, req=req) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
49 |
|
0 | 50 |
tests = [MyWebTest('test_error_view'), MyWebTest('test_correct_view')] |
51 |
result = self.runner.run(TestSuite(tests)) |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
52 |
self.assertEqual(result.testsRun, 2) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
53 |
self.assertEqual(len(result.errors), 0) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
54 |
self.assertEqual(len(result.failures), 1) |
4766
162b2b127b15
[test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4473
diff
changeset
|
55 |
clean_repo_test_cls(MyWebTest) |
0 | 56 |
|
57 |
||
58 |
HTML_PAGE = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
59 |
<html> |
|
60 |
<head><title>need a title</title></head> |
|
61 |
<body> |
|
62 |
<h1>Hello World !</h1> |
|
63 |
</body> |
|
64 |
</html> |
|
65 |
""" |
|
66 |
||
67 |
HTML_PAGE2 = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
68 |
<html> |
|
69 |
<head><title>need a title</title></head> |
|
70 |
<body> |
|
71 |
<h1>Test</h1> |
|
72 |
<h1>Hello <a href="http://www.google.com">world</a> !</h1> |
|
73 |
<h2>h2 title</h2> |
|
74 |
<h3>h3 title</h3> |
|
75 |
<h2>antoher h2 title</h2> |
|
76 |
<h4>h4 title</h4> |
|
77 |
<p><a href="http://www.logilab.org">Logilab</a> introduces CW !</p> |
|
78 |
</body> |
|
79 |
</html> |
|
80 |
""" |
|
81 |
||
82 |
HTML_PAGE_ERROR = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
83 |
<html> |
|
84 |
<head><title>need a title</title></head> |
|
85 |
<body> |
|
86 |
Logilab</a> introduces CW ! |
|
87 |
</body> |
|
88 |
</html> |
|
89 |
""" |
|
90 |
||
91 |
HTML_NON_STRICT = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
|
92 |
<html> |
|
93 |
<head><title>need a title</title></head> |
|
94 |
<body> |
|
95 |
<h1><a href="something.com">title</h1> |
|
96 |
</body> |
|
97 |
</html> |
|
98 |
""" |
|
99 |
||
100 |
||
101 |
class HTMLPageInfoTC(TestCase): |
|
102 |
"""test cases for PageInfo""" |
|
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
103 |
|
0 | 104 |
def setUp(self): |
8941
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8694
diff
changeset
|
105 |
parser = htmlparser.HTMLValidator() |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8694
diff
changeset
|
106 |
# disable cleanup that would remove doctype |
7b26fe71404f
drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8694
diff
changeset
|
107 |
parser.preprocess_data = lambda data: data |
0 | 108 |
self.page_info = parser.parse_string(HTML_PAGE2) |
109 |
||
110 |
def test_source1(self): |
|
111 |
"""make sure source is stored correctly""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
112 |
self.assertEqual(self.page_info.source, HTML_PAGE2) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
113 |
|
0 | 114 |
def test_source2(self): |
115 |
"""make sure source is stored correctly - raise exception""" |
|
116 |
parser = htmlparser.DTDValidator() |
|
117 |
self.assertRaises(AssertionError, parser.parse_string, HTML_PAGE_ERROR) |
|
118 |
||
119 |
def test_has_title_no_level(self): |
|
120 |
"""tests h? tags information""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
121 |
self.assertEqual(self.page_info.has_title('Test'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
122 |
self.assertEqual(self.page_info.has_title('Test '), False) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
123 |
self.assertEqual(self.page_info.has_title('Tes'), False) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
124 |
self.assertEqual(self.page_info.has_title('Hello world !'), True) |
0 | 125 |
|
126 |
def test_has_title_level(self): |
|
127 |
"""tests h? tags information""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
128 |
self.assertEqual(self.page_info.has_title('Test', level = 1), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
129 |
self.assertEqual(self.page_info.has_title('Test', level = 2), False) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
130 |
self.assertEqual(self.page_info.has_title('Test', level = 3), False) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
131 |
self.assertEqual(self.page_info.has_title('Test', level = 4), False) |
0 | 132 |
self.assertRaises(IndexError, self.page_info.has_title, 'Test', level = 5) |
133 |
||
134 |
def test_has_title_regexp_no_level(self): |
|
135 |
"""tests has_title_regexp() with no particular level specified""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
136 |
self.assertEqual(self.page_info.has_title_regexp('h[23] title'), True) |
0 | 137 |
|
138 |
def test_has_title_regexp_level(self): |
|
139 |
"""tests has_title_regexp() with a particular level specified""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
140 |
self.assertEqual(self.page_info.has_title_regexp('h[23] title', 2), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
141 |
self.assertEqual(self.page_info.has_title_regexp('h[23] title', 3), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
142 |
self.assertEqual(self.page_info.has_title_regexp('h[23] title', 4), False) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
143 |
|
0 | 144 |
def test_appears(self): |
145 |
"""tests PageInfo.appears()""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
146 |
self.assertEqual(self.page_info.appears('CW'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
147 |
self.assertEqual(self.page_info.appears('Logilab'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
148 |
self.assertEqual(self.page_info.appears('Logilab introduces'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
149 |
self.assertEqual(self.page_info.appears('H2 title'), False) |
0 | 150 |
|
151 |
def test_has_link(self): |
|
152 |
"""tests has_link()""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
153 |
self.assertEqual(self.page_info.has_link('Logilab'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
154 |
self.assertEqual(self.page_info.has_link('logilab'), False) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
155 |
self.assertEqual(self.page_info.has_link('Logilab', 'http://www.logilab.org'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
156 |
self.assertEqual(self.page_info.has_link('Logilab', 'http://www.google.com'), False) |
0 | 157 |
|
158 |
def test_has_link_regexp(self): |
|
159 |
"""test has_link_regexp()""" |
|
6340
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
160 |
self.assertEqual(self.page_info.has_link_regexp('L[oi]gilab'), True) |
470d8e828fda
[test] update test to unittest2 api (still using lgc.testlib though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
161 |
self.assertEqual(self.page_info.has_link_regexp('L[ai]gilab'), False) |
0 | 162 |
|
163 |
||
8460
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
164 |
class CWUtilitiesTC(CubicWebTC): |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
165 |
|
8460
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
166 |
def test_temporary_permissions_eschema(self): |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
167 |
eschema = self.schema['CWUser'] |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
168 |
with self.temporary_permissions(CWUser={'read': ()}): |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
169 |
self.assertEqual(eschema.permissions['read'], ()) |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
170 |
self.assertTrue(eschema.permissions['add']) |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
171 |
self.assertTrue(eschema.permissions['read'], ()) |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
172 |
|
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
173 |
def test_temporary_permissions_rdef(self): |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
174 |
rdef = self.schema['CWUser'].rdef('in_group') |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
175 |
with self.temporary_permissions((rdef, {'read': ()})): |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
176 |
self.assertEqual(rdef.permissions['read'], ()) |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
177 |
self.assertTrue(rdef.permissions['add']) |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
178 |
self.assertTrue(rdef.permissions['read'], ()) |
b1f6777fc839
[testlib] introduce temporary_permissions context manager
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6776
diff
changeset
|
179 |
|
9212
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
180 |
def test_temporary_appobjects_registered(self): |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
181 |
|
9212
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
182 |
class AnAppobject(object): |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
183 |
__registries__ = ('hip',) |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
184 |
__regid__ = 'hop' |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
185 |
__select__ = yes() |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
186 |
registered = None |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
187 |
|
9212
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
188 |
@classmethod |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
189 |
def __registered__(cls, reg): |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
190 |
cls.registered = reg |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
191 |
|
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
192 |
with self.temporary_appobjects(AnAppobject): |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
193 |
self.assertEqual(self.vreg['hip'], AnAppobject.registered) |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
194 |
self.assertIn(AnAppobject, self.vreg['hip']['hop']) |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
195 |
self.assertNotIn(AnAppobject, self.vreg['hip']['hop']) |
0d346a0a1451
[testlib] temporary_appobjects should call __registered__ if it exists. Closes #3064075
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8941
diff
changeset
|
196 |
|
9472
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
197 |
def test_login(self): |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
198 |
"""Calling login should not break hook control""" |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
199 |
with self.admin_access.repo_cnx() as cnx: |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
200 |
self.hook_executed = False |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
201 |
self.create_user(cnx, 'babar') |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
202 |
cnx.commit() |
9472
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
203 |
|
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
204 |
from cubicweb.server import hook |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
205 |
from cubicweb.predicates import is_instance |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
206 |
|
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
207 |
class MyHook(hook.Hook): |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
208 |
__regid__ = 'whatever' |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
209 |
__select__ = hook.Hook.__select__ & is_instance('CWProperty') |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
210 |
category = 'test-hook' |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
211 |
events = ('after_add_entity',) |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
212 |
test = self |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
213 |
|
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
214 |
def __call__(self): |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
215 |
self.test.hook_executed = True |
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
216 |
|
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
217 |
with self.new_access('babar').repo_cnx() as cnx: |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
218 |
with self.temporary_appobjects(MyHook): |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
219 |
with cnx.allow_all_hooks_but('test-hook'): |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
220 |
prop = cnx.create_entity('CWProperty', pkey=u'ui.language', value=u'en') |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
221 |
cnx.commit() |
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
222 |
self.assertFalse(self.hook_executed) |
9472
ae97167956b1
[devtools] fix self.session._cnx vs self.cnx._cnx confusion after self.login
Julien Cristau <julien.cristau@logilab.fr>
parents:
9402
diff
changeset
|
223 |
|
9402 | 224 |
|
9117
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
225 |
class RepoAccessTC(CubicWebTC): |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
226 |
|
9117
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
227 |
def test_repo_connection(self): |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
228 |
acc = self.new_access('admin') |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
229 |
with acc.repo_cnx() as cnx: |
9117
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
230 |
rset = cnx.execute('Any X WHERE X is CWUser') |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
231 |
self.assertTrue(rset) |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
232 |
|
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
233 |
def test_client_connection(self): |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
234 |
acc = self.new_access('admin') |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
235 |
with acc.client_cnx() as cnx: |
9117
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
236 |
rset = cnx.execute('Any X WHERE X is CWUser') |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
237 |
self.assertTrue(rset) |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
238 |
|
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
239 |
def test_web_request(self): |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
240 |
acc = self.new_access('admin') |
9857
618d45655865
[testlib] use the new connection api
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9472
diff
changeset
|
241 |
with acc.web_request(elephant='babar') as req: |
9117
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
242 |
rset = req.execute('Any X WHERE X is CWUser') |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
243 |
self.assertTrue(rset) |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
244 |
self.assertEqual('babar', req.form['elephant']) |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
245 |
|
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
246 |
def test_close(self): |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
247 |
acc = self.new_access('admin') |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
248 |
acc.close() |
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
249 |
|
9118
bb9e19df9a05
[testlib] add an default testcase.adminaccess (and use it for default session)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9117
diff
changeset
|
250 |
def test_admin_access(self): |
bb9e19df9a05
[testlib] add an default testcase.adminaccess (and use it for default session)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9117
diff
changeset
|
251 |
with self.admin_access.client_cnx() as cnx: |
bb9e19df9a05
[testlib] add an default testcase.adminaccess (and use it for default session)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9117
diff
changeset
|
252 |
self.assertEqual('admin', cnx.user.login) |
bb9e19df9a05
[testlib] add an default testcase.adminaccess (and use it for default session)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9117
diff
changeset
|
253 |
|
9117
e25c5abc667c
[testlib] introduce a RepoAccess class to easily create connection and request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8941
diff
changeset
|
254 |
|
0 | 255 |
if __name__ == '__main__': |
256 |
unittest_main() |