Пример #1
0
async def test_certificate_task_renew_cert(loop, auth_cloud_mock, acme_mock,
                                           mock_cognito, aioclient_mock,
                                           snitun_mock):
    """Initialize backend."""
    valid = utcnow() + timedelta(days=1)
    auth_cloud_mock.remote_api_url = "https://test.local/api"
    remote = RemoteUI(auth_cloud_mock)

    aioclient_mock.post(
        "https://test.local/api/register_instance",
        json={
            "domain": "test.dui.nabu.casa",
            "email": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    acme_mock.expire_date = utcnow() + timedelta(days=-40)

    with patch("hass_nabucasa.utils.next_midnight",
               return_value=0), patch("random.randint", return_value=0):
        remote._acme_task = loop.create_task(remote._certificate_handler())

        await remote.load_backend()
        await asyncio.sleep(0.1)
        assert acme_mock.call_issue
Пример #2
0
def test_subscription_expired(cloud_client):
    """Test subscription being expired after 3 days of expiration."""
    cl = cloud.Cloud(cloud_client, cloud.MODE_DEV)

    token_val = {"custom:sub-exp": "2017-11-13"}
    with patch.object(cl, "_decode_claims", return_value=token_val), patch(
            "hass_nabucasa.utcnow",
            return_value=utcnow().replace(year=2017, month=11, day=13),
    ):
        assert not cl.subscription_expired

    with patch.object(cl, "_decode_claims", return_value=token_val), patch(
            "hass_nabucasa.utcnow",
            return_value=utcnow().replace(year=2017,
                                          month=11,
                                          day=19,
                                          hour=23,
                                          minute=59,
                                          second=59),
    ):
        assert not cl.subscription_expired

    with patch.object(cl, "_decode_claims", return_value=token_val), patch(
            "hass_nabucasa.utcnow",
            return_value=utcnow().replace(year=2017,
                                          month=11,
                                          day=20,
                                          hour=0,
                                          minute=0,
                                          second=0),
    ):
        assert cl.subscription_expired
Пример #3
0
async def test_call_disconnect(auth_cloud_mock, acme_mock, mock_cognito,
                               aioclient_mock, snitun_mock):
    """Initialize backend."""
    valid = utcnow() + timedelta(days=1)
    auth_cloud_mock.remote_api_url = "https://test.local/api"
    remote = RemoteUI(auth_cloud_mock)

    aioclient_mock.post(
        "https://test.local/api/register_instance",
        json={
            "domain": "test.dui.nabu.casa",
            "email": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    assert not remote.is_connected
    await remote.load_backend()
    await asyncio.sleep(0.1)
    assert remote.is_connected

    await remote.disconnect()
    assert snitun_mock.call_disconnect
    assert not remote.is_connected
    assert auth_cloud_mock.client.mock_dispatcher[-1][
        0] == DISPATCH_REMOTE_DISCONNECT
Пример #4
0
async def test_load_backend_exists_cert(auth_cloud_mock, acme_mock,
                                        mock_cognito, aioclient_mock,
                                        snitun_mock):
    """Initialize backend."""
    valid = utcnow() + timedelta(days=1)
    auth_cloud_mock.remote_api_url = "https://test.local/api"
    remote = RemoteUI(auth_cloud_mock)

    aioclient_mock.post(
        "https://test.local/api/register_instance",
        json={
            "domain": "test.dui.nabu.casa",
            "email": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    assert not remote.is_connected
    await remote.load_backend()
    assert remote.snitun_server == "rest-remote.nabu.casa"
    assert remote.instance_domain == "test.dui.nabu.casa"
    await asyncio.sleep(0.1)

    assert not acme_mock.call_issue
    assert acme_mock.init_args == (
        auth_cloud_mock,
        "test.dui.nabu.casa",
        "*****@*****.**",
    )
    assert acme_mock.call_hardening
    assert snitun_mock.call_start
    assert snitun_mock.init_args == (auth_cloud_mock.client.aiohttp_runner,
                                     None)
    assert snitun_mock.init_kwarg == {
        "snitun_server": "rest-remote.nabu.casa",
        "snitun_port": 443,
    }

    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
    assert remote._reconnect_task

    assert auth_cloud_mock.client.mock_dispatcher[0][
        0] == DISPATCH_REMOTE_BACKEND_UP
    assert auth_cloud_mock.client.mock_dispatcher[1][
        0] == DISPATCH_REMOTE_CONNECT
Пример #5
0
def test_subscription_not_expired(cloud_client):
    """Test subscription not being expired."""
    cl = cloud.Cloud(cloud_client, cloud.MODE_DEV)

    token_val = {"custom:sub-exp": "2017-11-13"}
    with patch.object(cl, "_decode_claims", return_value=token_val), patch(
            "hass_nabucasa.utcnow",
            return_value=utcnow().replace(year=2017, month=11, day=9),
    ):
        assert not cl.subscription_expired
Пример #6
0
async def test_load_backend_exists_wrong_cert(
    auth_cloud_mock, valid_acme_mock, mock_cognito, aioclient_mock, snitun_mock
):
    """Initialize backend."""
    valid = utcnow() + timedelta(days=1)
    auth_cloud_mock.remote_api_url = "https://test.local/api"
    remote = RemoteUI(auth_cloud_mock)

    aioclient_mock.post(
        "https://test.local/api/register_instance",
        json={
            "domain": "test.dui.nabu.casa",
            "email": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    valid_acme_mock.common_name = "wrong.dui.nabu.casa"
    await remote.load_backend()
    await asyncio.sleep(0.1)

    assert remote.snitun_server == "rest-remote.nabu.casa"
    assert valid_acme_mock.call_reset
    assert valid_acme_mock.init_args == (
        auth_cloud_mock,
        "test.dui.nabu.casa",
        "*****@*****.**",
    )
    assert valid_acme_mock.call_hardening
    assert snitun_mock.call_start
    assert snitun_mock.init_args == (auth_cloud_mock.client.aiohttp_runner, None)
    assert snitun_mock.init_kwarg == {
        "snitun_server": "rest-remote.nabu.casa",
        "snitun_port": 443,
    }

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

    await remote.disconnect()
    await asyncio.sleep(0.1)

    assert snitun_mock.call_disconnect
Пример #7
0
async def test_load_backend_not_exists_cert(
    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": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    acme_mock.set_false()
    await remote.load_backend()
    await asyncio.sleep(0.1)

    assert remote.snitun_server == "rest-remote.nabu.casa"
    assert acme_mock.call_issue
    assert acme_mock.init_args == (
        cloud_mock,
        "test.dui.nabu.casa",
        "*****@*****.**",
    )
    assert acme_mock.call_hardening
    assert snitun_mock.call_start
    assert snitun_mock.init_args == (None, None)
    assert snitun_mock.init_kwarg == {
        "snitun_server": "rest-remote.nabu.casa",
        "snitun_port": 443,
    }

    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
Пример #8
0
async def test_load_backend_no_autostart(
    auth_cloud_mock, valid_acme_mock, mock_cognito, aioclient_mock, snitun_mock
):
    """Initialize backend."""
    valid = utcnow() + timedelta(days=1)
    auth_cloud_mock.remote_api_url = "https://test.local/api"
    remote = RemoteUI(auth_cloud_mock)

    aioclient_mock.post(
        "https://test.local/api/register_instance",
        json={
            "domain": "test.dui.nabu.casa",
            "email": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    auth_cloud_mock.client.prop_remote_autostart = False
    await remote.load_backend()
    await asyncio.sleep(0.1)

    assert remote.snitun_server == "rest-remote.nabu.casa"
    assert not valid_acme_mock.call_issue
    assert valid_acme_mock.call_hardening
    assert snitun_mock.call_start

    assert not snitun_mock.call_connect

    await remote.connect()

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

    await remote.disconnect()
    await asyncio.sleep(0.1)

    assert snitun_mock.call_disconnect
Пример #9
0
async def test_get_certificate_details(auth_cloud_mock, acme_mock,
                                       mock_cognito, aioclient_mock,
                                       snitun_mock):
    """Initialize backend."""
    valid = utcnow() + timedelta(days=1)
    auth_cloud_mock.remote_api_url = "https://test.local/api"
    remote = RemoteUI(auth_cloud_mock)

    assert remote.certificate is None

    aioclient_mock.post(
        "https://test.local/api/register_instance",
        json={
            "domain": "test.dui.nabu.casa",
            "email": "*****@*****.**",
            "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(),
            "throttling": 400,
        },
    )

    auth_cloud_mock.client.prop_remote_autostart = False
    await remote.load_backend()
    await asyncio.sleep(0.1)
    assert remote.certificate is None

    acme_mock.common_name = "test"
    acme_mock.expire_date = valid
    acme_mock.fingerprint = "ffff"

    certificate = remote.certificate
    assert certificate.common_name == "test"
    assert certificate.expire_date == valid
    assert certificate.fingerprint == "ffff"
Пример #10
0
def valid_acme_mock(acme_mock):
    """Mock ACME client with valid cert."""
    acme_mock.common_name = "test.dui.nabu.casa"
    acme_mock.expire_date = utcnow() + timedelta(days=60)
    return acme_mock