Exemple #1
0
async def setup_single_session(server, http_redirect):
    """

    Set up a single-car account authenticated session for testing.

    Use in a test case to obtain a controller object that is logged into a single-vehicle account.

    """
    http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
    controller = subarulink.Controller(
        http_redirect.session,
        TEST_USERNAME,
        TEST_PASSWORD,
        TEST_DEVICE_ID,
        TEST_PIN,
        TEST_DEVICE_NAME,
    )
    task = asyncio.create_task(controller.connect())

    await server_js_response(server,
                             LOGIN_SINGLE_REGISTERED,
                             path=sc.API_LOGIN)
    await server_js_response(
        server,
        REFRESH_VEHICLES_SINGLE,
        path=sc.API_REFRESH_VEHICLES,
        query={"_": str(int(time.time()))},
    )
    assert await task
    return controller
Exemple #2
0
async def test_connect_multi_car(http_redirect, ssl_certificate):
    async with CaseControlledTestServer(ssl=ssl_certificate.server_context()) as server:
        http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
        controller = subarulink.Controller(
            http_redirect.session, TEST_USERNAME, TEST_PASSWORD, TEST_DEVICE_ID, TEST_PIN, TEST_DEVICE_NAME,
        )
        task = asyncio.create_task(controller.connect())

        await server_js_response(server, LOGIN_MULTI_REGISTERED, path=sc.API_LOGIN)
        await server_js_response(
            server, REFRESH_VEHICLES_MULTI, path=sc.API_REFRESH_VEHICLES, query={"_": str(int(time.time()))},
        )
        await server_js_response(
            server,
            SELECT_VEHICLE_1,
            path=sc.API_SELECT_VEHICLE,
            query={"vin": TEST_VIN_1_G1, "_": str(int(time.time()))},
        )
        await server_js_response(
            server,
            SELECT_VEHICLE_2,
            path=sc.API_SELECT_VEHICLE,
            query={"vin": TEST_VIN_2_EV, "_": str(int(time.time()))},
        )
        await server_js_response(
            server,
            SELECT_VEHICLE_3,
            path=sc.API_SELECT_VEHICLE,
            query={"vin": TEST_VIN_3_G2, "_": str(int(time.time()))},
        )
        await server_js_response(
            server,
            SELECT_VEHICLE_4,
            path=sc.API_SELECT_VEHICLE,
            query={"vin": TEST_VIN_4_SAFETY_PLUS, "_": str(int(time.time()))},
        )
        await server_js_response(
            server,
            SELECT_VEHICLE_5,
            path=sc.API_SELECT_VEHICLE,
            query={"vin": TEST_VIN_5_G1_SECURITY, "_": str(int(time.time()))},
        )

        response = await task
        assert response
        vehicles = controller.get_vehicles()
        assert TEST_VIN_1_G1 in vehicles
        assert TEST_VIN_2_EV in vehicles
        assert TEST_VIN_3_G2 in vehicles
        assert TEST_VIN_4_SAFETY_PLUS in vehicles
        assert not controller.get_ev_status(TEST_VIN_1_G1)
        assert controller.get_ev_status(TEST_VIN_2_EV)
        assert not controller.get_remote_status(TEST_VIN_1_G1)
        assert controller.get_remote_status(TEST_VIN_3_G2)
        assert not controller.get_res_status(TEST_VIN_1_G1)
        assert controller.get_remote_status(TEST_VIN_3_G2)
        assert not controller.get_safety_status(TEST_VIN_1_G1)
        assert controller.get_safety_status(TEST_VIN_4_SAFETY_PLUS)
Exemple #3
0
async def test_connect_device_registration(http_redirect, ssl_certificate):
    with patch("asyncio.sleep", new=CoroutineMock()):
        async with CaseControlledTestServer(
                ssl=ssl_certificate.server_context()) as server:
            http_redirect.add_server(sc.MOBILE_API_SERVER[sc.COUNTRY_USA], 443,
                                     server.port)
            http_redirect.add_server(sc.WEB_API_SERVER[sc.COUNTRY_USA], 443,
                                     server.port)
            controller = subarulink.Controller(
                http_redirect.session,
                TEST_USERNAME,
                TEST_PASSWORD,
                TEST_DEVICE_ID,
                TEST_PIN,
                TEST_DEVICE_NAME,
            )
            task = asyncio.create_task(controller.connect())

            await server_js_response(server,
                                     LOGIN_SINGLE_NOT_REGISTERED,
                                     path=sc.API_LOGIN)
            await server_js_response(server,
                                     VALIDATE_SESSION_SUCCESS,
                                     path=sc.API_VALIDATE_SESSION)
            await server_js_response(
                server,
                SELECT_VEHICLE_1,
                path=sc.API_SELECT_VEHICLE,
                query={
                    "vin": TEST_VIN_1_G1,
                    "_": str(int(time.time()))
                },
            )
            await server_js_response(
                server,
                REFRESH_VEHICLES_SINGLE,
                path=sc.API_REFRESH_VEHICLES,
                query={"_": str(int(time.time()))},
            )
            await server_js_response(server, True, path=sc.WEB_API_LOGIN)
            await server_js_response(server,
                                     True,
                                     path=sc.WEB_API_AUTHORIZE_DEVICE)
            await server_js_response(server, True, path=sc.WEB_API_NAME_DEVICE)
            await server_js_response(server,
                                     LOGIN_SINGLE_NOT_REGISTERED,
                                     path=sc.API_LOGIN)
            await server_js_response(server,
                                     LOGIN_SINGLE_NOT_REGISTERED,
                                     path=sc.API_LOGIN)
            await server_js_response(server,
                                     LOGIN_SINGLE_REGISTERED,
                                     path=sc.API_LOGIN)

            response = await task
            assert response
