[sobjects] Drop custom unique constraint checks in user registration service
This constraints (on unique CWUser's login and EmailAddress's login) are
handled by the backend.
This follows "[hooks/integrity] kill CheckUniqueHook".
Extend test for the ValidationError along the way.
--- a/cubicweb/sobjects/services.py Thu Jun 23 11:30:10 2016 +0200
+++ b/cubicweb/sobjects/services.py Thu Jun 23 08:53:21 2016 +0200
@@ -125,15 +125,6 @@
def call(self, login, password, email=None, groups=None, **cwuserkwargs):
cnx = self._cw
- errmsg = cnx._('the value "%s" is already used, use another one')
-
- if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login},
- build_descr=False)
- or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s',
- {'login': login}, build_descr=False)):
- qname = role_name('login', 'subject')
- raise ValidationError(None, {qname: errmsg % login})
-
if isinstance(password, text_type):
# password should *always* be utf8 encoded
password = password.encode('UTF8')
@@ -147,13 +138,8 @@
group_names = ', '.join('%r' % group for group in groups)
cnx.execute('SET X in_group G WHERE X eid %%(x)s, G name IN (%s)' % group_names,
{'x': user.eid})
-
if email or '@' in login:
d = {'login': login, 'email': email or login}
- if cnx.execute('EmailAddress X WHERE X address %(email)s', d,
- build_descr=False):
- qname = role_name('address', 'subject')
- raise ValidationError(None, {qname: errmsg % d['email']})
cnx.execute('INSERT EmailAddress X: X address %(email)s, '
'U primary_email X, U use_email X '
'WHERE U login %(login)s', d, build_descr=False)
--- a/cubicweb/sobjects/test/unittest_register_user.py Thu Jun 23 11:30:10 2016 +0200
+++ b/cubicweb/sobjects/test/unittest_register_user.py Thu Jun 23 08:53:21 2016 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
@@ -15,7 +15,7 @@
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
-"""unittest for cubicweb.dbapi"""
+"""unittest for cubicweb user registration service"""
from cubicweb import ValidationError
from cubicweb.web import Unauthorized
@@ -36,9 +36,14 @@
password=u'bar3', email=u'foo3@bar3.com',
firstname=u'Foo3', surname=u'Bar3')
# same login
- with self.assertRaises(ValidationError):
+ with self.assertRaises(ValidationError) as cm:
cnx.call_service('register_user', login=u'foo3',
password=u'bar3')
+ expected_errors = {
+ '': u'some relations violate a unicity constraint',
+ 'login': u'%(KEY-rtype)s is part of violated unicity constraint',
+ }
+ self.assertEqual(cm.exception.errors, expected_errors)
def test_register_user_service_unique_email(self):
with self.admin_access.cnx() as cnx:
@@ -49,7 +54,8 @@
cnx.call_service('register_user', login=u'foo3@bar3.com',
password=u'bar3')
expected_errors = {
- 'login-subject': u'the value "foo3@bar3.com" is already used, use another one',
+ '': u'some relations violate a unicity constraint',
+ 'address': u'%(KEY-rtype)s is part of violated unicity constraint',
}
self.assertEqual(cm.exception.errors, expected_errors)