Skip to content
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

Validate url from webhook notification #4983

Merged
merged 7 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,14 @@ def save(self, commit=True):
self.project.webhook_notifications.add(self.webhook)
return self.project

def clean_url(self):
url = self.cleaned_data.get('url')
if not url:
raise forms.ValidationError(
_('This field is required.')
dojutsu-user marked this conversation as resolved.
Show resolved Hide resolved
)
return url

class Meta:
model = WebHook
fields = ['url']
Expand Down
21 changes: 10 additions & 11 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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 is_valid method on the right form only after checking request.POST.keys.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That raises this type of situation
screenshot from 2018-12-11 19-59-26

I have submitted empty form for Webhook Notification URL, but it is also showing error for Email Field.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

@dojutsu-user dojutsu-user Dec 11, 2018

Choose a reason for hiding this comment

The 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).
This is what is happening

>>> 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 is_valid() method is not run

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even the user hasn't submitted the email_form, the error will rise and will be shown in the templates even if the is_valid() method is not run

Really?! Wow! I didn't know that and it's an unexpected behavior to me.

Copy link
Member Author

@dojutsu-user dojutsu-user Dec 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it's because email form will then accept data=request.POST,
but yeahh.. it's unexpected the error is generated without calling is_valid() method

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()
Expand Down
69 changes: 69 additions & 0 deletions readthedocs/rtd_tests/tests/test_project_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,8 @@
ProjectExtraForm,
TranslationForm,
UpdateProjectForm,
WebHookForm,
EmailHookForm
)
from readthedocs.projects.models import Project

Expand Down Expand Up @@ -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()
dojutsu-user marked this conversation as resolved.
Show resolved Hide resolved
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': 'test@email.com'
}
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)