Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for using authenticated redis server #12

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ install:
- pip install coverage coveralls Mako
before_script:
- redis-server --port 6399 &
script: py.test --cov flask_bitmapist --cov-report term-missing --pep8 --flakes
- redis-server --port 6400 &
script:
- redis-cli -p 6400 config set requirepass foobared
- py.test --cov flask_bitmapist --cov-report term-missing --pep8 --flakes
after_success:
- coveralls
notifications:
Expand Down
11 changes: 8 additions & 3 deletions flask_bitmapist/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FlaskBitmapist(object):

app = None
redis_url = None
redis_auth = None
SYSTEMS = _bitmapist.SYSTEMS
TRACK_HOURLY = _bitmapist.TRACK_HOURLY

Expand All @@ -36,16 +37,20 @@ def __init__(self, app=None, config=None, **opts):
def init_app(self, app, config=None):
"This is used to initialize bitmapist with your app object"
self.app = app
self.redis_url = app.config.get('BITMAPIST_REDIS_URL', 'redis://localhost:6379')
self.redis_url = app.config.get('BITMAPIST_REDIS_URL',
'redis://localhost:6379')
self.redis_auth = app.config.get('BITMAPIST_REDIS_PASSWORD')

if self.redis_url not in _bitmapist.SYSTEMS.values():
host, port = _get_redis_connection(self.redis_url)
_bitmapist.setup_redis(
app.config.get('BITMAPIST_REDIS_SYSTEM', 'default'),
host,
port)
port,
password=self.redis_auth)

_bitmapist.TRACK_HOURLY = app.config.get('BITMAPIST_TRACK_HOURLY', False)
_bitmapist.TRACK_HOURLY = app.config.get('BITMAPIST_TRACK_HOURLY',
False)

if not hasattr(app, 'extensions'):
app.extensions = {}
Expand Down
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def app(request):
app = Flask(__name__)
app.debug = True
app.config['TESTING'] = True
# app.config['BITMAPIST_REDIS_URL'] = 'redis://localhost:6379'
app.config['BITMAPIST_REDIS_URL'] = 'redis://localhost:6399'
app.config['SECRET_KEY'] = 'secret'
app.config['SECRET_KEY'] = 'verysecret'
Expand Down Expand Up @@ -131,3 +130,14 @@ def teardown():
request.addfinalizer(teardown)
# TODO: may return just db with tests using sqlalchemy_user fixture directly
return db, sqlalchemy_user


@pytest.fixture
def auth_bitmap():
app = Flask(__name__)
app.config['BITMAPIST_REDIS_URL'] = 'redis://localhost:6400'
app.config['BITMAPIST_REDIS_PASSWORD'] = 'foobared'
app.config['SECRET_KEY'] = 'secret'
app.config['SECRET_KEY'] = 'verysecret'
auth_bitmap = FlaskBitmapist(app)
return auth_bitmap
22 changes: 16 additions & 6 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import mock
from random import randint

Expand Down Expand Up @@ -243,14 +244,10 @@ def _year(x):
assert _week(d) == _week(now - timedelta(weeks=3-idx))
# 2 - months
for idx, d in enumerate(d2):
this_month = now.replace(day=1) # work with first day of month
months_ago = (5 - idx) * 365 / 12 # no 'months' arg for timedelta
assert _month(d) == _month(this_month - timedelta(months_ago))
assert _month(d) == _month(now - relativedelta(months=(5 - idx)))
# 3 - years
for idx, d in enumerate(d3):
this_year = now.replace(month=1, day=1) # work with first day of year
years_ago = (1 - idx) * 365 # no 'years' arg for timedelta
assert _year(d) == _year(this_year - timedelta(years_ago))
assert _year(d) == _year(now - relativedelta(years=(1-idx)))


def test_chain_events():
Expand Down Expand Up @@ -632,3 +629,16 @@ def test_unmark_function(app, client):

unmark_event('active', 126)
assert 126 not in MonthEvents('active', now.year, now.month)


def test_authenticated_redis(app, auth_bitmap):
assert auth_bitmap.redis_auth == 'foobared'

# Test to make sure auth_bitmap setup with different set of params
assert auth_bitmap.redis_url != app.config['BITMAPIST_REDIS_URL']

mark_event('active', 42)
assert 42 in MonthEvents('active', now.year, now.month)

unmark_event('active', 42)
assert 42 not in MonthEvents('active', now.year, now.month)