author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 28 May 2009 09:20:07 +0200 | |
changeset 1977 | 606923dff11b |
parent 1802 | d628defebc17 |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
1 |
"""cubicweb.web.views.baseforms unit tests |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
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:
1802
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:
1802
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:
1802
diff
changeset
|
7 |
""" |
0 | 8 |
|
9 |
from StringIO import StringIO |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
10 |
from datetime import date |
0 | 11 |
import re |
12 |
||
801 | 13 |
|
0 | 14 |
from logilab.common.testlib import unittest_main |
801 | 15 |
from logilab.common.decorators import clear_cache |
0 | 16 |
from cubicweb.devtools.apptest import EnvBasedTC |
17 |
from cubicweb.entities import AnyEntity |
|
801 | 18 |
from cubicweb.web import widgets |
0 | 19 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
20 |
orig_now = widgets.datetime.now |
0 | 21 |
|
22 |
def setup_module(options): |
|
23 |
def _today(): |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
24 |
return date(0000, 1, 1) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
25 |
widgets.datetime.now = _today |
0 | 26 |
|
952
8e4da466efc7
teardown_module now gets test results as arguement
sylvain.thenault@logilab.fr
parents:
16
diff
changeset
|
27 |
def teardown_module(options, results): |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
28 |
widgets.datetime.now = orig_now |
0 | 29 |
|
30 |
||
31 |
def cleanup_text(text): |
|
32 |
return re.sub('\d\d:\d\d', 'hh:mm', re.sub('\d+/\d\d/\d\d', 'YYYY/MM/DD', '\n'.join(l.strip() for l in text.splitlines() if l.strip()))) |
|
33 |
||
34 |
||
35 |
||
36 |
class EditionFormTC(EnvBasedTC): |
|
37 |
||
38 |
def setup_database(self): |
|
39 |
self.create_user('joe') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
40 |
|
0 | 41 |
def _build_creation_form(self, etype): |
42 |
req = self.request() |
|
43 |
req.next_tabindex() |
|
44 |
req.next_tabindex() |
|
45 |
req.del_page_data() |
|
46 |
req.form['etype'] = etype |
|
47 |
view = self.vreg.select_view('creation', req, None) |
|
48 |
entity = self.vreg.etype_class(etype)(req, None, None) |
|
49 |
buffer = StringIO() |
|
50 |
view.w = buffer.write |
|
51 |
view.edit_form(entity, {}) |
|
52 |
return buffer.getvalue() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
53 |
|
0 | 54 |
def _test_view_for(self, etype, expected): |
55 |
self.assertTextEquals(expected, cleanup_text(self._build_creation_form(etype))) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
56 |
|
0 | 57 |
def test_base(self): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
58 |
self._test_view_for('CWGroup', '''\ |
0 | 59 |
<form id="entityForm" class="entityForm" cubicweb:target="eformframe" |
60 |
method="post" onsubmit="return freezeFormButtons('entityForm')" enctype="application/x-www-form-urlencoded" action="http://testing.fr/cubicweb/validateform"> |
|
61 |
<div class="formTitle"><span>egroup (creation)</span></div> |
|
62 |
<div id="progress">validating...</div> |
|
63 |
<div class="iformTitle"><span>main informations</span></div> |
|
64 |
<div class="formBody"><fieldset> |
|
65 |
<input type="hidden" name="eid" value="A" /> |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
66 |
<input type="hidden" name="__type:A" value="CWGroup" /> |
0 | 67 |
<input type="hidden" name="__maineid" value="A" /> |
68 |
<input id="errorurl" type="hidden" name="__errorurl" value="http://testing.fr/cubicweb/view?rql=Blop&vid=blop" /> |
|
69 |
<input type="hidden" name="__form_id" value="edition" /> |
|
70 |
<input type="hidden" name="__message" value="element created" /> |
|
71 |
<table id="entityFormA" class="attributeForm" style="width:100%;"> |
|
72 |
<tr> |
|
73 |
<th class="labelCol"><label class="required" for="name:A">name</label></th> |
|
74 |
<td style="width:100%;"> |
|
75 |
<input type="hidden" name="edits-name:A" value="__cubicweb_internal_field__"/> |
|
76 |
<input type="text" name="name:A" value="" accesskey="n" id="name:A" maxlength="64" size="40" tabindex="2"/> |
|
77 |
<br/> |
|
78 |
</td> |
|
79 |
</tr> |
|
80 |
</table> |
|
81 |
</fieldset> |
|
82 |
</div> |
|
83 |
<table width="100%"> |
|
84 |
<tbody> |
|
85 |
<tr><td align="center"> |
|
86 |
<input class="validateButton" type="submit" name="defaultsubmit" value="Button_ok" tabindex="3"/> |
|
87 |
</td><td style="align: right; width: 50%;"> |
|
88 |
<input class="validateButton" type="button" onclick="postForm('__action_apply', 'Button_apply', 'entityForm')" value="Button_apply" tabindex="4"/> |
|
89 |
<input class="validateButton" type="button" onclick="postForm('__action_cancel', 'Button_cancel', 'entityForm')" value="Button_cancel" tabindex="5"/> |
|
90 |
</td></tr> |
|
91 |
</tbody> |
|
92 |
</table> |
|
93 |
</form>''') |
|
94 |
||
95 |
def test_with_inline_view(self): |
|
96 |
activated = self.execute('Any X WHERE X is State, X name "activated"')[0][0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
97 |
self._test_view_for('CWUser', '''<form id="entityForm" class="entityForm" cubicweb:target="eformframe" |
0 | 98 |
method="post" onsubmit="return freezeFormButtons('entityForm')" enctype="application/x-www-form-urlencoded" action="http://testing.fr/cubicweb/validateform"> |
99 |
<div class="formTitle"><span>euser (creation)</span></div> |
|
100 |
<div id="progress">validating...</div> |
|
101 |
<div class="iformTitle"><span>main informations</span></div> |
|
102 |
<div class="formBody"><fieldset> |
|
103 |
<input type="hidden" name="eid" value="A" /> |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
104 |
<input type="hidden" name="__type:A" value="CWUser" /> |
0 | 105 |
<input type="hidden" name="__maineid" value="A" /> |
106 |
<input id="errorurl" type="hidden" name="__errorurl" value="http://testing.fr/cubicweb/view?rql=Blop&vid=blop" /> |
|
107 |
<input type="hidden" name="__form_id" value="edition" /> |
|
108 |
<input type="hidden" name="__message" value="element created" /> |
|
109 |
<table id="entityFormA" class="attributeForm" style="width:100%%;"> |
|
110 |
<tr> |
|
111 |
<th class="labelCol"><label class="required" for="login:A">login</label></th> |
|
112 |
<td style="width:100%%;"> |
|
113 |
<input type="hidden" name="edits-login:A" value="__cubicweb_internal_field__"/> |
|
114 |
<input type="text" name="login:A" value="" accesskey="l" id="login:A" maxlength="64" size="40" tabindex="2"/> |
|
115 |
<br/> <span class="helper">unique identifier used to connect to the application</span> |
|
116 |
</td> |
|
117 |
</tr> |
|
118 |
<tr> |
|
119 |
<th class="labelCol"><label class="required" for="upassword:A">upassword</label></th> |
|
120 |
<td style="width:100%%;"> |
|
121 |
<input type="hidden" name="edits-upassword:A" value="__cubicweb_internal_field__"/> |
|
122 |
<input type="password" name="upassword:A" value="" accesskey="u" id="upassword:A" tabindex="3"/><br/> |
|
123 |
<input type="password" name="upassword-confirm:A" id="upassword-confirm:A" tabindex="4"/> <span class="emphasis">(confirm password)</span> |
|
124 |
<br/> |
|
125 |
</td> |
|
126 |
</tr> |
|
127 |
<tr> |
|
128 |
<th class="labelCol"><label for="firstname:A">firstname</label></th> |
|
129 |
<td style="width:100%%;"> |
|
130 |
<input type="hidden" name="edits-firstname:A" value="__cubicweb_internal_field__"/> |
|
131 |
<input type="text" name="firstname:A" value="" accesskey="f" id="firstname:A" maxlength="64" size="40" tabindex="5"/> |
|
132 |
<br/> |
|
133 |
</td> |
|
134 |
</tr> |
|
135 |
<tr> |
|
136 |
<th class="labelCol"><label for="surname:A">surname</label></th> |
|
137 |
<td style="width:100%%;"> |
|
138 |
<input type="hidden" name="edits-surname:A" value="__cubicweb_internal_field__"/> |
|
139 |
<input type="text" name="surname:A" value="" accesskey="s" id="surname:A" maxlength="64" size="40" tabindex="6"/> |
|
140 |
<br/> |
|
141 |
</td> |
|
142 |
</tr> |
|
143 |
<tr> |
|
144 |
<th class="labelCol"><label class="required" for="in_group:A">in_group</label></th> |
|
145 |
<td style="width:100%%;"> |
|
146 |
<input type="hidden" name="edits-in_group:A" value="__cubicweb_internal_field__"/> |
|
147 |
<select name="in_group:A" id="in_group:A" multiple="multiple" size="5" tabindex="7"> |
|
148 |
<option value="3" >guests</option> |
|
149 |
<option value="1" >managers</option> |
|
150 |
<option value="2" >users</option> |
|
151 |
</select> |
|
152 |
<br/> <span class="helper">groups grant permissions to the user</span> |
|
153 |
</td> |
|
154 |
</tr> |
|
155 |
<tr> |
|
156 |
<th class="labelCol"><label class="required" for="in_state:A">in_state</label></th> |
|
157 |
<td style="width:100%%;"> |
|
158 |
<input type="hidden" name="edits-in_state:A" value="__cubicweb_internal_field__"/> |
|
159 |
<select name="in_state:A" id="in_state:A" tabindex="8"> |
|
160 |
<option value="%(activated)s" >activated</option> |
|
161 |
</select> |
|
162 |
<br/> <span class="helper">account state</span> |
|
163 |
</td> |
|
164 |
</tr> |
|
165 |
</table> |
|
166 |
<div id="inlineuse_emailslot"> |
|
167 |
<div class="inlinedform" id="addNewEmailAddressuse_emailsubject:A" cubicweb:limit="true"> |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
168 |
<a class="addEntity" id="adduse_email:Alink" href="javascript: addInlineCreationForm('A', 'CWUser', 'EmailAddress', 'use_email', 'subject')" >+ add a EmailAddress.</a> |
0 | 169 |
</div> |
170 |
<div class="trame_grise"> </div> |
|
171 |
</div> |
|
172 |
</fieldset> |
|
173 |
</div> |
|
174 |
<table width="100%%"> |
|
175 |
<tbody> |
|
176 |
<tr><td align="center"> |
|
177 |
<input class="validateButton" type="submit" name="defaultsubmit" value="Button_ok" tabindex="9"/> |
|
178 |
</td><td style="align: right; width: 50%%;"> |
|
179 |
<input class="validateButton" type="button" onclick="postForm('__action_apply', 'Button_apply', 'entityForm')" value="Button_apply" tabindex="10"/> |
|
180 |
<input class="validateButton" type="button" onclick="postForm('__action_cancel', 'Button_cancel', 'entityForm')" value="Button_cancel" tabindex="11"/> |
|
181 |
</td></tr> |
|
182 |
</tbody> |
|
183 |
</table> |
|
184 |
</form>''' % {'activated' : activated}) |
|
185 |
||
186 |
def test_redirection_after_creation(self): |
|
187 |
req = self.request() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
188 |
req.form['etype'] = 'CWUser' |
0 | 189 |
view = self.vreg.select_view('creation', req, None) |
190 |
self.assertEquals(view.redirect_url(), 'http://testing.fr/cubicweb/euser') |
|
191 |
req.form['__redirectrql'] = 'Any X WHERE X eid 3012' |
|
192 |
req.form['__redirectvid'] = 'avid' |
|
193 |
self.assertEquals(view.redirect_url(), 'http://testing.fr/cubicweb/view?rql=Any%20X%20WHERE%20X%20eid%203012&vid=avid') |
|
194 |
||
195 |
||
196 |
def test_need_multipart(self): |
|
197 |
req = self.request() |
|
198 |
class Salesterm(AnyEntity): |
|
199 |
id = 'Salesterm' |
|
200 |
__rtags__ = {'described_by_test' : 'inlineview'} |
|
201 |
vreg = self.vreg |
|
202 |
vreg.register_vobject_class(Salesterm) |
|
203 |
req.form['etype'] = 'Salesterm' |
|
204 |
entity = vreg.etype_class('Salesterm')(req, None, None) |
|
205 |
view = vreg.select_view('creation', req, None) |
|
206 |
self.failUnless(view.need_multipart(entity)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
207 |
|
0 | 208 |
|
209 |
||
210 |
def test_nonregr_check_add_permission_on_relation(self): |
|
16
a70ece4d9d1a
fix tests in web/test
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
211 |
from cubes.blog.entities import BlogEntry |
0 | 212 |
class BlogEntryPlus(BlogEntry): |
213 |
__rtags__ = {'checked_by': 'primary'} |
|
214 |
self.vreg.register_vobject_class(BlogEntryPlus) |
|
801 | 215 |
clear_cache(self.vreg, 'etype_class') |
0 | 216 |
# an admin should be able to edit the checked_by relation |
217 |
html = self._build_creation_form('BlogEntry') |
|
218 |
self.failUnless('name="edits-checked_by:A"' in html) |
|
219 |
# a regular user should not be able to see the relation |
|
220 |
self.login('joe') |
|
221 |
html = self._build_creation_form('BlogEntry') |
|
222 |
self.failIf('name="edits-checked_by:A"' in html) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
223 |
|
0 | 224 |
from cubicweb.devtools.testlib import WebTest |
225 |
from cubicweb.devtools.htmlparser import DTDValidator |
|
226 |
||
227 |
class CopyWebTest(WebTest): |
|
228 |
||
229 |
def setup_database(self): |
|
230 |
p = self.create_user("Doe") |
|
231 |
# do not try to skip 'primary_email' for this test |
|
232 |
e = self.add_entity('EmailAddress', address=u'doe@doe.com') |
|
233 |
self.execute('SET P use_email E, P primary_email E WHERE P eid %(p)s, E eid %(e)s', |
|
234 |
{'p' : p.eid, 'e' : e.eid}) |
|
235 |
||
236 |
||
237 |
def test_cloned_elements_in_copy_form(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
238 |
rset = self.execute('CWUser P WHERE P login "Doe"') |
0 | 239 |
output = self.view('copy', rset) |
240 |
clones = [attrs for _, attrs in output.input_tags |
|
241 |
if attrs.get('name', '').startswith('__cloned_eid')] |
|
242 |
# the only cloned entity should be the original person |
|
243 |
self.assertEquals(len(clones), 1) |
|
244 |
attrs = clones[0] |
|
245 |
self.assertEquals(attrs['name'], '__cloned_eid:A') |
|
246 |
self.assertEquals(int(attrs['value']), rset[0][0]) |
|
247 |
||
248 |
||
249 |
if __name__ == '__main__': |
|
250 |
unittest_main() |