Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion meemoo/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from io import BytesIO
from ftplib import FTP as BuiltinFTP
from urllib.parse import urlparse
import re

# Third-party imports
from viaa.configuration import ConfigParser
Expand Down Expand Up @@ -236,5 +235,26 @@ def put(self, content_bytes, destination_path, destination_filename):
raise e


def make_url(base_url, *path):
"""Creates a valid url.

Args:
base_url (string): The base url.
*path (variable length argument list, optional):
The path to be appended to the base url.

Returns:
url (string): The created url.

Raises a TypeError if base_url is None.
"""

if base_url is None:
raise TypeError('base_url is required')
url = base_url.rstrip('/') # Remove optional trailing slash
for item in path:
url = f"{url}/{item}"
return url

# vim modeline
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
4 changes: 3 additions & 1 deletion meemoo/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from viaa.observability import logging

# Local imports
from meemoo.helpers import make_url

# Get logger
config = ConfigParser()
Expand Down Expand Up @@ -103,7 +104,8 @@ def get_organisation(self, or_id):
# Make sure the OR is uppercase. Organisation api needs it.
or_id = or_id[:2].upper() + or_id[2:]

response = requests.get(f"{self.host}org/{or_id}")
url = make_url(self.host, 'org', or_id)
response = requests.get(url)
organisation = response.json()["data"]
return organisation

Expand Down
57 changes: 56 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
normalize_or_id,
is_event_valid,
InvalidEventException,
get_destination_for_cp
get_destination_for_cp,
make_url
)
from tests.resources import (
S3_MOCK_ESSENCE_EVENT,
Expand Down Expand Up @@ -169,6 +170,7 @@ def test_is_event_valid_extra_fields():
# Assert
assert event_valid is None


@pytest.mark.parametrize(
"environment, cp, file_type, expected_destination",
[
Expand All @@ -189,4 +191,57 @@ def test_file_destination(environment, cp, file_type, expected_destination):
assert destination == expected_destination


def test_make_url_with_trailing_slash():
# Arrange
base_url = 'https://example-url/'
# Act
url = make_url(base_url, 'org', 'test-org-id')
# Assert
assert url == 'https://example-url/org/test-org-id'


def test_make_url_multiple_trailing_slashes():
# Arrange
base_url = 'https://example-url////'
# Act
url = make_url(base_url, 'org', 'test-org-id')
# Assert
assert url == 'https://example-url/org/test-org-id'


def test_make_url_missing_trailing_slash():
# Arrange
base_url = 'https://example-url'
# Act
url = make_url(base_url, 'org', 'test-org-id')
# Assert
assert url == 'https://example-url/org/test-org-id'


def test_make_url_long_path():
# Arrange
base_url = 'https://example-url'
# Act
url = make_url(base_url, 'api', 'deeply', 'nested', 'test', 'path')
# Assert
assert url == 'https://example-url/api/deeply/nested/test/path'


def test_make_url_none_base_url():
# Arrange
base_url = None
# Act and assert
with pytest.raises(TypeError) as excinfo:
make_url(base_url, 'org', 'test-org-id')
assert 'base_url is required' in str(excinfo.value)


def test_make_url_no_path():
# Arrange
base_url = 'https://example-url'
# Act
url = make_url(base_url)
# Assert
assert url == 'https://example-url'

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 smartindent