async def test_batch_statuses_as_post(self):
        """Verifies a POST to /batch_statuses works properly.

        It will receive a Protobuf response with:
            - batch statuses of:
                * ID_A: COMMITTED
                * ID_B: PENDING
                * ID_C: UNKNOWN

        It should send a Protobuf request with:
            - a batch_ids property of [ID_A, ID_B, ID_C]

        It should send back a JSON response with:
            - a response status of 200
            - an empty link property
            - a data property matching the batch statuses received
        """
        statuses = [
            ClientBatchStatus(batch_id=ID_A,
                              status=ClientBatchStatus.COMMITTED),
            ClientBatchStatus(batch_id=ID_B, status=ClientBatchStatus.PENDING),
            ClientBatchStatus(batch_id=ID_C, status=ClientBatchStatus.UNKNOWN)
        ]
        self.connection.preset_response(batch_statuses=statuses)

        request = await self.client.post(
            '/batch_statuses',
            data=json.dumps([ID_A, ID_B, ID_C]).encode(),
            headers={'content-type': 'application/json'})
        self.connection.assert_valid_request_sent(batch_ids=[ID_A, ID_B, ID_C])
        self.assertEqual(200, request.status)

        response = await request.json()
        self.assertNotIn('link', response)
        self.assert_statuses_match(statuses, response['data'])
    async def test_batch_statuses_with_many_ids(self):
        """Verifies a GET /batch_statuses with many ids works properly.

        It will receive a Protobuf response with:
            - batch statuses of:
                * ID_A: COMMITTED
                * ID_B: UNKNOWN
                * ID_C: UNKNOWN

        It should send a Protobuf request with:
            - a batch_ids property of [ID_A, ID_B, ID_C]

        It should send back a JSON response with:
            - a response status of 200
            - link property ending in
              '/batch_statuses?id={},{},{}'.format(ID_A, ID_B, ID_C)
            - a data property matching the batch statuses received
        """
        statuses = [
            ClientBatchStatus(batch_id=ID_A,
                              status=ClientBatchStatus.COMMITTED),
            ClientBatchStatus(batch_id=ID_B, status=ClientBatchStatus.UNKNOWN),
            ClientBatchStatus(batch_id=ID_C, status=ClientBatchStatus.UNKNOWN)
        ]
        self.connection.preset_response(batch_statuses=statuses)

        response = await self.get_assert_200(
            '/batch_statuses?id={},{},{}'.format(ID_A, ID_B, ID_C))
        self.connection.assert_valid_request_sent(batch_ids=[ID_A, ID_B, ID_C])

        self.assert_has_valid_link(
            response, '/batch_statuses?id={},{},{}'.format(ID_A, ID_B, ID_C))
        self.assert_statuses_match(statuses, response['data'])
Example #3
0
    async def test_batch_statuses_with_many_ids(self):
        """Verifies a GET /batch_statuses with many ids works properly.

        It will receive a Protobuf response with:
            - batch statuses of:
                * 'committed': COMMITTED
                * 'unknown': UNKNOWN
                * 'bad': UNKNOWN

        It should send a Protobuf request with:
            - a batch_ids property of ['committed', 'unknown', 'bad']

        It should send back a JSON response with:
            - a response status of 200
            - link property ending in '/batch_statuses?id=committed,unknown,bad'
            - a data property matching the batch statuses received
        """
        statuses = [
            ClientBatchStatus(
                batch_id='committed', status=ClientBatchStatus.COMMITTED),
            ClientBatchStatus(
                batch_id='unknown', status=ClientBatchStatus.UNKNOWN),
            ClientBatchStatus(
                batch_id='bad', status=ClientBatchStatus.UNKNOWN)]
        self.connection.preset_response(batch_statuses=statuses)

        response = await self.get_assert_200(
            '/batch_statuses?id=committed,unknown,bad')
        self.connection.assert_valid_request_sent(
            batch_ids=['committed', 'unknown', 'bad'])

        self.assert_has_valid_link(
            response,
            '/batch_statuses?id=committed,unknown,bad')
        self.assert_statuses_match(statuses, response['data'])
Example #4
0
    async def test_batch_statuses_with_invalid_data(self):
        """Verifies a GET /batch_statuses with fetched invalid data works.

        It will receive a Protobuf response with:
            - batch_id: 'bad-batch'
            - status: INVALID
            - invalid_transaction: 'bad-transaction'
            - message: 'error message'
            - extended_data: b'error data'

        It should send a Protobuf request with:
            - a batch_ids property of ['bad']

        It should send back a JSON response with:
            - a response status of 200
            - a link property that ends in '/batch_statuses?id=bad'
            - a data property matching the batch statuses received
        """
        statuses = [ClientBatchStatus(
            batch_id='bad-batch',
            status=ClientBatchStatus.INVALID,
            invalid_transactions=[ClientBatchStatus.InvalidTransaction(
                transaction_id='bad-transaction',
                message='error message',
                extended_data=b'error data')])]
        self.connection.preset_response(batch_statuses=statuses)

        response = await self.get_assert_200('/batch_statuses?id=bad')
        self.connection.assert_valid_request_sent(batch_ids=['bad'])

        self.assert_has_valid_link(response, '/batch_statuses?id=bad')
        self.assert_statuses_match(statuses, response['data'])
    async def test_batch_statuses_with_wait(self):
        """Verifies a GET /batch_statuses with a wait set works properly.

        It will receive a Protobuf response with:
            - batch statuses of {batch_id: ID_D, status: COMMITTED}

        It should send a Protobuf request with:
            - a batch_ids property of [ID_D]
            - a wait property that is True
            - a timeout property of 4 (Rest Api default)

        It should send back a JSON response with:
            - a response status of 200
            - a link property that ends in
              '/batch_statuses?id={}&{}'.format(ID_C, ID_D)
            - a data property matching the batch statuses received
        """
        statuses = [
            ClientBatchStatus(batch_id=ID_D,
                              status=ClientBatchStatus.COMMITTED)
        ]
        self.connection.preset_response(batch_statuses=statuses)

        response = await self.get_assert_200(
            '/batch_statuses?id={}&wait'.format(ID_D))
        self.connection.assert_valid_request_sent(batch_ids=[ID_D],
                                                  wait=True,
                                                  timeout=4)

        self.assert_has_valid_link(response,
                                   '/batch_statuses?id={}&wait'.format(ID_D))
        self.assert_statuses_match(statuses, response['data'])
    async def test_batch_statuses_with_one_id(self):
        """Verifies a GET /batch_statuses with one id works properly.

        It will receive a Protobuf response with:
            - batch statuses of {batch_id: ID_D,  status: PENDING}

        It should send a Protobuf request with:
            - a batch_ids property of [ID_D]

        It should send back a JSON response with:
            - a response status of 200
            - a link property that ends in '/batch_statuses?id={}'.format(ID_D)
            - a data property matching the batch statuses received
        """
        statuses = [
            ClientBatchStatus(batch_id=ID_D, status=ClientBatchStatus.PENDING)
        ]
        self.connection.preset_response(batch_statuses=statuses)

        response = await self.get_assert_200(
            '/batch_statuses?id={}'.format(ID_D))
        self.connection.assert_valid_request_sent(batch_ids=[ID_D])

        self.assert_has_valid_link(response,
                                   '/batch_statuses?id={}'.format(ID_D))
        self.assert_statuses_match(statuses, response['data'])