示例#1
0
def test_notify_list(withings_api: WithingsApi) -> None:
    """Test function."""
    responses_add_notify_list()

    assert withings_api.notify_list() == NotifyListResponse(profiles=(
        NotifyListProfile(
            appli=NotifyAppli.WEIGHT,
            callbackurl="http://localhost/callback",
            comment="fake_comment1",
            expires=None,
        ),
        NotifyListProfile(
            appli=NotifyAppli.CIRCULATORY,
            callbackurl="http://localhost/callback2",
            comment="fake_comment2",
            expires=arrow.get("2019-09-02"),
        ),
        NotifyListProfile(
            appli=NotifyAppli.UNKNOWN,
            callbackurl="http://localhost/callback2",
            comment="fake_comment2",
            expires=arrow.get("2019-09-02"),
        ),
    ))

    assert_url_path(responses.calls[0].request.url, "/notify")
    assert_url_query_equals(responses.calls[0].request.url, {"action": "list"})
async def test_data_manager_webhook_subscription(
    hass: HomeAssistant,
    component_factory: ComponentFactory,
    aioclient_mock: AiohttpClientMocker,
) -> None:
    """Test data manager webhook subscriptions."""
    person0 = new_profile_config("person0", 0)
    await component_factory.configure_component(profile_configs=(person0, ))

    api: ConfigEntryWithingsApi = MagicMock(spec=ConfigEntryWithingsApi)
    data_manager = DataManager(
        hass,
        "person0",
        api,
        0,
        WebhookConfig(id="1234",
                      url="http://localhost/api/webhook/1234",
                      enabled=True),
    )

    # pylint: disable=protected-access
    data_manager._notify_subscribe_delay = datetime.timedelta(seconds=0)
    data_manager._notify_unsubscribe_delay = datetime.timedelta(seconds=0)

    api.notify_list.return_value = NotifyListResponse(profiles=(
        NotifyListProfile(
            appli=NotifyAppli.BED_IN,
            callbackurl="https://not.my.callback/url",
            expires=None,
            comment=None,
        ),
        NotifyListProfile(
            appli=NotifyAppli.BED_IN,
            callbackurl=data_manager.webhook_config.url,
            expires=None,
            comment=None,
        ),
        NotifyListProfile(
            appli=NotifyAppli.BED_OUT,
            callbackurl=data_manager.webhook_config.url,
            expires=None,
            comment=None,
        ),
    ))

    aioclient_mock.clear_requests()
    aioclient_mock.request(
        "HEAD",
        data_manager.webhook_config.url,
        status=200,
    )

    # Test subscribing
    await data_manager.async_subscribe_webhook()
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.WEIGHT)
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.CIRCULATORY)
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.ACTIVITY)
    api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                         NotifyAppli.SLEEP)
    try:
        api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                             NotifyAppli.USER)
        assert False
    except AssertionError:
        pass
    try:
        api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                             NotifyAppli.BED_IN)
        assert False
    except AssertionError:
        pass
    try:
        api.notify_subscribe.assert_any_call(data_manager.webhook_config.url,
                                             NotifyAppli.BED_OUT)
        assert False
    except AssertionError:
        pass

    # Test unsubscribing.
    await data_manager.async_unsubscribe_webhook()
    api.notify_revoke.assert_any_call(data_manager.webhook_config.url,
                                      NotifyAppli.BED_IN)
    api.notify_revoke.assert_any_call(data_manager.webhook_config.url,
                                      NotifyAppli.BED_OUT)