unittest_views_basecontrollers.py 38.3 KB
Newer Older
1
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2
3
4
5
6
7
8
9
10
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
12
13
14
15
16
17
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
18
"""cubicweb.web.views.basecontrollers unit tests"""
Adrien Di Mascio's avatar
Adrien Di Mascio committed
19

20
21
22
23
24
25
from urlparse import urlsplit, urlunsplit, urljoin
# parse_qs is deprecated in cgi and has been moved to urlparse in Python 2.6
try:
    from urlparse import parse_qs as url_parse_query
except ImportError:
    from cgi import parse_qs as url_parse_query
26
from logilab.common.testlib import unittest_main, mock_object
27
from logilab.common.decorators import monkeypatch
Adrien Di Mascio's avatar
Adrien Di Mascio committed
28

29
30
from cubicweb import Binary, NoSelectableObject, ValidationError
from cubicweb.view import STRICT_DOCTYPE
31
from cubicweb.devtools.testlib import CubicWebTC
32
from cubicweb.utils import json_dumps
Sylvain Thénault's avatar
Sylvain Thénault committed
33
from cubicweb.uilib import rql_for_eid
34
from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError, RemoteCallFailed
35
36
import cubicweb.server.session
from cubicweb.server.session import Transaction as OldTransaction
37
from cubicweb.entities.authobjs import CWUser
Sylvain Thénault's avatar
Sylvain Thénault committed
38
from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
39
40
from cubicweb.web.views.basecontrollers import JSonController, xhtmlize, jsonize
from cubicweb.web.views.ajaxcontroller import ajaxfunc, AjaxFunction
41
import cubicweb.transaction as tx
42

Sylvain Thénault's avatar
Sylvain Thénault committed
43
44
u = unicode

Sylvain Thénault's avatar
Sylvain Thénault committed
45
46
def req_form(user):
    return {'eid': [str(user.eid)],
47
            '_cw_entity_fields:%s' % user.eid: '_cw_generic_field',
Sylvain Thénault's avatar
Sylvain Thénault committed
48
49
            '__type:%s' % user.eid: user.__regid__
            }
Adrien Di Mascio's avatar
Adrien Di Mascio committed
50

51
class EditControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
52
    def setUp(self):
53
        CubicWebTC.setUp(self)
54
        self.assertTrue('users' in self.schema.eschema('CWGroup').get_groups('read'))
55

Adrien Di Mascio's avatar
Adrien Di Mascio committed
56
    def tearDown(self):
57
        CubicWebTC.tearDown(self)
58
        self.assertTrue('users' in self.schema.eschema('CWGroup').get_groups('read'))
59

Adrien Di Mascio's avatar
Adrien Di Mascio committed
60
61
62
    def test_noparam_edit(self):
        """check behaviour of this controller without any form parameter
        """
63
64
65
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(self.request())
        self.assertEqual(cm.exception.errors, {None: u'no selected entities'})
66

Adrien Di Mascio's avatar
Adrien Di Mascio committed
67
68
    def test_validation_unique(self):
        """test creation of two linked entities
69
        """
Adrien Di Mascio's avatar
Adrien Di Mascio committed
70
        user = self.user()
71
72
        req = self.request()
        req.form = {'eid': 'X', '__type:X': 'CWUser',
73
                    '_cw_entity_fields:X': 'login-subject,upassword-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
74
75
76
                    'login-subject:X': u'admin',
                    'upassword-subject:X': u'toto',
                    'upassword-subject-confirm:X': u'toto',
77
                    }
78
79
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
80
        cm.exception.translate(unicode)
81
        self.assertEqual(cm.exception.errors, {'login-subject': 'the value "admin" is already used, use another one'})
Adrien Di Mascio's avatar
Adrien Di Mascio committed
82
83
84
85
86

    def test_user_editing_itself(self):
        """checking that a manager user can edit itself
        """
        user = self.user()
Sylvain Thénault's avatar
Sylvain Thénault committed
87
        basegroups = [u(eid) for eid, in self.execute('CWGroup G WHERE X in_group G, X eid %(x)s', {'x': user.eid})]
88
        groupeids = [eid for eid, in self.execute('CWGroup G WHERE G name in ("managers", "users")')]
Sylvain Thénault's avatar
Sylvain Thénault committed
89
        groups = [u(eid) for eid in groupeids]
90
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
91
        eid = u(user.eid)
92
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
93
            'eid': eid, '__type:'+eid: 'CWUser',
94
            '_cw_entity_fields:'+eid: 'login-subject,firstname-subject,surname-subject,in_group-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
95
96
97
98
            'login-subject:'+eid:     u(user.login),
            'surname-subject:'+eid: u'Th\xe9nault',
            'firstname-subject:'+eid:   u'Sylvain',
            'in_group-subject:'+eid:  groups,
Adrien Di Mascio's avatar
Adrien Di Mascio committed
99
            }
100
        path, params = self.expect_redirect_handle_request(req, 'edit')
101
        e = self.execute('Any X WHERE X eid %(x)s', {'x': user.eid}).get_entity(0, 0)
102
103
104
105
        self.assertEqual(e.firstname, u'Sylvain')
        self.assertEqual(e.surname, u'Th\xe9nault')
        self.assertEqual(e.login, user.login)
        self.assertEqual([g.eid for g in e.in_group], groupeids)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
