class TestHttpEntrypoint(object): @pytest.mark.parametrize( ('exc', 'expected_error', 'expected_status_code', 'expected_message'), [ (ValueError('unexpected'), 'UNEXPECTED_ERROR', 500, 'unexpected'), (ValidationError('v1'), 'VALIDATION_ERROR', 400, 'v1'), (ProductNotFound('p1'), 'PRODUCT_NOT_FOUND', 404, 'p1'), (OrderNotFound('o1'), 'ORDER_NOT_FOUND', 404, 'o1'), (TypeError('t1'), 'BAD_REQUEST', 400, 't1'), ]) def test_error_handling(self, exc, expected_error, expected_status_code, expected_message): entrypoint = HttpEntrypoint('GET', 'url') entrypoint.expected_exceptions = ( ValidationError, ProductNotFound, OrderNotFound, TypeError, ) response = entrypoint.response_from_exception(exc) response_data = json.loads(response.data.decode()) assert response.mimetype == 'application/json' assert response.status_code == expected_status_code assert response_data['error'] == expected_error assert response_data['message'] == expected_message
def _getOrder(self, hashed_order_id): # Retrieve order data from the orders service. # Note - this may raise a remote exception that has been mapped to # raise``OrderNotFound`` try: order = self.orders_rpc.getOrder(hashed_order_id) except OrderNotFound as ex: self.logger.info(ex) raise OrderNotFound('Order with id {} not found'.format(hashed_order_id)) # Retrieve all products from the products service product_map = {prod['id']: prod for prod in self.products_rpc.list()} # self.logger.info("product_map: %s" % product_map) # get the configured image root image_root = self.config.readConfig(section='location', key='PRODUCT_IMAGE_ROOT') # Enhance order details with product and image details. for item in order['order_details']: product_id = item['product_id'] item['product'] = product_map[product_id] # Construct an image url. item['image'] = '{}/{}.jpg'.format(image_root, product_id) return order
def test_order_not_found(self, gateway_service, web_session): gateway_service.orders_rpc.get_order.side_effect = ( OrderNotFound('missing')) # call the gateway service to get order #1 response = web_session.get('/orders/1') assert response.status_code == 404 payload = response.json() assert payload['error'] == 'ORDER_NOT_FOUND' assert payload['message'] == 'missing'