async def test_connect_sync_success(v3_server):
    """Test triggering a synchronous handler upon connection to the websocket."""
    async with v3_server:
        async with aiohttp.ClientSession() as session:
            simplisafe = await API.login_via_credentials(
                TEST_EMAIL,
                TEST_PASSWORD,
                client_id=TEST_CLIENT_ID,
                session=session)

            simplisafe.websocket._sio.eio._trigger_event = async_mock()
            simplisafe.websocket._sio.eio.connect = async_mock()

            on_connect = AsyncMock()
            simplisafe.websocket.on_connect(on_connect)

            connect_params = {
                "ns": f"/v1/user/{TEST_USER_ID}",
                "accessToken": TEST_ACCESS_TOKEN,
            }

            await simplisafe.websocket.async_connect()
            simplisafe.websocket._sio.eio.connect.mock.assert_called_once_with(
                f"wss://api.simplisafe.com/socket.io?{urlencode(connect_params)}",
                engineio_path="socket.io",
                headers={},
                transports=["websocket"],
            )

            await simplisafe.websocket._sio._trigger_event("connect", "/")
            on_connect.assert_called_once()
async def test_watchdog_firing():
    """Test that the watchdog expiring fires the provided coroutine."""
    mock_coro = AsyncMock()
    mock_coro.__name__ = "mock_coro"

    watchdog = WebsocketWatchdog(mock_coro)

    await watchdog.on_expire()
    mock_coro.assert_called_once()
async def test_reconnect_on_new_access_token(aresponses, v3_server):
    """Test reconnecting to the websocket when the access token refreshes."""
    async with v3_server:
        v3_server.add(
            "api.simplisafe.com",
            "/v1/api/token",
            "post",
            aresponses.Response(text=load_fixture("api_token_response.json"),
                                status=200),
        )
        v3_server.add(
            "api.simplisafe.com",
            "/v1/api/authCheck",
            "get",
            aresponses.Response(text=load_fixture("auth_check_response.json"),
                                status=200),
        )

        async with aiohttp.ClientSession() as session:
            simplisafe = await API.login_via_credentials(
                TEST_EMAIL,
                TEST_PASSWORD,
                client_id=TEST_CLIENT_ID,
                session=session)

            simplisafe.websocket._sio.eio._trigger_event = async_mock()
            simplisafe.websocket._sio.eio.connect = async_mock()
            simplisafe.websocket._sio.eio.disconnect = async_mock()

            on_connect = AsyncMock()
            simplisafe.websocket.on_connect(on_connect)

            connect_params = {
                "ns": f"/v1/user/{TEST_USER_ID}",
                "accessToken": TEST_ACCESS_TOKEN,
            }

            await simplisafe.websocket.async_connect()
            simplisafe.websocket._sio.eio.connect.mock.assert_called_once_with(
                f"wss://api.simplisafe.com/socket.io?{urlencode(connect_params)}",
                engineio_path="socket.io",
                headers={},
                transports=["websocket"],
            )

            await simplisafe.websocket._sio._trigger_event("connect", "/")
            on_connect.assert_called_once()

            await simplisafe.refresh_access_token(TEST_REFRESH_TOKEN)
            simplisafe.websocket._sio.eio.disconnect.mock.assert_called_once()
            assert simplisafe.websocket._sio.eio.connect.mock.call_count == 2
Esempio n. 4
0
async def test_connect_sync_success():
    """Test connecting to the socket with a sync handler."""
    websocket = Websocket(TEST_API_KEY, TEST_APP_KEY)
    websocket._sio.connect = AsyncMock()
    websocket._sio.eio._trigger_event = AsyncMock()
    websocket._sio.namespaces = {"/": 1}

    async_on_connect = AsyncMock()
    websocket.async_on_connect(async_on_connect)

    await websocket.connect()
    websocket._sio.connect.assert_called_once_with(
        f"https://rt2.ambientweather.net/?api=1&applicationKey={TEST_APP_KEY}",
        transports=["websocket"],
    )

    await websocket._sio._trigger_event("connect", "/")
    async_on_connect.assert_called_once()
