Skip to content

Commit

Permalink
Add support for snitun throttling over API (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli authored Mar 19, 2019
1 parent bb5bd31 commit 92b7c93
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
5 changes: 4 additions & 1 deletion hass_nabucasa/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SniTunToken:
aes_key = attr.ib(type=bytes)
aes_iv = attr.ib(type=bytes)
valid = attr.ib(type=datetime)
throttling = attr.ib(type=int)


@attr.s
Expand Down Expand Up @@ -235,6 +236,7 @@ async def _refresh_snitun_token(self) -> None:
aes_key,
aes_iv,
utils.utc_from_timestamp(data["valid"]),
data["throttling"]
)

async def connect(self) -> None:
Expand All @@ -250,7 +252,8 @@ async def connect(self) -> None:
try:
await self._refresh_snitun_token()
await self._snitun.connect(
self._token.fernet, self._token.aes_key, self._token.aes_iv
self._token.fernet, self._token.aes_key, self._token.aes_iv,
throttling=self._token.throttling
)

self.cloud.client.dispatcher_message(const.DISPATCH_REMOTE_CONNECT)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
packages=["hass_nabucasa"],
install_requires=[
"warrant==0.6.1",
"snitun==0.13",
"snitun==0.15",
"acme==0.32.0",
"cryptography>=2.5",
"attrs>=18.2.0",
Expand Down
6 changes: 4 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,12 @@ async def stop(self):
"""Stop snitun."""
self.call_stop = True

async def connect(self, token: bytes, aes_key: bytes, aes_iv: bytes):
async def connect(
self, token: bytes, aes_key: bytes, aes_iv: bytes, throttling=None
):
"""Connect snitun."""
self.call_connect = True
self.connect_args = [token, aes_key, aes_iv]
self.connect_args = [token, aes_key, aes_iv, throttling]

async def disconnect(self):
"""Disconnect snitun."""
Expand Down
8 changes: 7 additions & 1 deletion tests/test_cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ async def test_remote_token(cloud_mock, aioclient_mock):
"""Test creating a cloudhook."""
aioclient_mock.post(
"https://example.com/bla/snitun_token",
json={"token": "123456", "server": "rest-remote.nabu.casa", "valid": 12345},
json={
"token": "123456",
"server": "rest-remote.nabu.casa",
"valid": 12345,
"throttling": 400,
},
)
cloud_mock.id_token = "mock-id-token"
cloud_mock.remote_api_url = "https://example.com/bla"
Expand All @@ -67,6 +72,7 @@ async def test_remote_token(cloud_mock, aioclient_mock):
"token": "123456",
"server": "rest-remote.nabu.casa",
"valid": 12345,
"throttling": 400,
}
assert aioclient_mock.mock_calls[0][2] == {"aes_iv": "6976", "aes_key": "616573"}

Expand Down
13 changes: 13 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async def test_load_backend_exists_cert(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand All @@ -93,6 +94,7 @@ async def test_load_backend_exists_cert(

assert snitun_mock.call_connect
assert snitun_mock.connect_args[0] == b"test-token"
assert snitun_mock.connect_args[3] == 400
assert remote.is_connected

assert remote._acme_task
Expand Down Expand Up @@ -124,6 +126,7 @@ async def test_load_backend_not_exists_cert(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand All @@ -147,6 +150,7 @@ async def test_load_backend_not_exists_cert(

assert snitun_mock.call_connect
assert snitun_mock.connect_args[0] == b"test-token"
assert snitun_mock.connect_args[3] == 400

assert remote._acme_task
assert remote._reconnect_task
Expand Down Expand Up @@ -174,6 +178,7 @@ async def test_load_and_unload_backend(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand Down Expand Up @@ -231,6 +236,7 @@ async def test_load_backend_exists_wrong_cert(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand All @@ -254,6 +260,7 @@ async def test_load_backend_exists_wrong_cert(

assert snitun_mock.call_connect
assert snitun_mock.connect_args[0] == b"test-token"
assert snitun_mock.connect_args[3] == 400


async def test_call_disconnect(
Expand All @@ -278,6 +285,7 @@ async def test_call_disconnect(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand Down Expand Up @@ -314,6 +322,7 @@ async def test_load_backend_no_autostart(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand All @@ -331,6 +340,7 @@ async def test_load_backend_no_autostart(

assert snitun_mock.call_connect
assert snitun_mock.connect_args[0] == b"test-token"
assert snitun_mock.connect_args[3] == 400
assert cloud_mock.client.mock_dispatcher[-1][0] == DISPATCH_REMOTE_CONNECT


Expand Down Expand Up @@ -358,6 +368,7 @@ async def test_get_certificate_details(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand Down Expand Up @@ -398,6 +409,7 @@ async def test_certificate_task_no_backend(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand Down Expand Up @@ -434,6 +446,7 @@ async def test_certificate_task_renew_cert(
"token": "test-token",
"server": "rest-remote.nabu.casa",
"valid": valid.timestamp(),
"throttling": 400
},
)

Expand Down

0 comments on commit 92b7c93

Please sign in to comment.