-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from django.test import RequestFactory, TestCase, override_settings | ||
from django.urls import reverse | ||
from .forms import ContactForm | ||
from unittest.mock import MagicMock, patch | ||
|
||
class ContactTestCase(TestCase): | ||
def test_form_displayed(self): | ||
|
@@ -105,6 +106,54 @@ def test_form_title_not_displayed(self): | |
self.assertContains(resp, 'csrfmiddlewaretoken', status_code=200) | ||
self.assertNotContains(resp, '<h3>Contact us</h3>', status_code=200) | ||
|
||
@override_settings(CONTACTFORM_RECIPIENTS=['[email protected]']) | ||
@override_settings(GOOGLE_RECAPTCHA_ENABLED=True) | ||
@override_settings(GOOGLE_RECAPTCHA_SECRET='recaptcha-secret') | ||
@patch('contactform.views.requests') | ||
def test_post_with_invalid_recaptcha(self, mock_requests): | ||
""" Tests than the email is not sent where recaptcha fail the | ||
validation | ||
""" | ||
|
||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {'success': False} | ||
mock_requests.post.return_value = mock_response | ||
|
||
resp = self.client.post(reverse('contactform:index'), { | ||
'email': '[email protected]', | ||
'phone': '06 00 00 00 00', | ||
'comment': 'This is my message content', | ||
'g-recaptcha-response': 'abcdefgh'}) | ||
|
||
self.assertEqual(len(mail.outbox), 0) | ||
|
||
self.assertContains(resp, 'Thanks for your message', status_code=200) | ||
|
||
@override_settings(CONTACTFORM_RECIPIENTS=['[email protected]']) | ||
@override_settings(GOOGLE_RECAPTCHA_ENABLED=True) | ||
@override_settings(GOOGLE_RECAPTCHA_SECRET='recaptcha-secret') | ||
@patch('contactform.views.requests') | ||
def test_post_with_valid_recaptcha(self, mock_requests): | ||
""" Tests than the email is not sent where recaptcha fail the | ||
validation | ||
""" | ||
|
||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {'success': True} | ||
mock_requests.post.return_value = mock_response | ||
|
||
resp = self.client.post(reverse('contactform:index'), { | ||
'email': '[email protected]', | ||
'phone': '06 00 00 00 00', | ||
'comment': 'This is my message content', | ||
'g-recaptcha-response': 'abcdefgh'}) | ||
|
||
self.assertEqual(len(mail.outbox), 1) | ||
|
||
self.assertContains(resp, 'Thanks for your message', status_code=200) | ||
|
||
|
||
class ContactFormTagTest(TestCase): | ||
def setUp(self): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters