Skip to content

Commit

Permalink
Make Moto (tests) compatible with flask/werkzeug 2.x (#3923)
Browse files Browse the repository at this point in the history
* Dont fail if CodeCov fails - for now

* CI - Force cache rebuild

* Bump werkzeug to latest version

* CI - Enforce cache flush

* ManagedBlockchain - fix error format

* ManagedBlockchain - Fix tests to use pytest.raises paradigm

* Revert "Lock Flask (#3925)"

This reverts commit 8bb0feb.

* CI - Enforce cache rebuild
  • Loading branch information
bblommers committed May 13, 2021
1 parent 8bb0feb commit 9e3faf7
Show file tree
Hide file tree
Showing 11 changed files with 599 additions and 317 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache-dir.outputs.dir }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}-4
- name: Update pip
if: ${{ steps.pip-cache.outputs.cache-hit != 'true' && matrix.python-version != '2.7' }}
run: |
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}-4
# Update PIP - recent version does not support PY2 though
- name: Update pip
if: ${{ matrix.python-version != '2.7' }}
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}-4
- name: Update pip
if: ${{ matrix.python-version != '2.7' }}
run: |
Expand All @@ -113,7 +113,7 @@ jobs:
if: ${{ github.repository == 'spulec/moto'}}
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
fail_ci_if_error: false
flags: unittests

testserver:
Expand Down Expand Up @@ -145,7 +145,7 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}
key: pip-${{ matrix.python-version }}-${{ hashFiles('**/setup.py') }}-4
- name: Update pip
if: ${{ matrix.python-version != '2.7' }}
run: |
Expand All @@ -162,7 +162,7 @@ jobs:
if: ${{ github.repository == 'spulec/moto'}}
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
fail_ci_if_error: false
flags: servertests

deploy:
Expand Down
49 changes: 47 additions & 2 deletions moto/managedblockchain/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
from __future__ import unicode_literals
from moto.core.exceptions import RESTError
from functools import wraps
from werkzeug.exceptions import HTTPException
from jinja2 import DictLoader, Environment


class ManagedBlockchainClientError(RESTError):
ERROR_JSON_RESPONSE = """{
"message": "{{message}}"
}
"""


def exception_handler(f):
@wraps(f)
def _wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except ManagedBlockchainClientError as err:
return err.code, err.get_headers(), err.description

return _wrapper


class ManagedBlockchainClientError(HTTPException):
code = 400

templates = {
"error": ERROR_JSON_RESPONSE,
}

def __init__(self, error_type, message, **kwargs):
super(HTTPException, self).__init__()
env = Environment(loader=DictLoader(self.templates))
self.error_type = error_type
self.message = message
self.description = env.get_template("error").render(
error_type=error_type, message=message, **kwargs
)

def get_headers(self, *args, **kwargs):
return [
("Content-Type", "application/json"),
("x-amzn-ErrorType", self.error_type),
]

@property
def response(self):
return self.get_body()

def get_body(self, *args, **kwargs):
return self.description


class BadRequestException(ManagedBlockchainClientError):
def __init__(self, pretty_called_method, operation_error):
Expand Down
12 changes: 12 additions & 0 deletions moto/managedblockchain/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from six.moves.urllib.parse import urlparse, parse_qs

from moto.core.responses import BaseResponse
from .exceptions import exception_handler
from .models import managedblockchain_backends
from .utils import (
region_from_managedblckchain_url,
Expand All @@ -21,6 +22,7 @@ def __init__(self, backend):
self.backend = backend

@classmethod
@exception_handler
def network_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down Expand Up @@ -73,6 +75,7 @@ def _network_response_post(self, json_body, querystring, headers):
return 200, headers, json.dumps(response)

@classmethod
@exception_handler
def networkid_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand All @@ -94,6 +97,7 @@ def _networkid_response_get(self, network_id, headers):
return 200, headers, response

@classmethod
@exception_handler
def proposal_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down Expand Up @@ -139,6 +143,7 @@ def _proposal_response_post(self, network_id, json_body, querystring, headers):
return 200, headers, json.dumps(response)

@classmethod
@exception_handler
def proposalid_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand All @@ -160,6 +165,7 @@ def _proposalid_response_get(self, network_id, proposal_id, headers):
return 200, headers, response

@classmethod
@exception_handler
def proposal_votes_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down Expand Up @@ -203,6 +209,7 @@ def _proposal_votes_response_post(
return 200, headers, ""

@classmethod
@exception_handler
def invitation_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand All @@ -224,6 +231,7 @@ def _all_invitation_response(self, request, full_url, headers):
return 200, headers, response

@classmethod
@exception_handler
def invitationid_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand All @@ -243,6 +251,7 @@ def _invitationid_response_delete(self, invitation_id, headers):
return 200, headers, ""

@classmethod
@exception_handler
def member_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down Expand Up @@ -283,6 +292,7 @@ def _member_response_post(self, network_id, json_body, querystring, headers):
return 200, headers, json.dumps(response)

@classmethod
@exception_handler
def memberid_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down Expand Up @@ -327,6 +337,7 @@ def _memberid_response_delete(self, network_id, member_id, headers):
return 200, headers, ""

@classmethod
@exception_handler
def node_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down Expand Up @@ -380,6 +391,7 @@ def _node_response_post(
return 200, headers, json.dumps(response)

@classmethod
@exception_handler
def nodeid_response(clazz, request, full_url, headers):
region_name = region_from_managedblckchain_url(full_url)
response_instance = ManagedBlockchainResponse(
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ coverage==4.5.4
flake8==3.7.8
boto>=2.45.0
prompt-toolkit==2.0.10 # 3.x is not available with python2
click==6.7
click
inflection==0.3.1
lxml
packaging
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def get_version():
"requests>=2.5",
"xmltodict",
"six>1.9",
# TODO: werkzeug 2.x currently breaks test_s3_server_post_without_content_length
"werkzeug<2.0.0",
"werkzeug",
"pytz",
"python-dateutil<3.0.0,>=2.1",
"responses>=0.9.0",
Expand Down Expand Up @@ -99,7 +98,7 @@ def get_version():
_dep_sshpubkeys_py2,
_dep_sshpubkeys_py3,
]
all_server_deps = all_extra_deps + ["flask<2.0.0", "flask-cors"]
all_server_deps = all_extra_deps + ["flask", "flask-cors"]

# TODO: do we want to add ALL services here?
# i.e. even those without extra dependencies.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import unicode_literals

import boto3
import pytest
import sure # noqa

from botocore.exceptions import ClientError
from moto import mock_managedblockchain
from . import helpers

Expand Down Expand Up @@ -136,6 +138,10 @@ def test_reject_invitation_badinvitation():
Vote="YES",
)

response = conn.reject_invitation.when.called_with(
InvitationId="in-ABCDEFGHIJKLMNOP0123456789",
).should.throw(Exception, "InvitationId in-ABCDEFGHIJKLMNOP0123456789 not found.")
with pytest.raises(ClientError) as ex:
conn.reject_invitation(InvitationId="in-ABCDEFGHIJKLMNOP0123456789")
err = ex.value.response["Error"]
err["Code"].should.equal("ResourceNotFoundException")
err["Message"].should.contain(
"InvitationId in-ABCDEFGHIJKLMNOP0123456789 not found."
)

0 comments on commit 9e3faf7

Please sign in to comment.