106
107
108

    def test_user_can_change_its_password(self):
        req = self.request()
109
110
        user = self.create_user(req, 'user')
        cnx = self.login('user')
Sylvain Thénault's avatar
Sylvain Thénault committed
111
        eid = u(user.eid)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
112
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
113
114
            'eid': eid, '__maineid' : eid,
            '__type:'+eid: 'CWUser',
115
            '_cw_entity_fields:'+eid: 'upassword-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
116
117
            'upassword-subject:'+eid: 'tournicoton',
            'upassword-subject-confirm:'+eid: 'tournicoton',
Adrien Di Mascio's avatar
Adrien Di Mascio committed
118
            }
119
        path, params = self.expect_redirect_handle_request(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
120
        cnx.commit() # commit to check we don't get late validation error for instance
121
        self.assertEqual(path, 'cwuser/user')
122
        self.assertFalse('vid' in params)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
123

Sylvain Thénault's avatar
Sylvain Thénault committed
124
    def test_user_editing_itself_no_relation(self):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
125
126
127
128
        """checking we can edit an entity without specifying some required
        relations (meaning no changes)
        """
        user = self.user()
Sylvain Thénault's avatar
Sylvain Thénault committed
129
        groupeids = [g.eid for g in user.in_group]
130
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
131
        eid = u(user.eid)
132
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
133
134
            'eid':       eid,
            '__type:'+eid:    'CWUser',
135
            '_cw_entity_fields:'+eid: 'login-subject,firstname-subject,surname-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
136
137
138
            'login-subject:'+eid:     u(user.login),
            'firstname-subject:'+eid: u'Th\xe9nault',
            'surname-subject:'+eid:   u'Sylvain',
Adrien Di Mascio's avatar
Adrien Di Mascio committed
139
            }
140
        path, params = self.expect_redirect_handle_request(req, 'edit')
141
        e = self.execute('Any X WHERE X eid %(x)s', {'x': user.eid}).get_entity(0, 0)
142
143
144
145
146
        self.assertEqual(e.login, user.login)
        self.assertEqual(e.firstname, u'Th\xe9nault')
        self.assertEqual(e.surname, u'Sylvain')
        self.assertEqual([g.eid for g in e.in_group], groupeids)
        self.assertEqual(e.cw_adapt_to('IWorkflowable').state, 'activated')
147
148


Adrien Di Mascio's avatar
Adrien Di Mascio committed
149
    def test_create_multiple_linked(self):
150
        gueid = self.execute('CWGroup G WHERE G name "users"')[0][0]
151
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
152
        req.form = {'eid': ['X', 'Y'], '__maineid' : 'X',
153

154
                    '__type:X': 'CWUser',
155
                    '_cw_entity_fields:X': 'login-subject,upassword-subject,surname-subject,in_group-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
156
157
158
159
                    'login-subject:X': u'adim',
                    'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
                    'surname-subject:X': u'Di Mascio',
                    'in_group-subject:X': u(gueid),
160

161
                    '__type:Y': 'EmailAddress',
162
                    '_cw_entity_fields:Y': 'address-subject,use_email-object',
Sylvain Thénault's avatar
Sylvain Thénault committed
163
164
                    'address-subject:Y': u'dima@logilab.fr',
                    'use_email-object:Y': 'X',
165
                    }
