unittest_views_basecontrollers.py 29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# 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
from __future__ import with_statement

22
from logilab.common.testlib import unittest_main, mock_object
Adrien Di Mascio's avatar
Adrien Di Mascio committed
23

24
25
from cubicweb import Binary, NoSelectableObject, ValidationError
from cubicweb.view import STRICT_DOCTYPE
26
from cubicweb.devtools.testlib import CubicWebTC
27
from cubicweb.utils import json_dumps
Sylvain Thénault's avatar
Sylvain Thénault committed
28
from cubicweb.uilib import rql_for_eid
29
from cubicweb.web import INTERNAL_FIELD_VALUE, Redirect, RequestError
30
from cubicweb.entities.authobjs import CWUser
Sylvain Thénault's avatar
Sylvain Thénault committed
31
from cubicweb.web.views.autoform import get_pending_inserts, get_pending_deletes
Sylvain Thénault's avatar
Sylvain Thénault committed
32
33
u = unicode

Sylvain Thénault's avatar
Sylvain Thénault committed
34
35
36
37
38
def req_form(user):
    return {'eid': [str(user.eid)],
            '_cw_edited_fields:%s' % user.eid: '_cw_generic_field',
            '__type:%s' % user.eid: user.__regid__
            }
Adrien Di Mascio's avatar
Adrien Di Mascio committed
39

40
class EditControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
41
    def setUp(self):
42
        CubicWebTC.setUp(self)
43
        self.failUnless('users' in self.schema.eschema('CWGroup').get_groups('read'))
44

Adrien Di Mascio's avatar
Adrien Di Mascio committed
45
    def tearDown(self):
46
        CubicWebTC.tearDown(self)
47
        self.failUnless('users' in self.schema.eschema('CWGroup').get_groups('read'))
48

Adrien Di Mascio's avatar
Adrien Di Mascio committed
49
50
51
    def test_noparam_edit(self):
        """check behaviour of this controller without any form parameter
        """
52
53
54
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(self.request())
        self.assertEqual(cm.exception.errors, {None: u'no selected entities'})
55

Adrien Di Mascio's avatar
Adrien Di Mascio committed
56
57
    def test_validation_unique(self):
        """test creation of two linked entities
58
        """
Adrien Di Mascio's avatar
Adrien Di Mascio committed
59
        user = self.user()
60
61
        req = self.request()
        req.form = {'eid': 'X', '__type:X': 'CWUser',
Sylvain Thénault's avatar
Sylvain Thénault committed
62
63
64
65
                    '_cw_edited_fields:X': 'login-subject,upassword-subject',
                    'login-subject:X': u'admin',
                    'upassword-subject:X': u'toto',
                    'upassword-subject-confirm:X': u'toto',
66
                    }
67
68
69
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        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
70
71
72
73
74

    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
75
        basegroups = [u(eid) for eid, in self.execute('CWGroup G WHERE X in_group G, X eid %(x)s', {'x': user.eid})]
76
        groupeids = [eid for eid, in self.execute('CWGroup G WHERE G name in ("managers", "users")')]
Sylvain Thénault's avatar
Sylvain Thénault committed
77
        groups = [u(eid) for eid in groupeids]
78
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
79
        eid = u(user.eid)
80
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
81
82
83
84
85
86
            'eid': eid, '__type:'+eid: 'CWUser',
            '_cw_edited_fields:'+eid: 'login-subject,firstname-subject,surname-subject,in_group-subject',
            '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
87
            }
Sylvain Thénault's avatar
Sylvain Thénault committed
88
        path, params = self.expect_redirect_publish(req, 'edit')
89
        e = self.execute('Any X WHERE X eid %(x)s', {'x': user.eid}).get_entity(0, 0)
90
91
92
93
        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
94
95
96
97
98

    def test_user_can_change_its_password(self):
        user = self.create_user('user')
        cnx = self.login('user')
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
99
        eid = u(user.eid)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
