|
1 |
| -from http import HTTPStatus |
2 | 1 | from unittest.mock import patch
|
3 | 2 |
|
4 |
| -import requests |
5 |
| -import requests_mock |
6 |
| - |
7 |
| -from django.test import TestCase |
| 3 | +from allauth.account.models import EmailAddress |
| 4 | +from allauth.account.signals import email_confirmed |
8 | 5 | from django.conf import settings
|
9 |
| - |
10 |
| -from readthedocs.forms import SignupFormWithNewsletter |
| 6 | +from django.test import TestCase |
11 | 7 |
|
12 | 8 |
|
13 | 9 | class TestNewsletterSignup(TestCase):
|
14 |
| - form_data = { |
15 |
| - |
16 |
| - "username": "test123", |
17 |
| - "password1": "123456", |
18 |
| - "password2": "123456", |
19 |
| - } |
20 |
| - form_data_plus_checkbox = form_data.copy() |
21 |
| - form_data_plus_checkbox["receive_newsletter"] = True |
22 |
| - |
23 |
| - @patch("readthedocs.forms.requests.post") |
| 10 | + def setUp(self): |
| 11 | + self.form_data = { |
| 12 | + |
| 13 | + "username": "test123", |
| 14 | + "password1": "123456", |
| 15 | + "password2": "123456", |
| 16 | + } |
| 17 | + self.form_data_plus_checkbox = self.form_data.copy() |
| 18 | + self.form_data_plus_checkbox["receive_newsletter"] = True |
| 19 | + |
| 20 | + @patch("readthedocs.core.signals.requests.post") |
24 | 21 | def test_signup_without_checkbox_does_not_subscribe(self, mock_requests_post):
|
25 | 22 | response = self.client.post("/accounts/signup/", data=self.form_data)
|
26 |
| - |
27 |
| - assert response.status_code == HTTPStatus.FOUND |
| 23 | + email_confirmed.send( |
| 24 | + sender=None, |
| 25 | + request=None, |
| 26 | + email_address=EmailAddress.objects.get(email=self.form_data["email"]), |
| 27 | + ) |
28 | 28 |
|
29 | 29 | mock_requests_post.assert_not_called()
|
30 | 30 |
|
31 |
| - |
32 |
| - @patch("readthedocs.forms.requests.post") |
33 |
| - def test_signup_with_checkbox_calls_subscribe_api(self, mock_requests_post): |
| 31 | + @patch("readthedocs.core.signals.requests.post") |
| 32 | + def test_signup_calls_subscribe_api(self, mock_requests_post): |
34 | 33 | response = self.client.post("/accounts/signup/", data=self.form_data_plus_checkbox)
|
35 |
| - |
36 |
| - assert response.status_code == HTTPStatus.FOUND, response |
| 34 | + email_confirmed.send( |
| 35 | + sender=None, |
| 36 | + request=None, |
| 37 | + email_address=EmailAddress.objects.get(email=self.form_data["email"]), |
| 38 | + ) |
37 | 39 |
|
38 | 40 | mock_requests_post.assert_called_with(
|
39 | 41 | settings.MAILERLITE_API_SUBSCRIBERS_URL,
|
40 | 42 | json={
|
41 |
| - "email": self.form_data_plus_checkbox["email"], |
| 43 | + "email": self.form_data["email"], |
42 | 44 | "resubscribe": True,
|
43 | 45 | },
|
44 | 46 | headers={"X-MailerLite-ApiKey": settings.MAILERLITE_API_KEY},
|
45 | 47 | timeout=3,
|
46 | 48 | )
|
47 |
| - |
48 |
| - @requests_mock.Mocker(kw='mock_request') |
49 |
| - def test_signup_with_checkbox_succeeds_if_timeout(self, mock_request): |
50 |
| - mock_request.post(settings.MAILERLITE_API_SUBSCRIBERS_URL, exc=requests.Timeout) |
51 |
| - |
52 |
| - response = self.client.post("/accounts/signup/", data=self.form_data_plus_checkbox) |
53 |
| - |
54 |
| - assert response.status_code == HTTPStatus.FOUND, response |
55 |
| - |
56 |
| - @requests_mock.Mocker(kw='mock_request') |
57 |
| - def test_signup_with_checkbox_succeeds_if_bad_response(self, mock_request): |
58 |
| - mock_request.post(settings.MAILERLITE_API_SUBSCRIBERS_URL, status_code=400) |
59 |
| - |
60 |
| - response = self.client.post("/accounts/signup/", data=self.form_data_plus_checkbox) |
61 |
| - |
62 |
| - assert response.status_code == HTTPStatus.FOUND, response |
0 commit comments