Пример #1
0
async def test_save_ut(users, redis_cache):
    owner_id = 1234
    ut = user_tokens.UserTokens(
        redis_cache,
        owner_id,
        users,
    )

    await ut.save_to_cache()
    rut = await user_tokens.UserTokens._retrieve_from_cache(
        redis_cache, owner_id)
    assert ut == rut
Пример #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()
Пример #3
0
async def dashboard(redis_cache: redis_utils.RedisCache,
                    request: pytest.FixtureRequest) -> DashboardFixture:
    is_unittest_class = request.cls is not None
    subscription_active = False
    marker = request.node.get_closest_marker("subscription")
    if marker:
        subscription_active = marker.args[0]
    elif is_unittest_class:
        subscription_active = request.cls.SUBSCRIPTION_ACTIVE

    api_key_admin = "a" * 64

    sub = subscription.Subscription(
        redis_cache,
        config.TESTING_ORGANIZATION_ID,
        "You're not nice",
        frozenset(
            getattr(subscription.Features, f)
            for f in subscription.Features.__members__) if subscription_active
        else frozenset([subscription.Features.PUBLIC_REPOSITORY]),
    )
    await sub._save_subscription_to_cache()
    user_tokens = user_tokens_mod.UserTokens(
        redis_cache,
        config.TESTING_ORGANIZATION_ID,
        [
            {
                "id": github_types.GitHubAccountIdType(config.ORG_ADMIN_ID),
                "login": github_types.GitHubLogin("mergify-test1"),
                "oauth_access_token": config.ORG_ADMIN_GITHUB_APP_OAUTH_TOKEN,
                "name": None,
                "email": None,
            },
            {
                "id": github_types.GitHubAccountIdType(config.ORG_USER_ID),
                "login": github_types.GitHubLogin("mergify-test4"),
                "oauth_access_token": config.ORG_USER_PERSONAL_TOKEN,
                "name": None,
                "email": None,
            },
        ],
    )
    await typing.cast(user_tokens_mod.UserTokensSaas,
                      user_tokens).save_to_cache()

    real_get_subscription = subscription.Subscription.get_subscription

    async def fake_retrieve_subscription_from_db(redis_cache, owner_id):
        if owner_id == config.TESTING_ORGANIZATION_ID:
            return sub
        return subscription.Subscription(
            redis_cache,
            owner_id,
            "We're just testing",
            set(subscription.Features.PUBLIC_REPOSITORY),
        )

    async def fake_subscription(redis_cache, owner_id):
        if owner_id == config.TESTING_ORGANIZATION_ID:
            return await real_get_subscription(redis_cache, owner_id)
        return subscription.Subscription(
            redis_cache,
            owner_id,
            "We're just testing",
            set(subscription.Features.PUBLIC_REPOSITORY),
        )

    patcher = mock.patch(
        "mergify_engine.dashboard.subscription.Subscription._retrieve_subscription_from_db",
        side_effect=fake_retrieve_subscription_from_db,
    )
    patcher.start()
    request.addfinalizer(patcher.stop)

    patcher = mock.patch(
        "mergify_engine.dashboard.subscription.Subscription.get_subscription",
        side_effect=fake_subscription,
    )
    patcher.start()
    request.addfinalizer(patcher.stop)

    async def fake_retrieve_user_tokens_from_db(redis_cache, owner_id):
        if owner_id == config.TESTING_ORGANIZATION_ID:
            return user_tokens
        return user_tokens_mod.UserTokens(redis_cache, owner_id, {})

    real_get_user_tokens = user_tokens_mod.UserTokens.get

    async def fake_user_tokens(redis_cache, owner_id):
        if owner_id == config.TESTING_ORGANIZATION_ID:
            return await real_get_user_tokens(redis_cache, owner_id)
        return user_tokens_mod.UserTokens(redis_cache, owner_id, {})

    patcher = mock.patch(
        "mergify_engine.dashboard.user_tokens.UserTokensSaas._retrieve_from_db",
        side_effect=fake_retrieve_user_tokens_from_db,
    )
    patcher.start()
    request.addfinalizer(patcher.stop)

    patcher = mock.patch(
        "mergify_engine.dashboard.user_tokens.UserTokensSaas.get",
        side_effect=fake_user_tokens,
    )
    patcher.start()
    request.addfinalizer(patcher.stop)

    async def fake_application_get(redis_cache, api_access_key, api_secret_key,
                                   account_scope):
        if (api_access_key == api_key_admin[:32]
                and api_secret_key == api_key_admin[32:]):
            return application_mod.Application(
                redis_cache,
                123,
                "testing application",
                api_access_key,
                api_secret_key,
                account_scope={
                    "id": config.TESTING_ORGANIZATION_ID,
                    "login": config.TESTING_ORGANIZATION_NAME,
                },
            )
        raise application_mod.ApplicationUserNotFound()

    patcher = mock.patch(
        "mergify_engine.dashboard.application.ApplicationSaas.get",
        side_effect=fake_application_get,
    )
    patcher.start()
    request.addfinalizer(patcher.stop)

    return DashboardFixture(
        api_key_admin,
        sub,
        user_tokens,
    )
Пример #4
0
 async def fake_user_tokens(redis_cache, owner_id):
     if owner_id == config.TESTING_ORGANIZATION_ID:
         return await real_get_user_tokens(redis_cache, owner_id)
     return user_tokens_mod.UserTokens(redis_cache, owner_id, {})
Пример #5
0
 async def fake_retrieve_user_tokens_from_db(redis_cache, owner_id):
     if owner_id == config.TESTING_ORGANIZATION_ID:
         return user_tokens
     return user_tokens_mod.UserTokens(redis_cache, owner_id, {})
Пример #6
0
async def test_init(redis_cache):
    user_tokens.UserTokens(redis_cache, 123, [])