Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip authorization if in development/testing #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions src/loader/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
"""Loader settings."""

from os import getenv

from pydantic_settings import BaseSettings

GIT_SHA = getenv("GIT_SHA", "development")
SKIP_AUTH = GIT_SHA in {"testing", "development"}


class _Settings(BaseSettings):
"""Settings for the Dragonfly Loader."""
Expand Down
4 changes: 2 additions & 2 deletions src/loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from httpx import Client
from letsbuilda.pypi import PyPIServices

from loader.constants import Settings
from loader.constants import SKIP_AUTH, Settings


def build_authorization_header(access_token: str) -> dict[str, str]:
"""Build authorization headers using the access token."""
return {"Authorization": "Bearer " + access_token}

Check failure on line 11 in src/loader/loader.py

View workflow job for this annotation

GitHub Actions / lint / lint

Ruff (DOC201)

src/loader/loader.py:11:5: DOC201 `return` is not documented in docstring


def get_access_token(*, http_client: Client) -> str:
Expand All @@ -25,7 +25,7 @@
res = http_client.post(f"https://{Settings.auth0_domain}/oauth/token", json=payload)
res.raise_for_status()
json = res.json()
return json["access_token"] # type: ignore[no-any-return]

Check failure on line 28 in src/loader/loader.py

View workflow job for this annotation

GitHub Actions / lint / lint

Ruff (DOC201)

src/loader/loader.py:28:5: DOC201 `return` is not documented in docstring


def fetch_packages(*, pypi_client: PyPIServices) -> list[tuple[str, str]]:
Expand All @@ -33,7 +33,7 @@
packages = pypi_client.get_rss_feed(PyPIServices.PACKAGE_UPDATES_FEED_URL)

# All packages in the feed are guaranteed to have both a name and version
return [(package.title, package.version) for package in packages if package.version is not None]

Check failure on line 36 in src/loader/loader.py

View workflow job for this annotation

GitHub Actions / lint / lint

Ruff (DOC201)

src/loader/loader.py:36:5: DOC201 `return` is not documented in docstring


def load_packages(packages: list[tuple[str, str]], *, http_client: Client, access_token: str) -> None:
Expand All @@ -47,7 +47,7 @@

def main(*, http_client: Client, pypi_client: PyPIServices) -> None:
"""Run the loader."""
access_token = get_access_token(http_client=http_client)
access_token = "DEVELOPMENT" if SKIP_AUTH else get_access_token(http_client=http_client)

packages = fetch_packages(pypi_client=pypi_client)

Expand Down
5 changes: 1 addition & 4 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,9 @@ def test_main(monkeypatch: pytest.MonkeyPatch) -> None:

fetch_packages_mock = Mock(return_value=mock_packages)
load_packages_mock = Mock()
get_access_token_mock = Mock(return_value="test-access-token")
monkeypatch.setattr("loader.loader.fetch_packages", fetch_packages_mock)
monkeypatch.setattr("loader.loader.load_packages", load_packages_mock)
monkeypatch.setattr("loader.loader.get_access_token", get_access_token_mock)

loader.main(http_client=mock_http_client, pypi_client=mock_pypi_client)
get_access_token_mock.assert_any_call(http_client=mock_http_client)
fetch_packages_mock.assert_any_call(pypi_client=mock_pypi_client)
load_packages_mock.assert_any_call(mock_packages, http_client=mock_http_client, access_token="test-access-token")
load_packages_mock.assert_any_call(mock_packages, http_client=mock_http_client, access_token="DEVELOPMENT")
Loading