|
1 from cubicweb.goa.testlib import * |
|
2 |
|
3 from urllib import unquote |
|
4 |
|
5 from cubicweb.common import ValidationError |
|
6 from cubicweb.common.uilib import rql_for_eid |
|
7 |
|
8 from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect |
|
9 |
|
10 from cubicweb.goa.goaconfig import GAEConfiguration |
|
11 from cubicweb.entities.authobjs import EUser |
|
12 |
|
13 |
|
14 class EditControllerTC(GAEBasedTC): |
|
15 |
|
16 config = GAEConfiguration('toto') |
|
17 config.global_set_option('use-google-auth', False) |
|
18 config.global_set_option('schema-type', 'yams') |
|
19 config.global_set_option('included-cubes', ()) |
|
20 config.global_set_option('included-yams-cubes', ('eblog',)) |
|
21 |
|
22 MODEL_CLASSES = () |
|
23 from cubicweb.web.views import editcontroller |
|
24 from cubicweb.entities import lib |
|
25 LOAD_APP_MODULES = (editcontroller, lib) |
|
26 |
|
27 def setUp(self): |
|
28 GAEBasedTC.setUp(self) |
|
29 self.req = self.request() |
|
30 self.ctrl = self.get_ctrl(self.req) |
|
31 |
|
32 def get_ctrl(self, req): |
|
33 return self.vreg.select(self.vreg.registry_objects('controllers', 'edit'), |
|
34 req=req, appli=self) |
|
35 |
|
36 def publish(self, req): |
|
37 assert req is self.ctrl.req |
|
38 try: |
|
39 result = self.ctrl.publish() |
|
40 req.cnx.commit() |
|
41 except Redirect: |
|
42 req.cnx.commit() |
|
43 raise |
|
44 except: |
|
45 req.cnx.rollback() |
|
46 raise |
|
47 return result |
|
48 |
|
49 def expect_redirect_publish(self, req=None): |
|
50 if req is not None: |
|
51 self.ctrl = self.get_ctrl(req) |
|
52 else: |
|
53 req = self.req |
|
54 try: |
|
55 res = self.publish(req) |
|
56 except Redirect, ex: |
|
57 try: |
|
58 path, params = ex.location.split('?', 1) |
|
59 except: |
|
60 path, params = ex.location, "" |
|
61 req._url = path |
|
62 cleanup = lambda p: (p[0], unquote(p[1])) |
|
63 params = dict(cleanup(p.split('=', 1)) for p in params.split('&') if p) |
|
64 return req.relative_path(False), params # path.rsplit('/', 1)[-1], params |
|
65 else: |
|
66 self.fail('expected a Redirect exception') |
|
67 |
|
68 def test_noparam_edit(self): |
|
69 """check behaviour of this controller without any form parameter""" |
|
70 self.req.form = {} |
|
71 self.assertRaises(ValidationError, self.publish, self.req) |
|
72 |
|
73 def test_validation_unique(self): |
|
74 """test creation of two linked entities""" |
|
75 user = self.user |
|
76 self.req.form = {'eid': 'X', '__type:X': 'EUser', |
|
77 'login:X': self.user.login, 'edits-login:X': u'', |
|
78 'upassword:X': u'toto', 'upassword-confirm:X': u'toto', 'edits-upassword:X': u'', |
|
79 } |
|
80 self.assertRaises(ValidationError, self.publish, self.req) |
|
81 |
|
82 |
|
83 def test_user_editing_itself(self): |
|
84 """checking that a manager user can edit itself""" |
|
85 self.skip('missing actual gae support, retry latter') |
|
86 user = self.user |
|
87 basegroups = [str(eid) for eid, in self.req.execute('EGroup G WHERE X in_group G, X eid %(x)s', {'x': user.eid})] |
|
88 groupeids = [eid for eid, in self.req.execute('EGroup G WHERE G name in ("managers", "users")')] |
|
89 groups = [str(eid) for eid in groupeids] |
|
90 stateeid = [eid for eid, in self.req.execute('State S WHERE S name "activated"')][0] |
|
91 self.req.form = { |
|
92 'eid': user.eid, |
|
93 '__type:'+user.eid: 'EUser', |
|
94 'login:'+user.eid: unicode(user.login), |
|
95 'firstname:'+user.eid: u'Th\xe9nault', |
|
96 'surname:'+user.eid: u'Sylvain', |
|
97 'in_group:'+user.eid: groups, |
|
98 'in_state:'+user.eid: stateeid, |
|
99 # |
|
100 'edits-login:'+user.eid: unicode(user.login), |
|
101 'edits-firstname:'+user.eid: u'', |
|
102 'edits-surname:'+user.eid: u'', |
|
103 'edits-in_group:'+user.eid: basegroups, |
|
104 'edits-in_state:'+user.eid: stateeid, |
|
105 } |
|
106 path, params = self.expect_redirect_publish() |
|
107 e = self.req.execute('Any X WHERE X eid %(x)s', {'x': user.eid}, 'x').get_entity(0, 0) |
|
108 self.assertEquals(e.firstname, u'Th\xe9nault') |
|
109 self.assertEquals(e.surname, u'Sylvain') |
|
110 self.assertEquals(e.login, user.login) |
|
111 self.assertEquals([g.eid for g in e.in_group], groupeids) |
|
112 self.assertEquals(e.in_state[0].eid, stateeid) |
|
113 |
|
114 def test_user_can_change_its_password(self): |
|
115 user = self.create_user('user') |
|
116 cnx = self.login('user') |
|
117 req = self.request() |
|
118 #self.assertEquals(self.ctrl.schema['EUser']._groups['read'], |
|
119 # ('managers', 'users')) |
|
120 req.form = { |
|
121 'eid': user.eid, '__type:'+user.eid: 'EUser', |
|
122 '__maineid' : str(user.eid), |
|
123 'upassword:'+user.eid: 'tournicoton', |
|
124 'upassword-confirm:'+user.eid: 'tournicoton', |
|
125 'edits-upassword:'+user.eid: '', |
|
126 } |
|
127 path, params = self.expect_redirect_publish(req) |
|
128 cnx.commit() # commit to check we don't get late validation error for instance |
|
129 self.assertEquals(path, 'euser/user') |
|
130 self.failIf('vid' in params) |
|
131 |
|
132 def test_user_editing_itself_no_relation(self): |
|
133 """checking we can edit an entity without specifying some required |
|
134 relations (meaning no changes) |
|
135 """ |
|
136 user = self.user |
|
137 groupeids = [eid for eid, in self.req.execute('EGroup G WHERE X in_group G, X eid %(x)s', {'x': user.eid})] |
|
138 self.req.form = { |
|
139 'eid': user.eid, |
|
140 '__type:'+user.eid: 'EUser', |
|
141 'login:'+user.eid: unicode(user.login), |
|
142 'firstname:'+user.eid: u'Th\xe9nault', |
|
143 'surname:'+user.eid: u'Sylvain', |
|
144 # |
|
145 'edits-login:'+user.eid: unicode(user.login), |
|
146 'edits-firstname:'+user.eid: u'', |
|
147 'edits-surname:'+user.eid: u'', |
|
148 } |
|
149 path, params = self.expect_redirect_publish() |
|
150 self.req.drop_entity_cache(user.eid) |
|
151 e = self.req.execute('Any X WHERE X eid %(x)s', {'x': user.eid}, 'x').get_entity(0, 0) |
|
152 self.assertEquals(e.login, user.login) |
|
153 self.assertEquals(e.firstname, u'Th\xe9nault') |
|
154 self.assertEquals(e.surname, u'Sylvain') |
|
155 self.assertUnorderedIterableEquals([g.eid for g in e.in_group], groupeids) |
|
156 #stateeids = [eid for eid, in self.req.execute('State S WHERE S name "activated"')] |
|
157 #self.assertEquals([s.eid for s in e.in_state], stateeids) |
|
158 |
|
159 |
|
160 def test_create_multiple_linked(self): |
|
161 gueid = self.req.execute('EGroup G WHERE G name "users"')[0][0] |
|
162 self.req.form = {'eid': ['X', 'Y'], |
|
163 |
|
164 '__type:X': 'EUser', |
|
165 '__maineid' : 'X', |
|
166 'login:X': u'adim', 'edits-login:X': u'', |
|
167 'upassword:X': u'toto', 'upassword-confirm:X': u'toto', 'edits-upassword:X': u'', |
|
168 'surname:X': u'Di Mascio', 'edits-surname:X': '', |
|
169 |
|
170 'in_group:X': gueid, 'edits-in_group:X': INTERNAL_FIELD_VALUE, |
|
171 |
|
172 '__type:Y': 'EmailAddress', |
|
173 'address:Y': u'dima@logilab.fr', 'edits-address:Y': '', |
|
174 'use_email:X': 'Y', 'edits-use_email:X': INTERNAL_FIELD_VALUE, |
|
175 } |
|
176 path, params = self.expect_redirect_publish() |
|
177 # should be redirected on the created person |
|
178 self.assertEquals(path, 'euser/adim') |
|
179 e = self.req.execute('Any P WHERE P surname "Di Mascio"').get_entity(0, 0) |
|
180 self.assertEquals(e.surname, 'Di Mascio') |
|
181 email = e.use_email[0] |
|
182 self.assertEquals(email.address, 'dima@logilab.fr') |
|
183 |
|
184 def test_edit_multiple_linked(self): |
|
185 peid = self.create_user('adim').eid |
|
186 self.req.form = {'eid': [peid, 'Y'], |
|
187 '__type:%s'%peid: 'EUser', |
|
188 'surname:%s'%peid: u'Di Masci', 'edits-surname:%s'%peid: '', |
|
189 |
|
190 '__type:Y': 'EmailAddress', |
|
191 'address:Y': u'dima@logilab.fr', 'edits-address:Y': '', |
|
192 'use_email:%s'%peid: 'Y', 'edits-use_email:%s'%peid: INTERNAL_FIELD_VALUE, |
|
193 |
|
194 '__redirectrql': 'Any X WHERE X eid %s'%peid, |
|
195 } |
|
196 path, params = self.expect_redirect_publish() |
|
197 # should be redirected on the created person |
|
198 eid = params['rql'].split()[-1] |
|
199 e = self.req.execute('Any X WHERE X eid %(x)s', {'x': eid}, 'x').get_entity(0, 0) |
|
200 self.assertEquals(e.surname, 'Di Masci') |
|
201 email = e.use_email[0] |
|
202 self.assertEquals(email.address, 'dima@logilab.fr') |
|
203 |
|
204 emaileid = email.eid |
|
205 self.req.form = {'eid': [peid, emaileid], |
|
206 '__type:%s'%peid: 'EUser', |
|
207 'surname:%s'%peid: u'Di Masci', 'edits-surname:%s'%peid: 'Di Masci', |
|
208 '__type:%s'%emaileid: 'EmailAddress', |
|
209 'address:%s'%emaileid: u'adim@logilab.fr', 'edits-address:%s'%emaileid: 'dima@logilab.fr', |
|
210 'use_email:%s'%peid: emaileid, 'edits-use_email:%s'%peid: emaileid, |
|
211 '__redirectrql': 'Any X WHERE X eid %s'%peid, |
|
212 } |
|
213 path, params = self.expect_redirect_publish() |
|
214 # should be redirected on the created person |
|
215 eid = params['rql'].split()[-1] |
|
216 # XXX this should not be necessary, it isn't with regular cubicweb |
|
217 self.req._eid_cache = {} |
|
218 e = self.req.execute('Any X WHERE X eid %(x)s', {'x': eid}, 'x').get_entity(0, 0) |
|
219 self.assertEquals(e.surname, 'Di Masci') |
|
220 email = e.use_email[0] |
|
221 self.assertEquals(email.address, 'adim@logilab.fr') |
|
222 |
|
223 |
|
224 def test_password_confirm(self): |
|
225 """test creation of two linked entities |
|
226 """ |
|
227 user = self.user |
|
228 self.req.form = {'__cloned_eid:X': user.eid, |
|
229 'eid': 'X', '__type:X': 'EUser', |
|
230 'login:X': u'toto', 'edits-login:X': u'', |
|
231 'upassword:X': u'toto', 'edits-upassword:X': u'', |
|
232 } |
|
233 self.assertRaises(ValidationError, self.publish, self.req) |
|
234 self.req.form = {'__cloned_eid:X': user.eid, |
|
235 'eid': 'X', '__type:X': 'EUser', |
|
236 'login:X': u'toto', 'edits-login:X': u'', |
|
237 'upassword:X': u'toto', 'upassword-confirm:X': u'tutu', 'edits-upassword:X': u'', |
|
238 } |
|
239 self.assertRaises(ValidationError, self.publish, self.req) |
|
240 |
|
241 |
|
242 def test_req_pending_insert(self): |
|
243 """make sure req's pending insertions are taken into account""" |
|
244 tmpgroup = self.add_entity('EGroup', name=u"test") |
|
245 user = self.user |
|
246 self.req.set_session_data('pending_insert', set([(user.eid, 'in_group', tmpgroup.eid)])) |
|
247 path, params = self.expect_redirect_publish() |
|
248 usergroups = [gname for gname, in |
|
249 self.req.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})] |
|
250 self.assertUnorderedIterableEquals(usergroups, ['managers', 'users', 'test']) |
|
251 self.assertEquals(self.req.get_pending_inserts(), []) |
|
252 |
|
253 |
|
254 def test_req_pending_delete(self): |
|
255 """make sure req's pending deletions are taken into account""" |
|
256 user = self.user |
|
257 groupeid = self.req.execute('INSERT EGroup G: G name "test", U in_group G WHERE U eid %(x)s', |
|
258 {'x': user.eid})[0][0] |
|
259 usergroups = [gname for gname, in |
|
260 self.req.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})] |
|
261 # just make sure everything was set correctly |
|
262 self.assertUnorderedIterableEquals(usergroups, ['managers', 'users', 'test']) |
|
263 # now try to delete the relation |
|
264 self.req.set_session_data('pending_delete', set([(user.eid, 'in_group', groupeid)])) |
|
265 path, params = self.expect_redirect_publish() |
|
266 usergroups = [gname for gname, in |
|
267 self.req.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})] |
|
268 self.assertUnorderedIterableEquals(usergroups, ['managers', 'users']) |
|
269 #self.assertUnorderedIterableEquals(usergroups, ['managers']) |
|
270 self.assertEquals(self.req.get_pending_deletes(), []) |
|
271 |
|
272 def test_custom_attribute_handler(self): |
|
273 def custom_login_edit(self, formparams, value, relations): |
|
274 formparams['login'] = value.upper() |
|
275 relations.append('X login %(login)s') |
|
276 EUser.custom_login_edit = custom_login_edit |
|
277 try: |
|
278 user = self.user |
|
279 eid = repr(user.eid) |
|
280 self.req.form = { |
|
281 'eid': eid, |
|
282 '__type:'+eid: 'EUser', |
|
283 'login:'+eid: u'foo', |
|
284 'edits-login:'+eid: unicode(user.login), |
|
285 } |
|
286 path, params = self.expect_redirect_publish() |
|
287 rset = self.req.execute('Any L WHERE X eid %(x)s, X login L', {'x': user.eid}, 'x') |
|
288 self.assertEquals(rset[0][0], 'FOO') |
|
289 finally: |
|
290 del EUser.custom_login_edit |
|
291 |
|
292 def test_redirect_apply_button(self): |
|
293 redirectrql = rql_for_eid(4012) # whatever |
|
294 self.req.form = { |
|
295 'eid': 'A', '__type:A': 'BlogEntry', |
|
296 '__maineid' : 'A', |
|
297 'content:A': u'"13:03:43"', 'edits-content:A': '', |
|
298 'title:A': u'huuu', 'edits-title:A': '', |
|
299 '__redirectrql': redirectrql, |
|
300 '__redirectvid': 'primary', |
|
301 '__redirectparams': 'toto=tutu&tata=titi', |
|
302 '__form_id': 'edition', |
|
303 '__action_apply': '', |
|
304 } |
|
305 path, params = self.expect_redirect_publish() |
|
306 self.failUnless(path.startswith('blogentry/')) |
|
307 eid = path.split('/')[1] |
|
308 self.assertEquals(params['vid'], 'edition') |
|
309 self.assertNotEquals(eid, '4012') |
|
310 self.assertEquals(params['__redirectrql'], redirectrql) |
|
311 self.assertEquals(params['__redirectvid'], 'primary') |
|
312 self.assertEquals(params['__redirectparams'], 'toto=tutu&tata=titi') |
|
313 |
|
314 def test_redirect_ok_button(self): |
|
315 redirectrql = rql_for_eid(4012) # whatever |
|
316 self.req.form = { |
|
317 'eid': 'A', '__type:A': 'BlogEntry', |
|
318 '__maineid' : 'A', |
|
319 'content:A': u'"13:03:43"', 'edits-content:A': '', |
|
320 'title:A': u'huuu', 'edits-title:A': '', |
|
321 '__redirectrql': redirectrql, |
|
322 '__redirectvid': 'primary', |
|
323 '__redirectparams': 'toto=tutu&tata=titi', |
|
324 '__form_id': 'edition', |
|
325 } |
|
326 path, params = self.expect_redirect_publish() |
|
327 self.assertEquals(path, 'view') |
|
328 self.assertEquals(params['rql'], redirectrql) |
|
329 self.assertEquals(params['vid'], 'primary') |
|
330 self.assertEquals(params['tata'], 'titi') |
|
331 self.assertEquals(params['toto'], 'tutu') |
|
332 |
|
333 def test_redirect_delete_button(self): |
|
334 eid = self.add_entity('BlogEntry', title=u'hop', content=u'hop').eid |
|
335 self.req.form = {'eid': str(eid), '__type:%s'%eid: 'BlogEntry', |
|
336 '__action_delete': ''} |
|
337 path, params = self.expect_redirect_publish() |
|
338 self.assertEquals(path, 'blogentry') |
|
339 self.assertEquals(params, {u'__message': u'entity deleted'}) |
|
340 eid = self.add_entity('EmailAddress', address=u'hop@logilab.fr').eid |
|
341 self.req.execute('SET X use_email E WHERE E eid %(e)s, X eid %(x)s', |
|
342 {'x': self.user.eid, 'e': eid}, 'x') |
|
343 self.commit() |
|
344 self.req.form = {'eid': str(eid), '__type:%s'%eid: 'EmailAddress', |
|
345 '__action_delete': ''} |
|
346 path, params = self.expect_redirect_publish() |
|
347 self.assertEquals(unquote(path), 'euser/'+self.user.login) |
|
348 self.assertEquals(params, {u'__message': u'entity deleted'}) |
|
349 eid1 = self.add_entity('BlogEntry', title=u'hop', content=u'hop').eid |
|
350 eid2 = self.add_entity('EmailAddress', address=u'hop@logilab.fr').eid |
|
351 self.req.form = {'eid': [str(eid1), str(eid2)], |
|
352 '__type:%s'%eid1: 'BlogEntry', |
|
353 '__type:%s'%eid2: 'EmailAddress', |
|
354 '__action_delete': ''} |
|
355 path, params = self.expect_redirect_publish() |
|
356 self.assertEquals(path, 'view') |
|
357 self.assertEquals(params, {u'__message': u'entities deleted'}) |
|
358 |
|
359 |
|
360 def test_nonregr_multiple_empty_email_addr(self): |
|
361 gueid = self.req.execute('EGroup G WHERE G name "users"')[0][0] |
|
362 self.req.form = {'eid': ['X', 'Y'], |
|
363 |
|
364 '__type:X': 'EUser', |
|
365 'login:X': u'adim', 'edits-login:X': u'', |
|
366 'upassword:X': u'toto', 'upassword-confirm:X': u'toto', 'edits-upassword:X': u'', |
|
367 'in_group:X': gueid, 'edits-in_group:X': INTERNAL_FIELD_VALUE, |
|
368 |
|
369 '__type:Y': 'EmailAddress', |
|
370 'address:Y': u'', 'edits-address:Y': '', |
|
371 'alias:Y': u'', 'edits-alias:Y': '', |
|
372 'use_email:X': 'Y', 'edits-use_email:X': INTERNAL_FIELD_VALUE, |
|
373 } |
|
374 self.assertRaises(ValidationError, self.publish, self.req) |
|
375 |
|
376 |
|
377 def test_nonregr_rollback_on_validation_error(self): |
|
378 self.skip('lax fix me') |
|
379 p = self.create_user("doe") |
|
380 # do not try to skip 'primary_email' for this test |
|
381 old_skips = p.__class__.skip_copy_for |
|
382 p.__class__.skip_copy_for = () |
|
383 try: |
|
384 e = self.add_entity('EmailAddress', address=u'doe@doe.com') |
|
385 self.req.execute('SET P use_email E, P primary_email E WHERE P eid %(p)s, E eid %(e)s', |
|
386 {'p' : p.eid, 'e' : e.eid}) |
|
387 self.req.form = {'__cloned_eid:X': p.eid, |
|
388 'eid': 'X', '__type:X': 'EUser', |
|
389 'login': u'dodo', 'edits-login': u'dodo', |
|
390 'surname:X': u'Boom', 'edits-surname:X': u'', |
|
391 '__errorurl' : "whatever but required", |
|
392 } |
|
393 # try to emulate what really happens in the web application |
|
394 # 1/ validate form => EditController.publish raises a ValidationError |
|
395 # which fires a Redirect |
|
396 # 2/ When re-publishing the copy form, the publisher implicitly commits |
|
397 try: |
|
398 self.env.app.publish('edit', self.req) |
|
399 except Redirect: |
|
400 self.req.form['rql'] = 'Any X WHERE X eid %s' % p.eid |
|
401 self.req.form['vid'] = 'copy' |
|
402 self.env.app.publish('view', self.req) |
|
403 rset = self.req.execute('EUser P WHERE P surname "Boom"') |
|
404 self.assertEquals(len(rset), 0) |
|
405 finally: |
|
406 p.__class__.skip_copy_for = old_skips |
|
407 |
|
408 |
|
409 if __name__ == '__main__': |
|
410 from logilab.common.testlib import unittest_main |
|
411 unittest_main() |