-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Validate url from webhook notification #4983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
62c3a63
03c4b9b
7e7d3c7
abb8a46
b985e45
8e314dd
4a7527f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -527,19 +527,18 @@ def project_notifications(request, project_slug): | |
slug=project_slug, | ||
) | ||
|
||
email_form = EmailHookForm(data=request.POST or None, project=project) | ||
webhook_form = WebHookForm(data=request.POST or None, project=project) | ||
email_form = EmailHookForm(data=None, project=project) | ||
webhook_form = WebHookForm(data=None, project=project) | ||
|
||
if request.method == 'POST': | ||
if email_form.is_valid(): | ||
email_form.save() | ||
if webhook_form.is_valid(): | ||
webhook_form.save() | ||
project_dashboard = reverse( | ||
'projects_notifications', | ||
args=[project.slug], | ||
) | ||
return HttpResponseRedirect(project_dashboard) | ||
if 'email' in request.POST.keys(): | ||
email_form = EmailHookForm(data=request.POST, project=project) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can leave the all this code as it was, but calling the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the flow to me: if 'url' in request.POST.keys() and webhook_form.is_valid():
webhook_form.save()
if 'email' in request.POST.keys() and email_form.is_valid():
email_form.save() The rest of the code should be as it was before. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will also give rise to the same situation (tried on local server). >>> from readthedocs.projects.forms import WebHookForm
>>> from readthedocs.projects.models import Project
>>> proj = Project.objects.get(slug='pikachu-demo')
>>> data = {'url': ''}
>>> form = WebHookForm(data=data, project=proj)
>>> form
<WebHookForm bound=True, valid=Unknown, fields=(url)>
>>> form.errors
{'url': ['This field is required.']}
>>> form
<WebHookForm bound=True, valid=False, fields=(url)> So with these lines email_form = EmailHookForm(data=request.POST or None, project=project)
webhook_form = WebHookForm(data=request.POST or None, project=project) even the user hasn't submitted the email_form, the error will rise and will be shown in the templates even if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Really?! Wow! I didn't know that and it's an unexpected behavior to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, it's because email form will then accept |
||
if email_form.is_valid(): | ||
email_form.save() | ||
elif 'url' in request.POST.keys(): | ||
webhook_form = WebHookForm(data=request.POST, project=project) | ||
if webhook_form.is_valid(): | ||
webhook_form.save() | ||
|
||
emails = project.emailhook_notifications.all() | ||
urls = project.webhook_notifications.all() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from django.test.utils import override_settings | ||
from django_dynamic_fixture import get | ||
from textclassifier.validators import ClassifierValidator | ||
from django.core.exceptions import ValidationError | ||
|
||
from readthedocs.builds.constants import LATEST | ||
from readthedocs.builds.models import Version | ||
|
@@ -30,6 +31,8 @@ | |
ProjectExtraForm, | ||
TranslationForm, | ||
UpdateProjectForm, | ||
WebHookForm, | ||
EmailHookForm | ||
) | ||
from readthedocs.projects.models import Project | ||
|
||
|
@@ -478,3 +481,69 @@ def test_can_change_language_to_self_lang(self): | |
instance=self.project_b_en | ||
) | ||
self.assertTrue(form.is_valid()) | ||
|
||
|
||
class TestNotificationForm(TestCase): | ||
|
||
def setUp(self): | ||
self.project = get(Project) | ||
|
||
def test_webhookform(self): | ||
self.assertEqual(self.project.webhook_notifications.all().count(), 0) | ||
|
||
data = { | ||
'url': 'http://www.example.com/' | ||
} | ||
form = WebHookForm(data=data, project=self.project) | ||
self.assertTrue(form.is_valid()) | ||
form.save() | ||
self.assertEqual(self.project.webhook_notifications.all().count(), 1) | ||
|
||
def test_wrong_inputs_in_webhookform(self): | ||
self.assertEqual(self.project.webhook_notifications.all().count(), 0) | ||
|
||
data = { | ||
'url': '' | ||
} | ||
form = WebHookForm(data=data, project=self.project) | ||
self.assertFalse(form.is_valid()) | ||
self.assertDictEqual(form.errors, {'url': ['This field is required.']}) | ||
self.assertEqual(self.project.webhook_notifications.all().count(), 0) | ||
|
||
data = { | ||
'url': 'wrong-url' | ||
} | ||
form = WebHookForm(data=data, project=self.project) | ||
self.assertFalse(form.is_valid()) | ||
self.assertDictEqual(form.errors, {'url': ['Enter a valid URL.']}) | ||
self.assertEqual(self.project.webhook_notifications.all().count(), 0) | ||
|
||
def test_emailhookform(self): | ||
self.assertEqual(self.project.emailhook_notifications.all().count(), 0) | ||
|
||
data = { | ||
'email': '[email protected]' | ||
} | ||
form = EmailHookForm(data=data, project=self.project) | ||
self.assertTrue(form.is_valid()) | ||
form.save() | ||
self.assertEqual(self.project.emailhook_notifications.all().count(), 1) | ||
|
||
def test_wrong_inputs_in_emailhookform(self): | ||
self.assertEqual(self.project.emailhook_notifications.all().count(), 0) | ||
|
||
data = { | ||
'email': 'wrong_email@' | ||
} | ||
form = EmailHookForm(data=data, project=self.project) | ||
self.assertFalse(form.is_valid()) | ||
self.assertDictEqual(form.errors, {'email': ['Enter a valid email address.']}) | ||
self.assertEqual(self.project.emailhook_notifications.all().count(), 0) | ||
|
||
data = { | ||
'email': '' | ||
} | ||
form = EmailHookForm(data=data, project=self.project) | ||
self.assertFalse(form.is_valid()) | ||
self.assertDictEqual(form.errors, {'email': ['This field is required.']}) | ||
self.assertEqual(self.project.emailhook_notifications.all().count(), 0) |
Uh oh!
There was an error while loading. Please reload this page.