100
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
101
102
103
104
105
            'eid': eid, '__maineid' : eid,
            '__type:'+eid: 'CWUser',
            '_cw_edited_fields:'+eid: 'upassword-subject',
            'upassword-subject:'+eid: 'tournicoton',
            'upassword-subject-confirm:'+eid: 'tournicoton',
Adrien Di Mascio's avatar
Adrien Di Mascio committed
106
            }
Sylvain Thénault's avatar
Sylvain Thénault committed
107
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
108
        cnx.commit() # commit to check we don't get late validation error for instance
109
        self.assertEqual(path, 'cwuser/user')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
110
111
        self.failIf('vid' in params)

Sylvain Thénault's avatar
Sylvain Thénault committed
112
    def test_user_editing_itself_no_relation(self):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
113
114
115
116
        """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
117
        groupeids = [g.eid for g in user.in_group]
118
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
119
        eid = u(user.eid)
120
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
121
122
123
124
125
126
            'eid':       eid,
            '__type:'+eid:    'CWUser',
            '_cw_edited_fields:'+eid: 'login-subject,firstname-subject,surname-subject',
            '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
127
            }
Sylvain Thénault's avatar
Sylvain Thénault committed
128
        path, params = self.expect_redirect_publish(req, 'edit')
129
        e = self.execute('Any X WHERE X eid %(x)s', {'x': user.eid}).get_entity(0, 0)
130
131
132
133
134
        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')
135
136


Adrien Di Mascio's avatar
Adrien Di Mascio committed
137
    def test_create_multiple_linked(self):
138
        gueid = self.execute('CWGroup G WHERE G name "users"')[0][0]
139
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
140
        req.form = {'eid': ['X', 'Y'], '__maineid' : 'X',
141

142
                    '__type:X': 'CWUser',
Sylvain Thénault's avatar
Sylvain Thénault committed
143
144
145
146
147
                    '_cw_edited_fields:X': 'login-subject,upassword-subject,surname-subject,in_group-subject',
                    '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),
148

149
                    '__type:Y': 'EmailAddress',
Sylvain Thénault's avatar
Sylvain Thénault committed
150
151
152
                    '_cw_edited_fields:Y': 'address-subject,use_email-object',
                    'address-subject:Y': u'dima@logilab.fr',
                    'use_email-object:Y': 'X',
153
                    }
Sylvain Thénault's avatar
Sylvain Thénault committed
154
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
155
        # should be redirected on the created person
156
        self.assertEqual(path, 'cwuser/adim')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
157
        e = self.execute('Any P WHERE P surname "Di Mascio"').get_entity(0, 0)
158
        self.assertEqual(e.surname, 'Di Mascio')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
159
        email = e.use_email[0]
160
        self.assertEqual(email.address, 'dima@logilab.fr')
161

Adrien Di Mascio's avatar
Adrien Di Mascio committed
162
    def test_edit_multiple_linked(self):
Sylvain Thénault's avatar
Sylvain Thénault committed
163
        peid = u(self.create_user('adim').eid)
164
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
165
        req.form = {'eid': [peid, 'Y'], '__maineid': peid,
166

Sylvain Thénault's avatar
Sylvain Thénault committed
167
168
169
                    '__type:'+peid: u'CWUser',
                    '_cw_edited_fields:'+peid: u'surname-subject',
                    'surname-subject:'+peid: u'Di Masci',
170

Sylvain Thénault's avatar
Sylvain Thénault committed
171
172
173
174
                    '__type:Y': u'EmailAddress',
                    '_cw_edited_fields:Y': u'address-subject,use_email-object',
                    'address-subject:Y': u'dima@logilab.fr',
                    'use_email-object:Y': peid,
175
                    }
Sylvain Thénault's avatar
Sylvain Thénault committed
176
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
177
        # should be redirected on the created person
178
        self.assertEqual(path, 'cwuser/adim')
Sylvain Thénault's avatar
Sylvain Thénault committed
179
        e = self.execute('Any P WHERE P surname "Di Masci"').get_entity(0, 0)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
180
        email = e.use_email[0]
181
        self.assertEqual(email.address, 'dima@logilab.fr')
182

Sylvain Thénault's avatar
Sylvain Thénault committed
183
        emaileid = u(email.eid)
184
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
185
186
187
188
189
190
191
192
193
194
195
196
197
        req.form = {'eid': [peid, emaileid],

                    '__type:'+peid: u'CWUser',
                    '_cw_edited_fields:'+peid: u'surname-subject',
                    'surname-subject:'+peid: u'Di Masci',

                    '__type:'+emaileid: u'EmailAddress',
                    '_cw_edited_fields:'+emaileid: u'address-subject,use_email-object',
                    'address-subject:'+emaileid: u'adim@logilab.fr',
                    'use_email-object:'+emaileid: peid,
                    }
        path, params = self.expect_redirect_publish(req, 'edit')
        email.clear_all_caches()
198
        self.assertEqual(email.address, 'adim@logilab.fr')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
199

200

Adrien Di Mascio's avatar
Adrien Di Mascio committed
201
202
    def test_password_confirm(self):
        """test creation of two linked entities
