async def test_subscription_db_unavailable(retrieve_subscription_from_db_mock,
                                           redis_cache):
    owner_id = 1234
    sub = subscription.Subscription(
        redis_cache,
        owner_id,
        "friend",
        frozenset([subscription.Features.PUBLIC_REPOSITORY]),
    )
    retrieve_subscription_from_db_mock.return_value = sub

    # no cache, no db -> reraise
    retrieve_subscription_from_db_mock.side_effect = http.HTTPServiceUnavailable(
        "boom!", response=mock.Mock(), request=mock.Mock())
    with pytest.raises(http.HTTPServiceUnavailable):
        await subscription.Subscription.get_subscription(redis_cache, owner_id)
        retrieve_subscription_from_db_mock.assert_called_once()

    # no cache, but db -> got db sub
    retrieve_subscription_from_db_mock.reset_mock()
    retrieve_subscription_from_db_mock.side_effect = None
    rsub = await subscription.Subscription.get_subscription(
        redis_cache, owner_id)
    assert sub == rsub
    retrieve_subscription_from_db_mock.assert_called_once()

    # cache not expired and not db -> got cached  sub
    retrieve_subscription_from_db_mock.reset_mock()
    rsub = await subscription.Subscription.get_subscription(
        redis_cache, owner_id)
    sub.ttl = 259200
    assert rsub == sub
    retrieve_subscription_from_db_mock.assert_not_called()

    # cache expired and not db -> got cached  sub
    retrieve_subscription_from_db_mock.reset_mock()
    retrieve_subscription_from_db_mock.side_effect = http.HTTPServiceUnavailable(
        "boom!", response=mock.Mock(), request=mock.Mock())
    await redis_cache.expire(f"subscription-cache-owner-{owner_id}", 7200)
    rsub = await subscription.Subscription.get_subscription(
        redis_cache, owner_id)
    sub.ttl = 7200
    assert rsub == sub
    retrieve_subscription_from_db_mock.assert_called_once()

    # cache expired and unexpected db issue -> reraise
    retrieve_subscription_from_db_mock.reset_mock()
    retrieve_subscription_from_db_mock.side_effect = Exception("WTF")
    await redis_cache.expire(f"subscription-cache-owner-{owner_id}", 7200)
    with pytest.raises(Exception):
        await subscription.Subscription.get_subscription(redis_cache, owner_id)
    retrieve_subscription_from_db_mock.assert_called_once()
Example #2
0
async def test_user_tokens_db_unavailable(retrieve_from_db_mock, redis_cache):
    owner_id = 1234
    ut = user_tokens.UserTokens(redis_cache, owner_id, [])
    retrieve_from_db_mock.return_value = ut

    # no cache, no db -> reraise
    retrieve_from_db_mock.side_effect = http.HTTPServiceUnavailable(
        "boom!", response=mock.Mock(), request=mock.Mock()
    )
    with pytest.raises(http.HTTPServiceUnavailable):
        await user_tokens.UserTokens.get(redis_cache, owner_id)
        retrieve_from_db_mock.assert_called_once()

    # no cache, but db -> got db ut
    retrieve_from_db_mock.reset_mock()
    retrieve_from_db_mock.side_effect = None
    rut = await user_tokens.UserTokens.get(redis_cache, owner_id)
    assert ut == rut
    retrieve_from_db_mock.assert_called_once()

    # cache not expired and not db -> got cached  ut
    retrieve_from_db_mock.reset_mock()
    rut = await user_tokens.UserTokens.get(redis_cache, owner_id)
    ut.ttl = 259200
    assert rut == ut
    retrieve_from_db_mock.assert_not_called()

    # cache expired and not db -> got cached  ut
    retrieve_from_db_mock.reset_mock()
    retrieve_from_db_mock.side_effect = http.HTTPServiceUnavailable(
        "boom!", response=mock.Mock(), request=mock.Mock()
    )
    await redis_cache.expire(f"user-tokens-cache-owner-{owner_id}", 7200)
    rut = await user_tokens.UserTokens.get(redis_cache, owner_id)
    ut.ttl = 7200
    assert rut == ut
    retrieve_from_db_mock.assert_called_once()

    # cache expired and unexpected db issue -> reraise
    retrieve_from_db_mock.reset_mock()
    retrieve_from_db_mock.side_effect = Exception("WTF")
    await redis_cache.expire(f"user-tokens-cache-owner-{owner_id}", 7200)
    with pytest.raises(Exception):
        await user_tokens.UserTokens.get(redis_cache, owner_id)
    retrieve_from_db_mock.assert_called_once()
Example #3
0
async def test_application_db_unavailable(retrieve_from_db_mock, redis_cache):
    api_access_key = "a" * 32
    api_secret_key = "s" * 32
    account_id = github_types.GitHubAccountIdType(12345)
    account_login = github_types.GitHubLogin("login")
    app = application.Application(
        redis_cache,
        0,
        "app name",
        api_access_key,
        api_secret_key,
        {"id": account_id, "login": account_login},
    )
    retrieve_from_db_mock.return_value = app

    # no cache, no db -> reraise
    retrieve_from_db_mock.side_effect = http.HTTPServiceUnavailable(
        "boom!", response=mock.Mock(), request=mock.Mock()
    )
    with pytest.raises(http.HTTPServiceUnavailable):
        await application.Application.get(
            redis_cache, api_access_key, api_secret_key, account_login
        )
        retrieve_from_db_mock.assert_called_once()

    # no cache, bapp db -> got db app
    retrieve_from_db_mock.reset_mock()
    retrieve_from_db_mock.side_effect = None
    rapp = await application.Application.get(
        redis_cache, api_access_key, api_secret_key, account_login
    )
    assert app == rapp
    retrieve_from_db_mock.assert_called_once()

    # cache not expired and not db -> got cached  app
    retrieve_from_db_mock.reset_mock()
    rapp = await application.Application.get(
        redis_cache, api_access_key, api_secret_key, account_login
    )
    app.ttl = 259200
    assert rapp == app
    retrieve_from_db_mock.assert_not_called()

    # cache expired and not db -> got cached  app
    retrieve_from_db_mock.reset_mock()
    retrieve_from_db_mock.side_effect = http.HTTPServiceUnavailable(
        "boom!", response=mock.Mock(), request=mock.Mock()
    )
    await redis_cache.expire(f"api-key-cache~{api_access_key}~{account_login}", 7200)
    rapp = await application.Application.get(
        redis_cache, api_access_key, api_secret_key, account_login
    )
    app.ttl = 7200
    assert rapp == app
    retrieve_from_db_mock.assert_called_once()

    # cache expired and unexpected db issue -> reraise
    retrieve_from_db_mock.reset_mock()
    retrieve_from_db_mock.side_effect = Exception("WTF")
    await redis_cache.expire(f"api-key-cache~{api_access_key}~{account_login}", 7200)
    with pytest.raises(Exception):
        await application.Application.get(
            redis_cache, api_access_key, api_secret_key, account_login
        )
    retrieve_from_db_mock.assert_called_once()