Commit 30ffd3cb authored by Julien Cristau's avatar Julien Cristau
Browse files

[test] update to cubicweb 3.19 APIs

parent 0b097f9603dd
import re, cgi, urlparse
from contextlib import contextmanager
from cubicweb import ValidationError
from cubicweb.crypto import encrypt, decrypt
......@@ -24,10 +25,10 @@ class RegistrationTC(CubicWebTC):
super(RegistrationTC, self).setup_database()
def _check_user_not_created(self):
self.restore_connection()
rset = self.execute('CWUser X WHERE X login %(login)s',
{'login': self.data['login-subject']})
self.failIf(rset)
with self.admin_access.repo_cnx() as cnx:
rset = cnx.execute('CWUser X WHERE X login %(login)s',
{'login': self.data['login-subject']})
self.assertFalse(rset)
def _check_error(self, req, path,
expected_path='registration',
......@@ -57,9 +58,9 @@ class RegistrationTC(CubicWebTC):
return data
def test_registration_form(self):
req = self.request()
req.form = {'firstname-subject': u'Toto'}
pageinfo = self.view('registration', req=req, rset=None)
with self.admin_access.web_request() as req:
req.form = {'firstname-subject': u'Toto'}
pageinfo = self.view('registration', req=req, rset=None)
# check form field names
names = pageinfo.etree.xpath('//form[@id="registrationForm"]//input[@type!="hidden"]/@name')
......@@ -71,86 +72,86 @@ class RegistrationTC(CubicWebTC):
self.assertEqual(firstname, [req.form['firstname-subject']])
def test_send_mail_ok(self):
self.login('anon')
req = self.request()
req.form = self._posted_form()
req.session.data['captcha'] = self.captcha_value
path, params = self.expect_redirect_publish(req, 'registration_sendmail')
self.assertEqual(path, '')
self.assertMessageEqual(req, params, 'Your registration email has been sent. Follow instructions in there to activate your account.')
# check email contains activation url...
URL_RE = re.compile('(%s[^.]+)$' % self.config['base-url'], re.M)
text = MAILBOX[-1].message.get_payload(decode=True)
url = URL_RE.search(text).group(1)
# ... and the registration key contains all data
key = dict(cgi.parse_qsl(urlparse.urlsplit(url)[3]))['key']
d = self._posted_form('upassword-subject-confirm')
self.assertDictEqual(decrypt(key, self.config['registration-cypher-seed']), d)
with self.new_access(u'anon').web_request() as req:
req.form = self._posted_form()
req.session.data['captcha'] = self.captcha_value
path, params = self.expect_redirect_publish(req, 'registration_sendmail')
self.assertEqual(path, '')
self.assertMessageEqual(req, params, 'Your registration email has been sent. Follow instructions in there to activate your account.')
# check email contains activation url...
URL_RE = re.compile('(%s[^.]+)$' % self.config['base-url'], re.M)
text = MAILBOX[-1].message.get_payload(decode=True)
url = URL_RE.search(text).group(1)
# ... and the registration key contains all data
key = dict(cgi.parse_qsl(urlparse.urlsplit(url)[3]))['key']
d = self._posted_form('upassword-subject-confirm')
self.assertDictEqual(decrypt(key, self.config['registration-cypher-seed']), d)
def test_send_mail_failure(self):
for param, msg, val in (('login-subject', 'required field', None),
('captcha', 'required field', None),
('captcha', 'incorrect captcha value', 'abc')):
req = self.request()
if val is None:
req.form = self._posted_form(param)
else:
req.form = self._posted_form()
req.form[param] = val
req.session.data['captcha'] = self.captcha_value
#expected
self._check_error(req, 'registration_sendmail',
expected_formvalues=req.form,
expected_errors={param: msg})
self.assertEqual(req.session.data.get('captcha'), None)
with self.admin_access.web_request() as req:
if val is None:
req.form = self._posted_form(param)
else:
req.form = self._posted_form()
req.form[param] = val
req.session.data['captcha'] = self.captcha_value
#expected
self._check_error(req, 'registration_sendmail',
expected_formvalues=req.form,
expected_errors={param: msg})
self.assertEqual(req.session.data.get('captcha'), None)
@contextmanager
def _confirm_req(self, key=None, overriden={}):
self.login('anon')
req = self.request()
data = self._posted_form('upassword-subject-confirm')
data.update(overriden)
if key is None:
key = encrypt(data, self.config['registration-cypher-seed'])
req.form = {'key': key}
return req
with self.new_access(u'anon').web_request() as req:
data = self._posted_form('upassword-subject-confirm')
data.update(overriden)
if key is None:
key = encrypt(data, self.config['registration-cypher-seed'])
req.form = {'key': key}
yield req
#self.vreg['controllers'].select('registration_confirm', req,
#appli=self.app)
def test_confirm_ok(self):
req = self._confirm_req()
path, params = self.expect_redirect_publish(req, 'registration_confirm')
self.assertEqual(path, '')
self.assertMessageEqual(req, params, 'Congratulations, your registration is complete. Welcome %(firstname-subject)s %(surname-subject)s !' % self.data)
self.restore_connection()
rset = self.execute('Any U WHERE U login %(login)s, U firstname %(firstname)s, '
'U surname %(surname)s, U use_email M, M address %(email_address)s',
dict((k.replace('-subject', ''), v)
for k, v in self.data.items()))
self.failUnless(rset.rowcount)
with self._confirm_req() as req:
path, params = self.expect_redirect_publish(req, 'registration_confirm')
self.assertEqual(path, '')
self.assertMessageEqual(req, params, 'Congratulations, your registration is complete. Welcome %(firstname-subject)s %(surname-subject)s !' % self.data)
with self.admin_access.repo_cnx() as cnx:
rset = cnx.execute('Any U WHERE U login %(login)s, U firstname %(firstname)s, '
'U surname %(surname)s, U use_email M, M address %(email_address)s',
dict((k.replace('-subject', ''), v)
for k, v in self.data.items()))
self.assertTrue(rset)
def test_confirm_failure_login_already_used(self):
# try to recreate a 'admin' user.
req = self._confirm_req(overriden={'login-subject': 'admin'})
formvalues = self._posted_form('upassword-subject',
'upassword-subject-confirm')
formvalues['login-subject'] = 'admin'
self._check_error(req, 'registration_confirm',
expected_formvalues=formvalues,
expected_errors={'login-subject': 'the value "admin" is already used, use another one'})
with self._confirm_req(overriden={'login-subject': 'admin'}) as req:
formvalues = self._posted_form('upassword-subject',
'upassword-subject-confirm')
formvalues['login-subject'] = 'admin'
self._check_error(req, 'registration_confirm',
expected_formvalues=formvalues,
expected_errors={'login-subject': 'the value "admin" is already used, use another one'})
def test_confirm_failure_invalid_data(self):
req = self._confirm_req('dummykey')
self._check_error(req, 'registration_confirm', 'register',
expected_msg='Invalid registration data. Please try registering again.')
with self._confirm_req('dummykey') as req:
self._check_error(req, 'registration_confirm', 'register',
expected_msg='Invalid registration data. Please try registering again.')
self._check_user_not_created()
def test_confirm_failure_email_already_used(self):
self.create_user(self.request(), 'test')
self.execute('INSERT EmailAddress X: U use_email X, X address %(email_address)s '
'WHERE U login "test"', {'email_address': self.data['email_address-subject']})
self.commit()
req = self._confirm_req()
req.form['__errorurl'] = 'registration'
with self.admin_access.web_request() as req:
self.create_user(req, 'test')
req.execute('INSERT EmailAddress X: U use_email X, X address %(email_address)s '
'WHERE U login "test"', {'email_address': self.data['email_address-subject']})
req.cnx.commit()
with self._confirm_req() as req:
req.form['__errorurl'] = 'registration'
self._check_user_not_created()
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment