Fix broken lower-constraints job

Despite no Flask update on stable branches, the lower-constraints job is
broken similarly to when Flask was bumped to 2.0.1. The broken test
cases were mocking the root object "flask.request".

Instead of mocking the root object, we address the issue by mocking only
the needed methods and attributes. This facilitates the understanding of
the unit test, and also helps people to pin-point problems right away.

Change-Id: I8703c7d3e69f35ef3e85234c27b4743242111f3d
(cherry picked from commit 885c9f077f)
This commit is contained in:
Rafael Weingärtner
2021-05-31 16:09:48 +02:00
committed by Pierre Riteau
parent c8f095c8f1
commit 1e964bdfd1
4 changed files with 25 additions and 15 deletions

View File

@@ -20,11 +20,8 @@ import flask_restful
from oslo_config import cfg
from oslo_log import log
from paste import deploy
try:
from werkzeug.middleware import dispatcher
# In case we have werkzeug<0.15
except ImportError:
from werkzeug import wsgi as dispatcher
from werkzeug.middleware import dispatcher
from cloudkitty.api import root as api_root
from cloudkitty.api.v1 import get_api_app as get_v1_app

View File

@@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import flask
from unittest import mock
from cloudkitty.api.v2.dataframes import dataframes
@@ -28,12 +30,15 @@ class TestDataframeListEndpoint(tests.TestCase):
def test_non_admin_request_is_filtered_on_project_id(self):
policy_mock = mock.patch('cloudkitty.common.policy.authorize')
flask.request.context = mock.Mock()
flask.request.context.project_id = 'test-project'
flask.request.context.is_admin = False
with mock.patch.object(self.endpoint._storage, 'retrieve') as ret_mock:
with policy_mock, mock.patch('flask.request') as fmock:
with policy_mock, mock.patch('flask.request.args.lists') as fmock:
ret_mock.return_value = {'total': 42, 'dataframes': []}
fmock.args.lists.return_value = []
fmock.context.is_admin = False
fmock.context.project_id = 'test-project'
fmock.return_value = []
self.endpoint.get()
ret_mock.assert_called_once_with(
begin=tzutils.get_month_start(),

View File

@@ -12,6 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
#
import flask
import uuid
from unittest import mock
from cloudkitty.api.v2.summary import summary
@@ -28,10 +31,15 @@ class TestSummaryEndpoint(tests.TestCase):
def test_type_filter_is_passed_separately(self):
policy_mock = mock.patch('cloudkitty.common.policy.authorize')
flask.request.context = mock.Mock()
flask.request.context.project_id = str(uuid.uuid4())
flask.request.context.is_admin = True
with mock.patch.object(self.endpoint._storage, 'total') as total_mock:
with policy_mock, mock.patch('flask.request') as fmock:
with policy_mock, mock.patch('flask.request.args.lists') as fmock:
total_mock.return_value = {'total': 0, 'results': []}
fmock.args.lists.return_value = [
fmock.return_value = [
('filters', 'a:b,type:awesome')]
self.endpoint.get()
total_mock.assert_called_once_with(

View File

@@ -243,12 +243,12 @@ class AddInputSchemaTest(tests.TestCase):
self.assertEqual(
list(test_func.input_schema.schema.keys())[0], 'arg_one')
with mock.patch('flask.request') as m:
m.get_json.return_value = {}
with mock.patch('flask.request.get_json') as m:
m.return_value = {}
test_func(self)
with mock.patch('flask.request') as m:
m.get_json.return_value = {'arg_one': 'one'}
with mock.patch('flask.request.get_json') as m:
m.return_value = {'arg_one': 'one'}
test_func(self)
def _test_multiple_add_input_schema_x(self, location):