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

Check on using auth for HTML source type #92

Merged
merged 2 commits into from
Dec 24, 2021
Merged
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
13 changes: 12 additions & 1 deletion omnikinverter/omnikinverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from aiohttp.hdrs import METH_GET
from yarl import URL

from .exceptions import OmnikInverterConnectionError, OmnikInverterError
from .exceptions import (
OmnikInverterAuthError,
OmnikInverterConnectionError,
OmnikInverterError,
)
from .models import Device, Inverter


Expand Down Expand Up @@ -50,6 +54,7 @@ async def request(
Raises:
OmnikInverterConnectionError: An error occurred while communicating
with the Omnik Inverter.
OmnikInverterAuthError: Authentication failed with the Omnik Inverter.
OmnikInverterError: Received an unexpected response from the Omnik Inverter.
"""
url = URL.build(scheme="http", host=self.host, path="/").join(URL(uri))
Expand All @@ -62,6 +67,12 @@ async def request(
self.session = ClientSession()
self._close_session = True

if self.source_type == "html" and (
self.username is None or self.password is None
):
raise OmnikInverterAuthError(
"A username and/or password is missing from the request"
)
auth = None
if self.username and self.password:
auth = aiohttp.BasicAuth(self.username, self.password)
Expand Down
21 changes: 20 additions & 1 deletion tests/test_omnik.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
OmnikInverterConnectionError,
OmnikInverterWrongSourceError,
)
from omnikinverter.exceptions import OmnikInverterError
from omnikinverter.exceptions import OmnikInverterAuthError, OmnikInverterError

from . import load_fixtures

Expand Down Expand Up @@ -97,6 +97,25 @@ async def test_wrong_html_source(aresponses):
assert await client.inverter()


@pytest.mark.asyncio
async def test_html_no_auth(aresponses):
"""Test on html request without auth."""
aresponses.add(
"example.com",
"/status.html",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "text/html"},
),
)

async with aiohttp.ClientSession() as session:
client = OmnikInverter(host="example.com", source_type="html", session=session)
with pytest.raises(OmnikInverterAuthError):
assert await client.inverter()


@pytest.mark.asyncio
async def test_timeout(aresponses):
"""Test request timeout from Omnik Inverter."""
Expand Down