-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle ACME errors and show messages to users (#23)
* Handle ACME errors and show messages to users * fix tests * Fix background task startup * Add tests * Address comments * Fix message * Fix break
- Loading branch information
Showing
5 changed files
with
168 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
flake8==3.7.7 | ||
pylint==2.3.0 | ||
pylint==2.3.1 | ||
pytest==4.3.0 | ||
pytest-timeout==1.3.3 | ||
pytest-aiohttp==0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,9 @@ async def test_load_backend_exists_cert( | |
assert snitun_mock.connect_args[0] == b"test-token" | ||
assert remote.is_connected | ||
|
||
assert remote._acme_task | ||
assert remote._reconnect_task | ||
|
||
|
||
async def test_load_backend_not_exists_cert( | ||
cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock | ||
|
@@ -136,6 +139,9 @@ async def test_load_backend_not_exists_cert( | |
assert snitun_mock.call_connect | ||
assert snitun_mock.connect_args[0] == b"test-token" | ||
|
||
assert remote._acme_task | ||
assert remote._reconnect_task | ||
|
||
|
||
async def test_load_and_unload_backend( | ||
cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock | ||
|
@@ -180,11 +186,17 @@ async def test_load_and_unload_backend( | |
"snitun_port": 443, | ||
} | ||
|
||
assert remote._acme_task | ||
assert remote._reconnect_task | ||
|
||
await remote.close_backend() | ||
await asyncio.sleep(0.1) | ||
|
||
assert snitun_mock.call_stop | ||
|
||
assert not remote._acme_task | ||
assert not remote._reconnect_task | ||
|
||
|
||
async def test_load_backend_exists_wrong_cert( | ||
cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock | ||
|
@@ -310,7 +322,8 @@ async def test_load_backend_no_autostart( | |
|
||
|
||
async def test_get_certificate_details( | ||
cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock): | ||
cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock | ||
): | ||
"""Initialize backend.""" | ||
valid = utcnow() + timedelta(days=1) | ||
cloud_mock.remote_api_url = "https://test.local/api" | ||
|
@@ -348,3 +361,74 @@ async def test_get_certificate_details( | |
assert certificate.common_name == "test" | ||
assert certificate.expire_date == valid | ||
assert certificate.fingerprint == "ffff" | ||
|
||
|
||
async def test_certificate_task_no_backend( | ||
loop, cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock | ||
): | ||
"""Initialize backend.""" | ||
valid = utcnow() + timedelta(days=1) | ||
cloud_mock.remote_api_url = "https://test.local/api" | ||
remote = RemoteUI(cloud_mock) | ||
|
||
aioclient_mock.post( | ||
"https://test.local/api/register_instance", | ||
json={ | ||
"domain": "test.dui.nabu.casa", | ||
"email": "[email protected]", | ||
"server": "rest-remote.nabu.casa", | ||
}, | ||
) | ||
aioclient_mock.post( | ||
"https://test.local/api/snitun_token", | ||
json={ | ||
"token": "test-token", | ||
"server": "rest-remote.nabu.casa", | ||
"valid": valid.timestamp(), | ||
}, | ||
) | ||
|
||
acme_mock.expire_date = valid | ||
|
||
with patch("hass_nabucasa.utils.next_midnight", return_value=0) as mock_midnight: | ||
remote._acme_task = loop.create_task(remote._certificate_handler()) | ||
|
||
await asyncio.sleep(0.1) | ||
assert mock_midnight.called | ||
assert acme_mock.call_issue | ||
assert snitun_mock.call_start | ||
|
||
|
||
async def test_certificate_task_renew_cert( | ||
loop, cloud_mock, acme_mock, mock_cognito, aioclient_mock, snitun_mock | ||
): | ||
"""Initialize backend.""" | ||
valid = utcnow() + timedelta(days=1) | ||
cloud_mock.remote_api_url = "https://test.local/api" | ||
remote = RemoteUI(cloud_mock) | ||
|
||
aioclient_mock.post( | ||
"https://test.local/api/register_instance", | ||
json={ | ||
"domain": "test.dui.nabu.casa", | ||
"email": "[email protected]", | ||
"server": "rest-remote.nabu.casa", | ||
}, | ||
) | ||
aioclient_mock.post( | ||
"https://test.local/api/snitun_token", | ||
json={ | ||
"token": "test-token", | ||
"server": "rest-remote.nabu.casa", | ||
"valid": valid.timestamp(), | ||
}, | ||
) | ||
|
||
acme_mock.expire_date = utcnow() + timedelta(days=-40) | ||
|
||
with patch("hass_nabucasa.utils.next_midnight", return_value=0) as mock_midnight: | ||
remote._acme_task = loop.create_task(remote._certificate_handler()) | ||
|
||
await remote.load_backend() | ||
await asyncio.sleep(0.1) | ||
assert acme_mock.call_issue |