Skip to content

Commit 2e07a45

Browse files
committed
Add initial unit tests for auth
1 parent 1307261 commit 2e07a45

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test for the Elmax Cloud service client."""

tests/test_authentication.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Test the authentication process."""
2+
import pytest
3+
from pytest_httpx import HTTPXMock
4+
5+
from elmax import Elmax, exceptions
6+
7+
RESPONSE_AUTHENTICATION_VALID = {
8+
"token": "JWT 123456.123456",
9+
"user": {"_id": "1234567", "email": "user@elmax-cloud.test", "role": "user"},
10+
}
11+
12+
RESPONSE_AUTHENTICATION_INVALID = {
13+
"user": {"_id": "1234567", "email": "user@elmax-cloud.test", "role": "user"},
14+
}
15+
16+
@pytest.mark.asyncio
17+
async def test_authentication_valid(httpx_mock: HTTPXMock):
18+
"""Test a valid authentication process."""
19+
httpx_mock.add_response(json=RESPONSE_AUTHENTICATION_VALID)
20+
21+
client = Elmax(username="username", password="password")
22+
await client.connect()
23+
24+
assert client.is_authenticated is True
25+
26+
27+
@pytest.mark.asyncio
28+
async def test_authentication_not_json(httpx_mock: HTTPXMock):
29+
"""Test if invalid response is handled correctly."""
30+
httpx_mock.add_response(json="This is my UTF-8 content")
31+
32+
with pytest.raises(exceptions.ElmaxConnectionError) as execinfo:
33+
client = Elmax(username="username", password="password")
34+
await client.connect()
35+
36+
assert execinfo.value.args[0] == 'Credentials are not valid'
37+
assert client.is_authenticated is False
38+
39+
40+
@pytest.mark.asyncio
41+
async def test_authentication_incomplete(httpx_mock: HTTPXMock):
42+
"""Test autnetication with an incomplete response."""
43+
httpx_mock.add_response(json=RESPONSE_AUTHENTICATION_INVALID)
44+
45+
with pytest.raises(exceptions.ElmaxConnectionError) as execinfo:
46+
client = Elmax(username="username", password="password")
47+
await client.connect()
48+
49+
assert execinfo.value.args[0] == 'Credentials are not valid'
50+
assert client.is_authenticated is False

0 commit comments

Comments
 (0)