Esempio n. 5
0
async def test_reconnect():
    """Test that reconnecting to the websocket does the right thing."""
    websocket = Websocket(TEST_API_KEY, TEST_APP_KEY)
    websocket._sio.connect = AsyncMock()
    websocket._sio.eio._trigger_event = AsyncMock()
    websocket._sio.namespaces = {"/": 1}

    async_on_connect = AsyncMock()
    async_on_disconnect = AsyncMock()

    websocket.async_on_connect(async_on_connect)
    websocket.async_on_disconnect(async_on_disconnect)

    await websocket.reconnect()
    await websocket._sio._trigger_event("disconnect", "/")
    async_on_disconnect.assert_called_once()
    await websocket._sio._trigger_event("connect", "/")
    async_on_connect.assert_called_once()
Esempio n. 6
0
async def test_reconnect(event_loop):
    """Test that reconnecting to the websocket does the right thing."""
    async with aiohttp.ClientSession(loop=event_loop) as session:
        client = Client(TEST_API_KEY, TEST_APP_KEY, session=session)
        client.websocket._sio.eio._trigger_event = AsyncMock()
        client.websocket._sio.eio.connect = AsyncMock()

        async_on_connect = AsyncMock()
        async_on_disconnect = AsyncMock()

        client.websocket.async_on_connect(async_on_connect)
        client.websocket.async_on_disconnect(async_on_disconnect)

        await client.websocket.reconnect()
        await client.websocket._sio._trigger_event("disconnect", "/")
        async_on_disconnect.assert_called_once()
        await client.websocket._sio._trigger_event("connect", "/")
        async_on_connect.assert_called_once()
Esempio n. 7
0
async def test_connect_sync_success(event_loop):
    """Test connecting to the socket with a sync handler."""
    async with aiohttp.ClientSession(loop=event_loop) as session:
        client = Client(TEST_API_KEY, TEST_APP_KEY, session=session)
        client.websocket._sio.eio._trigger_event = AsyncMock()
        client.websocket._sio.eio.connect = AsyncMock()

        async_on_connect = AsyncMock()
        client.websocket.async_on_connect(async_on_connect)

        await client.websocket.connect()
        client.websocket._sio.eio.connect.assert_called_once_with(
            f"https://api.ambientweather.net/?api=1&applicationKey={TEST_APP_KEY}",
            engineio_path="socket.io",
            headers={},
            transports=["websocket"],
        )

        await client.websocket._sio._trigger_event("connect", "/")
        async_on_connect.assert_called_once()
async def test_sync_events(v3_server):
    """Test events with synchronous handlers."""
    async with v3_server:
        async with aiohttp.ClientSession() as session:
            simplisafe = await API.login_via_credentials(
                TEST_EMAIL,
                TEST_PASSWORD,
                client_id=TEST_CLIENT_ID,
                session=session)

            simplisafe.websocket._sio.eio._trigger_event = async_mock()
            simplisafe.websocket._sio.eio.connect = async_mock()
            simplisafe.websocket._sio.eio.disconnect = async_mock()

            on_connect = AsyncMock()
            on_disconnect = AsyncMock()
            on_event = AsyncMock()

            simplisafe.websocket.on_connect(on_connect)
            simplisafe.websocket.on_disconnect(on_disconnect)
            simplisafe.websocket.on_event(on_event)

            connect_params = {
                "ns": f"/v1/user/{TEST_USER_ID}",
                "accessToken": TEST_ACCESS_TOKEN,
            }

            await simplisafe.websocket.async_connect()
            simplisafe.websocket._sio.eio.connect.mock.assert_called_once_with(
                f"wss://api.simplisafe.com/socket.io?{urlencode(connect_params)}",
                engineio_path="socket.io",
                headers={},
                transports=["websocket"],
            )

            await simplisafe.websocket._sio._trigger_event("connect", "/")
            on_connect.assert_called_once()

            await simplisafe.websocket._sio._trigger_event(
                "event",
                f"/v1/user/{TEST_USER_ID}",
                json.loads(
                    load_fixture("websocket_known_event_response.json")),
            )
            on_event.assert_called_once()

            await simplisafe.websocket.async_disconnect()
            await simplisafe.websocket._sio._trigger_event("disconnect", "/")
            simplisafe.websocket._sio.eio.disconnect.mock.assert_called_once_with(
                abort=True)
            on_disconnect.assert_called_once()
