Skip to content

Commit

Permalink
Configure Ruff to select ALL rules (#9)
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
shenanigansd authored Aug 1, 2023
1 parent 909810f commit 2aab44f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 51 deletions.
16 changes: 15 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "dragonfly-loader"
version = "1.0.0"
version = "1.0.1"
description = "Loads all the packages from the PyPI RSS feed into the Dragonfly API"
dynamic = ["dependencies"]

Expand All @@ -27,7 +27,21 @@ build-backend = "setuptools.build_meta"
file = ["requirements.txt"]

[tool.black]
target-version = ["py311"]
line-length = 120

[tool.ruff]
target-version = "py311"
line-length = 120
select = ["ALL"]

[tool.ruff.extend-per-file-ignores]
"tests/*" = [
"INP001", # (File `tests/*.py` is part of an implicit namespace package. Add an `__init__.py`.) - Tests are not modules
"S101", # (Use of `assert` detected) - Yes, that's the point
"PT004", # (Fixture `*` does not return anything, add leading underscore) - Used for pytest fixtures
"ARG001", # (Unused function argument: `*`) - Used for pytest fixtures
]

[tool.ruff.pydocstyle]
convention = "numpy"
2 changes: 1 addition & 1 deletion src/loader/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""Loads all the packages from the PyPI RSS feed into the Dragonfly API"""
"""Loads all the packages from the PyPI RSS feed into the Dragonfly API."""
2 changes: 1 addition & 1 deletion src/loader/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Entry point for the loader"""
"""Entry point for the loader."""

from .loader import main

Expand Down
22 changes: 13 additions & 9 deletions src/loader/loader.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
"""Main entrypoint for the loader."""

from letsbuilda.pypi import PyPIServices
from requests import Session

from loader.settings import Settings, get_settings


def get_access_token(*, http_session: Session, settings: Settings) -> str:
payload = dict(
client_id=settings.client_id,
client_secret=settings.client_secret,
username=settings.username,
password=settings.password,
audience=settings.audience,
grant_type="password",
)
"""Get an access token from Auth0."""
payload = {
"client_id": settings.client_id,
"client_secret": settings.client_secret,
"username": settings.username,
"password": settings.password,
"audience": settings.audience,
"grant_type": "password",
}

res = http_session.post(f"https://{settings.auth0_domain}/oauth/token", json=payload)
json = res.json()
return json["access_token"]


def main() -> None:
"""Run the loader."""
http_session = Session()
pypi_client = PyPIServices(http_session)
settings = get_settings()

access_token = get_access_token(http_session=http_session, settings=settings)

packages = pypi_client.get_rss_feed(PyPIServices.PACKAGE_UPDATES_FEED_URL)
payload = [dict(name=package.title, version=package.version) for package in packages]
payload = [{"name": package.title, "version": package.version} for package in packages]
headers = {"Authorization": "Bearer " + access_token}

http_session.post(f"{settings.base_url}/batch/package", json=payload, headers=headers)
Expand Down
57 changes: 35 additions & 22 deletions src/loader/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Settings for the Dragonfly Loader."""

from dataclasses import dataclass
from os import getenv

Expand All @@ -6,6 +8,8 @@

@dataclass
class Settings:
"""Settings for the Dragonfly Loader."""

base_url: str
auth0_domain: str

Expand All @@ -19,35 +23,44 @@ class Settings:


def get_settings() -> Settings:
"""Get the settings for the Dragonfly Loader."""
load_dotenv()

BASE_URL = getenv("DRAGONFLY_API_URL", "https://dragonfly.vipyrsec.com")
AUTH0_DOMAIN = getenv("AUTH0_DOMAIN", "vipyrsec.us.auth0.com")
base_url = getenv("DRAGONFLY_API_URL", "https://dragonfly.vipyrsec.com")
auth0_domain = getenv("AUTH0_DOMAIN", "vipyrsec.us.auth0.com")

CLIENT_ID = getenv("CLIENT_ID")
CLIENT_SECRET = getenv("CLIENT_SECRET")
USERNAME = getenv("USERNAME")
PASSWORD = getenv("PASSWORD")
AUDIENCE = getenv("AUDIENCE", "https://dragonfly.vipyrsec.com")
client_id = getenv("CLIENT_ID")
client_secret = getenv("CLIENT_SECRET")
username = getenv("USERNAME")
password = getenv("PASSWORD")
audience = getenv("AUDIENCE", "https://dragonfly.vipyrsec.com")

if not CLIENT_ID:
raise Exception("`CLIENT_ID` is a required environment variable!")
if not client_id:
msg = "`CLIENT_ID` is a required environment variable!"
raise Exception(msg) # noqa: TRY002 - TODO @Robin5605: Use a custom exception
# https://github.com/vipyrsec/dragonfly-loader/issues/10

if not CLIENT_SECRET:
raise Exception("`CLIENT_SECRET` is a required environment variable!")
if not client_secret:
msg = "`CLIENT_SECRET` is a required environment variable!"
raise Exception(msg) # noqa: TRY002 - TODO @Robin5605: Use a custom exception
# https://github.com/vipyrsec/dragonfly-loader/issues/10

if not USERNAME:
raise Exception("`USERNAME` is a required environment variable!")
if not username:
msg = "`USERNAME` is a required environment variable!"
raise Exception(msg) # noqa: TRY002 - TODO @Robin5605: Use a custom exception
# https://github.com/vipyrsec/dragonfly-loader/issues/10

if not PASSWORD:
raise Exception("`PASSWORD` is a required environment variable!")
if not password:
msg = "`PASSWORD` is a required environment variable!"
raise Exception(msg) # noqa: TRY002 - TODO @Robin5605: Use a custom exception
# https://github.com/vipyrsec/dragonfly-loader/issues/10

return Settings(
base_url=BASE_URL,
auth0_domain=AUTH0_DOMAIN,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
username=USERNAME,
password=PASSWORD,
audience=AUDIENCE,
base_url=base_url,
auth0_domain=auth0_domain,
client_id=client_id,
client_secret=client_secret,
username=username,
password=password,
audience=audience,
)
38 changes: 21 additions & 17 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Stub test file"""
"""Stub test file."""

from __future__ import annotations
from dataclasses import dataclass
from unittest.mock import DEFAULT, MagicMock, Mock, patch

import pytest
from loader import loader
from pytest import MonkeyPatch


@dataclass
class MockPackage:
"""Mock package data."""

title: str
version: str

Expand All @@ -26,22 +26,25 @@ class MockPackage:
}