166
        path, params = self.expect_redirect_handle_request(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
167
        # should be redirected on the created person
168
        self.assertEqual(path, 'cwuser/adim')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
169
        e = self.execute('Any P WHERE P surname "Di Mascio"').get_entity(0, 0)
170
        self.assertEqual(e.surname, 'Di Mascio')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
171
        email = e.use_email[0]
172
        self.assertEqual(email.address, 'dima@logilab.fr')
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    def test_create_mandatory_inlined(self):
        req = self.request()
        req.form = {'eid': ['X', 'Y'], '__maineid' : 'X',

                    '__type:X': 'Salesterm',
                    '_cw_entity_fields:X': '',

                    '__type:Y': 'File',
                    '_cw_entity_fields:Y': 'data-subject,described_by_test-object',
                    'data-subject:Y': (u'coucou.txt', Binary('coucou')),
                    'described_by_test-object:Y': 'X',
                    }
        path, params = self.expect_redirect_handle_request(req, 'edit')
        self.assertTrue(path.startswith('salesterm/'), path)
        eid = path.split('/')[1]
        salesterm = req.entity_from_eid(eid)
        # The NOT NULL constraint of mandatory relation implies that the File
        # must be created before the Salesterm, otherwise Salesterm insertion
        # will fail.
        # NOTE: sqlite does have NOT NULL constraint, unlike Postgres so the
        # insertion does not fail and we have to check dumbly that File is
        # created before.
        self.assertGreater(salesterm.eid, salesterm.described_by_test[0].eid)

Adrien Di Mascio's avatar
Adrien Di Mascio committed
198
    def test_edit_multiple_linked(self):
199
        req = self.request()
200
        peid = u(self.create_user(req, 'adim').eid)
Sylvain Thénault's avatar
Sylvain Thénault committed
201
        req.form = {'eid': [peid, 'Y'], '__maineid': peid,
202

Sylvain Thénault's avatar
Sylvain Thénault committed
203
                    '__type:'+peid: u'CWUser',
204
                    '_cw_entity_fields:'+peid: u'surname-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
205
                    'surname-subject:'+peid: u'Di Masci',
206

Sylvain Thénault's avatar
Sylvain Thénault committed
207
                    '__type:Y': u'EmailAddress',
208
                    '_cw_entity_fields:Y': u'address-subject,use_email-object',
Sylvain Thénault's avatar
Sylvain Thénault committed
209
210
                    'address-subject:Y': u'dima@logilab.fr',
                    'use_email-object:Y': peid,
211
                    }
212
        path, params = self.expect_redirect_handle_request(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
213
        # should be redirected on the created person
214
        self.assertEqual(path, 'cwuser/adim')
Sylvain Thénault's avatar
Sylvain Thénault committed
215
        e = self.execute('Any P WHERE P surname "Di Masci"').get_entity(0, 0)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
216
        email = e.use_email[0]
217
        self.assertEqual(email.address, 'dima@logilab.fr')
218

Sylvain Thénault's avatar
Sylvain Thénault committed
219
        emaileid = u(email.eid)
220
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
221
222
223
        req.form = {'eid': [peid, emaileid],

                    '__type:'+peid: u'CWUser',
224
                    '_cw_entity_fields:'+peid: u'surname-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
225
226
227
                    'surname-subject:'+peid: u'Di Masci',

                    '__type:'+emaileid: u'EmailAddress',
228
                    '_cw_entity_fields:'+emaileid: u'address-subject,use_email-object',
Sylvain Thénault's avatar
Sylvain Thénault committed
229
230
231
                    'address-subject:'+emaileid: u'adim@logilab.fr',
                    'use_email-object:'+emaileid: peid,
                    }
232
        path, params = self.expect_redirect_handle_request(req, 'edit')
233
        email.cw_clear_all_caches()
234
        self.assertEqual(email.address, 'adim@logilab.fr')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
235

236

Adrien Di Mascio's avatar
Adrien Di Mascio committed
237
238
    def test_password_confirm(self):
        """test creation of two linked entities
239
        """
Adrien Di Mascio's avatar
Adrien Di Mascio committed
240
        user = self.user()
241
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
242
243
        req.form = {'eid': 'X',
                    '__cloned_eid:X': u(user.eid), '__type:X': 'CWUser',
244
                    '_cw_entity_fields:X': 'login-subject,upassword-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
245
246
                    'login-subject:X': u'toto',
                    'upassword-subject:X': u'toto',
247
                    }
248
249
250
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        self.assertEqual(cm.exception.errors, {'upassword-subject': u'password and confirmation don\'t match'})
251
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
252
        req.form = {'__cloned_eid:X': u(user.eid),
253
                    'eid': 'X', '__type:X': 'CWUser',
254
                    '_cw_entity_fields:X': 'login-subject,upassword-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
255
256
257
                    'login-subject:X': u'toto',
                    'upassword-subject:X': u'toto',
                    'upassword-subject-confirm:X': u'tutu',
258
                    }
259
260
261
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        self.assertEqual(cm.exception.errors, {'upassword-subject': u'password and confirmation don\'t match'})
Adrien Di Mascio's avatar
Adrien Di Mascio committed
262
263
264


    def test_interval_bound_constraint_success(self):
Sylvain Thénault's avatar
Sylvain Thénault committed
265
        feid = self.execute('INSERT File X: X data_name "toto.txt", X data %(data)s',
Adrien Di Mascio's avatar
Adrien Di Mascio committed
266
                            {'data': Binary('yo')})[0][0]
267
        self.commit()
268
        req = self.request(rollbackfirst=True)
269
270
        req.form = {'eid': ['X'],
                    '__type:X': 'Salesterm',
271
                    '_cw_entity_fields:X': 'amount-subject,described_by_test-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
272
273
                    'amount-subject:X': u'-10',
                    'described_by_test-subject:X': u(feid),
274
                }
275
276
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
277
        cm.exception.translate(unicode)
278
        self.assertEqual(cm.exception.errors, {'amount-subject': 'value -10 must be >= 0'})
279
        req = self.request(rollbackfirst=True)
280
281
        req.form = {'eid': ['X'],
                    '__type:X': 'Salesterm',
282
                    '_cw_entity_fields:X': 'amount-subject,described_by_test-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
283
284
                    'amount-subject:X': u'110',
                    'described_by_test-subject:X': u(feid),
285
                    }
286
287
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
288
        cm.exception.translate(unicode)
289
        self.assertEqual(cm.exception.errors, {'amount-subject': 'value 110 must be <= 100'})
290
        req = self.request(rollbackfirst=True)
291
292
        req.form = {'eid': ['X'],
                    '__type:X': 'Salesterm',
293
                    '_cw_entity_fields:X': 'amount-subject,described_by_test-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
294
295
                    'amount-subject:X': u'10',
                    'described_by_test-subject:X': u(feid),
296
                    }
297
        self.expect_redirect_handle_request(req, 'edit')
298
        # should be redirected on the created
Adrien Di Mascio's avatar
Adrien Di Mascio committed
299
300
        #eid = params['rql'].split()[-1]
        e = self.execute('Salesterm X').get_entity(0, 0)
301
        self.assertEqual(e.amount, 10)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
302
303
304

    def test_req_pending_insert(self):
        """make sure req's pending insertions are taken into account"""
Sylvain Thénault's avatar
Sylvain Thénault committed
305
        tmpgroup = self.request().create_entity('CWGroup', name=u"test")
Adrien Di Mascio's avatar
Adrien Di Mascio committed
306
        user = self.user()
Sylvain Thénault's avatar
Sylvain Thénault committed
307
        req = self.request(**req_form(user))
308
        req.session.data['pending_insert'] = set([(user.eid, 'in_group', tmpgroup.eid)])
309
        path, params = self.expect_redirect_handle_request(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
310
311
        usergroups = [gname for gname, in
                      self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})]
312
313
        self.assertItemsEqual(usergroups, ['managers', 'test'])
        self.assertEqual(get_pending_inserts(req), [])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
314
315
316
317
318


    def test_req_pending_delete(self):
        """make sure req's pending deletions are taken into account"""
        user = self.user()
319
        groupeid = self.execute('INSERT CWGroup G: G name "test", U in_group G WHERE U eid %(x)s',
Adrien Di Mascio's avatar
Adrien Di Mascio committed
320
321
322
323
                                {'x': user.eid})[0][0]
        usergroups = [gname for gname, in
                      self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})]
        # just make sure everything was set correctly
