1 | """ |
||
2 | :Copyright: 2006-2021 Jochen Kupperschmidt |
||
3 | :License: Revised BSD (see `LICENSE` file for details) |
||
4 | """ |
||
5 | |||
6 | from unittest.mock import patch |
||
7 | |||
8 | import pytest |
||
9 | |||
10 | from tests.helpers import http_client, login_user |
||
11 | |||
12 | |||
13 | @pytest.fixture(scope='module') |
||
14 | def user_alice(make_user): |
||
15 | return make_user('Alice', email_address='[email protected]', locale='de') |
||
16 | |||
17 | |||
18 | @pytest.fixture(scope='module') |
||
19 | def user_bob(make_user): |
||
20 | return make_user('Bob', email_address='[email protected]', locale='en') |
||
21 | |||
22 | |||
23 | View Code Duplication | @patch('byceps.email.send') |
|
1 ignored issue
–
show
Duplication
introduced
by
Loading history...
|
|||
24 | def test_send_when_logged_in_without_brand_contact_address( |
||
25 | send_email_mock, site_app, user_alice, user_bob |
||
26 | ): |
||
27 | sender_id = user_alice.id |
||
28 | recipient_id = user_bob.id |
||
29 | text = '''\ |
||
30 | Hi Bob, |
||
31 | |||
32 | please don't forget to take out the trash. |
||
33 | |||
34 | kthxbye, |
||
35 | Alice |
||
36 | ''' |
||
37 | |||
38 | expected_response_location = f'http://www.acmecon.test/users/{recipient_id}' |
||
39 | |||
40 | expected_email_sender = ( |
||
41 | 'ACME Entertainment Convention <[email protected]>' |
||
42 | ) |
||
43 | expected_email_recipients = ['Bob <[email protected]>'] |
||
44 | expected_email_subject = 'Message from Alice (via www.acmecon.test)' |
||
45 | expected_email_body = f'''\ |
||
46 | Hello Bob, |
||
47 | |||
48 | Alice has sent you the following message. |
||
49 | |||
50 | You can reply here: http://www.acmecon.test/user_messages/to/{user_alice.id} |
||
51 | |||
52 | ATTENTION: Do *not* reply to this email. Follow the link instead. |
||
53 | |||
54 | ---8<------------------------------------- |
||
55 | |||
56 | Hi Bob, |
||
57 | |||
58 | please don't forget to take out the trash. |
||
59 | |||
60 | kthxbye, |
||
61 | Alice |
||
62 | |||
63 | ---8<------------------------------------- |
||
64 | |||
65 | -- |
||
66 | This message was sent via website www.acmecon.test. |
||
67 | If you have any questions, please contact us via email to: [email protected]\ |
||
68 | ''' |
||
69 | |||
70 | response = send_request( |
||
71 | site_app, recipient_id, text, current_user_id=sender_id |
||
72 | ) |
||
73 | |||
74 | assert response.status_code == 302 |
||
75 | assert response.location == expected_response_location |
||
76 | |||
77 | assert_email( |
||
78 | send_email_mock, |
||
79 | expected_email_sender, |
||
80 | expected_email_recipients, |
||
81 | expected_email_subject, |
||
82 | expected_email_body, |
||
83 | ) |
||
84 | |||
85 | |||
86 | View Code Duplication | @patch('byceps.email.send') |
|
87 | def test_send_when_logged_in_with_brand_contact_address( |
||
88 | send_email_mock, |
||
89 | site_app, |
||
90 | user_alice, |
||
91 | user_bob, |
||
92 | ): |
||
93 | sender_id = user_bob.id |
||
94 | recipient_id = user_alice.id |
||
95 | text = '''\ |
||
96 | Hey Alice, |
||
97 | |||
98 | nice to hear from you. |
||
99 | |||
100 | Best, |
||
101 | Bob |
||
102 | ''' |
||
103 | |||
104 | expected_response_location = f'http://www.acmecon.test/users/{recipient_id}' |
||
105 | |||
106 | expected_email_sender = ( |
||
107 | 'ACME Entertainment Convention <[email protected]>' |
||
108 | ) |
||
109 | expected_email_recipients = ['Alice <[email protected]>'] |
||
110 | expected_email_subject = 'Mitteilung von Bob (über www.acmecon.test)' |
||
111 | expected_email_body = f'''\ |
||
112 | Hallo Alice, |
||
113 | |||
114 | Bob möchte dir die folgende Mitteilung zukommen lassen. |
||
115 | |||
116 | Du kannst hier antworten: http://www.acmecon.test/user_messages/to/{user_bob.id} |
||
117 | |||
118 | ACHTUNG: Antworte *nicht* auf diese E-Mail, sondern folge dem Link. |
||
119 | |||
120 | ---8<------------------------------------- |
||
121 | |||
122 | Hey Alice, |
||
123 | |||
124 | nice to hear from you. |
||
125 | |||
126 | Best, |
||
127 | Bob |
||
128 | |||
129 | ---8<------------------------------------- |
||
130 | |||
131 | -- |
||
132 | Diese Mitteilung wurde über die Website www.acmecon.test gesendet. |
||
133 | Bei Fragen kontaktiere uns bitte per E-Mail an: [email protected]\ |
||
134 | ''' |
||
135 | |||
136 | response = send_request( |
||
137 | site_app, recipient_id, text, current_user_id=sender_id |
||
138 | ) |
||
139 | |||
140 | assert response.status_code == 302 |
||
141 | assert response.location == expected_response_location |
||
142 | |||
143 | assert_email( |
||
144 | send_email_mock, |
||
145 | expected_email_sender, |
||
146 | expected_email_recipients, |
||
147 | expected_email_subject, |
||
148 | expected_email_body, |
||
149 | ) |
||
150 | |||
151 | |||
152 | def test_send_when_not_logged_in(site_app): |
||
153 | recipient_id = '8e5037f6-3ca1-4981-b1e4-1998cbdf58e2' |
||
154 | text = 'Hello, Eve!' |
||
155 | |||
156 | response = send_request(site_app, recipient_id, text) |
||
157 | |||
158 | assert response.status_code == 302 |
||
159 | assert response.location == 'http://www.acmecon.test/authentication/login' |
||
160 | |||
161 | |||
162 | # helpers |
||
163 | |||
164 | |||
165 | def send_request(app, recipient_id, text, *, current_user_id=None): |
||
166 | url = f'/user_messages/to/{recipient_id}' |
||
167 | |||
168 | form_data = {'body': text} |
||
169 | |||
170 | if current_user_id is not None: |
||
171 | login_user(current_user_id) |
||
172 | |||
173 | with http_client(app, user_id=current_user_id) as client: |
||
174 | return client.post(url, data=form_data) |
||
175 | |||
176 | |||
177 | View Code Duplication | def assert_email( |
|
1 ignored issue
–
show
|
|||
178 | mock, expected_sender, expected_recipients, expected_subject, expected_body |
||
179 | ): |
||
180 | calls = mock.call_args_list |
||
181 | assert len(calls) == 1 |
||
182 | |||
183 | args = calls[0].args |
||
184 | assert args[0] == expected_sender |
||
185 | assert args[1] == expected_recipients |
||
186 | assert args[2] == expected_subject |
||
187 | assert args[3] == expected_body |
||
188 |