Skip to content

Commit 3bfec17

Browse files
authored
Merge pull request #3680 from chirathr/webhook_notifications_url_size
Webhook notification URL size validation check
2 parents fd73963 + 73c6ca8 commit 3bfec17

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

readthedocs/projects/forms.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -530,25 +530,24 @@ def save(self):
530530
return self.project
531531

532532

533-
class WebHookForm(forms.Form):
533+
class WebHookForm(forms.ModelForm):
534534

535535
"""Project webhook form."""
536536

537-
url = forms.URLField()
538-
539537
def __init__(self, *args, **kwargs):
540538
self.project = kwargs.pop('project', None)
541539
super(WebHookForm, self).__init__(*args, **kwargs)
542540

543-
def clean_url(self):
541+
def save(self, commit=True):
544542
self.webhook = WebHook.objects.get_or_create(
545543
url=self.cleaned_data['url'], project=self.project)[0]
546-
return self.webhook
547-
548-
def save(self):
549544
self.project.webhook_notifications.add(self.webhook)
550545
return self.project
551546

547+
class Meta:
548+
model = WebHook
549+
fields = ['url']
550+
552551

553552
class TranslationBaseForm(forms.Form):
554553

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.16 on 2018-11-06 23:12
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('projects', '0031_add_modified_date_importedfile'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='webhook',
17+
name='url',
18+
field=models.URLField(blank=True, help_text='URL to send the webhook to', max_length=600),
19+
),
20+
]

readthedocs/projects/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def __str__(self):
972972

973973
@python_2_unicode_compatible
974974
class WebHook(Notification):
975-
url = models.URLField(blank=True,
975+
url = models.URLField(max_length=600, blank=True,
976976
help_text=_('URL to send the webhook to'))
977977

978978
def __str__(self):

readthedocs/rtd_tests/tests/test_build_notifications.py

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from readthedocs.builds.models import Build, Version
1313
from readthedocs.projects.models import Project, EmailHook, WebHook
1414
from readthedocs.projects.tasks import send_notifications
15+
from readthedocs.projects.forms import WebHookForm
1516

1617

1718
class BuildNotificationsTests(TestCase):
@@ -46,3 +47,34 @@ def test_send_email_and_webhook__notification(self):
4647
send_notifications(self.version.pk, self.build.pk)
4748
mock.assert_called_once()
4849
self.assertEqual(len(mail.outbox), 1)
50+
51+
52+
class TestForms(TestCase):
53+
54+
def setUp(self):
55+
self.project = fixture.get(Project)
56+
self.version = fixture.get(Version, project=self.project)
57+
self.build = fixture.get(Build, version=self.version)
58+
59+
def test_webhook_form_url_length(self):
60+
form = WebHookForm(
61+
{
62+
'url': 'https://foobar.com',
63+
},
64+
project=self.project,
65+
)
66+
self.assertTrue(form.is_valid())
67+
68+
form = WebHookForm(
69+
{
70+
'url': 'foo' * 500,
71+
},
72+
project=self.project,
73+
)
74+
self.assertFalse(form.is_valid())
75+
self.assertEqual(
76+
form.errors,
77+
{'url':
78+
['Enter a valid URL.',
79+
'Ensure this value has at most 600 characters (it has 1507).']
80+
})

requirements/testing.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ pytest-mock==1.10.0
2121

2222
# local debugging tools
2323
datadiff
24+
ipdb

0 commit comments

Comments
 (0)