203
        """
Adrien Di Mascio's avatar
Adrien Di Mascio committed
204
        user = self.user()
205
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
206
207
208
209
210
        req.form = {'eid': 'X',
                    '__cloned_eid:X': u(user.eid), '__type:X': 'CWUser',
                    '_cw_edited_fields:X': 'login-subject,upassword-subject',
                    'login-subject:X': u'toto',
                    'upassword-subject:X': u'toto',
211
                    }
212
213
214
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        self.assertEqual(cm.exception.errors, {'upassword-subject': u'password and confirmation don\'t match'})
215
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
216
        req.form = {'__cloned_eid:X': u(user.eid),
217
                    'eid': 'X', '__type:X': 'CWUser',
Sylvain Thénault's avatar
Sylvain Thénault committed
218
219
220
221
                    '_cw_edited_fields:X': 'login-subject,upassword-subject',
                    'login-subject:X': u'toto',
                    'upassword-subject:X': u'toto',
                    'upassword-subject-confirm:X': u'tutu',
222
                    }
223
224
225
        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
226
227
228


    def test_interval_bound_constraint_success(self):
Sylvain Thénault's avatar
Sylvain Thénault committed
229
        feid = self.execute('INSERT File X: X data_name "toto.txt", X data %(data)s',
Adrien Di Mascio's avatar
Adrien Di Mascio committed
230
                            {'data': Binary('yo')})[0][0]
231
        self.commit()
232
        req = self.request(rollbackfirst=True)
233
234
        req.form = {'eid': ['X'],
                    '__type:X': 'Salesterm',
Sylvain Thénault's avatar
Sylvain Thénault committed
235
236
237
                    '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
                    'amount-subject:X': u'-10',
                    'described_by_test-subject:X': u(feid),
238
                }
239
240
241
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        self.assertEqual(cm.exception.errors, {'amount-subject': 'value must be >= 0'})
242
        req = self.request(rollbackfirst=True)
243
244
        req.form = {'eid': ['X'],
                    '__type:X': 'Salesterm',
Sylvain Thénault's avatar
Sylvain Thénault committed
245
246
247
                    '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
                    'amount-subject:X': u'110',
                    'described_by_test-subject:X': u(feid),
248
                    }
249
250
251
        with self.assertRaises(ValidationError) as cm:
            self.ctrl_publish(req)
        self.assertEqual(cm.exception.errors, {'amount-subject': 'value must be <= 100'})
252
        req = self.request(rollbackfirst=True)
253
254
        req.form = {'eid': ['X'],
                    '__type:X': 'Salesterm',
Sylvain Thénault's avatar
Sylvain Thénault committed
255
256
257
                    '_cw_edited_fields:X': 'amount-subject,described_by_test-subject',
                    'amount-subject:X': u'10',
                    'described_by_test-subject:X': u(feid),
258
                    }
Sylvain Thénault's avatar
Sylvain Thénault committed
259
        self.expect_redirect_publish(req, 'edit')
260
        # should be redirected on the created
Adrien Di Mascio's avatar
Adrien Di Mascio committed
261
262
        #eid = params['rql'].split()[-1]
        e = self.execute('Salesterm X').get_entity(0, 0)
263
        self.assertEqual(e.amount, 10)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
264
265
266

    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
267
        tmpgroup = self.request().create_entity('CWGroup', name=u"test")
Adrien Di Mascio's avatar
Adrien Di Mascio committed
268
        user = self.user()
Sylvain Thénault's avatar
Sylvain Thénault committed
269
        req = self.request(**req_form(user))
270
        req.session.data['pending_insert'] = set([(user.eid, 'in_group', tmpgroup.eid)])
Sylvain Thénault's avatar
Sylvain Thénault committed
271
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
272
273
        usergroups = [gname for gname, in
                      self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})]
274
275
        self.assertItemsEqual(usergroups, ['managers', 'test'])
        self.assertEqual(get_pending_inserts(req), [])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
276
277
278
279
280


    def test_req_pending_delete(self):
        """make sure req's pending deletions are taken into account"""
        user = self.user()
