def test_calendar_update(db, webhooks_client, watched_account):

    calendar_path = CALENDAR_LIST_PATH.format(watched_account.public_id)

    before = datetime.utcnow() - timedelta(seconds=1)
    assert watched_account.gpush_calendar_list_last_ping is None

    headers = UPDATE_HEADERS.copy()
    headers['X-Goog-Channel-Id'] = ACCOUNT_WATCH_UUID
    r = webhooks_client.post_data(calendar_path, {}, headers)
    assert r.status_code == 200
    db.session.refresh(watched_account)
    assert watched_account.gpush_calendar_list_last_ping > before

    unknown_id_path = CALENDAR_LIST_PATH.format(11111111111)
    r = webhooks_client.post_data(unknown_id_path, {}, headers)
    assert r.status_code == 404  # account not found

    invalid_id_path = CALENDAR_LIST_PATH.format('invalid_id')
    r = webhooks_client.post_data(invalid_id_path, {}, headers)
    assert r.status_code == 400

    bad_headers = UPDATE_HEADERS.copy()
    del bad_headers['X-Goog-Resource-State']
    r = webhooks_client.post_data(calendar_path, {}, bad_headers)
    assert r.status_code == 400
def test_calendar_update(db, webhooks_client, watched_account):

    calendar_path = CALENDAR_LIST_PATH.format(watched_account.public_id)

    before = datetime.utcnow() - timedelta(seconds=1)
    assert watched_account.gpush_calendar_list_last_ping is None

    headers = UPDATE_HEADERS.copy()
    headers['X-Goog-Channel-Id'] = ACCOUNT_WATCH_UUID
    r = webhooks_client.post_data(calendar_path, {}, headers)
    assert r.status_code == 200
    db.session.refresh(watched_account)
    assert watched_account.gpush_calendar_list_last_ping > before

    unknown_id_path = CALENDAR_LIST_PATH.format(11111111111)
    r = webhooks_client.post_data(unknown_id_path, {}, headers)
    assert r.status_code == 404  # account not found

    invalid_id_path = CALENDAR_LIST_PATH.format('invalid_id')
    r = webhooks_client.post_data(invalid_id_path, {}, headers)
    assert r.status_code == 400

    bad_headers = UPDATE_HEADERS.copy()
    del bad_headers['X-Goog-Resource-State']
    r = webhooks_client.post_data(calendar_path, {}, bad_headers)
    assert r.status_code == 400
def test_receive_sync_message(db, webhooks_client,
                              watched_account, watched_calendar):
    # Sync messages can basically be ignored
    # (see https://developers.google.com/google-apps/calendar/v3/push#sync)

    calendar_path = CALENDAR_LIST_PATH.format(watched_account.public_id)
    event_path = CALENDAR_PATH.format(watched_calendar.public_id)

    r = webhooks_client.post_data(calendar_path, {}, SYNC_HEADERS)
    assert r.status_code == 204  # No content

    r = webhooks_client.post_data(event_path, {}, SYNC_HEADERS)
    assert r.status_code == 204  # No content
def test_receive_sync_message(db, webhooks_client, watched_account,
                              watched_calendar):
    # Sync messages can basically be ignored
    # (see https://developers.google.com/google-apps/calendar/v3/push#sync)

    calendar_path = CALENDAR_LIST_PATH.format(watched_account.public_id)
    event_path = CALENDAR_PATH.format(watched_calendar.public_id)

    r = webhooks_client.post_data(calendar_path, {}, SYNC_HEADERS)
    assert r.status_code == 204  # No content

    r = webhooks_client.post_data(event_path, {}, SYNC_HEADERS)
    assert r.status_code == 204  # No content
def test_event_update(db, webhooks_client, watched_calendar):
    event_path = CALENDAR_PATH.format(watched_calendar.public_id)

    before = datetime.utcnow() - timedelta(seconds=1)
    assert watched_calendar.gpush_last_ping is None

    headers = UPDATE_HEADERS.copy()
    headers['X-Goog-Channel-Id'] = CALENDAR_WATCH_UUID
    r = webhooks_client.post_data(event_path, {}, headers)
    assert r.status_code == 200
    db.session.refresh(watched_calendar)
    gpush_last_ping = watched_calendar.gpush_last_ping
    assert gpush_last_ping > before

    # Test that gpush_last_ping is not updated if already recently updated
    watched_calendar.gpush_last_ping = gpush_last_ping - timedelta(seconds=2)
    gpush_last_ping = watched_calendar.gpush_last_ping
    db.session.commit()
    r = webhooks_client.post_data(event_path, {}, headers)
    db.session.refresh(watched_calendar)
    assert gpush_last_ping == watched_calendar.gpush_last_ping

    # Test that gpush_last_ping *is* updated if last updated too long ago
    watched_calendar.gpush_last_ping = gpush_last_ping - timedelta(seconds=22)
    db.session.commit()
    r = webhooks_client.post_data(event_path, {}, headers)
    db.session.refresh(watched_calendar)
    assert watched_calendar.gpush_last_ping > gpush_last_ping

    bad_event_path = CALENDAR_PATH.format(1111111111111)
    r = webhooks_client.post_data(bad_event_path, {}, headers)
    assert r.status_code == 404  # calendar not found

    invalid_id_path = CALENDAR_PATH.format('invalid_id')
    r = webhooks_client.post_data(invalid_id_path, {}, headers)
    assert r.status_code == 400

    bad_headers = UPDATE_HEADERS.copy()
    del bad_headers['X-Goog-Resource-State']
    r = webhooks_client.post_data(event_path, {}, bad_headers)
    assert r.status_code == 400
def test_event_update(db, webhooks_client, watched_calendar):
    event_path = CALENDAR_PATH.format(watched_calendar.public_id)

    before = datetime.utcnow() - timedelta(seconds=1)
    assert watched_calendar.gpush_last_ping is None

    headers = UPDATE_HEADERS.copy()
    headers['X-Goog-Channel-Id'] = CALENDAR_WATCH_UUID
    r = webhooks_client.post_data(event_path, {}, headers)
    assert r.status_code == 200
    db.session.refresh(watched_calendar)
    gpush_last_ping = watched_calendar.gpush_last_ping
    assert gpush_last_ping > before

    # Test that gpush_last_ping is not updated if already recently updated
    watched_calendar.gpush_last_ping = gpush_last_ping - timedelta(seconds=2)
    gpush_last_ping = watched_calendar.gpush_last_ping
    db.session.commit()
    r = webhooks_client.post_data(event_path, {}, headers)
    db.session.refresh(watched_calendar)
    assert gpush_last_ping == watched_calendar.gpush_last_ping

    # Test that gpush_last_ping *is* updated if last updated too long ago
    watched_calendar.gpush_last_ping = gpush_last_ping - timedelta(seconds=22)
    db.session.commit()
    r = webhooks_client.post_data(event_path, {}, headers)
    db.session.refresh(watched_calendar)
    assert watched_calendar.gpush_last_ping > gpush_last_ping

    bad_event_path = CALENDAR_PATH.format(1111111111111)
    r = webhooks_client.post_data(bad_event_path, {}, headers)
    assert r.status_code == 404  # calendar not found

    invalid_id_path = CALENDAR_PATH.format('invalid_id')
    r = webhooks_client.post_data(invalid_id_path, {}, headers)
    assert r.status_code == 400

    bad_headers = UPDATE_HEADERS.copy()
    del bad_headers['X-Goog-Resource-State']
    r = webhooks_client.post_data(event_path, {}, bad_headers)
    assert r.status_code == 400