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

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

(#61) Better templates for "verify your account" emails:

  • Send a multipart text/html email (instead text only)
  • Added some styling to the html version of the email, based on our current login page styles.

Important: as the html version contains html code generated using
request.resource_url() and request.static_url(), when running in
development mode under http://localhost, the links to images
and css files in the rendered email point to localhost, so they
won't work outside the machine running openworkouts.

  • Property mode set to 100644
File size: 2.6 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
44        txt_body = Mock()
45        html_body = Mock()
46        r.side_effect = [txt_body, html_body]
47
48        request = DummyRequest()
49        request.root = root
50        user = root.users[0]
51        send_verification_email(request, user)
52        verify_link = request.resource_url(
53            user, 'verify', user.verification_token)
54
55        # two render calls
56        assert r.call_count == 2
57
58        # first call renders the text version of the email
59        assert r.call_args_list[0][0][0] == (
60            'ow:templates/mail_verify_account_txt.pt')
61        assert r.call_args_list[0][0][1] == (
62            {'user': user, 'verify_link': verify_link})
63        assert r.call_args_list[0][0][2] == request
64
65        # second call renders the html version of the email
66        assert r.call_args_list[1][0][0] == (
67            'ow:templates/mail_verify_account_html.pt')
68        assert r.call_args_list[1][0][1] == (
69            {'user': user, 'verify_link': verify_link})
70        assert r.call_args_list[1][0][2] == request
71
72        m.assert_called_once
73        m.call_args_list[0][1]['recipients'] == user.email
74        m.call_args_list[0][1]['body'] == txt_body
75        m.call_args_list[0][1]['html'] == html_body
76        mailer.send_to_queue.assert_called_once_with(message)
Note: See TracBrowser for help on using the repository browser.