Skip to content

Commit

Permalink
test: make redis configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
utnapischtim committed Nov 7, 2024
1 parent d0c332a commit fb183bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
#
# 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.

"""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
Expand Down Expand Up @@ -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"
),
Expand Down Expand Up @@ -98,6 +105,18 @@ def cli_app(app):
return app


@pytest.fixture()
def redis_cache(app):
"""Redis cache."""
matches = re.search(
r".*/(?P<host>.*):(?P<port>\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."""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_permissions_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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="[email protected]")
user_can_open = User(email="[email protected]")
user_can_open_1 = User(email="[email protected]")
Expand Down

0 comments on commit fb183bc

Please sign in to comment.