|
2 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | 3 | # file, You can obtain one at https://mozilla.org/MPL/2.0/. |
4 | 4 |
|
5 | | -import json |
6 | 5 | from unittest.mock import patch |
7 | 6 |
|
8 | | -from django.core import mail |
9 | 7 | from django.http.response import HttpResponse |
10 | 8 | from django.test import override_settings |
11 | 9 | from django.test.client import RequestFactory |
@@ -167,102 +165,3 @@ def test_webvision_redirect(self): |
167 | 165 | resp = self.client.get(path, follow=True, headers={"accept-language": "en"}) |
168 | 166 | self.assertEqual(resp.redirect_chain[0], ("/about/webvision/", 301)) |
169 | 167 | self.assertEqual(resp.redirect_chain[1], ("/en-US/about/webvision/", 302)) |
170 | | - |
171 | | - |
172 | | -class TestMiecoEmail(TestCase): |
173 | | - def setUp(self): |
174 | | - self.factory = RequestFactory() |
175 | | - self.view = views.mieco_email_form |
176 | | - with self.activate_locale("en-US"): |
177 | | - self.url = reverse("mozorg.email_mieco") |
178 | | - |
179 | | - self.data = { |
180 | | - "name": "The Dude", |
181 | | - "email": "foo@bar.com", |
182 | | - "interests": "abiding, bowling", |
183 | | - "description": "The rug really tied the room together.", |
184 | | - } |
185 | | - |
186 | | - def tearDown(self): |
187 | | - mail.outbox = [] |
188 | | - |
189 | | - def test_not_post(self): |
190 | | - resp = self.client.get(self.url) |
191 | | - self.assertEqual(resp.status_code, 400) |
192 | | - self.assertEqual(resp.text, '{"error": 400, "message": "Only POST requests are allowed"}') |
193 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
194 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
195 | | - |
196 | | - def test_bad_json(self): |
197 | | - resp = self.client.post(self.url, content_type="application/json", data='{{"bad": "json"}') |
198 | | - self.assertEqual(resp.status_code, 400) |
199 | | - self.assertEqual(resp.text, '{"error": 400, "message": "Error decoding JSON"}') |
200 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
201 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
202 | | - |
203 | | - def test_invalid_email(self): |
204 | | - resp = self.client.post(self.url, content_type="application/json", data='{"email": "foo@bar"}') |
205 | | - self.assertEqual(resp.status_code, 400) |
206 | | - self.assertEqual(resp.text, '{"error": 400, "message": "Invalid form data"}') |
207 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
208 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
209 | | - |
210 | | - def test_invalid_message_id(self): |
211 | | - self.data["message_id"] = "the-dude" |
212 | | - resp = self.client.post(self.url, content_type="application/json", data=json.dumps(self.data)) |
213 | | - self.assertEqual(resp.status_code, 400) |
214 | | - self.assertEqual(resp.text, '{"error": 400, "message": "Invalid form data"}') |
215 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
216 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
217 | | - |
218 | | - @patch("bedrock.mozorg.views.render_to_string", return_value="rendered") |
219 | | - @patch("bedrock.mozorg.views.EmailMessage") |
220 | | - def test_success(self, mock_emailMessage, mock_render_to_string): |
221 | | - resp = self.client.post(self.url, content_type="application/json", data=json.dumps(self.data)) |
222 | | - |
223 | | - self.assertEqual(resp.status_code, 200) |
224 | | - mock_emailMessage.assert_called_once_with( |
225 | | - views.MIECO_EMAIL_SUBJECT["mieco"], "rendered", views.MIECO_EMAIL_SENDER, views.MIECO_EMAIL_TO["mieco"] |
226 | | - ) |
227 | | - self.assertEqual(resp.text, '{"status": "ok"}') |
228 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
229 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
230 | | - |
231 | | - @patch("bedrock.mozorg.views.render_to_string", return_value="rendered") |
232 | | - @patch("bedrock.mozorg.views.EmailMessage") |
233 | | - def test_success_mieco(self, mock_emailMessage, mock_render_to_string): |
234 | | - self.data["message_id"] = "mieco" |
235 | | - resp = self.client.post(self.url, content_type="application/json", data=json.dumps(self.data)) |
236 | | - |
237 | | - self.assertEqual(resp.status_code, 200) |
238 | | - mock_emailMessage.assert_called_once_with( |
239 | | - views.MIECO_EMAIL_SUBJECT["mieco"], "rendered", views.MIECO_EMAIL_SENDER, views.MIECO_EMAIL_TO["mieco"] |
240 | | - ) |
241 | | - self.assertEqual(resp.text, '{"status": "ok"}') |
242 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
243 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
244 | | - |
245 | | - @patch("bedrock.mozorg.views.render_to_string", return_value="rendered") |
246 | | - @patch("bedrock.mozorg.views.EmailMessage") |
247 | | - def test_success_innovations(self, mock_emailMessage, mock_render_to_string): |
248 | | - self.data["message_id"] = "innovations" |
249 | | - resp = self.client.post(self.url, content_type="application/json", data=json.dumps(self.data)) |
250 | | - |
251 | | - self.assertEqual(resp.status_code, 200) |
252 | | - mock_emailMessage.assert_called_once_with( |
253 | | - views.MIECO_EMAIL_SUBJECT["innovations"], "rendered", views.MIECO_EMAIL_SENDER, views.MIECO_EMAIL_TO["innovations"] |
254 | | - ) |
255 | | - self.assertEqual(resp.text, '{"status": "ok"}') |
256 | | - self.assertIn("Access-Control-Allow-Origin", resp.headers) |
257 | | - self.assertIn("Access-Control-Allow-Headers", resp.headers) |
258 | | - |
259 | | - def test_outbox(self): |
260 | | - resp = self.client.post(self.url, content_type="application/json", data=json.dumps(self.data)) |
261 | | - self.assertEqual(resp.status_code, 200) |
262 | | - self.assertEqual(len(mail.outbox), 1) |
263 | | - |
264 | | - email = mail.outbox[0] |
265 | | - self.assertIn(f"Name: {self.data['name']}", email.body) |
266 | | - self.assertIn(f"E-mail: {self.data['email']}", email.body) |
267 | | - self.assertIn(f"Interests: {self.data['interests']}", email.body) |
268 | | - self.assertIn(f"Message:\n{self.data['description']}", email.body) |
0 commit comments