281
        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
282
283
284
285
                                {'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
286
        self.assertItemsEqual(usergroups, ['managers', 'test'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
287
        # now try to delete the relation
Sylvain Thénault's avatar
Sylvain Thénault committed
288
        req = self.request(**req_form(user))
289
        req.session.data['pending_delete'] = set([(user.eid, 'in_group', groupeid)])
Sylvain Thénault's avatar
Sylvain Thénault committed
290
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
291
292
        usergroups = [gname for gname, in
                      self.execute('Any N WHERE G name N, U in_group G, U eid %(u)s', {'u': user.eid})]
293
294
        self.assertItemsEqual(usergroups, ['managers'])
        self.assertEqual(get_pending_deletes(req), [])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
295
296
297

    def test_redirect_apply_button(self):
        redirectrql = rql_for_eid(4012) # whatever
298
299
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
300
301
302
303
            'eid': 'A', '__maineid' : 'A',
            '__type:A': 'BlogEntry', '_cw_edited_fields:A': 'content-subject,title-subject',
            'content-subject:A': u'"13:03:43"',
            'title-subject:A': u'huuu',
304
305
306
307
308
309
            '__redirectrql': redirectrql,
            '__redirectvid': 'primary',
            '__redirectparams': 'toto=tutu&tata=titi',
            '__form_id': 'edition',
            '__action_apply': '',
            }
Sylvain Thénault's avatar
Sylvain Thénault committed
310
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
311
312
        self.failUnless(path.startswith('blogentry/'))
        eid = path.split('/')[1]
313
        self.assertEqual(params['vid'], 'edition')
314
        self.assertNotEqual(int(eid), 4012)
315
316
317
        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
318
319
320

    def test_redirect_ok_button(self):
        redirectrql = rql_for_eid(4012) # whatever
321
322
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
323
324
325
326
            'eid': 'A', '__maineid' : 'A',
            '__type:A': 'BlogEntry', '_cw_edited_fields:A': 'content-subject,title-subject',
            'content-subject:A': u'"13:03:43"',
            'title-subject:A': u'huuu',
327
328
329
330
331
            '__redirectrql': redirectrql,
            '__redirectvid': 'primary',
            '__redirectparams': 'toto=tutu&tata=titi',
            '__form_id': 'edition',
            }
Sylvain Thénault's avatar
Sylvain Thénault committed
332
        path, params = self.expect_redirect_publish(req, 'edit')
333
334
335
336
337
        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
338
339

    def test_redirect_delete_button(self):
340
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
341
        eid = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
Sylvain Thénault's avatar
Sylvain Thénault committed
342
        req.form = {'eid': u(eid), '__type:%s'%eid: 'BlogEntry',
343
                    '__action_delete': ''}
Sylvain Thénault's avatar
Sylvain Thénault committed
344
        path, params = self.expect_redirect_publish(req, 'edit')
345
        self.assertEqual(path, 'blogentry')
Sylvain Thénault's avatar
Sylvain Thénault committed
346
        self.assertIn('_cwmsgid', params)
Sylvain Thénault's avatar
Sylvain Thénault committed
347
        eid = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
Adrien Di Mascio's avatar
Adrien Di Mascio committed
348
        self.execute('SET X use_email E WHERE E eid %(e)s, X eid %(x)s',
349
                     {'x': self.session.user.eid, 'e': eid})
Adrien Di Mascio's avatar
Adrien Di Mascio committed
350
        self.commit()
Sylvain Thénault's avatar
Sylvain Thénault committed
351
        req = req
Sylvain Thénault's avatar
Sylvain Thénault committed
352
        req.form = {'eid': u(eid), '__type:%s'%eid: 'EmailAddress',
353
                    '__action_delete': ''}
Sylvain Thénault's avatar
Sylvain Thénault committed
354
        path, params = self.expect_redirect_publish(req, 'edit')
355
        self.assertEqual(path, 'cwuser/admin')
Sylvain Thénault's avatar
Sylvain Thénault committed
356
        self.assertIn('_cwmsgid', params)
Sylvain Thénault's avatar
Sylvain Thénault committed
357
358
        eid1 = req.create_entity('BlogEntry', title=u'hop', content=u'hop').eid
        eid2 = req.create_entity('EmailAddress', address=u'hop@logilab.fr').eid
359
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
360
        req.form = {'eid': [u(eid1), u(eid2)],
361
362
363
                    '__type:%s'%eid1: 'BlogEntry',
                    '__type:%s'%eid2: 'EmailAddress',
                    '__action_delete': ''}
Sylvain Thénault's avatar
Sylvain Thénault committed
364
        path, params = self.expect_redirect_publish(req, 'edit')
365
        self.assertEqual(path, 'view')
Sylvain Thénault's avatar
Sylvain Thénault committed
366
        self.assertIn('_cwmsgid', params)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
367
368

    def test_nonregr_eetype_etype_editing(self):
369
        """non-regression test checking that a manager user can edit a CWEType entity
Adrien Di Mascio's avatar
Adrien Di Mascio committed
370
        """
371
        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
372
373
374
375
        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)
