-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use redis parse_url method instead of a custom one (#234)
* Use redis parse_url method instead of a custom one The custom method defined here has no real advantage - the redis lib implements it better and have support for many use cases - maintaining this implementation is error-prone and unnecessary work for overworked open-source contributors :) Especially, when you want to pass query parameters here, they are not supported (for eg a custom certificate authority) * remove test about url parsing * remove unused imports
- Loading branch information
1 parent
f6c73e0
commit ba548fa
Showing
2 changed files
with
3 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,8 @@ | |
import time | ||
from unittest.mock import patch | ||
|
||
from datetime import datetime, timedelta | ||
|
||
from django.contrib.auth.models import AnonymousUser, User | ||
from django.contrib.sessions.backends.db import SessionStore | ||
from django.db.models import Q | ||
from django.http import HttpRequest, HttpResponse | ||
from django.test.client import RequestFactory | ||
from django.test.testcases import TestCase | ||
|
@@ -26,7 +23,7 @@ | |
username_block as username_block_signal, | ||
username_unblock as username_unblock_signal, | ||
) | ||
from .connection import parse_redis_url, get_redis_connection | ||
from .connection import get_redis_connection | ||
from .decorators import watch_login | ||
from .models import AccessAttempt | ||
from .test import DefenderTestCase, DefenderTransactionTestCase | ||
|
@@ -478,74 +475,6 @@ def test_is_valid_ip(self): | |
self.assertEqual(utils.is_valid_ip("::ffff:192.0.2.128"), True) | ||
self.assertEqual(utils.is_valid_ip("::ffff:8.8.8.8"), True) | ||
|
||
def test_parse_redis_url(self): | ||
""" test the parse_redis_url method """ | ||
# full regular | ||
conf = parse_redis_url("redis://user:password@localhost2:1234/2", False) | ||
self.assertEqual(conf.get("HOST"), "localhost2") | ||
self.assertEqual(conf.get("DB"), 2) | ||
self.assertEqual(conf.get("PASSWORD"), "password") | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
self.assertEqual(conf.get("USERNAME"), "user") | ||
|
||
# full non local | ||
conf = parse_redis_url( | ||
"redis://user:[email protected]:1234/2", False) | ||
self.assertEqual(conf.get("HOST"), "www.localhost.com") | ||
self.assertEqual(conf.get("DB"), 2) | ||
self.assertEqual(conf.get("PASSWORD"), "pass") | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
self.assertEqual(conf.get("USERNAME"), "user") | ||
|
||
# no user name | ||
conf = parse_redis_url("redis://password@localhost2:1234/2", False) | ||
self.assertEqual(conf.get("HOST"), "localhost2") | ||
self.assertEqual(conf.get("DB"), 2) | ||
self.assertEqual(conf.get("PASSWORD"), None) | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
|
||
# no user name 2 with colon | ||
conf = parse_redis_url("redis://:password@localhost2:1234/2", False) | ||
self.assertEqual(conf.get("HOST"), "localhost2") | ||
self.assertEqual(conf.get("DB"), 2) | ||
self.assertEqual(conf.get("PASSWORD"), "password") | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
|
||
# Empty | ||
conf = parse_redis_url(None, False) | ||
self.assertEqual(conf.get("HOST"), "localhost") | ||
self.assertEqual(conf.get("DB"), 0) | ||
self.assertEqual(conf.get("PASSWORD"), None) | ||
self.assertEqual(conf.get("PORT"), 6379) | ||
|
||
# no db | ||
conf = parse_redis_url("redis://:password@localhost2:1234", False) | ||
self.assertEqual(conf.get("HOST"), "localhost2") | ||
self.assertEqual(conf.get("DB"), 0) | ||
self.assertEqual(conf.get("PASSWORD"), "password") | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
|
||
# no password | ||
conf = parse_redis_url("redis://localhost2:1234/0", False) | ||
self.assertEqual(conf.get("HOST"), "localhost2") | ||
self.assertEqual(conf.get("DB"), 0) | ||
self.assertEqual(conf.get("PASSWORD"), None) | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
|
||
# password with special character and set the password_quote = True | ||
conf = parse_redis_url("redis://:calmkart%23%40%21@localhost:6379/0", True) | ||
self.assertEqual(conf.get("HOST"), "localhost") | ||
self.assertEqual(conf.get("DB"), 0) | ||
self.assertEqual(conf.get("PASSWORD"), "calmkart#@!") | ||
self.assertEqual(conf.get("PORT"), 6379) | ||
|
||
# password without special character and set the password_quote = True | ||
conf = parse_redis_url("redis://:password@localhost2:1234", True) | ||
self.assertEqual(conf.get("HOST"), "localhost2") | ||
self.assertEqual(conf.get("DB"), 0) | ||
self.assertEqual(conf.get("PASSWORD"), "password") | ||
self.assertEqual(conf.get("PORT"), 1234) | ||
|
||
@patch("defender.config.DEFENDER_REDIS_NAME", "default") | ||
def test_get_redis_connection_django_conf(self): | ||
""" get the redis connection """ | ||
|