added method that tests endpoints on trailing slashes and removes them
This commit is contained in:
parent
3c01d3953f
commit
0b6d8a0a0c
2 changed files with 96 additions and 5 deletions
|
@ -1,7 +1,6 @@
|
|||
"""
|
||||
Unit tests for `toddlers.utils.anitya`.
|
||||
"""
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
@ -64,6 +63,9 @@ class TestAnityaDoesProjectExistInAnitya:
|
|||
}
|
||||
self.anitya_obj = anitya.set_anitya(config)
|
||||
self.anitya_obj._requests_session = Mock()
|
||||
self.anitya_obj.remove_trailing_slashes_from_url = Mock(
|
||||
return_value="https://release-monitoring.org/api/v2/projects/"
|
||||
)
|
||||
|
||||
def test_does_project_exists_in_anitya(self):
|
||||
"""
|
||||
|
@ -169,6 +171,9 @@ class TestAnityaDoesPackageExistInAnitya:
|
|||
}
|
||||
self.anitya_obj = anitya.set_anitya(config)
|
||||
self.anitya_obj._requests_session = Mock()
|
||||
self.anitya_obj.remove_trailing_slashes_from_url = Mock(
|
||||
return_value="https://release-monitoring.org/api/v2/packages/"
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"project_name, expected_project_name, expected_result",
|
||||
|
@ -279,6 +284,9 @@ class TestAnityaCreateProjectInAnitya:
|
|||
}
|
||||
self.anitya_obj = anitya.set_anitya(config)
|
||||
self.anitya_obj._requests_session = Mock()
|
||||
self.anitya_obj.remove_trailing_slashes_from_url = Mock(
|
||||
return_value="https://release-monitoring.org/api/v2/projects/"
|
||||
)
|
||||
|
||||
def test_create_project_in_anitya_successful_creation(self):
|
||||
"""
|
||||
|
@ -354,6 +362,9 @@ class TestAnityaCreatePackageInAnitya:
|
|||
}
|
||||
self.anitya_obj = anitya.set_anitya(config)
|
||||
self.anitya_obj._requests_session = Mock()
|
||||
self.anitya_obj.remove_trailing_slashes_from_url = Mock(
|
||||
return_value="https://release-monitoring.org/api/v2/packages/"
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"response_code, expected_result",
|
||||
|
@ -394,3 +405,54 @@ class TestAnityaCreatePackageInAnitya:
|
|||
data=test_data,
|
||||
headers={"Authorization": "token TOKEN"},
|
||||
)
|
||||
|
||||
|
||||
class TestAnityaRemoveTrailingSlashesFromUrl:
|
||||
"""
|
||||
Test class for `toddlers.anitya.Anitya.remove_trailing_slashes_from_url` method.
|
||||
"""
|
||||
|
||||
def setup_method(self):
|
||||
"""
|
||||
Setup method for test class.
|
||||
"""
|
||||
config = {
|
||||
"anitya_endpoint": "https://release-monitoring.org",
|
||||
"anitya_access_token": "TOKEN",
|
||||
}
|
||||
self.anitya_obj = anitya.set_anitya(config)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"url, expected_result",
|
||||
[
|
||||
("https://release-monitoring.org/project/123", "https://release-monitoring.org/project/123"),
|
||||
("http://lalalal.com", "http://lalalal.com"),
|
||||
("https://lalalal.com//lalal/la", "https://lalalal.com/lalal/la"),
|
||||
],
|
||||
)
|
||||
def test_remove_trailing_slashes_from_url(self, url, expected_result):
|
||||
"""
|
||||
Assert that method successfully removes trailing slashes from url.
|
||||
"""
|
||||
result = self.anitya_obj.remove_trailing_slashes_from_url(url)
|
||||
assert result == expected_result
|
||||
|
||||
def test_remove_trailing_slashes_from_url_url_does_not_contain_protocol(self):
|
||||
"""
|
||||
Assert that if method url without protocol it raises ValueError.
|
||||
"""
|
||||
url = "https://"
|
||||
with pytest.raises(
|
||||
ValueError, match="Url must contain protocol and rest."
|
||||
):
|
||||
self.anitya_obj.remove_trailing_slashes_from_url(url)
|
||||
|
||||
def test_remove_trailing_slashes_from_url_url_does_not_contain_rest_part(self):
|
||||
"""
|
||||
Assert that if method url without rest part after protocol it raises ValueError.
|
||||
"""
|
||||
url = "release-monitoring.org/project/123"
|
||||
with pytest.raises(
|
||||
ValueError, match="Url must contain protocol and rest."
|
||||
):
|
||||
self.anitya_obj.remove_trailing_slashes_from_url(url)
|
||||
|
|
|
@ -16,6 +16,7 @@ Examples:
|
|||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
from toddlers.utils import requests
|
||||
|
@ -78,9 +79,10 @@ class Anitya(object):
|
|||
projects_params = {
|
||||
"name": project_name,
|
||||
}
|
||||
projects_endpoint = self._anitya_endpoint + "/api/v2/projects/"
|
||||
endpoint = self._anitya_endpoint + "/api/v2/projects/"
|
||||
endpoint = self.remove_trailing_slashes_from_url(endpoint)
|
||||
projects_response = self._requests_session.get(
|
||||
projects_endpoint, params=projects_params
|
||||
endpoint, params=projects_params
|
||||
)
|
||||
if projects_response.status_code != 200:
|
||||
log.debug("Project '{0}' not found in Anitya.".format(project_name))
|
||||
|
@ -115,13 +117,14 @@ class Anitya(object):
|
|||
False if package exist but his project is different from provided project name
|
||||
True if package exist and his project is correct
|
||||
"""
|
||||
packages_endpoint = self._anitya_endpoint + "/api/v2/packages/"
|
||||
endpoint = self._anitya_endpoint + "/api/v2/packages/"
|
||||
endpoint = self.remove_trailing_slashes_from_url(endpoint)
|
||||
packages_params = {
|
||||
"name": package_name,
|
||||
"distribution": distribution,
|
||||
}
|
||||
packages_response = self._requests_session.get(
|
||||
packages_endpoint, params=packages_params
|
||||
endpoint, params=packages_params
|
||||
)
|
||||
if packages_response.status_code != 200:
|
||||
log.info("Package '{0}' not found in Anitya.".format(package_name))
|
||||
|
@ -157,6 +160,7 @@ class Anitya(object):
|
|||
"""
|
||||
headers = {"Authorization": "token " + self._anitya_token}
|
||||
endpoint = self._anitya_endpoint + "/api/v2/projects/"
|
||||
endpoint = self.remove_trailing_slashes_from_url(endpoint)
|
||||
payload = {
|
||||
"name": name,
|
||||
"homepage": homepage,
|
||||
|
@ -195,6 +199,7 @@ class Anitya(object):
|
|||
"""
|
||||
headers = {"Authorization": "token " + self._anitya_token}
|
||||
endpoint = self._anitya_endpoint + "/api/v2/packages/"
|
||||
endpoint = self.remove_trailing_slashes_from_url(endpoint)
|
||||
payload = {
|
||||
"package_name": package_name,
|
||||
"project_name": project_name,
|
||||
|
@ -214,3 +219,27 @@ class Anitya(object):
|
|||
elif response.status_code == 201:
|
||||
return "Success"
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def remove_trailing_slashes_from_url(url: str) -> Optional[str]:
|
||||
"""
|
||||
Remove trailing slashes from url.
|
||||
|
||||
Params:
|
||||
url (str): The url to remove trailing slashes from.
|
||||
|
||||
Returns:
|
||||
url without trailing slashes.
|
||||
|
||||
Raises:
|
||||
ValueError: If url does not contain two parts, basically wrong url.
|
||||
"""
|
||||
url_parts = re.split(r"(https?://)", url)
|
||||
print(url_parts)
|
||||
if len(url_parts) > 1 and url_parts[2] != "":
|
||||
protocol = url_parts[1]
|
||||
rest = url_parts[2]
|
||||
rest = re.sub(r"/+", "/", rest)
|
||||
return protocol + rest
|
||||
else:
|
||||
raise ValueError("Url must contain protocol and rest.")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue