Esempio n. 1
0
    def test_etag_verify_check_etag_warning(self, app, method):
        blp = Blueprint('test', __name__)
        old_item = {'item_id': 1, 'db_field': 0}
        old_etag = blp._generate_etag(old_item)

        with pytest.warns(None) as record:
            with app.test_request_context(
                    '/',
                    method=method,
                    headers={'If-Match': old_etag},
            ):
                blp._verify_check_etag()
                if method in ['PUT', 'PATCH', 'DELETE']:
                    assert len(record) == 1
                    assert record[0].category == UserWarning
                    assert str(record[0].message) == (
                        'ETag not checked in endpoint {} on {} request.'
                        .format(f_request.endpoint, method)
                    )
                else:
                    assert not record
                blp.check_etag(old_item)
                record.clear()
                blp._verify_check_etag()
                assert not record
Esempio n. 2
0
    def test_etag_check_etag_wrong_method_warning(
            self, app, method, etag_disabled
    ):
        app.config['ETAG_DISABLED'] = etag_disabled
        blp = Blueprint('test', __name__)

        with pytest.warns(None) as record:
            with app.test_request_context(
                    '/',
                    method=method,
                    headers={'If-Match': ''},
            ):
                # Ignore ETag check fail. Just testing the warning.
                try:
                    blp.check_etag(None)
                except PreconditionFailed:
                    pass
                if method in ['PUT', 'PATCH', 'DELETE']:
                    assert not record
                else:
                    assert len(record) == 1
                    assert record[0].category == UserWarning
                    assert str(record[0].message) == (
                        'ETag cannot be checked on {} request.'
                        .format(method)
                    )
Esempio n. 3
0
    def test_etag_verify_check_etag_warning(self, app, method):
        blp = Blueprint('test', __name__)
        old_item = {'item_id': 1, 'db_field': 0}
        old_etag = blp._generate_etag(old_item)

        with mock.patch.object(app.logger, 'warning') as mock_warning:
            with app.test_request_context('/',
                                          method=method,
                                          headers={'If-Match': old_etag}):
                blp._verify_check_etag()
                if method in ['PUT', 'PATCH', 'DELETE']:
                    assert mock_warning.called
                    mock_warning.reset_mock()
                else:
                    assert not mock_warning.called
                blp.check_etag(old_item)
                blp._verify_check_etag()
                assert not mock_warning.called
Esempio n. 4
0
    def test_etag_check_etag_wrong_method_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:
            with app.test_request_context(
                    '/',
                    method=method,
                    headers={'If-Match': ''},
            ):
                # Ignore ETag check fail. Just testing the warning.
                try:
                    blp.check_etag(None)
                except PreconditionFailed:
                    pass
                if method in ['PUT', 'PATCH', 'DELETE']:
                    assert not mock_warning.called
                else:
                    assert mock_warning.called
Esempio n. 5
0
    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}
        new_item = {'item_id': 1, 'db_field': 1}
        old_etag = blp._generate_etag(old_item)
        old_etag_with_schema = blp._generate_etag(old_item, etag_schema)

        with app.test_request_context('/', headers={'If-Match': old_etag}):
            blp.check_etag(old_item)
            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 not etag_disabled:
                with pytest.raises(PreconditionFailed):
                    blp.check_etag(new_item, etag_schema)
            else:
                blp.check_etag(new_item)