Esempio n. 9
0
async def test_data_async(event_loop):
    """Test data and subscription with async handlers."""
    async with aiohttp.ClientSession(loop=event_loop) as session:
        client = Client(TEST_API_KEY, TEST_APP_KEY, session=session)
        client.websocket._sio.eio._trigger_event = AsyncMock()
        client.websocket._sio.eio.connect = AsyncMock()
        client.websocket._sio.eio.disconnect = AsyncMock()

        async_on_connect = AsyncMock()
        async_on_data = AsyncMock()
        async_on_disconnect = AsyncMock()
        async_on_subscribed = AsyncMock()

        client.websocket.async_on_connect(async_on_connect)
        client.websocket.async_on_data(async_on_data)
        client.websocket.async_on_disconnect(async_on_disconnect)
        client.websocket.async_on_subscribed(async_on_subscribed)

        await client.websocket.connect()
        client.websocket._sio.eio.connect.assert_called_once_with(
            f"https://api.ambientweather.net/?api=1&applicationKey={TEST_APP_KEY}",
            engineio_path="socket.io",
            headers={},
            transports=["websocket"],
        )

        await client.websocket._sio._trigger_event("connect", "/")
        async_on_connect.assert_called_once()

        await client.websocket._sio._trigger_event("data", "/", {"foo": "bar"})
        async_on_data.assert_called_once()

        await client.websocket._sio._trigger_event("subscribed", "/", {"foo": "bar"})
        async_on_subscribed.assert_called()

        await client.websocket.disconnect()
        await client.websocket._sio._trigger_event("disconnect", "/")
        async_on_disconnect.assert_called_once()
        client.websocket._sio.eio.disconnect.assert_called_once_with(abort=True)
Esempio n. 10
0
async def test_data_async():
    """Test data and subscription with async handlers."""
    websocket = Websocket(TEST_API_KEY, TEST_APP_KEY)
    websocket._sio.connect = AsyncMock()
    websocket._sio.disconnect = AsyncMock()
    websocket._sio.eio._trigger_event = AsyncMock()
    websocket._sio.namespaces = {"/": 1}

    async_on_connect = AsyncMock()
    async_on_data = AsyncMock()
    async_on_disconnect = AsyncMock()
    async_on_subscribed = AsyncMock()

    websocket.async_on_connect(async_on_connect)
    websocket.async_on_data(async_on_data)
    websocket.async_on_disconnect(async_on_disconnect)
    websocket.async_on_subscribed(async_on_subscribed)

    await websocket.connect()
    websocket._sio.connect.assert_called_once_with(
        f"https://rt2.ambientweather.net/?api=1&applicationKey={TEST_APP_KEY}",
        transports=["websocket"],
    )

    await websocket._sio._trigger_event("connect", "/")
    async_on_connect.assert_called_once()

    await websocket._sio._trigger_event("data", "/", {"foo": "bar"})
    async_on_data.assert_called_once()

    await websocket._sio._trigger_event("subscribed", "/", {"foo": "bar"})
    async_on_subscribed.assert_called()

    await websocket.disconnect()
    await websocket._sio._trigger_event("disconnect", "/")
    async_on_disconnect.assert_called_once()
    websocket._sio.disconnect.assert_called_once()