unittest_registration.py 4.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import urllib, re, cgi, urlparse

from cubicweb import ValidationError
from cubicweb.web import Redirect
from cubicweb.devtools.testlib import WebTest

from cubes.registration.views import RegistrationSendMailController, deserialize, serialize


EMAILS = []
def my_send_email(self, to, msg):
    EMAILS.append(msg)
RegistrationSendMailController.send_email = my_send_email


class RegistrationTC(WebTest):

    data = {'firstname': u'Toto', 'surname': u'Toto', 'email': u'toto@secondweb.fr',
            'login': u'toto', 'password': 'toto', 'password-confirm': 'toto'}

    def setup_database(self):
        self.config.set_option('registration_cypherkey', u'dummy cypher key')
        super(RegistrationTC, self).setup_database()

    def test_registration_form(self):
        req = self.request()
        req.form = {'firstname': u'Toto'}
        pageinfo = self.view('registration', req=req, rset=None)
        ns = {'ns': pageinfo.default_ns}
        # check form field names
        names = pageinfo.etree.xpath('//ns:form[@id="form"]//ns:input[@type!="hidden"]/@name',
                                           namespaces=ns)
        self.assertEquals(names, ['login', 'password', 'password-confirm',
                                  'email', 'firstname', 'surname'])
        # check form field value
        firstname = pageinfo.etree.xpath('//ns:input[@name="firstname"]/@value', namespaces=ns)
        self.assertEquals(firstname, [req.form['firstname']])

    def _check_redirect(self, ctrl, urlpath, **urlparams):
        try:
            ctrl.publish()
            assert False, 'should redirect to ' + urlpath
        except Redirect, e:
            url, params = (e.location.split('?')+[''])[:2]
            self.assertEquals(url, self.config['base-url'] + urlpath)
            self.assertDictEquals(dict(cgi.parse_qsl(params)), urlparams)

    def test_send_mail_ok(self):
        req = self.request()
        req.form = self.data.copy()
        ctrl = self.env.app.select_controller('registration_sendmail', req)
        # check redirect
        self._check_redirect(ctrl, '', __message='Your registration email has been sent.')
        # check email contains activation url...
        URL_RE = re.compile('(%s[^.]+)\.' % self.config['base-url'])
        text = EMAILS[-1].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.data.copy()
        d.pop('password-confirm')
        self.assertDictEquals(deserialize(key, self.config['registration_cypherkey']), d)

    def test_send_mail_failure(self):
        req = self.request()
        req.form = self.data.copy()
        req.form.pop('firstname')
        ctrl = self.env.app.select_controller('registration_sendmail', req)
        try:
            ctrl.publish()
            assert False, 'should raise ValidationError'
        except ValidationError, e:
            self.assertDictEquals(e.errors, {'firstname': u'required attribute'})

    def test_confirm_ok(self):
        self.login('anon')
        req = self.request()
        req.form = {'key': serialize(self.data, self.config['registration_cypherkey'])}
        ctrl = self.env.app.select_controller('registration_confirm', req)
        self._check_redirect(ctrl, '', __message=u'Congratulations, your registration is complete. Welcome %(firstname)s %(surname)s !' % self.data)
        req.cnx.commit()
        req.cnx.close()
        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)s', self.data)
        self.failUnless(rset.rowcount)

    def test_confirm_failure_login_exists(self):
        self.create_user(self.data['login'])
        self.commit()
        self.login('anon')
        req = self.request()
        req.form = {'key': serialize(self.data, self.config['registration_cypherkey'])}
        ctrl = self.env.app.select_controller('registration_confirm', req)
        # check user is redirected to a url without its password in the url params
        d = self.data.copy()
        d.pop('login'), d.pop('password'), d.pop('password-confirm')
        d['__message'] = u'Login is already used. Please try another one.'
        self._check_redirect(ctrl, 'register', **d)

    def test_confirm_failure_invalid_data(self):
        self.create_user(self.data['login'])
        self.commit()
        self.login('anon')
        req = self.request()
        req.form = {'key': u'dummykey'}
        ctrl = self.env.app.select_controller('registration_confirm', req)
        self._check_redirect(ctrl, 'register', __message=u'Invalid registration data. Please try registering again.')