324
        self.assertItemsEqual(usergroups, ['managers', 'test'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
325
        # now try to delete the relation
Sylvain Thénault's avatar
Sylvain Thénault committed
326
        req = self.request(**req_form(user))
327
        req.session.data['pending_delete'] = set([(user.eid, 'in_group', groupeid)])
328
        path, params = self.expect_redirect_handle_request(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
329
330
        usergroups = [gname for gname, in
                      self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})]
331
332
        self.assertItemsEqual(usergroups, ['managers'])
        self.assertEqual(get_pending_deletes(req), [])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
333
334
335

    def test_redirect_apply_button(self):
        redirectrql = rql_for_eid(4012) # whatever
336
337
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
338
            'eid': 'A', '__maineid' : 'A',
339
            '__type:A': 'BlogEntry', '_cw_entity_fields:A': 'content-subject,title-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
340
341
            'content-subject:A': u'"13:03:43"',
            'title-subject:A': u'huuu',
342
343
344
345
346
347
            '__redirectrql': redirectrql,
            '__redirectvid': 'primary',
            '__redirectparams': 'toto=tutu&tata=titi',
            '__form_id': 'edition',
            '__action_apply': '',
            }
348
        path, params = self.expect_redirect_handle_request(req, 'edit')
349
        self.assertTrue(path.startswith('blogentry/'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
350
        eid = path.split('/')[1]
351
        self.assertEqual(params['vid'], 'edition')
352
        self.assertNotEqual(int(eid), 4012)
353
354
355
        self.assertEqual(params['__redirectrql'], redirectrql)
        self.assertEqual(params['__redirectvid'], 'primary')
        self.assertEqual(params['__redirectparams'], 'toto=tutu&tata=titi')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
356
357
358

    def test_redirect_ok_button(self):
        redirectrql = rql_for_eid(4012) # whatever
359
360
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
361
            'eid': 'A', '__maineid' : 'A',
362
            '__type:A': 'BlogEntry', '_cw_entity_fields:A': 'content-subject,title-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
363
364
            'content-subject:A': u'"13:03:43"',
            'title-subject:A': u'huuu',
365
366
367
368
369
            '__redirectrql': redirectrql,
            '__redirectvid': 'primary',
            '__redirectparams': 'toto=tutu&tata=titi',
            '__form_id': 'edition',
            }
370
        path, params = self.expect_redirect_handle_request(req, 'edit')
371
372
373
374
375
        self.assertEqual(path, 'view')
        self.assertEqual(params['rql'], redirectrql)
        self.assertEqual(params['vid'], 'primary')
        self.assertEqual(params['tata'], 'titi')
        self.assertEqual(params['toto'], 'tutu')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
376
377

    def test_redirect_delete_button(self):
378
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
379
        eid = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
Sylvain Thénault's avatar
Sylvain Thénault committed
380
        req.form = {'eid': u(eid), '__type:%s'%eid: 'BlogEntry',
381
                    '__action_delete': ''}
382
        path, params = self.expect_redirect_handle_request(req, 'edit')
383
        self.assertEqual(path, 'blogentry')
Sylvain Thénault's avatar
Sylvain Thénault committed
384
        self.assertIn('_cwmsgid', params)
Sylvain Thénault's avatar
Sylvain Thénault committed
385
        eid = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
Adrien Di Mascio's avatar
Adrien Di Mascio committed
386
        self.execute('SET X use_email E WHERE E eid %(e)s, X eid %(x)s',
387
                     {'x': self.session.user.eid, 'e': eid})
Adrien Di Mascio's avatar
Adrien Di Mascio committed
388
        self.commit()
Sylvain Thénault's avatar
Sylvain Thénault committed
389
        req = req
Sylvain Thénault's avatar
Sylvain Thénault committed
390
        req.form = {'eid': u(eid), '__type:%s'%eid: 'EmailAddress',
391
                    '__action_delete': ''}
392
        path, params = self.expect_redirect_handle_request(req, 'edit')
393
        self.assertEqual(path, 'cwuser/admin')
Sylvain Thénault's avatar
Sylvain Thénault committed
394
        self.assertIn('_cwmsgid', params)
Sylvain Thénault's avatar
Sylvain Thénault committed
395
396
        eid1 = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
        eid2 = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
397
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
398
        req.form = {'eid': [u(eid1), u(eid2)],
399
400
401
                    '__type:%s'%eid1: 'BlogEntry',
                    '__type:%s'%eid2: 'EmailAddress',
                    '__action_delete': ''}
402
        path, params = self.expect_redirect_handle_request(req, 'edit')
403
        self.assertEqual(path, 'view')
Sylvain Thénault's avatar
Sylvain Thénault committed
404
        self.assertIn('_cwmsgid', params)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
405

406
407
408
409
410
411
412
413
414
415
416
417
    def test_simple_copy(self):
        req = self.request()
        blog = req.create_entity('Blog', title=u'my-blog')
        blogentry = req.create_entity('BlogEntry', title=u'entry1',
                                      content=u'content1', entry_of=blog)
        req = self.request()
        req.form = {'__maineid' : 'X', 'eid': 'X',
                    '__cloned_eid:X': blogentry.eid, '__type:X': 'BlogEntry',
                    '_cw_entity_fields:X': 'title-subject,content-subject',
                    'title-subject:X': u'entry1-copy',
                    'content-subject:X': u'content1',
                    }
418
        self.expect_redirect_handle_request(req, 'edit')
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
        blogentry2 = req.find_one_entity('BlogEntry', title=u'entry1-copy')
        self.assertEqual(blogentry2.entry_of[0].eid, blog.eid)

    def test_skip_copy_for(self):
        req = self.request()
        blog = req.create_entity('Blog', title=u'my-blog')
        blogentry = req.create_entity('BlogEntry', title=u'entry1',
                                      content=u'content1', entry_of=blog)
        blogentry.__class__.cw_skip_copy_for = [('entry_of', 'subject')]
        try:
            req = self.request()
            req.form = {'__maineid' : 'X', 'eid': 'X',
                        '__cloned_eid:X': blogentry.eid, '__type:X': 'BlogEntry',
                        '_cw_entity_fields:X': 'title-subject,content-subject',
                        'title-subject:X': u'entry1-copy',
                        'content-subject:X': u'content1',
                        }
436
            self.expect_redirect_handle_request(req, 'edit')
437
438
439
440
441
442
            blogentry2 = req.find_one_entity('BlogEntry', title=u'entry1-copy')
            # entry_of should not be copied
            self.assertEqual(len(blogentry2.entry_of), 0)
        finally:
            blogentry.__class__.cw_skip_copy_for = []

Adrien Di Mascio's avatar
Adrien Di Mascio committed
443
    def test_nonregr_eetype_etype_editing(self):
444
        """non-regression test checking that a manager user can edit a CWEType entity
Adrien Di Mascio's avatar
Adrien Di Mascio committed
445
        """
446
        groupeids = sorted(eid for eid, in self.execute('CWGroup G WHERE G name in ("managers", "users")'))
Sylvain Thénault's avatar
Sylvain Thénault committed
447
448
449
450
        groups = [u(eid) for eid in groupeids]
        cwetypeeid = self.execute('CWEType X WHERE X name "CWEType"')[0][0]
        basegroups = [u(eid) for eid, in self.execute('CWGroup G WHERE X read_permission G, X eid %(x)s', {'x': cwetypeeid})]
        cwetypeeid = u(cwetypeeid)
451
452
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
453
454
            'eid':      cwetypeeid,
            '__type:'+cwetypeeid:  'CWEType',
455
            '_cw_entity_fields:'+cwetypeeid: 'name-subject,final-subject,description-subject,read_permission-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
456
            'name-subject:'+cwetypeeid:     u'CWEType',
Sylvain Thénault's avatar
Sylvain Thénault committed
457
            'final-subject:'+cwetypeeid:    '',
Sylvain Thénault's avatar
Sylvain Thénault committed
458
459
            'description-subject:'+cwetypeeid:     u'users group',
            'read_permission-subject:'+cwetypeeid:  groups,
460
            }
