Skip to content

Fix flaky gap-analysis tests connecting to real Redis instead of mock - #1010

Open
DevPatils wants to merge 1 commit into
OWASP:mainfrom
DevPatils:1009-fix-flaky-redis-mock-in-tests
Open

Fix flaky gap-analysis tests connecting to real Redis instead of mock#1010
DevPatils wants to merge 1 commit into
OWASP:mainfrom
DevPatils:1009-fix-flaky-redis-mock-in-tests

Conversation

@DevPatils

Copy link
Copy Markdown
Contributor

Summary

Fixes #1009 — several tests in web_main_test.py covering
/rest/v1/map_analysis were silently connecting to a real local Redis
instance instead of the mock, causing flaky, order-dependent,
environment-dependent test failures.

Root cause

The tests patched redis.from_url (the third-party library's URL-based
connection factory), but application/utils/redis.py's connect()
function has three possible branches and takes a different, unmocked
one — redis.StrictRedis(host=..., port=...) — whenever REDIS_HOST and
REDIS_PORT environment variables are set. These are set by default for
anyone following this project's own documented local setup
(.env.example sets REDIS_HOST=localhost / REDIS_PORT=6379, loaded
automatically via load_dotenv() in cre.py). So the standard,
documented local dev setup silently defeated the mock for anyone running
these tests.

The fix

Patch application.utils.redis.connect directly, at its actual point of
use in web_main.py, instead of patching one specific branch
(redis.from_url) of its internal implementation. This makes the tests
immune to which branch connect() takes based on environment variables —
there's now exactly one thing to mock, and it's the thing that's actually
called.

# before
@patch.object(redis, "from_url")

# after
@patch.object(app_redis, "connect")

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro Plus

Run ID: caa22e3c-56fc-4ca2-804e-6048ffba2358

📥 Commits

Reviewing files that changed from the base of the PR and between 5197093 and 109d73e.

📒 Files selected for processing (1)
  • application/tests/web_main_test.py

Summary by CodeRabbit

  • Tests
    • Updated Redis-related test setup to use the application’s standard Redis connection utility.
    • Preserved existing test behavior and assertions.

Walkthrough

The test module now imports the application Redis utility and patches app_redis.connect across gap-analysis and standards tests.

Changes

Redis test isolation

Layer / File(s) Summary
Update application Redis patches
application/tests/web_main_test.py
The tests import app_redis and patch app_redis.connect across gap-analysis and standards scenarios instead of patching the direct Redis client.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: northdpole, pa04rth, paoga87

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the fix for flaky gap-analysis tests that connected to real Redis instead of the mock.
Description check ✅ Passed The description directly explains the Redis mocking failure, its environment-dependent cause, and the applied test fix.
Linked Issues check ✅ Passed The changes satisfy issue #1009 by patching application.utils.redis.connect and preventing tests from reaching real Redis.
Out of Scope Changes check ✅ Passed The changes are limited to updating Redis mocking in the affected gap-analysis tests and contain no unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@DevPatils
DevPatils force-pushed the 1009-fix-flaky-redis-mock-in-tests branch from 5197093 to 109d73e Compare August 10, 2026 09:25
@DevPatils

Copy link
Copy Markdown
Contributor Author

@northdpole can you please review it once?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gap-analysis tests silently connect to real Redis instead of the mock, causing flaky/order-dependent failures

1 participant