Skip to content

Commit 3809e45

Browse files
committed
more tests + validation
1 parent bdd3332 commit 3809e45

3 files changed

Lines changed: 107 additions & 7 deletions

File tree

backend/api/submissions/tests/test_edit_submission.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from uuid import uuid4
23
from users.tests.factories import UserFactory
34
from conferences.tests.factories import ConferenceFactory
@@ -615,6 +616,101 @@ def test_update_submission_with_wrong_file_type(graphql_client, user):
615616
] == ["File not found"]
616617

617618

619+
def test_update_submission_with_too_long_url(graphql_client, user):
620+
conference = ConferenceFactory(
621+
topics=("life", "diy"),
622+
languages=("it", "en"),
623+
durations=("10", "20"),
624+
active_cfp=True,
625+
audience_levels=("adult", "senior"),
626+
submission_types=("talk", "workshop"),
627+
)
628+
629+
submission = SubmissionFactory(
630+
speaker_id=user.id,
631+
custom_topic="life",
632+
custom_duration="10m",
633+
custom_audience_level="adult",
634+
custom_submission_type="talk",
635+
languages=["it"],
636+
tags=["python", "ml"],
637+
conference=conference,
638+
speaker_level=Submission.SPEAKER_LEVELS.intermediate,
639+
previous_talk_video="https://www.youtube.com/watch?v=SlPhMPnQ58k",
640+
)
641+
642+
graphql_client.force_login(user)
643+
644+
response = _update_submission(
645+
graphql_client,
646+
submission=submission,
647+
new_materials=[
648+
{
649+
"fileId": None,
650+
"url": f"https://www.googl{'e' * 2049}.com",
651+
"name": "name",
652+
},
653+
],
654+
)
655+
656+
assert response["data"]["updateSubmission"]["__typename"] == "SendSubmissionErrors"
657+
assert response["data"]["updateSubmission"]["errors"]["validationMaterials"][0][
658+
"url"
659+
] == ["URL is too long"]
660+
661+
662+
@pytest.mark.parametrize(
663+
"url",
664+
[
665+
"ftp://www.google.com",
666+
"//www.google.com",
667+
"google.com/test",
668+
"no/url",
669+
],
670+
)
671+
def test_update_submission_with_invalid_urls(graphql_client, user, url):
672+
conference = ConferenceFactory(
673+
topics=("life", "diy"),
674+
languages=("it", "en"),
675+
durations=("10", "20"),
676+
active_cfp=True,
677+
audience_levels=("adult", "senior"),
678+
submission_types=("talk", "workshop"),
679+
)
680+
681+
submission = SubmissionFactory(
682+
speaker_id=user.id,
683+
custom_topic="life",
684+
custom_duration="10m",
685+
custom_audience_level="adult",
686+
custom_submission_type="talk",
687+
languages=["it"],
688+
tags=["python", "ml"],
689+
conference=conference,
690+
speaker_level=Submission.SPEAKER_LEVELS.intermediate,
691+
previous_talk_video="https://www.youtube.com/watch?v=SlPhMPnQ58k",
692+
)
693+
694+
graphql_client.force_login(user)
695+
696+
response = _update_submission(
697+
graphql_client,
698+
submission=submission,
699+
new_materials=[
700+
{
701+
"fileId": None,
702+
"url": url,
703+
"name": "name",
704+
},
705+
],
706+
)
707+
708+
assert response["data"]["updateSubmission"]["__typename"] == "SendSubmissionErrors"
709+
assert response["data"]["updateSubmission"]["errors"]["validationMaterials"][0][
710+
"url"
711+
] == ["Invalid URL"]
712+
713+
618714
def test_update_submission_with_too_many_materials(graphql_client, user):
619715
conference = ConferenceFactory(
620716
topics=("life", "diy"),

backend/api/submissions/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def validate(
234234
errors.add_error("file_id", "File not found")
235235

236236
if self.url:
237-
if not validate_url(self.url):
237+
if len(self.url) > 2048:
238+
errors.add_error("url", "URL is too long")
239+
elif not validate_url(self.url):
238240
errors.add_error("url", "Invalid URL")
239241

240242
return errors

backend/api/utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from urllib.parse import urlparse
12
from django.core.validators import (
2-
URLValidator,
33
validate_email as original_validate_email,
44
)
55
from django.core.exceptions import ValidationError
@@ -22,10 +22,12 @@ def validate_email(email: str) -> bool:
2222

2323

2424
def validate_url(url: str) -> bool:
25-
validate = URLValidator()
25+
parsed_url = urlparse(url)
2626

27-
try:
28-
validate(url)
29-
return True
30-
except ValidationError:
27+
if parsed_url.scheme not in ["http", "https"]:
28+
return False
29+
30+
if not parsed_url.netloc:
3131
return False
32+
33+
return True

0 commit comments

Comments
 (0)