Adrien Di Mascio's avatar
Adrien Di Mascio committed
461
        try:
462
            path, params = self.expect_redirect_handle_request(req, 'edit')
463
            e = self.execute('Any X WHERE X eid %(x)s', {'x': cwetypeeid}).get_entity(0, 0)
464
465
            self.assertEqual(e.name, 'CWEType')
            self.assertEqual(sorted(g.eid for g in e.read_permission), groupeids)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
466
467
        finally:
            # restore
468
            self.execute('SET X read_permission Y WHERE X name "CWEType", Y eid IN (%s), NOT X read_permission Y' % (','.join(basegroups)))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
469
            self.commit()
470

Adrien Di Mascio's avatar
Adrien Di Mascio committed
471
472
473
474
    def test_nonregr_strange_text_input(self):
        """non-regression test checking text input containing "13:03:43"

        this seems to be postgres (tsearch?) specific
475
        """
476
477
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
478
            'eid': 'A', '__maineid' : 'A',
479
            '__type:A': 'BlogEntry', '_cw_entity_fields:A': 'title-subject,content-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
480
481
            'title-subject:A': u'"13:03:40"',
            'content-subject:A': u'"13:03:43"',}
482
        path, params = self.expect_redirect_handle_request(req, 'edit')
483
        self.assertTrue(path.startswith('blogentry/'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
484
        eid = path.split('/')[1]
485
        e = self.execute('Any C, T WHERE C eid %(x)s, C content T', {'x': eid}).get_entity(0, 0)
486
487
        self.assertEqual(e.title, '"13:03:40"')
        self.assertEqual(e.content, '"13:03:43"')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
488
489
490


    def test_nonregr_multiple_empty_email_addr(self):
491
        gueid = self.execute('CWGroup G WHERE G name "users"')[0][0]
492
493
        req = self.request()
        req.form = {'eid': ['X', 'Y'],
494

495
                    '__type:X': 'CWUser',
496
                    '_cw_entity_fields:X': 'login-subject,upassword-subject,in_group-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
497
498
499
                    'login-subject:X': u'adim',
                    'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
                    'in_group-subject:X': `gueid`,
500

501
                    '__type:Y': 'EmailAddress',
502
                    '_cw_entity_fields:Y': 'address-subject,alias-subject,use_email-object',
Sylvain Thénault's avatar
Sylvain Thénault committed
503
504
505
                    'address-subject:Y': u'',
                    'alias-subject:Y': u'',
                    'use_email-object:Y': 'X',
506
                    }
507
508
509
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        self.assertEqual(cm.exception.errors, {'address-subject': u'required field'})
Adrien Di Mascio's avatar
Adrien Di Mascio committed
510
511
512

    def test_nonregr_copy(self):
        user = self.user()
513
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
514
515
        req.form = {'__maineid' : 'X', 'eid': 'X',
                    '__cloned_eid:X': user.eid, '__type:X': 'CWUser',
516
                    '_cw_entity_fields:X': 'login-subject,upassword-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
517
518
                    'login-subject:X': u'toto',
                    'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
519
                    }
520
        path, params = self.expect_redirect_handle_request(req, 'edit')
521
        self.assertEqual(path, 'cwuser/toto')
522
        e = self.execute('Any X WHERE X is CWUser, X login "toto"').get_entity(0, 0)
523
524
        self.assertEqual(e.login, 'toto')
        self.assertEqual(e.in_group[0].name, 'managers')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
525
526
527


    def test_nonregr_rollback_on_validation_error(self):
528
529
        req = self.request()
        p = self.create_user(req, "doe")
Adrien Di Mascio's avatar
Adrien Di Mascio committed
530
531
532
533
        # do not try to skip 'primary_email' for this test
        old_skips = p.__class__.skip_copy_for
        p.__class__.skip_copy_for = ()
        try:
Sylvain Thénault's avatar
Sylvain Thénault committed
534
            e = self.request().create_entity('EmailAddress', address=u'doe@doe.com')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
535
536
            self.execute('SET P use_email E, P primary_email E WHERE P eid %(p)s, E eid %(e)s',
                         {'p' : p.eid, 'e' : e.eid})
537
            req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
538
539
            req.form = {'eid': 'X',
                        '__cloned_eid:X': p.eid, '__type:X': 'CWUser',
540
                        '_cw_entity_fields:X': 'login-subject,surname-subject',
Sylvain Thénault's avatar
Sylvain Thénault committed
541
542
                        'login-subject': u'dodo',
                        'surname-subject:X': u'Boom',
543
                        '__errorurl' : "whatever but required",
Sylvain Thénault's avatar
Sylvain Thénault committed
544
                        }
Adrien Di Mascio's avatar
Adrien Di Mascio committed
545
546
547
548
549
            # try to emulate what really happens in the web application
            # 1/ validate form => EditController.publish raises a ValidationError
            #    which fires a Redirect
            # 2/ When re-publishing the copy form, the publisher implicitly commits
            try:
550
                self.app_handle_request(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
551
            except Redirect:
Sylvain Thénault's avatar
Sylvain Thénault committed
552
                req = self.request()
553
554
                req.form['rql'] = 'Any X WHERE X eid %s' % p.eid
                req.form['vid'] = 'copy'
555
                self.app_handle_request(req, 'view')
556
            rset = self.execute('CWUser P WHERE P surname "Boom"')
557
            self.assertEqual(len(rset), 0)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
558
559
560
561
        finally:
            p.__class__.skip_copy_for = old_skips


562
class ReportBugControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
563

564
    def test_usable_by_guest(self):
565
        self.login('anon')
Sylvain Thénault's avatar
Sylvain Thénault committed
566
567
568
        self.assertRaises(NoSelectableObject,
                          self.vreg['controllers'].select, 'reportbug', self.request())
        self.vreg['controllers'].select('reportbug', self.request(description='hop'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
569
570


571
572
class AjaxControllerTC(CubicWebTC):
    tested_controller = 'ajax'
Adrien Di Mascio's avatar
Adrien Di Mascio committed
573
574
575

    def ctrl(self, req=None):
        req = req or self.request(url='http://whatever.fr/')
576
        return self.vreg['controllers'].select(self.tested_controller, req)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
577
578

    def setup_database(self):
Sylvain Thénault's avatar
Sylvain Thénault committed
579
580
581
        req = self.request()
        self.pytag = req.create_entity('Tag', name=u'python')
        self.cubicwebtag = req.create_entity('Tag', name=u'cubicweb')
582
        self.john = self.create_user(req, u'John')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
583
584
585
586


    ## tests ##################################################################
    def test_simple_exec(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
587
588
589
590
        req = self.request(rql='CWUser P WHERE P login "John"',
                           pageid='123', fname='view')
        ctrl = self.ctrl(req)
        rset = self.john.as_rset()
591
        rset.req = req
592
        source = ctrl.publish()
593
        self.assertTrue(source.startswith('<div>'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
594

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
595
596
597
#     def test_json_exec(self):
#         rql = 'Any T,N WHERE T is Tag, T name N'
#         ctrl = self.ctrl(self.request(mode='json', rql=rql, pageid='123'))
598
#         self.assertEqual(ctrl.publish(),
599
#                           json_dumps(self.execute(rql).rows))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
600
601
602

    def test_remote_add_existing_tag(self):
        self.remote_call('tag_entity', self.john.eid, ['python'])
603
        self.assertItemsEqual(
604
605
            [tname for tname, in self.execute('Any N WHERE T is Tag, T name N')],
            ['python', 'cubicweb'])
606
        self.assertEqual(
607
608
            self.execute('Any N WHERE T tags P, P is CWUser, T name N').rows,
            [['python']])
609

Adrien Di Mascio's avatar
Adrien Di Mascio committed
610
611
    def test_remote_add_new_tag(self):
        self.remote_call('tag_entity', self.john.eid, ['javascript'])
612
        self.assertItemsEqual(
613
614
            [tname for tname, in self.execute('Any N WHERE T is Tag, T name N')],
            ['python', 'cubicweb', 'javascript'])
615
        self.assertEqual(
616
617
            self.execute('Any N WHERE T tags P, P is CWUser, T name N').rows,
            [['javascript']])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
618
619

    def test_pending_insertion(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
620
        res, req = self.remote_call('add_pending_inserts', [['12', 'tags', '13']])
Sylvain Thénault's avatar
Sylvain Thénault committed
621
        deletes = get_pending_deletes(req)
622
        self.assertEqual(deletes, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
623
        inserts = get_pending_inserts(req)
624
        self.assertEqual(inserts, ['12:tags:13'])
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
625
        res, req = self.remote_call('add_pending_inserts', [['12', 'tags', '14']])
Sylvain Thénault's avatar
Sylvain Thénault committed
626
        deletes = get_pending_deletes(req)
627
        self.assertEqual(deletes, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
628
        inserts = get_pending_inserts(req)
629
        self.assertEqual(inserts, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
630
        inserts = get_pending_inserts(req, 12)
631
        self.assertEqual(inserts, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
632
        inserts = get_pending_inserts(req, 13)
633
        self.assertEqual(inserts, ['12:tags:13'])
Sylvain Thénault's avatar
Sylvain Thénault committed
634
        inserts = get_pending_inserts(req, 14)
635
        self.assertEqual(inserts, ['12:tags:14'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
636
637
638
639
        req.remove_pending_operations()

    def test_pending_deletion(self):
        res, req = self.remote_call('add_pending_delete', ['12', 'tags', '13'])
Sylvain Thénault's avatar
Sylvain Thénault committed
640
        inserts = get_pending_inserts(req)
641
        self.assertEqual(inserts, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
642
        deletes = get_pending_deletes(req)
643
        self.assertEqual(deletes, ['12:tags:13'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
644
        res, req = self.remote_call('add_pending_delete', ['12', 'tags', '14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
645
        inserts = get_pending_inserts(req)
646
        self.assertEqual(inserts, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
647
        deletes = get_pending_deletes(req)
648
        self.assertEqual(deletes, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
649
        deletes = get_pending_deletes(req, 12)
650
        self.assertEqual(deletes, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
651
        deletes = get_pending_deletes(req, 13)
652
        self.assertEqual(deletes, ['12:tags:13'])
Sylvain Thénault's avatar
Sylvain Thénault committed
653
        deletes = get_pending_deletes(req, 14)
654
        self.assertEqual(deletes, ['12:tags:14'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
655
656
657
658
        req.remove_pending_operations()

    def test_remove_pending_operations(self):
        self.remote_call('add_pending_delete', ['12', 'tags', '13'])
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
659
        _, req = self.remote_call('add_pending_inserts', [['12', 'tags', '14']])
Sylvain Thénault's avatar
Sylvain Thénault committed
660
        inserts = get_pending_inserts(req)
661
        self.assertEqual(inserts, ['12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
662
        deletes = get_pending_deletes(req)
663
        self.assertEqual(deletes, ['12:tags:13'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
664
        req.remove_pending_operations()
665
666
        self.assertEqual(get_pending_deletes(req), [])
        self.assertEqual(get_pending_inserts(req), [])
667

Adrien Di Mascio's avatar
Adrien Di Mascio committed
668
669
670
671

    def test_add_inserts(self):
        res, req = self.remote_call('add_pending_inserts',
                                    [('12', 'tags', '13'), ('12', 'tags', '14')])
Sylvain Thénault's avatar
Sylvain Thénault committed
672
        inserts = get_pending_inserts(req)
673
        self.assertEqual(inserts, ['12:tags:13', '12:tags:14'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
674
        req.remove_pending_operations()
675

Adrien Di Mascio's avatar
Adrien Di Mascio committed
676
677
678

    # silly tests
    def test_external_resource(self):
679
        self.assertEqual(self.remote_call('external_resource', 'RSS_LOGO')[0],
680
                          json_dumps(self.config.uiprops['RSS_LOGO']))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
681
    def test_i18n(self):
682
        self.assertEqual(self.remote_call('i18n', ['bimboom'])[0],
683
                          json_dumps(['bimboom']))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
684
685

    def test_format_date(self):
686
        self.assertEqual(self.remote_call('format_date', '2007-01-01 12:00:00')[0],
687
                          json_dumps('2007/01/01'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
688

689
690
691
692
    def test_ajaxfunc_noparameter(self):
        @ajaxfunc
        def foo(self, x, y):
            return 'hello'
693
694
695
696
697
698
        self.assertEqual(foo(object, 1, 2), 'hello')
        appobject = foo.__appobject__
        self.assertTrue(issubclass(appobject, AjaxFunction))
        self.assertEqual(appobject.__regid__, 'foo')
        self.assertEqual(appobject.check_pageid, False)
        self.assertEqual(appobject.output_type, None)
699
        req = self.request()
700
        f = appobject(req)
701
702
703
        self.assertEqual(f(12, 13), 'hello')

    def test_ajaxfunc_checkpageid(self):
704
        @ajaxfunc(check_pageid=True)
705
        def foo(self, x, y):
706
707
708
709
710
711
712
            return 'hello'
        self.assertEqual(foo(object, 1, 2), 'hello')
        appobject = foo.__appobject__
        self.assertTrue(issubclass(appobject, AjaxFunction))
        self.assertEqual(appobject.__regid__, 'foo')
        self.assertEqual(appobject.check_pageid, True)
        self.assertEqual(appobject.output_type, None)
713
714
        # no pageid
        req = self.request()
715
        f = appobject(req)
716
717
718
719
720
721
        self.assertRaises(RemoteCallFailed, f, 12, 13)

    def test_ajaxfunc_json(self):
        @ajaxfunc(output_type='json')
        def foo(self, x, y):
            return x + y
722
723
724
725
726
727
        self.assertEqual(foo(object, 1, 2), 3)
        appobject = foo.__appobject__
        self.assertTrue(issubclass(appobject, AjaxFunction))
        self.assertEqual(appobject.__regid__, 'foo')
        self.assertEqual(appobject.check_pageid, False)
        self.assertEqual(appobject.output_type, 'json')
728
729
        # no pageid
        req = self.request()
730
        f = appobject(req)
731
732
733
734
735
736
737
        self.assertEqual(f(12, 13), '25')


class JSonControllerTC(AjaxControllerTC):
    # NOTE: this class performs the same tests as AjaxController but with
    #       deprecated 'json' controller (i.e. check backward compatibility)
    tested_controller = 'json'
Adrien Di Mascio's avatar
Adrien Di Mascio committed
738

739
740
741
742
    def setUp(self):
        super(JSonControllerTC, self).setUp()
        self.exposed_remote_funcs = [fname for fname in dir(JSonController)
                                     if fname.startswith('js_')]
743

744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
    def tearDown(self):
        super(JSonControllerTC, self).tearDown()
        for funcname in dir(JSonController):
            # remove functions added dynamically during tests
            if funcname.startswith('js_') and funcname not in self.exposed_remote_funcs:
                delattr(JSonController, funcname)

    def test_monkeypatch_jsoncontroller(self):
        self.assertRaises(RemoteCallFailed, self.remote_call, 'foo')
        @monkeypatch(JSonController)
        def js_foo(self):
            return u'hello'
        res, req = self.remote_call('foo')
        self.assertEqual(res, u'hello')

    def test_monkeypatch_jsoncontroller_xhtmlize(self):
        self.assertRaises(RemoteCallFailed, self.remote_call, 'foo')
        @monkeypatch(JSonController)
        @xhtmlize
        def js_foo(self):
            return u'hello'
        res, req = self.remote_call('foo')
766
        self.assertEqual(u'<div>hello</div>', res)
767
768
769
770
771
772
773
774
775

    def test_monkeypatch_jsoncontroller_jsonize(self):
        self.assertRaises(RemoteCallFailed, self.remote_call, 'foo')
        @monkeypatch(JSonController)
        @jsonize
        def js_foo(self):
            return 12
        res, req = self.remote_call('foo')
        self.assertEqual(res, '12')
776
777
778
779
780
781
782
783

    def test_monkeypatch_jsoncontroller_stdfunc(self):
        @monkeypatch(JSonController)
        @jsonize
        def js_reledit_form(self):
            return 12