Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TESTING = sys.argv[1:2] == ["test"]

DEBUG = True

Expand Down Expand Up @@ -34,6 +36,17 @@

ROOT_URLCONF = "openwisp2.urls"

ASGI_APPLICATION = "openwisp2.routing.application"
if not TESTING:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {"hosts": ["redis://localhost/3"]},
}
}
else:
CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}

TIME_ZONE = "Europe/Rome"
LANGUAGE_CODE = "en-gb"
USE_TZ = True
Expand Down Expand Up @@ -61,6 +74,29 @@
}
]

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://localhost/0",
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
},
"sessions": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://localhost/1",
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
},
}

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "sessions"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Cache and session separation correctly implemented.

The configuration successfully achieves the PR objective by using separate Redis databases:

  • default cache → Redis database 0
  • sessions cache → Redis database 1
  • SESSION_CACHE_ALIAS = "sessions" correctly points Django sessions to the dedicated cache

This ensures clearing the default cache won't terminate user sessions.

Note that both caches require Redis even during testing. Ensure Redis is available in all test environments, or consider adding conditional in-memory cache backends for TESTING mode if needed:

if TESTING:
    CACHES = {
        "default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"},
        "sessions": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"},
    }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/openwisp2/settings.py` around lines 77 - 91, The cache/session config
correctly separates Redis DBs using CACHES, SESSION_ENGINE and
SESSION_CACHE_ALIAS, but tests will still require Redis; update the settings to
detect TESTING (or DJANGO_SETTINGS_TEST) and switch CACHES to in-memory backends
during tests (use django.core.cache.backends.locmem.LocMemCache for both
"default" and "sessions") or add clear documentation/guards to ensure Redis is
available in CI; modify the module where CACHES, SESSION_ENGINE and
SESSION_CACHE_ALIAS are defined to branch on TESTING and set the alternate
backends so tests run without a Redis dependency.


if not TESTING:
CELERY_BROKER_URL = "redis://localhost/2"
else:
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = True
CELERY_BROKER_URL = "memory://"

if os.environ.get("SAMPLE_APP", False):
INSTALLED_APPS.remove("django_x509")
EXTENDED_APPS = ["django_x509"]
Expand Down
Loading