source: OpenWorkouts-current/ow/tests/test_mail.py @ 76ebb1b

currentfeature/docs
Last change on this file since 76ebb1b was 76ebb1b, checked in by Borja Lopez <borja@…>, 5 years ago

(#29) Add user verification by email on signup.

From now on, when a new user signs up, we set the account into an "unverified"
state. In order to complete the signup procedure, the user has to click on a
link we send by email to the email address provided on signup.

IMPORTANT: A new dependency has been added, pyramid_mailer, so remember to
install it in any existing openworkouts environment (this is done automatically
if you use the ./bin/start script):

pip install pyramid_mailer

  • Property mode set to 100644
File size: 2.0 KB
Line 
1from unittest.mock import Mock, patch
2
3import pytest
4
5from pyramid.testing import DummyRequest
6from pyramid_mailer.message import Message
7
8from ow.models.root import OpenWorkouts
9from ow.models.user import User
10from ow.utilities import get_verification_token
11from ow.mail import idna_encode_recipients, send_verification_email
12
13
14class TestMail(object):
15
16    @pytest.fixture
17    def root(self):
18        root = OpenWorkouts()
19        user = User(email='user@example.net')
20        user.verification_token = get_verification_token()
21        root.add_user(user)
22        return root
23
24    def test_idna_encode_recipients(self):
25        message = Message(subject='s', recipients=['a@a.com'], body='b')
26        assert message.recipients == ['a@a.com']
27        message = idna_encode_recipients(message)
28        assert message.recipients == ['a@a.com']
29        message.recipients.append(u'a@\xfc.de')
30        assert message.recipients == ['a@a.com', u'a@\xfc.de']
31        message = idna_encode_recipients(message)
32        assert message.recipients == ['a@a.com', 'a@xn--tda.de']
33
34    @patch('ow.mail.get_mailer')
35    @patch('ow.mail.Message')
36    @patch('ow.mail.render')
37    def test_send_verification_email(self, r, m, gm, root):
38        mailer = Mock()
39        gm.return_value = mailer
40        message = Mock()
41        message.recipients = ['user@example.net']
42        m.return_value = message
43        body = Mock()
44        r.return_value = body
45        request = DummyRequest()
46        request.root = root
47        user = root.users[0]
48        send_verification_email(request, user)
49        verify_link = request.resource_url(
50            user, 'verify', user.verification_token)
51        r.assert_called_once_with(
52            'ow:templates/mail_verify_account.pt',
53            {'user': user, 'verify_link': verify_link},
54            request
55        )
56        m.assert_called_once
57        m.call_args_list[0][1]['recipients'] == user.email
58        m.call_args_list[0][1]['body'] == body
59        mailer.send_to_queue.assert_called_once_with(message)
Note: See TracBrowser for help on using the repository browser.