Skip to content

Commit

Permalink
Replace ETAG_ENABLED config param with ETAG_DISABLED
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed Nov 9, 2018
1 parent 5f71768 commit 24a2fc7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
2 changes: 1 addition & 1 deletion flask_rest_api/etag.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

def _is_etag_enabled():
"""Return True if ETag feature enabled application-wise"""
return current_app.config.get('ETAG_ENABLED', False)
return not current_app.config.get('ETAG_DISABLED', False)


def _get_etag_ctx():
Expand Down
42 changes: 18 additions & 24 deletions tests/test_etag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@
from flask.views import MethodView

from flask_rest_api import Api, Blueprint, abort
from flask_rest_api.etag import _is_etag_enabled, _get_etag_ctx
from flask_rest_api.etag import _get_etag_ctx
from flask_rest_api.exceptions import (
CheckEtagNotCalledError,
NotModified, PreconditionRequired, PreconditionFailed)
from flask_rest_api.blueprint import HTTP_METHODS
from flask_rest_api.compat import MARSHMALLOW_VERSION_MAJOR

from .mocks import ItemNotFound
from .conftest import AppConfig
from .utils import NoLoggingContext


class AppConfigEtagEnabled(AppConfig):
"""Basic config with ETag feature enabled"""
ETAG_ENABLED = True


@pytest.fixture(params=[True, False])
def app_with_etag(request, collection, schemas, app):
"""Return a basic API sample with ETag"""
Expand Down Expand Up @@ -216,9 +210,9 @@ def test_etag_check_precondition(self, app, method):
else:
blp._check_precondition()

@pytest.mark.parametrize(
'app', [AppConfig, AppConfigEtagEnabled], indirect=True)
def test_etag_check_etag(self, app, schemas):
@pytest.mark.parametrize('etag_disabled', (True, False))
def test_etag_check_etag(self, app, schemas, etag_disabled):
app.config['ETAG_DISABLED'] = etag_disabled
blp = Blueprint('test', __name__)
etag_schema = schemas.DocEtagSchema
old_item = {'item_id': 1, 'db_field': 0}
Expand All @@ -228,21 +222,20 @@ def test_etag_check_etag(self, app, schemas):

with app.test_request_context('/', headers={'If-Match': old_etag}):
blp.check_etag(old_item)
if _is_etag_enabled():
if not etag_disabled:
with pytest.raises(PreconditionFailed):
blp.check_etag(new_item)
else:
blp.check_etag(new_item)
with app.test_request_context(
'/', headers={'If-Match': old_etag_with_schema}):
blp.check_etag(old_item, etag_schema)
if _is_etag_enabled():
if not etag_disabled:
with pytest.raises(PreconditionFailed):
blp.check_etag(new_item, etag_schema)
else:
blp.check_etag(new_item)

@pytest.mark.parametrize('app', [AppConfigEtagEnabled], indirect=True)
@pytest.mark.parametrize('method', HTTP_METHODS)
def test_etag_verify_check_etag_warning(self, app, method):
blp = Blueprint('test', __name__)
Expand Down Expand Up @@ -282,9 +275,9 @@ def test_etag_verify_check_etag_exception(
else:
blp._verify_check_etag()

@pytest.mark.parametrize(
'app', [AppConfig, AppConfigEtagEnabled], indirect=True)
def test_etag_set_etag(self, app, schemas):
@pytest.mark.parametrize('etag_disabled', (True, False))
def test_etag_set_etag(self, app, schemas, etag_disabled):
app.config['ETAG_DISABLED'] = etag_disabled
blp = Blueprint('test', __name__)
etag_schema = schemas.DocEtagSchema
item = {'item_id': 1, 'db_field': 0}
Expand All @@ -293,30 +286,30 @@ def test_etag_set_etag(self, app, schemas):

with app.test_request_context('/'):
blp.set_etag(item)
if _is_etag_enabled():
if not etag_disabled:
assert _get_etag_ctx()['etag'] == etag
del _get_etag_ctx()['etag']
else:
assert 'etag' not in _get_etag_ctx()
with app.test_request_context(
'/', headers={'If-None-Match': etag}):
if _is_etag_enabled():
if not etag_disabled:
with pytest.raises(NotModified):
blp.set_etag(item)
else:
blp.set_etag(item)
assert 'etag' not in _get_etag_ctx()
with app.test_request_context(
'/', headers={'If-None-Match': etag_with_schema}):
if _is_etag_enabled():
if not etag_disabled:
with pytest.raises(NotModified):
blp.set_etag(item, etag_schema)
else:
blp.set_etag(item, etag_schema)
assert 'etag' not in _get_etag_ctx()
with app.test_request_context(
'/', headers={'If-None-Match': 'dummy'}):
if _is_etag_enabled():
if not etag_disabled:
blp.set_etag(item)
assert _get_etag_ctx()['etag'] == etag
del _get_etag_ctx()['etag']
Expand All @@ -329,10 +322,11 @@ def test_etag_set_etag(self, app, schemas):
blp.set_etag(item, etag_schema)
assert 'etag' not in _get_etag_ctx()

@pytest.mark.parametrize(
'app', [AppConfig, AppConfigEtagEnabled], indirect=True)
@pytest.mark.parametrize('etag_disabled', (True, False))
@pytest.mark.parametrize('method', HTTP_METHODS)
def test_set_etag_method_not_allowed_warning(self, app, method):
def test_set_etag_method_not_allowed_warning(
self, app, method, etag_disabled):
app.config['ETAG_DISABLED'] = etag_disabled
blp = Blueprint('test', __name__)

with mock.patch.object(app.logger, 'warning') as mock_warning:
Expand Down Expand Up @@ -362,7 +356,6 @@ def test_etag_set_etag_in_response(self, app, schemas, paginate):
blp._set_etag_in_response(resp, item, etag_schema)
assert resp.get_etag() == (etag_with_schema, False)

@pytest.mark.parametrize('app', [AppConfigEtagEnabled], indirect=True)
def test_etag_operations_etag_enabled(self, app_with_etag):

client = app_with_etag.test_client()
Expand Down Expand Up @@ -464,6 +457,7 @@ def test_etag_operations_etag_enabled(self, app_with_etag):

def test_etag_operations_etag_disabled(self, app_with_etag):

app_with_etag.config['ETAG_DISABLED'] = True
client = app_with_etag.test_client()

# GET without ETag: OK
Expand Down
7 changes: 0 additions & 7 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@

from flask_rest_api import Api, Blueprint, abort, Page

from .conftest import AppConfig
from .mocks import ItemNotFound


class AppConfigFullExample(AppConfig):
"""Basic config with ETag feature enabled"""
ETAG_ENABLED = True


def implicit_data_and_schema_etag_blueprint(collection, schemas):
"""Blueprint with implicit data and schema ETag computation
Expand Down Expand Up @@ -231,7 +225,6 @@ def blueprint_fixture(request, collection, schemas):

class TestFullExample():

@pytest.mark.parametrize('app', [AppConfigFullExample], indirect=True)
def test_examples(self, app, blueprint_fixture, schemas):

blueprint, bp_schema = blueprint_fixture
Expand Down

0 comments on commit 24a2fc7

Please sign in to comment.