376
377
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
378
379
380
381
            'eid':      cwetypeeid,
            '__type:'+cwetypeeid:  'CWEType',
            '_cw_edited_fields:'+cwetypeeid: 'name-subject,final-subject,description-subject,read_permission-subject',
            'name-subject:'+cwetypeeid:     u'CWEType',
Sylvain Thénault's avatar
Sylvain Thénault committed
382
            'final-subject:'+cwetypeeid:    '',
Sylvain Thénault's avatar
Sylvain Thénault committed
383
384
            'description-subject:'+cwetypeeid:     u'users group',
            'read_permission-subject:'+cwetypeeid:  groups,
385
            }
Adrien Di Mascio's avatar
Adrien Di Mascio committed
386
        try:
Sylvain Thénault's avatar
Sylvain Thénault committed
387
            path, params = self.expect_redirect_publish(req, 'edit')
388
            e = self.execute('Any X WHERE X eid %(x)s', {'x': cwetypeeid}).get_entity(0, 0)
389
390
            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
391
392
        finally:
            # restore
393
            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
394
            self.commit()
395

Adrien Di Mascio's avatar
Adrien Di Mascio committed
396
397
398
399
    def test_nonregr_strange_text_input(self):
        """non-regression test checking text input containing "13:03:43"

        this seems to be postgres (tsearch?) specific
400
        """
