From 331d421e101a353ae07de7681525f76146969856 Mon Sep 17 00:00:00 2001 From: Nicholas Charriere Date: Sun, 6 May 2018 14:28:46 -0700 Subject: [PATCH 1/4] Mock redis in tests using mockredis --- dev-requirements.txt | 6 +++++- snappass/main.py | 5 ++++- tests.py | 6 ++++++ tox.ini | 8 ++++---- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 908833f..fc7f09d 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1,7 @@ +mock==1.0.1 +pytest==3.5.1 +pytest-cov==2.5.1 +mockredispy==2.9.3 coverage==4.2 flake8==3.0.4 -tox==2.3.1 +tox==3.0.0 diff --git a/snappass/main.py b/snappass/main.py index 15de24e..39a82be 100644 --- a/snappass/main.py +++ b/snappass/main.py @@ -25,7 +25,10 @@ app.secret_key = os.environ.get('SECRET_KEY', 'Secret Key') app.config.update( dict(STATIC_URL=os.environ.get('STATIC_URL', 'static'))) -if os.environ.get('REDIS_URL'): +if os.environ.get('MOCK_REDIS'): + from mockredis import mock_strict_redis_client + redis_client = mock_strict_redis_client() +elif os.environ.get('REDIS_URL'): redis_client = redis.StrictRedis.from_url(os.environ.get('REDIS_URL')) else: redis_host = os.environ.get('REDIS_HOST', 'localhost') diff --git a/tests.py b/tests.py index c94332c..c5418af 100644 --- a/tests.py +++ b/tests.py @@ -1,3 +1,4 @@ +from mock import patch import time import unittest import uuid @@ -5,6 +6,7 @@ from unittest import TestCase from cryptography.fernet import Fernet from werkzeug.exceptions import BadRequest +from mockredis import mock_strict_redis_client # noinspection PyPep8Naming import snappass.main as snappass @@ -14,6 +16,7 @@ __author__ = 'davedash' class SnapPassTestCase(TestCase): + @patch('redis.client.StrictRedis', mock_strict_redis_client) def test_get_password(self): password = "melatonin overdose 1337!$" key = snappass.set_password(password, 30) @@ -91,6 +94,9 @@ class SnapPassTestCase(TestCase): password = 'open sesame' key = snappass.set_password(password, 1) time.sleep(1.5) + # Expire functionality must be explicitly invoked using do_expire(time). + # mockredis does not support automatic expiration at this time + snappass.redis_client.do_expire() self.assertEqual(None, snappass.get_password(key)) diff --git a/tox.ini b/tox.ini index 8cb330f..ef0fe93 100644 --- a/tox.ini +++ b/tox.ini @@ -2,12 +2,12 @@ envlist = py27, py34, py35, py36, flake8 [testenv] -deps = - pytest - pytest-cov +setenv = + MOCK_REDIS = 1 commands = pip install -r requirements.txt - py.test --junitxml=junit-{envname}.xml --cov-report xml tests.py + pip install -r dev-requirements.txt + py.test --junitxml=junit-{envname}.xml --cov-report xml tests.py -s [testenv:flake8] commands = From 2e0a2962224bfa13b2f3fab7d712ea96027df730 Mon Sep 17 00:00:00 2001 From: Nicholas Charriere Date: Sun, 6 May 2018 14:30:21 -0700 Subject: [PATCH 2/4] Remove -s flag in tests --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index ef0fe93..115f142 100644 --- a/tox.ini +++ b/tox.ini @@ -7,7 +7,7 @@ setenv = commands = pip install -r requirements.txt pip install -r dev-requirements.txt - py.test --junitxml=junit-{envname}.xml --cov-report xml tests.py -s + py.test --junitxml=junit-{envname}.xml --cov-report xml tests.py [testenv:flake8] commands = From f59fd3379e6abc2b8aacdf40bdaa269ca1d3a8ff Mon Sep 17 00:00:00 2001 From: Nicholas Charriere Date: Sun, 6 May 2018 14:41:44 -0700 Subject: [PATCH 3/4] Remove 2.6 and 3.3 from .travis.yml --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index fc2de17..1a1e848 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: python python: - - "2.6" - "2.7" - - "3.3" - "3.4" - "3.5" - "3.6" From 75b6a6919facbdc7101f2458ee3392292a205442 Mon Sep 17 00:00:00 2001 From: Nicholas Charriere Date: Mon, 7 May 2018 08:23:30 -0700 Subject: [PATCH 4/4] Make mock a requirement, not dev-requirement --- dev-requirements.txt | 1 - requirements.txt | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index fc7f09d..8fa761f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,4 +1,3 @@ -mock==1.0.1 pytest==3.5.1 pytest-cov==2.5.1 mockredispy==2.9.3 diff --git a/requirements.txt b/requirements.txt index eb11347..c18fe8e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ Werkzeug==0.9.4 itsdangerous==0.23 redis==2.8.0 cryptography==1.8.1 +mock==1.0.1