From 8c417064ddbca0a9c5da87cacddf3ac2770edc7e Mon Sep 17 00:00:00 2001 From: jacekv Date: Tue, 26 Nov 2024 14:27:56 +0100 Subject: [PATCH 1/7] PAN-2339: Adding Schemas for 2XX responses --- pantos/servicenode/restapi.py | 80 ++++++++++++++------------- tests/restapi/test_transfer.py | 5 +- tests/restapi/test_transfer_status.py | 1 - 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index c0917c4..e7d7f61 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -186,6 +186,13 @@ def make_initiate_transfer_request( return TransferInteractor.InitiateTransferRequest(**data) +class _TransferResponseSchema(marshmallow.Schema): + """Validation schema for the transfer response. + + """ + task_id = marshmallow.fields.UUID(required=True) + + class _TransferStatusSchema(marshmallow.Schema): """Validation schema for the transfer status endpoint parameters. @@ -198,6 +205,28 @@ def make_task_id(self, data: typing.Dict[str, typing.Any], return data['task_id'] +class _TransferStatusResponseSchema(marshmallow.Schema): + """Validation schema for the transfer status response. + + """ + task_id = marshmallow.fields.String(required=True) + source_blockchain_id = marshmallow.fields.Integer( + required=True, validate=marshmallow.validate.OneOf( + [blockchain.value for blockchain in Blockchain])) + destination_blockchain_id = marshmallow.fields.Integer( + required=True, validate=marshmallow.validate.OneOf( + [blockchain.value for blockchain in Blockchain])) + sender_address = marshmallow.fields.String(required=True) + recipient_address = marshmallow.fields.String(required=True) + source_token_address = marshmallow.fields.String(required=True) + destination_token_address = marshmallow.fields.String(required=True) + amount = marshmallow.fields.Integer(required=True) + fee = marshmallow.fields.Integer(required=True) + status = marshmallow.fields.String(required=True) + transfer_id = marshmallow.fields.Integer(required=True) + transaction_id = marshmallow.fields.String(required=True) + + class _BidsSchema(marshmallow.Schema): """Validation schema for the bids endpoint parameters. @@ -233,27 +262,24 @@ def post(self) -> flask.Response: content: application/json: schema: - type: object - properties: - task_id: - type: string + $ref: "#/components/schemas/_TransferResponse" 406: description: Transfer request no accepted content: application/json: schema: - type: array + type: string items: type: string - example: "[bid has been rejected by service node: \ -'bid not accepted']" + example: {message': 'bid has been rejected by service \ +node: bid not accepted'} 409: description: Sender nonce from transfer request is not unique content: application/json: schema: type: string - example: sender nonce 1337 is not unique + example: {'message': 'sender nonce 1337 is not unique'} 500: description: Internal server error """ @@ -265,6 +291,7 @@ def post(self) -> flask.Response: _logger.info('new transfer request', extra=arguments) task_id = TransferInteractor().initiate_transfer( initiate_transfer_request) + response = _TransferResponseSchema().dump({'task_id': task_id}) except marshmallow.ValidationError as error: not_acceptable(error.messages) except SenderNonceNotUniqueError as error: @@ -273,12 +300,12 @@ def post(self) -> flask.Response: f'{initiate_transfer_request.nonce} is not unique') except TransferInteractorBidNotAcceptedError as error: _logger.warning(f'bid has been rejected by service node: {error}') - not_acceptable([f'bid has been rejected by service node: {error}']) + not_acceptable(f'bid has been rejected by service node: {error}') except Exception: _logger.critical('unable to process a transfer request', exc_info=True) internal_server_error() - return ok_response({'task_id': str(task_id)}) + return ok_response(response) class _TransferStatus(flask_restful.Resource): @@ -305,34 +332,7 @@ def get(self, task_id: str) -> flask.Response: content: application/json: schema: - type: object - properties: - task_id: - type: string - source_blockchain_id: - $ref: "#/components/schemas/\ -_Bids/properties/source_blockchain" - destination_blockchain_id: - $ref: "#/components/schemas/\ -_Bids/properties/destination_blockchain" - sender_address: - type: string - recipient_address: - type: string - source_token_address: - type: string - destination_token_address: - type: string - amount: - type: integer - fee: - type: integer - status: - type: string - transfer_id: - type: string - transaction_id: - type: string + $ref: '#/components/schemas/_TransferStatusResponse' 404: description: 'not found' content: @@ -360,7 +360,8 @@ def get(self, task_id: str) -> flask.Response: _logger.critical('unable to process a transfer status request', exc_info=True) internal_server_error() - return ok_response({ + + response = _TransferStatusResponseSchema().load({ 'task_id': str(task_id_uuid), 'source_blockchain_id': find_transfer_response.source_blockchain. value, @@ -381,6 +382,7 @@ def get(self, task_id: str) -> flask.Response: 'transaction_id': '' if find_transfer_response.transaction_id is None else find_transfer_response.transaction_id }) + return response class _Bids(flask_restful.Resource): diff --git a/tests/restapi/test_transfer.py b/tests/restapi/test_transfer.py index a72a059..722638a 100644 --- a/tests/restapi/test_transfer.py +++ b/tests/restapi/test_transfer.py @@ -56,7 +56,6 @@ def test_transfer_sender_nonce_not_unique_error(mocked_load, 406, message=error_message) response = test_client.post('/transfer', json={}) - mocked_conflict.assert_called_once_with( f'sender nonce {mocked_load().nonce} is not unique') assert response.status_code == 406 @@ -77,9 +76,9 @@ def test_transfer_bid_not_accepted_error(mocked_load, mocked_initiate_transfer, 406, message=error_message) response = test_client.post('/transfer', json={}) - assert response.status_code == 406 - mocked_not_acceptable.assert_called_once() + mocked_not_acceptable.assert_called_once_with( + 'bid has been rejected by service node: bid not accepted') @unittest.mock.patch('pantos.servicenode.restapi.internal_server_error') diff --git a/tests/restapi/test_transfer_status.py b/tests/restapi/test_transfer_status.py index 1bfe40f..82543cf 100644 --- a/tests/restapi/test_transfer_status.py +++ b/tests/restapi/test_transfer_status.py @@ -40,7 +40,6 @@ def test_transfer_status_correct(mocked_load, mocked_find_transfer, mocked_find_transfer.return_value = find_transfer_response response = test_client.get(f'/transfer/{uuid_}/status') - assert response.status_code == 200 assert json.loads(response.text) == expected_transfer_status From 6585f8cf60eb492e3e2d1dd2dec7ffe832f06ad4 Mon Sep 17 00:00:00 2001 From: jacekv Date: Tue, 26 Nov 2024 21:00:33 +0100 Subject: [PATCH 2/7] PAN-2339: Testing return value --- pantos/servicenode/restapi.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index e7d7f61..ff74d60 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -305,7 +305,8 @@ def post(self) -> flask.Response: _logger.critical('unable to process a transfer request', exc_info=True) internal_server_error() - return ok_response(response) + # return ok_response(response) + return ok_response({'task_id': str(task_id)}) class _TransferStatus(flask_restful.Resource): From 47c700ea705269f6db3d60ee2b5056df6eba6a6e Mon Sep 17 00:00:00 2001 From: jacekv Date: Tue, 26 Nov 2024 21:06:24 +0100 Subject: [PATCH 3/7] PAN-2339: Fixing lint error --- pantos/servicenode/restapi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index ff74d60..f1ae2e7 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -291,7 +291,7 @@ def post(self) -> flask.Response: _logger.info('new transfer request', extra=arguments) task_id = TransferInteractor().initiate_transfer( initiate_transfer_request) - response = _TransferResponseSchema().dump({'task_id': task_id}) + # response = _TransferResponseSchema().dump({'task_id': task_id}) except marshmallow.ValidationError as error: not_acceptable(error.messages) except SenderNonceNotUniqueError as error: From 219bbfb9986bd6c96838f5a376afb524ca3b2cdc Mon Sep 17 00:00:00 2001 From: jacekv Date: Tue, 26 Nov 2024 21:20:45 +0100 Subject: [PATCH 4/7] PAN-2339: Reversing some other code for test --- openapi.py | 5 ++++- pantos/servicenode/restapi.py | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/openapi.py b/openapi.py index d5578ca..dbd2e75 100644 --- a/openapi.py +++ b/openapi.py @@ -10,7 +10,9 @@ from pantos.servicenode.restapi import _BidSchema from pantos.servicenode.restapi import _BidsSchema +from pantos.servicenode.restapi import _TransferResponseSchema from pantos.servicenode.restapi import _TransferSchema +from pantos.servicenode.restapi import _TransferStatusResponseSchema from pantos.servicenode.restapi import _TransferStatusSchema from pantos.servicenode.restapi import flask_app @@ -24,7 +26,8 @@ template = spec.to_flasgger( flask_app, definitions=[ - _BidSchema, _BidsSchema, _TransferSchema, _TransferStatusSchema + _BidSchema, _BidsSchema, _TransferSchema, _TransferResponseSchema, + _TransferStatusSchema, _TransferStatusResponseSchema ]) swagger = Swagger(flask_app, template=template, parse=True) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index f1ae2e7..61fd556 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -362,7 +362,8 @@ def get(self, task_id: str) -> flask.Response: exc_info=True) internal_server_error() - response = _TransferStatusResponseSchema().load({ + # response = _TransferStatusResponseSchema().load({ + return ok_response({ 'task_id': str(task_id_uuid), 'source_blockchain_id': find_transfer_response.source_blockchain. value, @@ -383,7 +384,7 @@ def get(self, task_id: str) -> flask.Response: 'transaction_id': '' if find_transfer_response.transaction_id is None else find_transfer_response.transaction_id }) - return response + # return response class _Bids(flask_restful.Resource): From dce4ca808703bafb9ab51ebe229343ebdf450edb Mon Sep 17 00:00:00 2001 From: jacekv Date: Tue, 26 Nov 2024 21:37:55 +0100 Subject: [PATCH 5/7] PAN-2339: Putting back in --- pantos/servicenode/restapi.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index 61fd556..76ff016 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -291,7 +291,7 @@ def post(self) -> flask.Response: _logger.info('new transfer request', extra=arguments) task_id = TransferInteractor().initiate_transfer( initiate_transfer_request) - # response = _TransferResponseSchema().dump({'task_id': task_id}) + response = _TransferResponseSchema().dump({'task_id': task_id}) except marshmallow.ValidationError as error: not_acceptable(error.messages) except SenderNonceNotUniqueError as error: @@ -305,8 +305,7 @@ def post(self) -> flask.Response: _logger.critical('unable to process a transfer request', exc_info=True) internal_server_error() - # return ok_response(response) - return ok_response({'task_id': str(task_id)}) + return ok_response(response) class _TransferStatus(flask_restful.Resource): @@ -362,8 +361,7 @@ def get(self, task_id: str) -> flask.Response: exc_info=True) internal_server_error() - # response = _TransferStatusResponseSchema().load({ - return ok_response({ + response = _TransferStatusResponseSchema().dump({ 'task_id': str(task_id_uuid), 'source_blockchain_id': find_transfer_response.source_blockchain. value, @@ -384,7 +382,7 @@ def get(self, task_id: str) -> flask.Response: 'transaction_id': '' if find_transfer_response.transaction_id is None else find_transfer_response.transaction_id }) - # return response + return response class _Bids(flask_restful.Resource): From 0eb8e6db931e4645119c71ea62925c67b614322e Mon Sep 17 00:00:00 2001 From: jacekv Date: Tue, 26 Nov 2024 21:57:00 +0100 Subject: [PATCH 6/7] PAN-2339: Adding ok response --- pantos/servicenode/restapi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index 76ff016..0df0e9d 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -382,7 +382,7 @@ def get(self, task_id: str) -> flask.Response: 'transaction_id': '' if find_transfer_response.transaction_id is None else find_transfer_response.transaction_id }) - return response + return ok_response(response) class _Bids(flask_restful.Resource): From a8fcf091b45f3382dadb732b131fd3c8071ea767 Mon Sep 17 00:00:00 2001 From: jacekv Date: Wed, 27 Nov 2024 10:41:18 +0100 Subject: [PATCH 7/7] PAN-2339: Adjust dataclass --- pantos/servicenode/restapi.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pantos/servicenode/restapi.py b/pantos/servicenode/restapi.py index 0df0e9d..d6af8f0 100644 --- a/pantos/servicenode/restapi.py +++ b/pantos/servicenode/restapi.py @@ -377,8 +377,7 @@ def get(self, task_id: str) -> flask.Response: 'fee': find_transfer_response.fee, 'status': find_transfer_response.status.to_public_status().name. lower(), - 'transfer_id': '' if find_transfer_response.transfer_id is None - else find_transfer_response.transfer_id, + 'transfer_id': find_transfer_response.transfer_id, 'transaction_id': '' if find_transfer_response.transaction_id is None else find_transfer_response.transaction_id })