401
402
        req = self.request()
        req.form = {
Sylvain Thénault's avatar
Sylvain Thénault committed
403
404
405
406
407
            'eid': 'A', '__maineid' : 'A',
            '__type:A': 'BlogEntry', '_cw_edited_fields:A': 'title-subject,content-subject',
            'title-subject:A': u'"13:03:40"',
            'content-subject:A': u'"13:03:43"',}
        path, params = self.expect_redirect_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
408
409
        self.failUnless(path.startswith('blogentry/'))
        eid = path.split('/')[1]
410
        e = self.execute('Any C, T WHERE C eid %(x)s, C content T', {'x': eid}).get_entity(0, 0)
411
412
        self.assertEqual(e.title, '"13:03:40"')
        self.assertEqual(e.content, '"13:03:43"')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
413
414
415


    def test_nonregr_multiple_empty_email_addr(self):
416
        gueid = self.execute('CWGroup G WHERE G name "users"')[0][0]
417
418
        req = self.request()
        req.form = {'eid': ['X', 'Y'],
419

420
                    '__type:X': 'CWUser',
Sylvain Thénault's avatar
Sylvain Thénault committed
421
422
423
424
                    '_cw_edited_fields:X': 'login-subject,upassword-subject,in_group-subject',
                    'login-subject:X': u'adim',
                    'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
                    'in_group-subject:X': `gueid`,
425

426
                    '__type:Y': 'EmailAddress',
Sylvain Thénault's avatar
Sylvain Thénault committed
427
428
429
430
                    '_cw_edited_fields:Y': 'address-subject,alias-subject,use_email-object',
                    'address-subject:Y': u'',
                    'alias-subject:Y': u'',
                    'use_email-object:Y': 'X',
431
                    }
432
433
434
        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
435
436
437

    def test_nonregr_copy(self):
        user = self.user()
438
        req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
439
440
441
442
443
        req.form = {'__maineid' : 'X', 'eid': 'X',
                    '__cloned_eid:X': user.eid, '__type:X': 'CWUser',
                    '_cw_edited_fields:X': 'login-subject,upassword-subject',
                    'login-subject:X': u'toto',
                    'upassword-subject:X': u'toto', 'upassword-subject-confirm:X': u'toto',
444
                    }
Sylvain Thénault's avatar
Sylvain Thénault committed
445
        path, params = self.expect_redirect_publish(req, 'edit')
446
        self.assertEqual(path, 'cwuser/toto')
447
        e = self.execute('Any X WHERE X is CWUser, X login "toto"').get_entity(0, 0)
448
449
        self.assertEqual(e.login, 'toto')
        self.assertEqual(e.in_group[0].name, 'managers')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
450
451
452
453
454
455
456
457


    def test_nonregr_rollback_on_validation_error(self):
        p = self.create_user("doe")
        # 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
458
            e = self.request().create_entity('EmailAddress', address=u'doe@doe.com')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
459
460
            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})
461
            req = self.request()
Sylvain Thénault's avatar
Sylvain Thénault committed
462
463
464
465
466
            req.form = {'eid': 'X',
                        '__cloned_eid:X': p.eid, '__type:X': 'CWUser',
                        '_cw_edited_fields:X': 'login-subject,surname-subject',
                        'login-subject': u'dodo',
                        'surname-subject:X': u'Boom',
467
                        '__errorurl' : "whatever but required",
Sylvain Thénault's avatar
Sylvain Thénault committed
468
                        }
Adrien Di Mascio's avatar
Adrien Di Mascio committed
469
470
471
472
473
            # 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:
Sylvain Thénault's avatar
Sylvain Thénault committed
474
                self.app_publish(req, 'edit')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
475
            except Redirect:
Sylvain Thénault's avatar
Sylvain Thénault committed
476
                req = self.request()
477
478
                req.form['rql'] = 'Any X WHERE X eid %s' % p.eid
                req.form['vid'] = 'copy'
Sylvain Thénault's avatar
Sylvain Thénault committed
479
                self.app_publish(req, 'view')
