Skip to content

Commit 52cff72

Browse files
tr4nt0rLudy87
andauthored
Fix token response parsing (#167)
* Fix token response parsing - Removed unnecessary rounding in token expiration check. - Updated token parsing to align with API response structure. - Adjusted `GetTokenResponse` typing attributes accordingly. * Add pytests for login * Add GitHub Actions workflow for pytest - Introduced GitHub Actions workflow for running tests on push and pull requests. - Configured matrix strategy to test with Python versions 3.11 and 3.12. - Installed project dependencies and ran pytest with coverage reporting. - Integrated Codecov for coverage reporting using a GitHub Actions workflow. * Update pyecotrend_ista.py --------- Co-authored-by: Ludy <[email protected]>
1 parent 5f25c8c commit 52cff72

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

.github/workflows/pytest.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python_version: ['3.11','3.12']
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: ${{ matrix.python_version }}
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
pip install -r requirements.txt
23+
pip install -r requirements_test.txt
24+
- name: Test
25+
run: |
26+
pytest --cov-report=xml
27+
- uses: codecov/codecov-action@v4
28+
with:
29+
token: ${{ secrets.CODECOV_TOKEN }}
30+
fail_ci_if_error: false
31+
verbose: true

src/pyecotrend_ista/pyecotrend_ista.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def access_token(self):
108108
self._accessTokenExpiresIn > 0
109109
and self._is_connected()
110110
and self._refreshToken
111-
and self._accessTokenExpiresIn <= (time.time() - self.start_timer).__round__(2)
111+
and self._accessTokenExpiresIn <= time.time() - self.start_timer
112112
):
113113
self.__refresh()
114114
return self._accessToken
@@ -922,7 +922,10 @@ def demo_user_login(self) -> GetTokenResponse:
922922
) as r:
923923
r.raise_for_status()
924924
try:
925-
return cast(GetTokenResponse, r.json())
925+
data = r.json()
926+
key = iter(GetTokenResponse.__annotations__)
927+
token = dict((next(key), value) for value in data.values())
928+
return cast(GetTokenResponse, token)
926929
except requests.JSONDecodeError as e:
927930
self._LOGGER.debug("JSONDecodeError: %s", e)
928931
raise ServerError from e

src/pyecotrend_ista/types.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ class GetTokenResponse(TypedDict):
88
99
Attributes
1010
----------
11-
accessToken : str
11+
access_token : str
1212
The access token issued by the authentication provider.
13-
accessTokenExpiresIn : int
13+
expires_in : int
1414
The number of seconds until the access token expires.
15-
refreshToken : str
15+
refresh_token : str
1616
The refresh token that can be used to obtain new access tokens.
17-
refreshTokenExpiresIn : int
17+
refresh_expires_in : int
1818
The number of seconds until the refresh token expires.
1919
2020
"""
2121

22-
accessToken: str
23-
accessTokenExpiresIn: int
24-
refreshToken: str
25-
refreshTokenExpiresIn: int
22+
access_token: str
23+
expires_in: int
24+
refresh_token: str
25+
refresh_expires_in: int

tests/conftest.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ def mock_requests_login(requests_mock):
2525
requests_mock.post(
2626
PROVIDER_URL + "token",
2727
json={
28-
"accessToken": "ACCESS_TOKEN",
29-
"accessTokenExpiresIn": 60,
30-
"refreshToken": "REFRESH_TOKEN",
31-
"refreshTokenExpiresIn": 5184000,
28+
"access_token": "ACCESS_TOKEN",
29+
"expires_in": 60,
30+
"refresh_expires_in": 5183999,
31+
"refresh_token": "REFRESH_TOKEN",
32+
"token_type": "Bearer",
33+
"id_token": "ID_TOKEN",
34+
"not-before-policy": 0,
35+
"session_state": "SESSION_STATE",
36+
"scope": "openid profile email",
3237
},
3338
)
3439
requests_mock.get(

tests/test_login.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Tests for Login methods."""
2+
3+
4+
5+
import pytest
6+
from pyecotrend_ista.pyecotrend_ista import PyEcotrendIsta
7+
8+
from tests.conftest import DEMO_EMAIL, TEST_EMAIL
9+
10+
11+
@pytest.mark.parametrize('ista_client', [TEST_EMAIL, DEMO_EMAIL], indirect=True)
12+
@pytest.mark.usefixtures("mock_requests_login")
13+
def test_login(ista_client: PyEcotrendIsta) -> None:
14+
"""Test Login method."""
15+
assert ista_client.login() == "ACCESS_TOKEN"
16+
17+

0 commit comments

Comments
 (0)