Skip to content

Commit

Permalink
Rename construct_url to format_url
Browse files Browse the repository at this point in the history
  • Loading branch information
nf679 committed Dec 4, 2024
1 parent 299135c commit 570143b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 27 deletions.
6 changes: 3 additions & 3 deletions nlds/authenticators/jasmin_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from nlds.authenticators.base_authenticator import BaseAuthenticator
from nlds.server_config import load_config
from nlds.utils.construct_url import construct_url
from nlds.utils.format_url import format_url
from nlds_processors.catalog.catalog_models import File, Holding, Transaction
from retry import retry
import requests
Expand Down Expand Up @@ -195,7 +195,7 @@ def authenticate_user_group_role(self, oauth_token: str, user: str, group: str):
"Authorization": f"Bearer {oauth_token}",
}
# Construct the URL
url = construct_url(
url = format_url(
[config["user_grants_url"], user, "grants"],
{"category": "GWS", "service": group},
)
Expand Down Expand Up @@ -301,7 +301,7 @@ def get_service_information(self, service_name: str):
"Authorization": f"Bearer {config["client_token"]}",
}
# Contact the user_services_url to get the information about the services
url = construct_url([config["project_services_url"]], {"name": {service_name}})
url = format_url([config["project_services_url"]], {"name": service_name})
print("jasmin_authenticator.py URL:", url)
print("jasmin_authenticator.py token_headers:", token_headers)
try:
Expand Down
3 changes: 3 additions & 0 deletions nlds/routers/quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async def get(token: str = Depends(authenticate_token),
RMQP.MSG_DATA: {},
RMQP.MSG_TYPE: RMQP.MSG_TYPE_STANDARD
}
print("quota.py MESSAGE DICT:", msg_dict)
# add the metadata
meta_dict = {}
if (label):
Expand Down Expand Up @@ -94,8 +95,10 @@ async def get(token: str = Depends(authenticate_token),
response = await rpc_publisher.call(
msg_dict=msg_dict, routing_key=routing_key
)
print("quota.py REACHED RESPONSE", response)
# Check if response is valid or whether the request timed out
if response is not None:
print('quota.py RESPONSE:', response)
# convert byte response to str
response = response.decode()

Expand Down
2 changes: 1 addition & 1 deletion nlds/utils/construct_url.py → nlds/utils/format_url.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from urllib.parse import urljoin, urlencode


def construct_url(url_parts, query_params=None):
def format_url(url_parts, query_params=None):
"""
Constructs a URL from a list of parts.
Expand Down
6 changes: 0 additions & 6 deletions nlds_processors/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
from sqlalchemy import func, Enum
from sqlalchemy.exc import IntegrityError, OperationalError, ArgumentError, \
NoResultFound
from retry import retry
import requests
import json
from nlds.server_config import load_config
from nlds.utils.construct_url import construct_url

from nlds_processors.catalog.catalog_models import CatalogBase, File, Holding,\
Location, Transaction, Aggregation, Storage, Tag
from nlds_processors.db_mixin import DBMixin
Expand Down
6 changes: 5 additions & 1 deletion nlds_processors/catalog/catalog_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def __init__(self, queue=DEFAULT_QUEUE_NAME):
self.catalog = None
self.reroutelist = []
self.retrievedict = {}
self.authenticator = Authenticator()


@property
Expand Down Expand Up @@ -1865,10 +1866,12 @@ def _catalog_quota(self, body: Dict, properties: Header) -> None:
return
else:
# Unpack if no problems found in parsing
print("MESSAGE VARS", message_vars)
user, group, token = message_vars

try:
group_quota = Authenticator.get_tape_quota(oauth_token=token, service_name=group)
group_quota = self.authenticator.get_tape_quota(service_name=group)
print("catalog_worker.py GROUP_QUOTA:", group_quota)
except CatalogError as e:
# failed to get the holdings - send a return message saying so
self.log(e.message, self.RK_LOG_ERROR)
Expand Down Expand Up @@ -2066,6 +2069,7 @@ def callback(self, ch: Channel, method: Method, properties: Header,

elif (api_method == self.RK_QUOTA):
# don't need to split any routing key for an RPC method
print("catalog_worker.py ELIF LOOP !!")
self._catalog_quota(body, properties)

# If received system test message, reply to it (this is for system status check)
Expand Down
18 changes: 9 additions & 9 deletions tests/nlds/test_jasmin_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from nlds.authenticators.jasmin_authenticator import JasminAuthenticator
from nlds_processors.catalog.catalog_models import Holding
from nlds.utils.construct_url import construct_url
from nlds.utils.format_url import format_url


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -227,7 +227,7 @@ def test_authenticate_user_group_role(
):
"""Check whether the user has a manager/deputy role within the specified group."""
# Create the URL
url = construct_url(
url = format_url(
["https://mock.url/api/v1/users", user, "grants"],
{"category": "GWS", "service": group},
)
Expand Down Expand Up @@ -289,15 +289,15 @@ class TestGetProjectsServices:
url = f"{user_services_url}?name=test_service"

@pytest.fixture()
def mock_construct_url(self, *args, **kwargs):
"""Mock the construct_url function to make it return the test url."""
def mock_format_url(self, *args, **kwargs):
"""Mock the format_url function to make it return the test url."""
return self.url

def test_get_projects_services_success(self,monkeypatch):
"""Test a successful instance of get_projects_services."""

monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.load_config", mock_load_config)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.construct_url", self.mock_construct_url)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.format_url", self.mock_format_url)

class MockResponse:
"""Mock the response to return a 200 status code and the test text."""
Expand All @@ -324,7 +324,7 @@ def test_get_projects_services_connection_error(self,monkeypatch, quotas):
"""Test an unsuccessful instance of get_projects_services due to connection error."""

monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.load_config", mock_load_config)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.construct_url", self.mock_construct_url)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.format_url", self.mock_format_url)

def mock_get(*args, **kwargs):
"""Mock the get function to give a ConnectionError."""
Expand All @@ -346,7 +346,7 @@ def mock_load_config_key_error():
return {"authentication": {"jasmin_authenticator": {"other_url": "test.com"}}}

monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.load_config", mock_load_config_key_error)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.construct_url", self.mock_construct_url)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.format_url", self.mock_format_url)

def mock_get(*args, **kwargs):
"""Mock the get function to give the KeyError."""
Expand All @@ -365,7 +365,7 @@ def test_get_projects_services_json_error(self, monkeypatch):
"""Test an unsuccessful instance of get_projects_services due to a JSON error."""

monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.load_config", mock_load_config)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.construct_url", self.mock_construct_url)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.format_url", self.mock_format_url)

class MockInvalidJSONResponse:
"""Mock the response to return a 200 status code and the JSON decode error."""
Expand All @@ -392,7 +392,7 @@ def test_get_projects_services_404_error(self,monkeypatch):
"""Test an unsuccessful instance of get_projects_services due to a 404 error."""

monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.load_config", mock_load_config)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.construct_url", self.mock_construct_url)
monkeypatch.setattr("jasmin_authenticator.JasminAuthenticator.format_url", self.mock_format_url)

class MockResponse:
"""Mock the response to return a 401 status code and the relevant text."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
from nlds.utils.construct_url import construct_url
from nlds.utils.format_url import format_url


def test_no_parts():
assert construct_url([]) == ""
assert format_url([]) == ""


def test_single_part():
assert construct_url(["http://example.com"]) == "http://example.com"
assert format_url(["http://example.com"]) == "http://example.com"


def test_multiple_parts():
url_parts = ["http://example.com", "path", "to", "resource"]
expected_url = "http://example.com/path/to/resource"
assert construct_url(url_parts) == expected_url
assert format_url(url_parts) == expected_url


def test_with_query_params():
url_parts = ["http://example.com", "path", "to", "resource"]
query_params = {"key1": "value1", "key2": "value2"}
expected_url = "http://example.com/path/to/resource?key1=value1&key2=value2"
assert construct_url(url_parts, query_params) == expected_url
assert format_url(url_parts, query_params) == expected_url


def test_empty_query_params():
url_parts = ["http://example.com", "path", "to", "resource"]
query_params = {}
expected_url = "http://example.com/path/to/resource"
assert construct_url(url_parts, query_params) == expected_url
assert format_url(url_parts, query_params) == expected_url


def test_complex_query_params():
url_parts = ["http://example.com", "search"]
query_params = {"q": "test search", "page": "1", "sort": "asc"}
expected_url = "http://example.com/search?q=test+search&page=1&sort=asc"
assert construct_url(url_parts, query_params) == expected_url
assert format_url(url_parts, query_params) == expected_url

0 comments on commit 570143b

Please sign in to comment.