From fb183bcd1db279c9a0ebb7b7aeed86474fb2561f Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Thu, 7 Nov 2024 09:47:42 +0100 Subject: [PATCH] test: make redis configurable --- tests/conftest.py | 19 +++++++++++++++++++ tests/test_permissions_cache.py | 8 ++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 610d611..96274f4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2018 CERN. +# Copyright (C) 2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -9,9 +10,11 @@ """Pytest configuration.""" import os +import re import warnings import pytest +from cachelib import RedisCache from flask import Flask from flask.cli import ScriptInfo from flask_mail import Mail @@ -50,6 +53,10 @@ def base_app(): ACCOUNTS_USE_CELERY=False, SECRET_KEY="CHANGE_ME", SECURITY_PASSWORD_SALT="CHANGE_ME_ALSO", + CACHE_REDIS_URL=os.environ.get("CACHE_REDIS_URL", "redis://127.0.0.1:6379/0"), + CELERY_RESULT_BACKEND=os.environ.get( + "CELERY_RESULT_BACKEND", "redis://127.0.0.1:6379/2" + ), SQLALCHEMY_DATABASE_URI=os.environ.get( "SQLALCHEMY_DATABASE_URI", "sqlite:///test.db" ), @@ -98,6 +105,18 @@ def cli_app(app): return app +@pytest.fixture() +def redis_cache(app): + """Redis cache.""" + matches = re.search( + r".*/(?P.*):(?P\d\d\d\d).*", + app.config["CACHE_REDIS_URL"], + ) + redis_host = matches["host"] + redis_port = matches["port"] + return RedisCache(redis_host, redis_port) + + @pytest.fixture() def dynamic_permission(): """Dynamic permission fixture.""" diff --git a/tests/test_permissions_cache.py b/tests/test_permissions_cache.py index 7310c18..c1241cb 100644 --- a/tests/test_permissions_cache.py +++ b/tests/test_permissions_cache.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2023 CERN. +# Copyright (C) 2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -10,7 +11,7 @@ import time -from cachelib import RedisCache, SimpleCache +from cachelib import SimpleCache from flask_principal import ActionNeed, Need, RoleNeed, UserNeed from invenio_accounts.models import Role, User from invenio_db import db @@ -86,10 +87,9 @@ def test_invenio_access_permission_cache(app, dynamic_permission): ) -def test_invenio_access_permission_cache_redis(app, dynamic_permission): +def test_invenio_access_permission_cache_redis(app, redis_cache, dynamic_permission): """Caching the user using redis.""" - cache = RedisCache() - InvenioAccess(app, cache=cache) + InvenioAccess(app, cache=redis_cache) user_can_all = User(email="all@inveniosoftware.org") user_can_open = User(email="open@inveniosoftware.org") user_can_open_1 = User(email="open1@inveniosoftware.org")