@pytest.fixture
def mock_env(monkeypatch: MonkeyPatch):
@pytest.fixture()
def mock_env(monkeypatch: pytest.MonkeyPatch) -> None:
"""Mock the environment variables."""
for k, v in environment_variables.items():
monkeypatch.setenv(k, v)


@pytest.fixture
@pytest.fixture()
def mock_rss_data() -> list[MockPackage]:
"""Mock the RSS data."""
return [
MockPackage(title="a", version="1.0.0"),
MockPackage(title="b", version="1.0.1"),
MockPackage(title="c", version="1.0.2"),
]


def mock_http_session_post_side_effect(*args, **kwargs):
def mock_http_session_post_side_effect(*args: list, **_kwargs: dict) -> Mock:
"""Mock the HTTP auth call."""
if args[0] == "https://" + environment_variables["AUTH0_DOMAIN"] + "/oauth/token":
mock = Mock()
mock.json = Mock()
Expand All @@ -56,7 +59,8 @@ def mock_http_session_post_side_effect(*args, **kwargs):
return DEFAULT


def test_loader(mock_env, mock_rss_data: list[MockPackage]):
def test_loader(mock_env, mock_rss_data: list[MockPackage]) -> None: # noqa: ANN001 - unclear
"""Test the loader."""
mock_http_session = MagicMock()
mock_http_session.post = MagicMock(side_effect=mock_http_session_post_side_effect)

Expand All @@ -71,18 +75,18 @@ def test_loader(mock_env, mock_rss_data: list[MockPackage]):
loader.main()
mock_http_session.post.assert_any_call(
f"https://{environment_variables['AUTH0_DOMAIN']}/oauth/token",
json=dict(
client_id=environment_variables["CLIENT_ID"],
client_secret=environment_variables["CLIENT_SECRET"],
username=environment_variables["USERNAME"],
password=environment_variables["PASSWORD"],
audience=environment_variables["AUDIENCE"],
grant_type="password",
),
json={
"client_id": environment_variables["CLIENT_ID"],
"client_secret": environment_variables["CLIENT_SECRET"],
"username": environment_variables["USERNAME"],
"password": environment_variables["PASSWORD"],
"audience": environment_variables["AUDIENCE"],
"grant_type": "password",
},
)

mock_http_session.post.assert_any_call(
f"{environment_variables['DRAGONFLY_API_URL']}/batch/package",
json=[dict(name=p.title, version=p.version) for p in mock_rss_data],
json=[{"name": p.title, "version": p.version} for p in mock_rss_data],
headers={"Authorization": "Bearer test-access-token"},
)

0 comments on commit 2aab44f

Please sign in to comment.