480
            rset = self.execute('CWUser P WHERE P surname "Boom"')
481
            self.assertEqual(len(rset), 0)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
482
483
484
485
        finally:
            p.__class__.skip_copy_for = old_skips


486
class EmbedControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
487
488
489
490
491
492

    def test_nonregr_embed_publish(self):
        # This test looks a bit stupid but at least it will probably
        # fail if the controller API changes and if EmbedController is not
        # updated (which is what happened before this test)
        req = self.request()
493
        req.form['url'] = 'http://www.logilab.fr/'
494
        controller = self.vreg['controllers'].select('embed', req)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
495
496
497
        result = controller.publish(rset=None)


498
class ReportBugControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
499
500

    def test_usable_by_guets(self):
501
        self.login('anon')
Sylvain Thénault's avatar
Sylvain Thénault committed
502
503
504
        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
505
506


507
class SendMailControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
508
509

    def test_not_usable_by_guets(self):
Sylvain Thénault's avatar
Sylvain Thénault committed
510
511
512
513
514
515
        self.assertRaises(NoSelectableObject,
                          self.vreg['controllers'].select, 'sendmail', self.request())
        self.vreg['controllers'].select('sendmail',
                                        self.request(subject='toto',
                                                     recipient='toto@logilab.fr',
                                                     mailbody='hop'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
516
        self.login('anon')
517
518
        self.assertRaises(NoSelectableObject,
                          self.vreg['controllers'].select, 'sendmail', self.request())
519

Adrien Di Mascio's avatar
Adrien Di Mascio committed
520
521


522
class JSONControllerTC(CubicWebTC):
Adrien Di Mascio's avatar
Adrien Di Mascio committed
523
524
525

    def ctrl(self, req=None):
        req = req or self.request(url='http://whatever.fr/')
526
        return self.vreg['controllers'].select('json', req)
Adrien Di Mascio's avatar
Adrien Di Mascio committed
527
528

    def setup_database(self):
Sylvain Thénault's avatar
Sylvain Thénault committed
529
530
531
        req = self.request()
        self.pytag = req.create_entity('Tag', name=u'python')
        self.cubicwebtag = req.create_entity('Tag', name=u'cubicweb')
Adrien Di Mascio's avatar
Adrien Di Mascio committed
532
533
534
535
536
        self.john = self.create_user(u'John')


    ## tests ##################################################################
    def test_simple_exec(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
537
538
539
540
        req = self.request(rql='CWUser P WHERE P login "John"',
                           pageid='123', fname='view')
        ctrl = self.ctrl(req)
        rset = self.john.as_rset()
541
        rset.req = req
542
543
544
545
546
547
548
        source = ctrl.publish()
        self.failUnless(source.startswith('<?xml version="1.0"?>\n' + STRICT_DOCTYPE +
                                          u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
                        )
        req.xhtml_browser = lambda: False
        source = ctrl.publish()
        self.failUnless(source.startswith('<div>'))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
549

sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
550
551
552
#     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'))
553
#         self.assertEqual(ctrl.publish(),
554
#                           json_dumps(self.execute(rql).rows))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
555
556
557

    def test_remote_add_existing_tag(self):
        self.remote_call('tag_entity', self.john.eid, ['python'])
558
        self.assertItemsEqual(
559
560
            [tname for tname, in self.execute('Any N WHERE T is Tag, T name N')],
            ['python', 'cubicweb'])
561
        self.assertEqual(
562
563
            self.execute('Any N WHERE T tags P, P is CWUser, T name N').rows,
            [['python']])
564

Adrien Di Mascio's avatar
Adrien Di Mascio committed
565
566
    def test_remote_add_new_tag(self):
        self.remote_call('tag_entity', self.john.eid, ['javascript'])
567
        self.assertItemsEqual(
568
569
            [tname for tname, in self.execute('Any N WHERE T is Tag, T name N')],
            ['python', 'cubicweb', 'javascript'])
570
        self.assertEqual(
571
572
            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
573
574

    def test_pending_insertion(self):
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
575
        res, req = self.remote_call('add_pending_inserts', [['12', 'tags', '13']])
Sylvain Thénault's avatar
Sylvain Thénault committed
576
        deletes = get_pending_deletes(req)
577
        self.assertEqual(deletes, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
578
        inserts = get_pending_inserts(req)
579
        self.assertEqual(inserts, ['12:tags:13'])
sylvain.thenault@logilab.fr's avatar
sylvain.thenault@logilab.fr committed
580
        res, req = self.remote_call('add_pending_inserts', [['12', 'tags', '14']])
Sylvain Thénault's avatar
Sylvain Thénault committed
581
        deletes = get_pending_deletes(req)
582
        self.assertEqual(deletes, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
583
        inserts = get_pending_inserts(req)
584
        self.assertEqual(inserts, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
585
        inserts = get_pending_inserts(req, 12)
586
        self.assertEqual(inserts, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
587
        inserts = get_pending_inserts(req, 13)
588
        self.assertEqual(inserts, ['12:tags:13'])
Sylvain Thénault's avatar
Sylvain Thénault committed
589
        inserts = get_pending_inserts(req, 14)
590
        self.assertEqual(inserts, ['12:tags:14'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
591
592
593
594
        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
595
        inserts = get_pending_inserts(req)
596
        self.assertEqual(inserts, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
597
        deletes = get_pending_deletes(req)
598
        self.assertEqual(deletes, ['12:tags:13'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
599
        res, req = self.remote_call('add_pending_delete', ['12', 'tags', '14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
600
        inserts = get_pending_inserts(req)
601
        self.assertEqual(inserts, [])
Sylvain Thénault's avatar
Sylvain Thénault committed
602
        deletes = get_pending_deletes(req)
603
        self.assertEqual(deletes, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
604
        deletes = get_pending_deletes(req, 12)
605
        self.assertEqual(deletes, ['12:tags:13', '12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
606
        deletes = get_pending_deletes(req, 13)
607
        self.assertEqual(deletes, ['12:tags:13'])
Sylvain Thénault's avatar
Sylvain Thénault committed
608
        deletes = get_pending_deletes(req, 14)
609
        self.assertEqual(deletes, ['12:tags:14'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
610
611
612
613
        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
614
        _, req = self.remote_call('add_pending_inserts', [['12', 'tags', '14']])
Sylvain Thénault's avatar
Sylvain Thénault committed
615
        inserts = get_pending_inserts(req)
616
        self.assertEqual(inserts, ['12:tags:14'])
Sylvain Thénault's avatar
Sylvain Thénault committed
617
        deletes = get_pending_deletes(req)
618
        self.assertEqual(deletes, ['12:tags:13'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
619
        req.remove_pending_operations()
620
621
        self.assertEqual(get_pending_deletes(req), [])
        self.assertEqual(get_pending_inserts(req), [])
622

Adrien Di Mascio's avatar
Adrien Di Mascio committed
623
624
625
626

    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
627
        inserts = get_pending_inserts(req)
628
        self.assertEqual(inserts, ['12:tags:13', '12:tags:14'])
Adrien Di Mascio's avatar
Adrien Di Mascio committed
629
        req.remove_pending_operations()
630

Adrien Di Mascio's avatar
Adrien Di Mascio committed
631
632
633

    # silly tests
    def test_external_resource(self):
634
        self.assertEqual(self.remote_call('external_resource', 'RSS_LOGO')[0],
635
                          json_dumps(self.config.uiprops['RSS_LOGO']))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
636
    def test_i18n(self):
637
        self.assertEqual(self.remote_call('i18n', ['bimboom'])[0],
638
                          json_dumps(['bimboom']))
Adrien Di Mascio's avatar
Adrien Di Mascio committed
639
640

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


645
646


Adrien Di Mascio's avatar
Adrien Di Mascio committed
647
648
if __name__ == '__main__':
    unittest_main()