Skip to content

Commit

Permalink
Fix JWT fixture to adjust expiration.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Dec 4, 2024
1 parent e5a7300 commit d0d77c4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions tests/components/elmax/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Configuration for Elmax tests."""

from collections.abc import Generator
from datetime import datetime, timedelta
import json
from unittest.mock import AsyncMock, patch

Expand All @@ -11,6 +12,7 @@
ENDPOINT_LOGIN,
)
from httpx import Response
import jwt
import pytest
import respx

Expand Down Expand Up @@ -64,9 +66,20 @@ def httpx_mock_direct_fixture() -> Generator[respx.MockRouter]:
) as respx_mock:
# Mock Login POST.
login_route = respx_mock.post(f"/api/v2/{ENDPOINT_LOGIN}", name="login")
login_route.return_value = Response(
200, json=json.loads(load_fixture("direct/login.json", "elmax"))

login_json = json.loads(load_fixture("direct/login.json", "elmax"))
decoded_jwt = jwt.decode_complete(
login_json["token"].split(" ")[1],
algorithms="HS256",
options={"verify_signature": False},
)
expiration = datetime.now() + timedelta(hours=1)
decoded_jwt["payload"]["exp"] = int(expiration.timestamp())
jws_string = jwt.encode(
payload=decoded_jwt["payload"], algorithm="HS256", key=""
)
login_json["token"] = f"JWT {jws_string}"
login_route.return_value = Response(200, json=login_json)

# Mock Device list GET.
list_devices_route = respx_mock.get(
Expand Down

0 comments on commit d0d77c4

Please sign in to comment.