Files
galaxy/lib/galaxy_test/base/api_util.py
T
Nicola Soranzo 7f89e0eab9 Code cleanups from pyupgrade
Found by running:

```
make pyupgrade
ruff check --fix .
ruff check --preview --unsafe-fixes --select=F401 --fix .
make format
make update-client-api-schema
```

The bulk of the changes are replacements of typing deprecated aliases
such as `Dict` and `List` with the standard library classes. These
aliases became redundant in Python 3.9 when the corresponding
pre-existing classes were enhanced to support `[]`, see
https://docs.python.org/3/library/typing.html#deprecated-aliases

Also:
- Update the `pyupgrade` Makefile target to use `--py38-plus` for the
  packages needed by Pulsar, including the new galaxy-tool-util-models
  package.
2025-07-31 11:17:22 +01:00

58 lines
1.9 KiB
Python

import base64
import os
import random
import string
from typing import (
Optional,
)
DEFAULT_GALAXY_MASTER_API_KEY = "TEST123"
DEFAULT_GALAXY_USER_API_KEY = None
DEFAULT_TEST_USER = "user@bx.psu.edu"
DEFAULT_ADMIN_TEST_USER = "test@bx.psu.edu"
DEFAULT_OTHER_USER = "otheruser@bx.psu.edu" # A second user for API testing.
TEST_USER = os.environ.get("GALAXY_TEST_USER_EMAIL", DEFAULT_TEST_USER)
ADMIN_TEST_USER = os.environ.get("GALAXY_TEST_ADMIN_USER_EMAIL", DEFAULT_ADMIN_TEST_USER)
OTHER_USER = os.environ.get("GALAXY_TEST_OTHER_USER_EMAIL", DEFAULT_OTHER_USER)
def get_admin_api_key() -> str:
"""Test admin API key to use for functional tests.
This key should be configured as a admin API key and should be able
to create additional users and keys.
"""
for key in ["GALAXY_CONFIG_MASTER_API_KEY", "GALAXY_CONFIG_OVERRIDE_MASTER_API_KEY"]:
value = os.environ.get(key, None)
if value:
return value
return DEFAULT_GALAXY_MASTER_API_KEY
def get_user_api_key() -> Optional[str]:
"""Test user API key to use for functional tests.
If set, this should drive API based testing - if not set an admin API key will
be used to create a new user and API key for tests.
"""
return os.environ.get("GALAXY_TEST_USER_API_KEY", DEFAULT_GALAXY_USER_API_KEY)
def baseauth_headers(username: str, password: str) -> dict[str, str]:
unencoded_credentials = f"{username}:{password}"
authorization = base64.b64encode(unencoded_credentials.encode("utf-8")).decode("utf-8")
headers = {
"Authorization": authorization,
}
return headers
def random_name(prefix: Optional[str] = None, suffix: Optional[str] = None, len: int = 10) -> str:
return "{}{}{}".format(
prefix or "",
"".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(len)),
suffix or "",
)