Skip to content

Commit 5d622a2

Browse files
committed
Use httpx-auth to remove Enode auth code
1 parent 381f849 commit 5d622a2

9 files changed

Lines changed: 44 additions & 111 deletions

File tree

poetry.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pv_site_api/enode_auth.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

pv_site_api/main.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Any
44

55
import httpx
6+
from httpx_auth import OAuth2ClientCredentials
67
import pandas as pd
78
import sentry_sdk
89
import structlog
@@ -30,7 +31,6 @@
3031
)
3132
from .auth import Auth
3233
from .cache import cache_response
33-
from .enode_auth import EnodeAuth
3434
from .fake import (
3535
fake_site_uuid,
3636
make_fake_enode_link_url,
@@ -113,10 +113,10 @@ def is_fake():
113113
algorithm=os.getenv("AUTH0_ALGORITHM"),
114114
)
115115

116-
enode_auth = EnodeAuth(
117-
os.getenv("ENODE_CLIENT_ID", ""),
118-
os.getenv("ENODE_CLIENT_SECRET", ""),
119-
os.getenv("ENODE_TOKEN_URL", "https://oauth.sandbox.enode.io/oauth2/token"),
116+
enode_auth = OAuth2ClientCredentials(
117+
token_url=os.getenv("ENODE_TOKEN_URL", "https://oauth.sandbox.enode.io/oauth2/token"),
118+
client_id=os.getenv("ENODE_CLIENT_ID", ""),
119+
client_secret=os.getenv("ENODE_CLIENT_SECRET", ""),
120120
)
121121

122122
enode_api_base_url = os.getenv("ENODE_API_BASE_URL", "https://enode-api.sandbox.enode.io")

pv_site_api/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
import httpx
99

10-
from .enode_auth import EnodeAuth
1110
from .pydantic_models import Inverters, InverterValues
1211

1312
TOTAL_MINUTES_IN_ONE_DAY = 24 * 60
1413

1514

1615
async def get_inverters_list(
17-
client_uuid: uuid.UUID, inverter_ids: list[str], enode_auth: EnodeAuth, enode_api_base_url: str
16+
client_uuid: uuid.UUID, inverter_ids: list[str], enode_auth: httpx.Auth, enode_api_base_url: str
1817
) -> Inverters:
1918
async with httpx.AsyncClient(base_url=enode_api_base_url, auth=enode_auth) as httpx_client:
2019
headers = {"Enode-User-Id": str(client_uuid)}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ sentry-sdk = "^1.16.0"
1919
pvlib = "^0.9.5"
2020
structlog = "^22.3.0"
2121
pyjwt = {extras = ["crypto"], version = "^2.6.0"}
22+
httpx-auth = "^0.17.0"
2223

2324
[tool.poetry.group.dev.dependencies]
2425
isort = "^5.12.0"

tests/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from datetime import datetime, timedelta
44

5+
import pytest_httpx
56
import freezegun
67
import pytest
78
from fastapi.testclient import TestClient
@@ -23,6 +24,9 @@
2324
from pv_site_api.session import get_session
2425

2526

27+
enode_token_url = os.getenv("ENODE_TOKEN_URL", "https://oauth.sandbox.enode.io/oauth2/token")
28+
29+
2630
@pytest.fixture
2731
def non_mocked_hosts() -> list:
2832
"""Prevent TestClient fixture from being mocked"""
@@ -71,6 +75,16 @@ def db_session(engine):
7175
engine.dispose()
7276

7377

78+
@pytest.fixture()
79+
def mock_enode_auth(httpx_mock):
80+
"""Adds mocked response for Enode authentication"""
81+
httpx_mock.add_response(
82+
url=enode_token_url,
83+
# Ensure token expires immediately so that every test must go through Enode auth
84+
json={"access_token": "test.test", "expires_in": 1, "scope": "", "token_type": "bearer"},
85+
)
86+
87+
7488
@pytest.fixture()
7589
def clients(db_session):
7690
"""Make fake client sql"""

tests/test_enode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_get_enode_link_fake(client, fake):
1111
assert len(response.json()) > 0
1212

1313

14-
def test_get_enode_link(client, clients, httpx_mock):
14+
def test_get_enode_link(client, clients, httpx_mock, mock_enode_auth):
1515
test_enode_link_uri = "https://example.com"
1616

1717
httpx_mock.add_response(

tests/test_enode_auth.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/test_inverters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_put_inverters_for_site_fake(client, sites, fake):
1414
assert response.status_code == 200
1515

1616

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

4242

43-
def test_get_inverters_for_site(client, sites, inverters, httpx_mock):
43+
def test_get_inverters_for_site(client, sites, inverters, httpx_mock, mock_enode_auth):
4444
mock_inverter_response("id1", httpx_mock)
4545
mock_inverter_response("id2", httpx_mock)
4646
mock_inverter_response("id3", httpx_mock)
@@ -66,7 +66,7 @@ def test_get_enode_inverters_fake(client, fake):
6666
assert len(response_inverters.inverters) > 0
6767

6868

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

0 commit comments

Comments
 (0)