Exemple #4
0
async def test_connect_incomplete_credentials(http_redirect, ssl_certificate):
    async with CaseControlledTestServer(ssl=ssl_certificate.server_context()) as server:
        http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
        controller = subarulink.Controller(
            http_redirect.session, TEST_USERNAME, None, TEST_DEVICE_ID, TEST_PIN, TEST_DEVICE_NAME,
        )
        task = asyncio.create_task(controller.connect())

        with pytest.raises(subarulink.SubaruException):
            await task
Exemple #5
0
async def test_handle_404(http_redirect, ssl_certificate):
    async with CaseControlledTestServer(ssl=ssl_certificate.server_context()) as server:
        http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
        controller = subarulink.Controller(
            http_redirect.session, TEST_USERNAME, TEST_PASSWORD, TEST_DEVICE_ID, TEST_PIN, TEST_DEVICE_NAME,
        )
        task = asyncio.create_task(controller.connect())

        await server_js_response(server, LOGIN_INVALID_PASSWORD, path=sc.API_LOGIN, status=404)
        with pytest.raises(subarulink.SubaruException):
            await task
Exemple #6
0
async def test_test_login_fail(http_redirect, ssl_certificate):
    async with CaseControlledTestServer(ssl=ssl_certificate.server_context()) as server:
        http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
        controller = subarulink.Controller(
            http_redirect.session, TEST_USERNAME, TEST_PASSWORD, TEST_DEVICE_ID, TEST_PIN, TEST_DEVICE_NAME,
        )

        for fail_msg in LOGIN_ERRORS:
            task = asyncio.create_task(controller.connect(test_login=True))
            await server_js_response(server, fail_msg, path=sc.API_LOGIN)
            with pytest.raises(subarulink.SubaruException):
                await task
Exemple #7
0
async def test_test_login_success(http_redirect, ssl_certificate):
    async with CaseControlledTestServer(ssl=ssl_certificate.server_context()) as server:
        http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
        controller = subarulink.Controller(
            http_redirect.session, TEST_USERNAME, TEST_PASSWORD, TEST_DEVICE_ID, TEST_PIN, TEST_DEVICE_NAME,
        )
        task = asyncio.create_task(controller.connect(test_login=True))

        await server_js_response(server, LOGIN_SINGLE_REGISTERED, path=sc.API_LOGIN)
        await server_js_response(
            server, REFRESH_VEHICLES_SINGLE, path=sc.API_REFRESH_VEHICLES, query={"_": str(int(time.time()))},
        )

        assert await task is True
Exemple #8
0
async def test_connect_single_car(http_redirect, ssl_certificate):
    async with CaseControlledTestServer(ssl=ssl_certificate.server_context()) as server:
        http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
        controller = subarulink.Controller(
            http_redirect.session, TEST_USERNAME, TEST_PASSWORD, TEST_DEVICE_ID, TEST_PIN, TEST_DEVICE_NAME,
        )
        task = asyncio.create_task(controller.connect())

        await server_js_response(server, LOGIN_SINGLE_REGISTERED, path=sc.API_LOGIN)
        await server_js_response(
            server, REFRESH_VEHICLES_SINGLE, path=sc.API_REFRESH_VEHICLES, query={"_": str(int(time.time()))},
        )

        response = await task
        assert response is True
        assert controller.get_vehicles() == [TEST_VIN_1_G1]
        assert controller.get_ev_status(TEST_VIN_1_G1) is False
Exemple #9
0
async def setup_multi_session(server, http_redirect):
    """

    Set up a multi-car account authenticated session for testing.

    Use in a test case to obtain a controller object that is logged into a multi-vehicle account.

    """
    http_redirect.add_server(sc.MOBILE_API_SERVER, 443, server.port)
    controller = subarulink.Controller(
        http_redirect.session,
        TEST_USERNAME,
        TEST_PASSWORD,
        TEST_DEVICE_ID,
        TEST_PIN,
        TEST_DEVICE_NAME,
    )
    task = asyncio.create_task(controller.connect())

    await server_js_response(server, LOGIN_MULTI_REGISTERED, path=sc.API_LOGIN)
    await server_js_response(
        server,
        REFRESH_VEHICLES_MULTI,
        path=sc.API_REFRESH_VEHICLES,
        query={"_": str(int(time.time()))},
    )
    await server_js_response(
        server,
        SELECT_VEHICLE_1,
        path=sc.API_SELECT_VEHICLE,
        query={
            "vin": TEST_VIN_1_G1,
            "_": str(int(time.time()))
        },
    )
    await server_js_response(
        server,
        SELECT_VEHICLE_2,
        path=sc.API_SELECT_VEHICLE,
        query={
            "vin": TEST_VIN_2_EV,
            "_": str(int(time.time()))
        },
    )
    await server_js_response(
        server,
        SELECT_VEHICLE_3,
        path=sc.API_SELECT_VEHICLE,
        query={
            "vin": TEST_VIN_3_G2,
            "_": str(int(time.time()))
        },
    )
    await server_js_response(
        server,
        SELECT_VEHICLE_4,
        path=sc.API_SELECT_VEHICLE,
        query={
            "vin": TEST_VIN_4_SAFETY_PLUS,
            "_": str(int(time.time()))
        },
    )
    await server_js_response(
        server,
        SELECT_VEHICLE_5,
        path=sc.API_SELECT_VEHICLE,
        query={
            "vin": TEST_VIN_5_G1_SECURITY,
            "_": str(int(time.time()))
        },
    )
    assert await task
    return controller