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