more negative url tests

This commit is contained in:
jbannon 2022-06-05 23:12:45 +00:00
parent af236cf1bb
commit 0f48564821
2 changed files with 46 additions and 10 deletions

View file

@ -33,7 +33,8 @@ class YoutubeVideoUrlValidator(StringValidator):
if query.hostname in ("youtube.com", "www.youtube.com"): if query.hostname in ("youtube.com", "www.youtube.com"):
if query.path == "/watch": if query.path == "/watch":
parsed_q = parse_qs(query.query) parsed_q = parse_qs(query.query)
return parsed_q["v"][0] if "v" in parsed_q:
return parsed_q["v"][0]
if query.path[:7] == "/embed/": if query.path[:7] == "/embed/":
return query.path.split("/")[2] return query.path.split("/")[2]
if query.path[:3] == "/v/": if query.path[:3] == "/v/":
@ -80,7 +81,8 @@ class YoutubePlaylistUrlValidator(StringValidator):
if query.hostname in ("youtube.com", "www.youtube.com"): if query.hostname in ("youtube.com", "www.youtube.com"):
if query.path == "/playlist": if query.path == "/playlist":
parsed_q = parse_qs(query.query) parsed_q = parse_qs(query.query)
return parsed_q["list"][0] if "list" in parsed_q:
return parsed_q["list"][0]
return None return None
@ -124,6 +126,10 @@ class YoutubeChannelUrlValidator(StringValidator):
query = urlparse(url) query = urlparse(url)
if query.hostname in ("youtube.com", "www.youtube.com"): if query.hostname in ("youtube.com", "www.youtube.com"):
if any(query.path.startswith(qpath) for qpath in ("/channel/", "/user/", "/c/")): if any(query.path.startswith(qpath) for qpath in ("/channel/", "/user/", "/c/")):
# Ensure there is "/path/id".split('/') = ['', 'path', 'id'] at least three
query_path_split = query.path.split("/")
if len(query_path_split) < 3 or len(query_path_split[2]) == 0:
return None
return f"https://youtube.com{query.path}" return f"https://youtube.com{query.path}"
return None return None

View file

@ -26,8 +26,19 @@ class TestYoutubeVideoUrlValidator:
video_url = YoutubeVideoUrlValidator(name="unit test", value=url).video_url video_url = YoutubeVideoUrlValidator(name="unit test", value=url).video_url
assert video_url == "https://youtube.com/watch?v=dQw4w9WgXcQ" assert video_url == "https://youtube.com/watch?v=dQw4w9WgXcQ"
def test_youtube_video_url_validator_fail(self): @pytest.mark.parametrize(
bad_url = "youtube.cum/asdfd" "bad_url",
[
"utube.com/watch?v=sdfsadf",
"youtube.com/nope?v=sdfsadf",
"youtube.com",
"youtube.com/watch",
"youtube.com/watch?v=",
"youtu.be/",
"youtu.be",
],
)
def test_youtube_video_url_validator_fail(self, bad_url):
expected_error_msg = f"'{bad_url}' is not a valid Youtube video url." expected_error_msg = f"'{bad_url}' is not a valid Youtube video url."
with pytest.raises(ValidationException, match=re.escape(expected_error_msg)): with pytest.raises(ValidationException, match=re.escape(expected_error_msg)):
YoutubeVideoUrlValidator(name="unit test", value=bad_url) YoutubeVideoUrlValidator(name="unit test", value=bad_url)
@ -49,8 +60,18 @@ class TestYoutubePlaylistUrlValidator:
playlist_url == "https://youtube.com/playlist?list=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc" playlist_url == "https://youtube.com/playlist?list=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc"
) )
def test_youtube_playlist_url_validator_fail(self): @pytest.mark.parametrize(
bad_url = "youpoop.com/playlist?list=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc" "bad_url",
[
"youpoop.com/playlist?list=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc",
"youtube.com/playlistlist=PLlaN88a7y2_plecYoJxvRFTLHVbIVAOoc",
"youtube.com",
"youtube.com/playlist/asdfsdfsdf",
"youtube.com/playlist?list=",
"youtube.com/playlist",
],
)
def test_youtube_playlist_url_validator_fail(self, bad_url):
expected_error_msg = f"'{bad_url}' is not a valid Youtube playlist url." expected_error_msg = f"'{bad_url}' is not a valid Youtube playlist url."
with pytest.raises(ValidationException, match=re.escape(expected_error_msg)): with pytest.raises(ValidationException, match=re.escape(expected_error_msg)):
YoutubePlaylistUrlValidator(name="unit test", value=bad_url) YoutubePlaylistUrlValidator(name="unit test", value=bad_url)
@ -90,8 +111,17 @@ class TestYoutubeChannelUrlValidator:
channel_url = YoutubeChannelUrlValidator(name="unit test", value=url).channel_url channel_url = YoutubeChannelUrlValidator(name="unit test", value=url).channel_url
assert channel_url == expected_url assert channel_url == expected_url
def test_youtube_channel_url_validator_fail(self): @pytest.mark.parametrize(
bad_url = "www.youtube.com/cha/UCuAXFkgsw1L7xaCfnd5JJOw" "bad_url",
[
"www.youtube.com/cha/UCuAXFkgsw1L7xaCfnd5JJOw",
"www.nopetube.com/channel/asdfdsf",
"www.youtube.com/channel/",
"www.youtube.com/channel",
"www.youtube.com/channell/asdfasdf",
],
)
def test_youtube_channel_url_validator_fail(self, bad_url):
expected_error_msg = f"'{bad_url}' is not a valid Youtube channel url." expected_error_msg = f"'{bad_url}' is not a valid Youtube channel url."
with pytest.raises(ValidationException, match=re.escape(expected_error_msg)): with pytest.raises(ValidationException, match=re.escape(expected_error_msg)):
YoutubeChannelUrlValidator(name="unit test", value=bad_url) YoutubeChannelUrlValidator(name="unit test", value=bad_url)
@ -113,8 +143,8 @@ class TestSoundcloudUsernameUrlValidator:
username_url = SoundcloudUsernameUrlValidator(name="unit test", value=url).username_url username_url = SoundcloudUsernameUrlValidator(name="unit test", value=url).username_url
assert username_url == "https://soundcloud.com/poop" assert username_url == "https://soundcloud.com/poop"
def test_youtube_playlist_url_validator_fail(self): @pytest.mark.parametrize("bad_url", ["soundcloud.com", "soundnope.lol", "soundcloud.comm/"])
bad_url = "soundcloud.com" def test_youtube_playlist_url_validator_fail(self, bad_url):
expected_error_msg = f"'{bad_url}' is not a valid Soundcloud username url." expected_error_msg = f"'{bad_url}' is not a valid Soundcloud username url."
with pytest.raises(ValidationException, match=re.escape(expected_error_msg)): with pytest.raises(ValidationException, match=re.escape(expected_error_msg)):
SoundcloudUsernameUrlValidator(name="unit test", value=bad_url) SoundcloudUsernameUrlValidator(name="unit test", value=bad_url)