Skip to content

Commit

Permalink
Use httpx-auth to remove Enode auth code
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewLester committed May 4, 2023
1 parent 381f849 commit 5d622a2
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 111 deletions.
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 0 additions & 53 deletions pv_site_api/enode_auth.py

This file was deleted.

10 changes: 5 additions & 5 deletions pv_site_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any

import httpx
from httpx_auth import OAuth2ClientCredentials
import pandas as pd
import sentry_sdk
import structlog
Expand Down Expand Up @@ -30,7 +31,6 @@
)
from .auth import Auth
from .cache import cache_response
from .enode_auth import EnodeAuth
from .fake import (
fake_site_uuid,
make_fake_enode_link_url,
Expand Down Expand Up @@ -113,10 +113,10 @@ def is_fake():
algorithm=os.getenv("AUTH0_ALGORITHM"),
)

enode_auth = EnodeAuth(
os.getenv("ENODE_CLIENT_ID", ""),
os.getenv("ENODE_CLIENT_SECRET", ""),
os.getenv("ENODE_TOKEN_URL", "https://oauth.sandbox.enode.io/oauth2/token"),
enode_auth = OAuth2ClientCredentials(
token_url=os.getenv("ENODE_TOKEN_URL", "https://oauth.sandbox.enode.io/oauth2/token"),
client_id=os.getenv("ENODE_CLIENT_ID", ""),
client_secret=os.getenv("ENODE_CLIENT_SECRET", ""),
)

enode_api_base_url = os.getenv("ENODE_API_BASE_URL", "https://enode-api.sandbox.enode.io")
Expand Down
3 changes: 1 addition & 2 deletions pv_site_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

import httpx

from .enode_auth import EnodeAuth
from .pydantic_models import Inverters, InverterValues

TOTAL_MINUTES_IN_ONE_DAY = 24 * 60


async def get_inverters_list(
client_uuid: uuid.UUID, inverter_ids: list[str], enode_auth: EnodeAuth, enode_api_base_url: str
client_uuid: uuid.UUID, inverter_ids: list[str], enode_auth: httpx.Auth, enode_api_base_url: str
) -> Inverters:
async with httpx.AsyncClient(base_url=enode_api_base_url, auth=enode_auth) as httpx_client:
headers = {"Enode-User-Id": str(client_uuid)}
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sentry-sdk = "^1.16.0"
pvlib = "^0.9.5"
structlog = "^22.3.0"
pyjwt = {extras = ["crypto"], version = "^2.6.0"}
httpx-auth = "^0.17.0"

[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from datetime import datetime, timedelta

import pytest_httpx
import freezegun
import pytest
from fastapi.testclient import TestClient
Expand All @@ -23,6 +24,9 @@
from pv_site_api.session import get_session


enode_token_url = os.getenv("ENODE_TOKEN_URL", "https://oauth.sandbox.enode.io/oauth2/token")


@pytest.fixture
def non_mocked_hosts() -> list:
"""Prevent TestClient fixture from being mocked"""
Expand Down Expand Up @@ -71,6 +75,16 @@ def db_session(engine):
engine.dispose()


@pytest.fixture()
def mock_enode_auth(httpx_mock):
"""Adds mocked response for Enode authentication"""
httpx_mock.add_response(
url=enode_token_url,
# Ensure token expires immediately so that every test must go through Enode auth
json={"access_token": "test.test", "expires_in": 1, "scope": "", "token_type": "bearer"},
)


@pytest.fixture()
def clients(db_session):
"""Make fake client sql"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_enode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_get_enode_link_fake(client, fake):
assert len(response.json()) > 0


def test_get_enode_link(client, clients, httpx_mock):
def test_get_enode_link(client, clients, httpx_mock, mock_enode_auth):
test_enode_link_uri = "https://example.com"

httpx_mock.add_response(
Expand Down
46 changes: 0 additions & 46 deletions tests/test_enode_auth.py

This file was deleted.

6 changes: 3 additions & 3 deletions tests/test_inverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_put_inverters_for_site_fake(client, sites, fake):
assert response.status_code == 200


def test_put_inverters_for_site(client, sites, httpx_mock):
def test_put_inverters_for_site(client, sites, httpx_mock, mock_enode_auth):
test_inverter_client_id = "6c078ca2-2e75-40c8-9a7f-288bd0b70065"
json = [test_inverter_client_id]
response = client.put(f"/sites/{sites[0].site_uuid}/inverters", json=json)
Expand All @@ -40,7 +40,7 @@ def test_get_inverters_for_site_fake(client, sites, inverters, fake):
assert response.status_code == 200


def test_get_inverters_for_site(client, sites, inverters, httpx_mock):
def test_get_inverters_for_site(client, sites, inverters, httpx_mock, mock_enode_auth):
mock_inverter_response("id1", httpx_mock)
mock_inverter_response("id2", httpx_mock)
mock_inverter_response("id3", httpx_mock)
Expand All @@ -66,7 +66,7 @@ def test_get_enode_inverters_fake(client, fake):
assert len(response_inverters.inverters) > 0


def test_get_enode_inverters(client, httpx_mock, clients):
def test_get_enode_inverters(client, httpx_mock, clients, mock_enode_auth):
httpx_mock.add_response(url=f"{enode_api_base_url}/inverters", json=["id1"])
mock_inverter_response("id1", httpx_mock)

Expand Down

0 comments on commit 5d622a2

Please sign in to comment.