Skip to content

Commit

Permalink
feat: Initial set up + test for login (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: Rob Beal <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent 5812940 commit c7a5eeb
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ Contains the library of cards. Each card being an object with the data values y

ym.library

Get Set Up For Development
==========================

Set up pyenv::

pyenv install

Install the dependencies::

pip install -r requirements.txt
pip install -r requirements_dev.txt

Tests
=====

Create a .env file in the root of the project with the following content::

YOTO_USERNAME=your_username
YOTO_PASSWORD=your_password

Run the tests with::

python -m pytest

Other Notes
===========

Expand Down
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pytz
requests
paho-mqtt
pytest
python-dotenv
45 changes: 45 additions & 0 deletions tests/YotoAPI_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest
from dotenv import load_dotenv
import os
import pytz
from yoto_api.YotoAPI import YotoAPI
from datetime import datetime


class ValidLogin(unittest.TestCase):
@classmethod
def setUpClass(cls):
load_dotenv()
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
api = YotoAPI()
cls.token = api.login(username, password)

def test_access_token(self):
self.assertIsNotNone(self.token.access_token)

def test_refresh_token(self):
self.assertIsNotNone(self.token.refresh_token)

def test_token_type(self):
self.assertIsNotNone(self.token.token_type)

def test_scope(self):
self.assertIsNotNone(self.token.scope)

def test_valid_until_is_greater_than_now(self):
self.assertGreater(self.token.valid_until, datetime.now(pytz.utc))


class InvalidLogin(unittest.TestCase):
def test_it_throws_an_error(self):
api = YotoAPI()

with self.assertRaises(Exception) as error:
api.login("invalid", "invalid")

self.assertEqual(str(error.exception), "Wrong email or password.")


if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions yoto_api/YotoAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def login(self, username: str, password: str) -> Token:
response = requests.post(url, data=data, headers=headers).json()
_LOGGER.debug(f"{DOMAIN} - Sign In Response {response.keys()}")

if "error" in response:
raise Exception(response["error_description"])

valid_until = datetime.datetime.now(pytz.utc) + datetime.timedelta(
seconds=response["expires_in"]
)
Expand Down

0 comments on commit c7a5eeb

Please sign in to comment.