~homeworkprod/byceps

ref: f8c663d0c8d818803b7d3711c3f71fc4f86b6c29 byceps/tests/blueprints/user_message/test_send.py -rw-r--r-- 5.1 KiB
f8c663d0 — Jochen Kupperschmidt Add optional contact address attribute to e-mail configuration model 2 years ago
                                                                                
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
:Copyright: 2006-2019 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from unittest.mock import patch

from byceps.services.brand import settings_service as brand_settings_service

from tests.base import AbstractAppTestCase
from tests.helpers import create_brand, create_email_config, create_party, \
    create_site, create_user, http_client, login_user


class SendUserMessageTest(AbstractAppTestCase):

    def setUp(self):
        super().setUp()

        self.brand = create_brand()
        party = create_party(self.brand.id)

        email_config_id = self.brand.id
        create_email_config(email_config_id, 'noreply@example.com',
                            'ACME Entertainment Convention')

        create_site(party.id, server_name='acme.example.com')

    @patch('byceps.email.send')
    def test_send_when_logged_in_without_brand_contact_address(self,
                                                               send_email_mock):
        sender = create_user('Alice',
            user_id='a4903d8f-0bc6-4af9-aeb9-d7534a0a22e8',
            email_address='alice@example.com')
        recipient = create_user('Bob',
            email_address='bob@example.com')
        text = '''\
Hi Bob,

please don't forget to take out the trash.

kthxbye,
Alice
'''

        expected_response_location \
            = 'http://example.com/users/{}'.format(recipient.id)

        expected_email_sender = 'ACME Entertainment Convention <noreply@example.com>'
        expected_email_recipients = ['Bob <bob@example.com>']
        expected_email_subject = 'Mitteilung von Alice (über acme.example.com)'
        expected_email_body = '''\
Hallo Bob,

Alice möchte dir die folgende Mitteilung zukommen lassen.

Du kannst Alice hier antworten: http://example.com/user_messages/to/a4903d8f-0bc6-4af9-aeb9-d7534a0a22e8/create

ACHTUNG: Antworte *nicht* auf diese E-Mail, sondern folge dem Link.

---8<-------------------------------------

Hi Bob,

please don't forget to take out the trash.

kthxbye,
Alice

---8<-------------------------------------

-- 
Diese Mitteilung wurde über die Website acme.example.com gesendet.\
'''

        response = self.send_request(recipient.id, text,
                                     current_user_id=sender.id)

        assert response.status_code == 302
        assert response.location == expected_response_location

        send_email_mock.assert_called_once_with(
            expected_email_sender,
            expected_email_recipients,
            expected_email_subject,
            expected_email_body)

    @patch('byceps.email.send')
    def test_send_when_logged_in_with_brand_contact_address(self,
                                                            send_email_mock):
        brand_settings_service.create_setting(
            self.brand.id, 'contact_email_address', 'info@example.com')

        sender = create_user('Bob',
            user_id='11d72bab-3646-4199-b96c-e5e4c6f972bc',
            email_address='bob@example.com')
        recipient = create_user('Alice',
            email_address='alice@example.com')
        text = '''\
Hey Alice,

nice to hear from you.

Best,
Bob
'''

        expected_response_location \
            = 'http://example.com/users/{}'.format(recipient.id)

        expected_email_sender = 'ACME Entertainment Convention <noreply@example.com>'
        expected_email_recipients = ['Alice <alice@example.com>']
        expected_email_subject = 'Mitteilung von Bob (über acme.example.com)'
        expected_email_body = '''\
Hallo Alice,

Bob möchte dir die folgende Mitteilung zukommen lassen.

Du kannst Bob hier antworten: http://example.com/user_messages/to/11d72bab-3646-4199-b96c-e5e4c6f972bc/create

ACHTUNG: Antworte *nicht* auf diese E-Mail, sondern folge dem Link.

---8<-------------------------------------

Hey Alice,

nice to hear from you.

Best,
Bob

---8<-------------------------------------

-- 
Diese Mitteilung wurde über die Website acme.example.com gesendet.
Bei Fragen kontaktiere uns bitte per E-Mail an: info@example.com\
'''

        response = self.send_request(recipient.id, text,
                                     current_user_id=sender.id)

        assert response.status_code == 302
        assert response.location == expected_response_location

        send_email_mock.assert_called_once_with(
            expected_email_sender,
            expected_email_recipients,
            expected_email_subject,
            expected_email_body)

    def test_send_when_not_logged_in(self):
        recipient_id = '8e5037f6-3ca1-4981-b1e4-1998cbdf58e2'
        text = 'Hello, Eve!'

        response = self.send_request(recipient_id, text)

        assert response.status_code == 302
        assert response.location == 'http://example.com/authentication/login'

    # helpers

    def send_request(self, recipient_id, text, *, current_user_id=None):
        url = '/user_messages/to/{}/create'.format(recipient_id)

        form_data = {
            'body': text,
        }

        if current_user_id is not None:
            login_user(current_user_id)

        with http_client(self.app, user_id=current_user_id) as client:
            return client.post(url, data=form_data)