Skip to content

Commit

Permalink
Merge pull request #3 from mochipon/feat/validate_keys
Browse files Browse the repository at this point in the history
Validate provided keys
  • Loading branch information
mochipon committed Jun 12, 2021
2 parents aaf9e03 + 70c710b commit 97514e1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pysesame3/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def __init__(self, apikey: str):
"""
self._apikey = apikey

if len(self._apikey) != 40:
raise ValueError("Invalid API Key - length should be 40.")

def __call__(self, r):
r.headers["x-api-key"] = self._apikey
return r
4 changes: 3 additions & 1 deletion pysesame3/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def setSecretKey(self, key: str) -> None:
ValueError: If `key` is invalid.
"""
if not isinstance(key, str):
raise ValueError("Invalid SecretKey")
raise ValueError("Invalid SecretKey - should be string.")
if len(key) != 32:
raise ValueError("Invalid SecretKey - length should be 32.")
self._secretKey = key

def setSesame2PublicKey(self, key: str) -> None:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python

"""Tests for `pysesame3` package."""

import pytest

from pysesame3.auth import WebAPIAuth


def test_WebAPIAuth_raises_exception_on_missing_arguments():
with pytest.raises(TypeError):
WebAPIAuth()


def test_WebAPIAuth_raises_exception_on_invalid_apikey():
with pytest.raises(ValueError) as excinfo:
WebAPIAuth(apikey="thisisfake")
assert "length should be 40" in str(excinfo.value)
16 changes: 11 additions & 5 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import uuid

import pytest

from pysesame3.auth import WebAPIAuth
from pysesame3.device import CHDevices, SesameLocker
from pysesame3.helper import CHProductModel


@pytest.fixture(autouse=True)
def cl():
yield WebAPIAuth(apikey="FAKEAPIKEY")
yield WebAPIAuth(apikey="FAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKE")


class TestCHDevices:
Expand Down Expand Up @@ -77,15 +78,20 @@ def test_CHDevices_deviceUUID(self):
def test_SesameLocker_secretKey_raises_exception_on_invalid_value(self):
d = SesameLocker(cl)

with pytest.raises(ValueError):
with pytest.raises(ValueError) as excinfo:
d.setSecretKey(123)
assert "should be string" in str(excinfo.value)

with pytest.raises(ValueError) as excinfo:
d.setSecretKey("FAKE")
assert "length should be 32" in str(excinfo.value)

def test_CHDevices_secretKey(self):
d = SesameLocker(cl)

assert d.getSecretKey() is None

secret = "TestSecret"
secret = "FAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKE"

assert d.setSecretKey(secret) is None
assert d.getSecretKey() == secret
Expand Down Expand Up @@ -115,13 +121,13 @@ def test_CHDevices(self):
test_model = CHProductModel.SS2
d.setProductModel(test_model)

secret = "TestSecret"
secret = "FAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKE"
d.setSecretKey(secret)

pubkey = "TestPubKey"
d.setSesame2PublicKey(pubkey)

assert (
str(d)
== "SesameLocker(deviceUUID=42918AD1-8154-4AFF-BD1F-F0CDE88A8DE1, deviceModel=CHProductModel.SS2, secretKey=TestSecret, sesame2PublicKey=TestPubKey)"
== "SesameLocker(deviceUUID=42918AD1-8154-4AFF-BD1F-F0CDE88A8DE1, deviceModel=CHProductModel.SS2, secretKey=FAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKE, sesame2PublicKey=TestPubKey)"
)
20 changes: 18 additions & 2 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import requests_mock

from pysesame3.auth import WebAPIAuth
from pysesame3.lock import CHSesame2, CHSesame2ShadowStatus

Expand Down Expand Up @@ -35,18 +36,33 @@ def mock_requests():

@pytest.fixture()
def mock_cloud():
cl = WebAPIAuth(apikey="FAKEAPIKEY")
cl = WebAPIAuth(apikey="FAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKEFAKE")
yield cl


class TestCHSesame2Broken:
def test_CHSesame2_raises_exception_on_invalid_arguments(self):
def test_CHSesame2_raises_exception_on_missing_arguments(self):
with pytest.raises(TypeError):
CHSesame2()

with pytest.raises(TypeError):
CHSesame2(mock_cloud)

def test_CHSesame2_raises_exception_on_invalid_arguments(self):
with pytest.raises(ValueError):
key = {
"device_uuid": "FAKEUUID",
"secret_key": "0b3e5f1665e143b59180c915fa4b06d9",
}
CHSesame2(mock_cloud, **key)

with pytest.raises(ValueError):
key = {
"device_uuid": "126D3D66-9222-4E5A-BCDE-0C6629D48D43",
"secret_key": "FAKE_SECRET_KEY",
}
CHSesame2(mock_cloud, **key)


class TestCHSesame2:
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 97514e1

Please sign in to comment.