mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-11-25 00:30:09 +08:00
Python 3.10 reached its end-of-life in October 2026. Also: - Run `make pyupgrade` and `make format` - Fix new ruff and mypy errors - Run `uv lock` and `sh lib/galaxy/dependencies/update.sh --export-only` - Run `make update-client-api-schema`
108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
import json
|
|
from datetime import (
|
|
datetime,
|
|
timedelta,
|
|
UTC,
|
|
)
|
|
from typing import (
|
|
Any,
|
|
cast,
|
|
)
|
|
|
|
from galaxy.model import User
|
|
from galaxy.tools.actions.upload import FetchUploadToolAction
|
|
from galaxy.tools.data_fetch_utils import (
|
|
compute_token_expiry_for_provider,
|
|
iter_fetch_request_urls,
|
|
iter_fetch_urls,
|
|
)
|
|
|
|
|
|
class ConcreteFetchUploadToolAction(FetchUploadToolAction):
|
|
def get_output_name(self, *args: Any, **kwargs: Any) -> str:
|
|
raise NotImplementedError
|
|
|
|
|
|
class DummyToken:
|
|
def __init__(self, provider, expiration_time):
|
|
self.provider = provider
|
|
now_ts = int(datetime.now(UTC).timestamp())
|
|
self.extra_data = {
|
|
"auth_time": now_ts,
|
|
"expires": int(expiration_time.timestamp()) - now_ts,
|
|
}
|
|
|
|
|
|
class DummyUser:
|
|
def __init__(self, social_auth):
|
|
self.social_auth = social_auth
|
|
|
|
|
|
def _truncate_to_seconds(value: datetime) -> datetime:
|
|
return value.replace(microsecond=0)
|
|
|
|
|
|
def test_compute_token_expiry_for_provider_returns_none_for_no_user():
|
|
assert compute_token_expiry_for_provider(None, "oidc") is None
|
|
|
|
|
|
def test_compute_token_expiry_for_provider_returns_none_for_empty_social_auth():
|
|
assert compute_token_expiry_for_provider(cast(User, DummyUser([])), "oidc") is None
|
|
|
|
|
|
def test_compute_token_expiry_for_provider_returns_expiry_for_matching_provider():
|
|
expiry = datetime.now(UTC) + timedelta(hours=1)
|
|
user = DummyUser([DummyToken("oidc", expiry)])
|
|
assert compute_token_expiry_for_provider(cast(User, user), "oidc") == _truncate_to_seconds(expiry)
|
|
|
|
|
|
def test_compute_token_expiry_for_provider_ignores_other_providers():
|
|
expiry = datetime.now(UTC) + timedelta(hours=1)
|
|
user = DummyUser([DummyToken("google", expiry)])
|
|
assert compute_token_expiry_for_provider(cast(User, user), "oidc") is None
|
|
|
|
|
|
def test_compute_token_expiry_for_provider_returns_correct_expiry_among_multiple_providers():
|
|
oidc_expiry = datetime.now(UTC) + timedelta(hours=2)
|
|
google_expiry = datetime.now(UTC) + timedelta(minutes=5)
|
|
user = DummyUser([DummyToken("google", google_expiry), DummyToken("oidc", oidc_expiry)])
|
|
result = compute_token_expiry_for_provider(cast(User, user), "oidc")
|
|
assert result == _truncate_to_seconds(oidc_expiry)
|
|
|
|
|
|
def test_compute_token_expiry_for_provider_returns_none_when_token_missing_auth_time_or_expires():
|
|
token = DummyToken.__new__(DummyToken)
|
|
token.provider = "oidc"
|
|
token.extra_data = {}
|
|
user = DummyUser([token])
|
|
assert compute_token_expiry_for_provider(cast(User, user), "oidc") is None
|
|
|
|
|
|
def test_iter_fetch_request_urls_extracts_urls():
|
|
request = {
|
|
"targets": [
|
|
{"elements": [{"src": "url", "url": "gxuserfiles://abc/x"}, {"src": "url", "url": "https://e.com/y"}]}
|
|
]
|
|
}
|
|
param_dict = {"request_json": json.dumps(request)}
|
|
assert set(iter_fetch_request_urls(param_dict)) == {"gxuserfiles://abc/x", "https://e.com/y"}
|
|
|
|
|
|
def test_iter_fetch_request_urls_empty_without_request_json():
|
|
assert list(iter_fetch_request_urls({})) == []
|
|
assert list(iter_fetch_request_urls({"request_json": ""})) == []
|
|
|
|
|
|
def test_iter_fetch_request_urls_ignores_non_string_and_empty_urls():
|
|
request = {"targets": [{"elements": [{"src": "url", "url": None}, {"src": "url", "url": ""}]}]}
|
|
assert list(iter_fetch_urls(request)) == [None, ""]
|
|
assert list(iter_fetch_request_urls({"request_json": json.dumps(request)})) == []
|
|
|
|
|
|
def test_fetch_upload_action_reports_referenced_file_source_uris():
|
|
request = {"targets": [{"elements": [{"src": "url", "url": "gxfiles://source/input"}]}]}
|
|
action = ConcreteFetchUploadToolAction()
|
|
assert list(action.iter_referenced_file_source_uris({"request_json": json.dumps(request)})) == [
|
|
"gxfiles://source/input"
|
|
]
|