github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/rest_api/tests/unit/test_receipt_requests.py (about)

     1  # Copyright 2017 Intel Corporation
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  # ------------------------------------------------------------------------------
    15  
    16  import json
    17  
    18  from aiohttp.test_utils import unittest_run_loop
    19  
    20  from components import BaseApiTest
    21  from sawtooth_rest_api.protobuf.validator_pb2 import Message
    22  from sawtooth_rest_api.protobuf import client_receipt_pb2
    23  from sawtooth_rest_api.protobuf.transaction_receipt_pb2 import  \
    24      TransactionReceipt
    25  
    26  
    27  ID_A = 'a' * 128
    28  ID_B = 'b' * 128
    29  ID_C = 'c' * 128
    30  ID_D = 'd' * 128
    31  
    32  
    33  class ReceiptGetRequestTests(BaseApiTest):
    34      async def get_application(self):
    35          self.set_status_and_connection(
    36              Message.CLIENT_RECEIPT_GET_REQUEST,
    37              client_receipt_pb2.ClientReceiptGetRequest,
    38              client_receipt_pb2.ClientReceiptGetResponse)
    39  
    40          handlers = self.build_handlers(self.loop, self.connection)
    41          return self.build_app(self.loop, '/receipts', handlers.list_receipts)
    42  
    43      def assert_receipts_match(self, proto_receipts, json_receipts):
    44          """Asserts that JSON statuses match the original enum statuses dict
    45          """
    46          self.assertEqual(len(proto_receipts), len(json_receipts))
    47          for pb_receipt, js_receipt in zip(proto_receipts, json_receipts):
    48              self.assertEqual(pb_receipt.transaction_id, js_receipt['id'])
    49  
    50      @unittest_run_loop
    51      async def test_receipts_with_one_id(self):
    52          """Verifies a GET /receipts with one id works properly.
    53  
    54          It will receive a Protobuf response with:
    55              - a transaction receipt with matching id
    56  
    57          It should send a Protobuf request with:
    58              - a transaction_ids property of [ID_B]
    59  
    60          It should send back a JSON response with:
    61              - a response status of 200
    62              - a link property that ends in '/receipts?id={}'.format(ID_B)
    63              - a data property matching the receipts received
    64          """
    65          receipts = [TransactionReceipt(transaction_id=ID_B)]
    66          self.connection.preset_response(
    67              receipts=receipts,
    68              status=self.status.OK)
    69  
    70          response = await self.get_assert_200('/receipts?id={}'.format(ID_B))
    71          self.connection.assert_valid_request_sent(transaction_ids=[ID_B])
    72  
    73          self.assert_has_valid_link(response, '/receipts?id={}'.format(ID_B))
    74          self.assert_receipts_match(receipts, response['data'])
    75  
    76      @unittest_run_loop
    77      async def test_receipts_with_missing_id(self):
    78          """Verifies a GET /receipts with fetched invalid data works.
    79  
    80          It will receive a Protobuf response with:
    81              - status: NO_RESOURCE
    82  
    83          It should send a Protobuf request with:
    84              - a transaction_ids property of ['missing']
    85  
    86          It should send back a JSON response with:
    87              - a response status of 404
    88              - a link property that ends in '/receipts?id={}'.format(ID_D)
    89              - a data property matching the receipts statuses received
    90          """
    91          receipts = []
    92          self.connection.preset_response(
    93              receipts=receipts,
    94              status=self.status.NO_RESOURCE)
    95  
    96          response = await self.get_assert_status('/receipts?id={}'.format(ID_D),
    97                                                  404)
    98          self.connection.assert_valid_request_sent(transaction_ids=[ID_D])
    99  
   100          self.assert_has_valid_error(response, 80)
   101  
   102      @unittest_run_loop
   103      async def test_receipts_with_many_ids(self):
   104          """Verifies a GET /receipts with many ids works properly.
   105  
   106          It will receive a Protobuf response with receipts with ids:
   107              - ID_B
   108              - ID_C
   109              - ID_D
   110  
   111          It should send a Protobuf request with:
   112              - a transaction_ids property of [ID_B, ID_C, ID_D]
   113  
   114          It should send back a JSON response with:
   115              - a response status of 200
   116              - link property ending in
   117                  '/receipts?id={},{},{}' .format(ID_B, ID_C, ID_D)
   118              - a data property matching the batch statuses received
   119          """
   120          receipts = [
   121              TransactionReceipt(transaction_id=t_id)
   122              for t_id in (ID_B, ID_C, ID_D)
   123          ]
   124          self.connection.preset_response(
   125              status=self.status.OK,
   126              receipts=receipts)
   127  
   128          response = await self.get_assert_200(
   129              '/receipts?id={},{},{}'.format(ID_B, ID_C, ID_D))
   130          self.connection.assert_valid_request_sent(
   131              transaction_ids=[ID_B, ID_C, ID_D])
   132  
   133          self.assert_has_valid_link(
   134              response,
   135              '/receipts?id={},{},{}'.format(ID_B, ID_C, ID_D))
   136          self.assert_receipts_match(receipts, response['data'])
   137  
   138      @unittest_run_loop
   139      async def test_batch_statuses_as_post(self):
   140          """Verifies a POST to /receipts with many ids works properly.
   141  
   142          It will receive a Protobuf response with receipts with ids:
   143              - ID_B
   144              - ID_C
   145              - ID_D
   146  
   147          It should send a Protobuf request with:
   148              - a transaction_ids property of [ID_B, ID_C, ID_D]
   149  
   150          It should send back a JSON response with:
   151              - a response status of 200
   152              - link property ending in
   153                  '/receipts?id={},{},{}'.format(ID_B, ID_C, ID_D)
   154              - a data property matching the batch statuses received
   155          """
   156          receipts = [
   157              TransactionReceipt(transaction_id=t_id)
   158              for t_id in (ID_B, ID_C, ID_D)
   159          ]
   160          self.connection.preset_response(
   161              status=self.status.OK,
   162              receipts=receipts)
   163  
   164          request = await self.client.post(
   165              '/receipts',
   166              data=json.dumps([ID_B, ID_C, ID_D]).encode(),
   167              headers={'content-type': 'application/json'})
   168          self.connection.assert_valid_request_sent(
   169              transaction_ids=[ID_B, ID_C, ID_D])
   170          self.assertEqual(200, request.status)
   171  
   172          response = await request.json()
   173          self.assertNotIn('link', response)
   174          self.assert_receipts_match(receipts, response['data'])