class TestDeleteUser(TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_delete_user(self): response_1 = self.lg.handle_request( method='DELETE', path='/users/{user_id}'.format(user_id=user_id_1), headers=dict(), body='') assert response_1['statusCode'] == 204 response_2 = self.lg.handle_request( method='GET', path='/users/{user_id}'.format(user_id=user_id_1), headers=dict(), body='') assert response_2['statusCode'] == 404 def test_delete_non_existent_user(self): response_1 = self.lg.handle_request( method='DELETE', path='/users/{user_id}'.format( user_id='7c098790-fe62-4903-8699-0ea958e18602'), headers=dict(), body='') assert response_1['statusCode'] == 404
def test_does_deny_with_forbidden_when_route_not_found( self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(ForbiddenError) as ei: gateway.handle_request('GET', '/badindex', {}, '') exception_body = str(ei.value.body) assert 'Missing Authentication Token' in exception_body
class UnitTests(unittest.TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_insert_volunteer(self): request = {'body': {'name': 'Raul', 'midname': 'Silverio', 'neighbor': 'Jurere', 'city': 'Florianopolis'}} request_body = request['body'] response = self.lg.handle_request(method='POST', path='/voluntario/add', headers={ 'Content-Type': 'application/json' }, body=json.dumps(request_body)) assert response['statusCode'] == 200 def test_insert_action(self): request = {'body': {"name": "Ambiental", "institution": "IBAMA", "location": "Amazonas", "description": "Organizacao nacional responsavel pela protecao ambiental"}} request_body = request['body'] response = self.lg.handle_request(method='POST', path='/voluntario/add', headers={ 'Content-Type': 'application/json' }, body=json.dumps(request_body)) assert response['statusCode'] == 200
class TestGetUser(TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_get_user_basic_works(self): response = self.lg.handle_request( method='GET', path='/users/{user_id}'.format(user_id=user_id_1), headers=dict(), body='') assert response['statusCode'] == 200 assert json.loads(response['body'])['data'] == { 'emailAddress': '*****@*****.**', 'firstName': 'Nick', 'lastName': 'Plutt', 'metaData': [{ 'fieldData': '200 Grand Avenue', 'fieldType': 'address' }], 'userId': '9f1b15e8-dca7-427d-816d-44f519010c6b' } def test_get_user_raises_not_found(self): response = self.lg.handle_request( method='GET', path='/users/{user_id}'.format( user_id='7c098790-fe62-4903-8699-0ea958e18602'), headers=dict(), body='') assert response['statusCode'] == 404
def test_does_populate_context(self): demo = app.Chalice('app-name') @demo.route('/context') def context_view(): context = demo.lambda_context return { 'name': context.function_name, 'memory': context.memory_limit_in_mb, 'version': context.function_version, 'timeout': context.get_remaining_time_in_millis(), 'request_id': context.aws_request_id, } disk_config = { 'lambda_timeout': 10, 'lambda_memory_size': 256, } config = Config(chalice_stage='api', config_from_disk=disk_config) gateway = LocalGateway(demo, config) response = gateway.handle_request('GET', '/context', {}, '') body = json.loads(response['body']) assert body['name'] == 'api_handler' assert body['memory'] == 256 assert body['version'] == '$LATEST' assert body['timeout'] > 10 assert body['timeout'] <= 10000 assert AWS_REQUEST_ID_PATTERN.match(body['request_id'])
def test_does_throw_unauthorized_when_no_auth_token_present_on_valid_route( self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(NotAuthorizedError) as ei: gateway.handle_request('GET', '/index', {}, '') exception_body = str(ei.value.body) assert '{"message":"Unauthorized"}' in exception_body
def request(self, method="GET", path="/", headers=None, body=None): """Simulates a request to the app local gateway. Performs a request against the application. :path: (str) The URL path to request (default: '/'). :method: (str) An HTTP method to use in the request (default: GET). :headers: (dict) Additional headers to include in the request (default: ``None``). :body: (str) A string to send as the body of the request (default: ``None``). :returns: (dict) The result of the request. """ if not path.startswith("/"): raise ValueError("path must start with '/'") valid_methods = ["GET", "HEAD", "POST", "PUT", "DELETE"] if method not in valid_methods: raise ValueError(f"method must be one of: {valid_methods}") body = body or "" headers = headers or {} gateway = LocalGateway(self.app, Config()) response = gateway.handle_request(method, path, headers, body) return response
def test_does_throw_unauthorized_when_no_auth_token_present_on_valid_route( self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(NotAuthorizedError) as ei: gateway.handle_request( 'GET', '/index', {}, '') exception_body = str(ei.value.body) assert '{"message":"Unauthorized"}' in exception_body
def test_does_deny_unauthed_request(self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(ForbiddenError) as ei: gateway.handle_request( 'GET', '/index', {'Authorization': 'deny'}, '') exception_body = str(ei.value.body) assert ('{"Message": ' '"User is not authorized to ' 'access this resource"}') in exception_body
def call_api(method: str, path: str, headers: (dict, None) = None, body: str = ""): gateway = LocalGateway(app, Config()) headers = HEADERS if headers is None else {**HEADERS, **headers} return gateway.handle_request(method=method, path=path, headers=headers, body=body)
def test_does_deny_with_forbidden_when_auth_token_present( self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(ForbiddenError) as ei: gateway.handle_request('GET', '/badindex', {'Authorization': 'foobar'}, '') # The message should be a more complicated error message to do with # signing the request. It always ends with the Authorization token # that we passed up, so we can check for that. exception_body = str(ei.value.body) assert 'Authorization=foobar' in exception_body
def test_can_invoke_function(self): demo = app.Chalice('app-name') @demo.route('/') def index_view(): return {'foo': 'bar'} gateway = LocalGateway(demo, Config()) response = gateway.handle_request('GET', '/', {}, '') body = json.loads(response['body']) assert body['foo'] == 'bar'
class TestApp(TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_get_books(self): response = self.lg.handle_request(method='GET', path='/books', headers={}, body='') assert response['statusCode'] == 200 assert json.loads( response['body']) == [dict(book_id=1), dict(book_id=2)] def test_post_book(self): body = dict(book_info='hello') response = self.lg.handle_request( method='POST', path='/books', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) assert response['statusCode'] == 201 assert response['headers'].get('Location') == '/books/3' def test_get_book_by_id(self): response = self.lg.handle_request(method='GET', path='/books/4', headers={}, body='') assert response['statusCode'] == 200 assert json.loads(response['body']) == dict(book_id=4) def test_put_book_by_id(self): body = dict(book_info='hello') response = self.lg.handle_request(method='PUT', path='/books/4', headers={}, body=json.dumps(body)) assert response['statusCode'] == 204 assert json.loads(response['body']) is None def test_delete_book_by_id(self): response = self.lg.handle_request(method='DELETE', path='/books/4', headers={}, body='') assert response['statusCode'] == 204 assert json.loads(response['body']) is None
class TestCreateUser(TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_create_user_returns_code_and_header(self): body = dict( emailAddress='*****@*****.**', firstName='Joe', lastName='Schmo', metaData=[dict(fieldType='favorite food', fieldData='tacos')]) response_1 = self.lg.handle_request( method='POST', path='/users', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) assert response_1['statusCode'] == 201 assert response_1['headers']['Location'].index('users') response_2 = self.lg.handle_request( method='GET', path=response_1['headers']['Location'], headers=dict(), body='') assert response_2['statusCode'] == 200 body['userId'] = ANY assert json.loads(response_2['body'])['data'] == body def test_create_user_fails_on_badrequest(self): body = dict(emailAddress='*****@*****.**', firstName='Joe', lastName='Schmo', metaData=[dict(fieldType='favorite food')]) response_1 = self.lg.handle_request( method='POST', path='/users', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) assert response_1['statusCode'] == 400 def test_create_user_fails_on_conflict(self): body = dict( emailAddress='*****@*****.**', firstName='Nick', lastName='Plutt', metaData=[dict(fieldType='favorite food', fieldData='tacos')]) response = self.lg.handle_request( method='POST', path='/users', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) assert response['statusCode'] == 409
class ChaliceTestHarness: def __init__(self): project_dir = os.path.join(os.path.dirname(__file__), "..") config = CLIFactory(project_dir=project_dir).create_config_obj( chalice_stage_name="dev") self._chalice_app = config.chalice_app self._gateway = LocalGateway(self._chalice_app, config) @functools.lru_cache(maxsize=64, typed=False) def __getattr__(self, method): return functools.partial(self.request, method=method.upper()) def request(self, path, headers={}, body={}, method="GET"): resp_obj = requests.Response() try: response = self._gateway.handle_request(method, path, headers, body) except LocalGatewayException as error: resp_obj.status_code = error.CODE resp_obj.headers = error.headers resp_obj.body = error.body else: resp_obj.status_code = response["statusCode"] resp_obj.headers = response["headers"] resp_obj.body = response["body"] resp_obj.headers["Content-Length"] = str(len(body)) return resp_obj
def test_defaults_timeout_if_needed(self): demo = app.Chalice('app-name') @demo.route('/context') def context_view(): context = demo.lambda_context return { 'remaining': context.get_remaining_time_in_millis(), } disk_config = {} config = Config(chalice_stage='api', config_from_disk=disk_config) gateway = LocalGateway(demo, config) response = gateway.handle_request('GET', '/context', {}, '') body = json.loads(response['body']) assert body['remaining'] <= gateway.MAX_LAMBDA_EXECUTION_TIME * 1000
def test_oauth(self): app = App( client=self.web_client, signing_secret=self.signing_secret, oauth_settings=OAuthSettings( client_id="111.111", client_secret="xxx", scopes=["chat:write", "commands"], ), ) chalice_app = Chalice(app_name="bolt-python-chalice") slack_handler = ChaliceSlackRequestHandler(app=app, chalice=chalice_app) @chalice_app.route("/slack/install", methods=["GET"]) def install() -> Response: return slack_handler.handle(chalice_app.current_request) response: Dict[str, Any] = LocalGateway( chalice_app, Config()).handle_request(method="GET", path="/slack/install", body="", headers={}) assert response["statusCode"] == 200 assert response["headers"][ "content-type"] == "text/html; charset=utf-8" assert response["headers"]["content-length"] == "565" assert "https://slack.com/oauth/v2/authorize?state=" in response.get( "body")
class ChaliceTestHarness: def __init__(self): project_dir = os.path.join(os.path.dirname(__file__), "..", "..") config = CLIFactory(project_dir=project_dir).create_config_obj( chalice_stage_name="dev") self._chalice_app = config.chalice_app self._gateway = LocalGateway(self._chalice_app, config) @functools.lru_cache(maxsize=128, typed=False) def __getattr__(self, item): item = item.upper() return functools.partial(self.request, method=item) def request(self, path, headers=None, data='', method="GET"): resp_obj = requests.Response() if not headers: headers = {} try: response = self._gateway.handle_request(method, path, headers, data) except LocalGatewayException as error: resp_obj.status_code = error.CODE resp_obj.headers = error.headers resp_obj.body = error.body else: resp_obj.status_code = response['statusCode'] resp_obj.headers = response['headers'] resp_obj.body = response['body'] resp_obj.headers['Content-Length'] = str(len(data)) return resp_obj
class TestApp(TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_img_post(self): response = self._new_img_post() assert response['statusCode'] == 200 assert json.loads(response['body']).get("id", "") is not None # Use stored image in the next steps self.uuid = json.loads(response['body']).get("id", "") def test_img_get(self): uuid = json.loads(self._new_img_post()['body']).get("id", "") response = self.lg.handle_request(method='GET', path=f'/v1/img/{uuid}.png', headers={"Accept": "image/png"}, body='') assert response['statusCode'] == 200 assert response['body'][:4] == b'\x89PNG' def test_img_get_unsupported_format(self): uuid = json.loads(self._new_img_post()['body']).get("id", "") response = self.lg.handle_request(method='GET', path='/v1/img/{uuid}.bad', headers={"Accept": "image/png"}, body='') assert response['statusCode'] == 400 def _new_img_post(self): body = { "type": "base64", "data": "iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAACk5JREFUeNrUmQlsW/Udx7/P933EdkqcO016xKXt2AohrNBWdKNQWi6BxKVtQKVs0ujEhiYBHamEJlhBZdU0qdI0iWNapQEdA1bGBgqMlgKZmjRx4vTK1eZyEif2e8/2O7zf/9m5tlISmgOe9PJ8PDuf3+///V1/c5lMBt/kw3C5X7Dm/ehlff5A/Trt+i97xcsr09E7uQy406a8hn6D45F7xlt6LvXZzQ3nwS31Ctjvf/zK/cnPjly/rC9YuMEDTsdhoHkc3Z/H8KZ95c9vTUSev5QBuqWEv3fPc3c8a+999xprdzBQZEa6T4QpmkRFtQOhzT7s4CP7/uZY+dilvkO3lPD5NuPeLY8+WWD51t1o/3AE0U4BsfE0hroTCDh1CF3r/lIjlkRCE/C7dnw/VF1dDVEUMdLSiOiRQ+A/PoyCSgssdj0CdgNGx2S0NsYvKqcliYEJ+B1XrwsFAgHk5+dDr9dr77Hr2Ad/Redvn4AvaITbr4fXokeSV9HaKv6fEYseAyxgGfzD27eG/H4/urq60NOTTTQ6nQ5GoxGO67dj+QuvgVt7Jwa70xgcScHIqQgtN11UTobFhF/ndzx93fKikMvlgslk0rzPcRw8Hg8SiYR2ssMYLIPv3p8iSuoYPfpnpB0SAlYdVgZ12HE+su8N5+qB2+JtryxaEDN4utRdWL/1jn8Yi/Dee++hubkZ8Xhce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LSoByVjbIFSd52vSm5toEevmR5RG8834QJot1GryMDYZIW4Eh9tRCwF/KACaYPXl2I3VY5H1TTj6s+DIDmD1aLLDn2etWSxceLOjAq4PV1JTqyVYFG4wdbWXG4frXUt993fvQnov+ozQF9kIYcIvTZoTJoEISFRj1uqx01NxMkoMGGxGlDBQaWNic/oD/JF7qW6FV4KtNpzT4v4i1hzDZTi/eCtR5mfZlRdtMMpIh2baS6T+jgasEnpSmPqDQipSpvXi1+BUcGKrNwgs1Cwr/RQZYKRveZCQvCvEsYSoOmMWsaqZDs+c8DS1xOrWZlya9Mu7Coc+lqvfpXJpfKW886N/141oR968XUGA3aLtpsqIV4slZXJBVjNMKsCs9f5leZgP3kdquX+Tyk7xovzkY/gf+UbPZvD9adDfe6nwDO8v6scySzTo86T5BXDxJiDz/Br30cg5aXMofCg3T4U0m0/61a9eira0Nn8YLER+P4Z41IotTJv23c55+k84xfE0OQw7eodPp9vt8Pm1XmG2wDg4O4kCnfffOarGRbonQOYSv4aH9yMe2FsmIervdvodtZ7N9SVVVd/9zV/TFr7LNuCQS0posnocgCHvIqFnBf61WYFosOAg+cTkbvYtuwDf50OEbfvxXgAEAFpyqPqutRYcAAAAASUVORK5CYII=" } response = self.lg.handle_request(method='POST', path='/v1/img', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) return response
class TestHTTPClient(BaseClient): def __init__(self, app, config): # type: (Chalice, Config) -> None self._app = app self._config = config self._local_gateway = LocalGateway(app, self._config) def request(self, method, path, headers=None, body=b''): # type: (str, str, Optional[Dict[str, str]], bytes) -> HTTPResponse if headers is None: headers = {} scoped = self._config.scope(self._config.chalice_stage, 'api_handler') with self._patched_env_vars(scoped.environment_variables): try: response = self._local_gateway.handle_request( method=method.upper(), path=path, headers=headers, body=body ) except LocalGatewayException as e: return self._error_response(e) return HTTPResponse.create_from_dict(response) def _error_response(self, e): # type: (LocalGatewayException) -> HTTPResponse return HTTPResponse( headers=e.headers, body=e.body if e.body else b'', status_code=e.CODE ) def get(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('GET', path, **kwargs) def post(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('POST', path, **kwargs) def put(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('PUT', path, **kwargs) def patch(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('PATCH', path, **kwargs) def options(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('OPTIONS', path, **kwargs) def delete(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('DELETE', path, **kwargs) def head(self, path, **kwargs): # type: (str, **Any) -> HTTPResponse return self.request('HEAD', path, **kwargs)
def setUp(self): with open(".chalice/config.json") as file: self.lg = LocalGateway( app, Config(chalice_stage="beta", config_from_disk=json.load(file))) body = dict(schema=TEST_SCHEMA) response = self.lg.handle_request( method='POST', path='/centers/{}/forms/new'.format(CENTER_ID), headers={ "authorization": "auth", "Content-Type": "application/json" }, body=json.dumps(body)) self.assertEqual(response['statusCode'], 200, response) body = json.loads(response['body']) self.form = body['res']['form'] self.formId = self.form['id']
def test_shortcuts(self): app = App( client=self.web_client, signing_secret=self.signing_secret, ) @app.shortcut("test-shortcut") def shortcut_handler(ack): ack() input = { "type": "shortcut", "token": "verification_token", "action_ts": "111.111", "team": { "id": "T111", "domain": "workspace-domain", "enterprise_id": "E111", "enterprise_name": "Org Name", }, "user": { "id": "W111", "username": "******", "team_id": "T111" }, "callback_id": "test-shortcut", "trigger_id": "111.111.xxxxxx", } timestamp, body = str(int( time())), f"payload={quote(json.dumps(input))}" chalice_app = Chalice(app_name="bolt-python-chalice") slack_handler = ChaliceSlackRequestHandler(app=app, chalice=chalice_app) @chalice_app.route( "/slack/events", methods=["POST"], content_types=[ "application/x-www-form-urlencoded", "application/json" ], ) def events() -> Response: return slack_handler.handle(chalice_app.current_request) response: Dict[str, Any] = LocalGateway(chalice_app, Config()).handle_request( method="POST", path="/slack/events", body=body, headers=self.build_headers( timestamp, body), ) assert response["statusCode"] == 200 assert self.mock_received_requests["/auth.test"] == 1
def test_events(self): app = App( client=self.web_client, signing_secret=self.signing_secret, ) @app.event("app_mention") def event_handler(): pass input = { "token": "verification_token", "team_id": "T111", "enterprise_id": "E111", "api_app_id": "A111", "event": { "client_msg_id": "9cbd4c5b-7ddf-4ede-b479-ad21fca66d63", "type": "app_mention", "text": "<@W111> Hi there!", "user": "******", "ts": "1595926230.009600", "team": "T111", "channel": "C111", "event_ts": "1595926230.009600", }, "type": "event_callback", "event_id": "Ev111", "event_time": 1595926230, "authed_users": ["W111"], } timestamp, body = str(int(time())), json.dumps(input) chalice_app = Chalice(app_name="bolt-python-chalice") slack_handler = ChaliceSlackRequestHandler(app=app, chalice=chalice_app) @chalice_app.route( "/slack/events", methods=["POST"], content_types=[ "application/x-www-form-urlencoded", "application/json" ], ) def events() -> Response: return slack_handler.handle(chalice_app.current_request) response: Dict[str, Any] = LocalGateway(chalice_app, Config()).handle_request( method="POST", path="/slack/events", body=body, headers=self.build_headers( timestamp, body), ) assert response["statusCode"] == 200 assert self.mock_received_requests["/auth.test"] == 1
class SelfContainedForm(unittest.TestCase): def setUp(self): with open(".chalice/config.json") as file: self.lg = LocalGateway( app, Config(chalice_stage="beta", config_from_disk=json.load(file))) body = dict(schema=TEST_SCHEMA) response = self.lg.handle_request( method='POST', path='/centers/{}/forms/new'.format(CENTER_ID), headers={ "authorization": "auth", "Content-Type": "application/json" }, body=json.dumps(body)) self.assertEqual(response['statusCode'], 200, response) body = json.loads(response['body']) self.form = body['res']['form'] self.formId = self.form['id'] def tearDown(self): response = self.lg.handle_request(method='DELETE', path='/forms/{}'.format(self.formId), headers={ "authorization": "auth", }, body='') self.assertEqual(response['statusCode'], 200, response) body = json.loads(response['body']) self.assertEqual(body, { "res": None, "success": True, "action": "delete" }) response = self.lg.handle_request(method='GET', path='/forms/{}/render'.format( self.formId), headers={ "authorization": "auth", }, body='') self.assertEqual(response['statusCode'], 500, response)
class TestApp(TestCase): def setUp(self): self.lg = LocalGateway(app, Config()) def test_get_index(self): response = self.lg.handle_request(method='GET', path='/', headers={}, body='') assert response['statusCode'] == 200 assert json.loads(response['hello']) == 'friend'
def test_commands(self): app = App( client=self.web_client, signing_secret=self.signing_secret, ) @app.command("/hello-world") def command_handler(ack): ack() input = ( "token=verification_token" "&team_id=T111" "&team_domain=test-domain" "&channel_id=C111" "&channel_name=random" "&user_id=W111" "&user_name=primary-owner" "&command=%2Fhello-world" "&text=Hi" "&enterprise_id=E111" "&enterprise_name=Org+Name" "&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT111%2F111%2Fxxxxx" "&trigger_id=111.111.xxx") timestamp, body = str(int(time())), input chalice_app = Chalice(app_name="bolt-python-chalice") slack_handler = ChaliceSlackRequestHandler(app=app, chalice=chalice_app) @chalice_app.route( "/slack/events", methods=["POST"], content_types=[ "application/x-www-form-urlencoded", "application/json" ], ) def events() -> Response: return slack_handler.handle(chalice_app.current_request) response: Dict[str, Any] = LocalGateway(chalice_app, Config()).handle_request( method="POST", path="/slack/events", body=body, headers=self.build_headers( timestamp, body), ) assert response["statusCode"] == 200 assert self.mock_received_requests["/auth.test"] == 1
class TestApp(unittest.TestCase): def setUp(self): self.gateway = LocalGateway(app, Config()) @mock.patch("app.RankStatistics._load") def test_contains_all(self, mock_statistics_load): mock_statistics_load.return_value = { "PAGE_1": 5, "PAGE_2": 4, "PAGE_3": 7, "PAGE_4": 2 } states = ["PAGE_1", "PAGE_2", "PAGE_3", "PAGE_4", "PAGE_5"] body = {"states": states} response = self.gateway.handle_request( method='POST', path='/order', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) logging.info(response) body = json.loads(response['body']) assert response['statusCode'] == 200 for state in states: assert state in body['order'] @mock.patch("app.write_metric", return_value=None) def test_reward(self, mock_write_metric): body = {"state": "PAGE_3", "reward": 700} response = self.gateway.handle_request( method='POST', path='/metric', headers={'Content-Type': 'application/json'}, body=json.dumps(body)) body = json.loads(response['body']) assert response['statusCode'] == 200 mock_write_metric.assert_called_once()
def test_can_deny_route_with_variables(self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(ForbiddenError): gateway.handle_request( 'GET', '/resource/foobarbaz', {'Authorization': 'allow'}, '')
def test_does_send_500_when_authorizer_returns_none(self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) with pytest.raises(InvalidAuthorizerError): gateway.handle_request( 'GET', '/none', {'Authorization': 'foobarbaz'}, '')
def test_can_allow_route_with_variables(self, demo_app_auth): gateway = LocalGateway(demo_app_auth, Config()) response = gateway.handle_request( 'GET', '/resource/foobar', {'Authorization': 'allow'}, '') json_body = json.loads(response['body']) assert json_body['resource'] == 'foobar'
def setUp(self): self.lg = LocalGateway(app, Config())