예제 #1
0
    async def test_block_list_with_ids(self):
        """Verifies GET /blocks with an id filter works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 2 total resources
            - two blocks with ids '0' and '2'

        It should send a Protobuf request with:
            - a block_ids property of ['0', '2']
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2', the latest
            - a link property that ends in '/blocks?head=2&id=0,2'
            - a paging property that matches the paging response
            - a data property that is a list of 2 dicts
            - and those dicts are full blocks with ids '0' and '2'
        """
        paging = Mocks.make_paging_response(0, 2)
        blocks = Mocks.make_blocks('0', '2')
        self.stream.preset_response(head_id='2', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?id=0,2')
        controls = Mocks.make_paging_controls()
        self.stream.assert_valid_request_sent(block_ids=['0', '2'],
                                              paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/blocks?head=2&id=0,2')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 2)
        self.assert_blocks_well_formed(response['data'], '0', '2')
예제 #2
0
    async def test_state_list_paginated_by_max_index(self):
        """Verifies GET /state paginated by a max index works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - three leaves with the ids {'d': b'4'}, {'c': b'3'} and {'b': b'2'}

        It should send a Protobuf request with:
            - a paging controls with a count of 2 and an start_index of 0

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=3&count=7'
            - paging that matches the response, with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(0, 4)
        leaves = Mocks.make_leaves(d=b'4', c=b'3', b=b'2')
        self.connection.preset_response(head_id='d',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200('/state?max=2&count=7')
        controls = Mocks.make_paging_controls(3, start_index=0)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&max=2&count=7')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=3&count=7')
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
    async def test_batch_list_paginated_without_count(self):
        """Verifies GET /batches paginated without count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 2, and 4 total resources
            - two batches with the ids 'b' and 'a'

        It should send a Protobuf request with:
            - paging controls with a start_index of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/batches?head=d&min=2'
            - paging that matches the response, with a previous link
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids 'd' and 'c'
        """
        paging = Mocks.make_paging_response(2, 4)
        batches = Mocks.make_batches('b', 'a')
        self.connection.preset_response(head_id='d', paging=paging, batches=batches)

        response = await self.get_assert_200('/batches?min=2')
        controls = Mocks.make_paging_controls(None, start_index=2)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/batches?head=d&min=2')
        self.assert_has_valid_paging(response, paging,
                                     previous_link='/batches?head=d&min=0&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_batches_well_formed(response['data'], 'b', 'a')
예제 #4
0
    async def test_state_list_paginated_without_count(self):
        """Verifies GET /state paginated without count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 2, and 4 total resources
            - two leaves of {'b': b'2'} and {'a': b'1'}

        It should send a Protobuf request with:
            - a paging controls with a start_index of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=2&count=2'
            - paging that matches the response, with a previous link
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(2, 4)
        leaves = Mocks.make_leaves(b=b'2', a=b'1')
        self.connection.preset_response(head_id='d',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200('/state?min=2')
        controls = Mocks.make_paging_controls(None, start_index=2)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&min=2')
        self.assert_has_valid_paging(
            response, paging, previous_link='/state?head=d&min=0&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
예제 #5
0
    async def test_block_list_paginated(self):
        """Verifies GET /blocks paginated by min id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 1, and 4 total resources
            - one block with the id 'c'

        It should send a Protobuf request with:
            - paging controls with a count of 1, and a start_index of 1

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/blocks?head=d&min=1&count=1'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 1 dict
            - and that dict is a full block with the id 'c'
        """
        paging = Mocks.make_paging_response(1, 4)
        blocks = Mocks.make_blocks('c')
        self.stream.preset_response(head_id='d', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?min=1&count=1')
        controls = Mocks.make_paging_controls(1, start_index=1)
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/blocks?head=d&min=1&count=1')
        self.assert_has_valid_paging(response, paging,
                                     '/blocks?head=d&min=2&count=1',
                                     '/blocks?head=d&min=0&count=1')
        self.assert_has_valid_data_list(response, 1)
        self.assert_blocks_well_formed(response['data'], 'c')
    async def test_batch_list_with_head(self):
        """Verifies a GET /batches with a head parameter works properly.

        It will receive a Protobuf response with:
            - a head id of '1'
            - a paging response with a start of 0, and 2 total resources
            - two batches with ids of 1' and '0'

        It should send a Protobuf request with:
            - a head_id property of '1'
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/batches?head=1'
            - a paging property that matches the paging response
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids '1' and '0'
        """
        paging = Mocks.make_paging_response(0, 2)
        batches = Mocks.make_batches('1', '0')
        self.connection.preset_response(head_id='1', paging=paging, batches=batches)

        response = await self.get_assert_200('/batches?head=1')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(head_id='1', paging=controls)

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/batches?head=1')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 2)
        self.assert_batches_well_formed(response['data'], '1', '0')
예제 #7
0
    async def test_state_list_paginated_with_just_count(self):
        """Verifies GET /state paginated just by count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - two leaves of {'d': b'4'}, and {'c': b'3'}

        It should send a Protobuf request with:
            - a paging controls with a count of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=0&count=2'
            - paging that matches the response with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(0, 4)
        leaves = Mocks.make_leaves(d=b'4', c=b'3')
        self.connection.preset_response(head_id='d',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200('/state?count=2')
        controls = Mocks.make_paging_controls(2)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=2&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
예제 #8
0
    async def test_block_list_paginated_without_count(self):
        """Verifies GET /blocks paginated without count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 2, and 4 total resources
            - two blocks with the ids 'b' and 'a'

        It should send a Protobuf request with:
            - paging controls with a start_index of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/blocks?head=d&min=2&count=2'
            - paging that matches the response, with a previous link
            - a data property that is a list of 2 dicts
            - and those dicts are full blocks with ids 'd' and 'c'
        """
        paging = Mocks.make_paging_response(2, 4)
        blocks = Mocks.make_blocks('b', 'a')
        self.stream.preset_response(head_id='d', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?min=2')
        controls = Mocks.make_paging_controls(None, start_index=2)
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/blocks?head=d&min=2')
        self.assert_has_valid_paging(
            response, paging, previous_link='/blocks?head=d&min=0&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_blocks_well_formed(response['data'], 'b', 'a')
예제 #9
0
    async def test_block_list(self):
        """Verifies a GET /blocks without parameters works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three blocks with ids '2', '1', and '0'

        It should send a Protobuf request with:
            - empty paging controls

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link property that ends in '/blocks?head=2'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full blocks with ids '2', '1', and '0'
        """
        paging = Mocks.make_paging_response(0, 3)
        blocks = Mocks.make_blocks('2', '1', '0')
        self.stream.preset_response(head_id='2', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks')
        controls = Mocks.make_paging_controls()
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/blocks?head=2')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_blocks_well_formed(response['data'], '2', '1', '0')
예제 #10
0
    async def test_block_list_paginated_by_max_index(self):
        """Verifies GET /blocks paginated by a max index works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - three blocks with the ids 'd', 'c' and 'b'

        It should send a Protobuf request with:
            - paging controls with a count of 3, and an start_index of 0

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/blocks?head=d&min=3&count=7'
            - paging that matches the response, with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are full blocks with ids 'd', 'c', and 'b'
        """
        paging = Mocks.make_paging_response(0, 4)
        blocks = Mocks.make_blocks('d', 'c', 'b')
        self.stream.preset_response(head_id='d', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?max=2&count=7')
        controls = Mocks.make_paging_controls(3, start_index=0)
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/blocks?head=d&max=2&count=7')
        self.assert_has_valid_paging(response, paging,
                                     '/blocks?head=d&min=3&count=7')
        self.assert_has_valid_data_list(response, 3)
        self.assert_blocks_well_formed(response['data'], 'd', 'c', 'b')
    async def test_state_list_paginated_by_max_index(self):
        """Verifies GET /state paginated by a max index works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - three leaves with the ids {'d': b'4'}, {'c': b'3'} and {'b': b'2'}

        It should send a Protobuf request with:
            - a paging controls with a count of 2 and an start_index of 0

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=3&count=7'
            - paging that matches the response, with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(0, 4)
        leaves = Mocks.make_leaves(d=b'4', c=b'3', b=b'2')
        self.connection.preset_response(head_id='d', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?max=2&count=7')
        controls = Mocks.make_paging_controls(3, start_index=0)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&max=2&count=7')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=3&count=7')
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
    async def test_state_list_paginated_without_count(self):
        """Verifies GET /state paginated without count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 2, and 4 total resources
            - two leaves of {'b': b'2'} and {'a': b'1'}

        It should send a Protobuf request with:
            - a paging controls with a start_index of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=2&count=2'
            - paging that matches the response, with a previous link
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(2, 4)
        leaves = Mocks.make_leaves(b=b'2', a=b'1')
        self.connection.preset_response(head_id='d', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?min=2')
        controls = Mocks.make_paging_controls(None, start_index=2)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&min=2')
        self.assert_has_valid_paging(response, paging,
                                     previous_link='/state?head=d&min=0&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
    async def test_state_list(self):
        """Verifies a GET /state without parameters works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three leaves with addresses/data of:
                * 'a': b'3'
                * 'b': b'5'
                * 'c': b'7'

        It should send a Protobuf request with:
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2'
            - a link property that ends in '/state?head=2&min=0&count=3'
            - a paging property that matches the paging response
            - a data property that is a list of 3 leaf dicts
            - three leaves that match those in Protobuf response
        """
        paging = Mocks.make_paging_response(0, 3)
        leaves = Mocks.make_leaves(a=b'3', b=b'5', c=b'7')
        self.connection.preset_response(head_id='2', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
    async def test_state_list_paginated_with_just_count(self):
        """Verifies GET /state paginated just by count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - two leaves of {'d': b'4'}, and {'c': b'3'}

        It should send a Protobuf request with:
            - a paging controls with a count of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=0&count=2'
            - paging that matches the response with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(0, 4)
        leaves = Mocks.make_leaves(d=b'4', c=b'3')
        self.connection.preset_response(head_id='d', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?count=2')
        controls = Mocks.make_paging_controls(2)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=2&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
예제 #15
0
    async def test_batch_list_with_head(self):
        """Verifies a GET /batches with a head parameter works properly.

        It will receive a Protobuf response with:
            - a head id of '1'
            - a paging response with a start of 0, and 2 total resources
            - two batches with ids of 1' and '0'

        It should send a Protobuf request with:
            - a head_id property of '1'
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/batches?head=1'
            - a paging property that matches the paging response
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids '1' and '0'
        """
        paging = Mocks.make_paging_response(0, 2)
        batches = Mocks.make_batches('1', '0')
        self.stream.preset_response(head_id='1',
                                    paging=paging,
                                    batches=batches)

        response = await self.get_assert_200('/batches?head=1')
        controls = Mocks.make_paging_controls()
        self.stream.assert_valid_request_sent(head_id='1', paging=controls)

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/batches?head=1')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 2)
        self.assert_batches_well_formed(response['data'], '1', '0')
예제 #16
0
    async def test_state_list_with_address(self):
        """Verifies a GET /state works properly filtered by address.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 1 total resource
            - one leaf with addresses/data of: 'c': b'7'

        It should send a Protobuf request with:
            - an address property of 'c'
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2'
            - a link property that ends in '/state?head=2&min=0&count=1&address=c'
            - a paging property that matches the paging response
            - a data property that is a list of 1 leaf dict
            - one leaf that matches the Protobuf response
        """
        paging = Mocks.make_paging_response(0, 1)
        leaves = Mocks.make_leaves(c=b'7')
        self.connection.preset_response(head_id='2',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200('/state?address=c')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(address='c', paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2&address=c')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 1)
        self.assert_leaves_match(leaves, response['data'])
예제 #17
0
    async def test_batch_list_paginated_with_just_count(self):
        """Verifies GET /batches paginated just by count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - two batches with the ids 'd' and 'c'

        It should send a Protobuf request with:
            - paging controls with a count of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/batches?head=d&count=2'
            - paging that matches the response with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids 'd' and 'c'
        """
        paging = Mocks.make_paging_response(0, 4)
        batches = Mocks.make_batches('d', 'c')
        self.stream.preset_response(head_id='d',
                                    paging=paging,
                                    batches=batches)

        response = await self.get_assert_200('/batches?count=2')
        controls = Mocks.make_paging_controls(2)
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/batches?head=d&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/batches?head=d&min=2&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_batches_well_formed(response['data'], 'd', 'c')
예제 #18
0
    async def test_block_list_with_head_and_ids(self):
        """Verifies GET /blocks with head and id parameters work properly.

        It will receive a Protobuf response with:
            - a head id of '1'
            - a paging response with a start of 0, and 1 total resource
            - one block with an id of '0'

        It should send a Protobuf request with:
            - a head_id property of '1'
            - a block_ids property of ['0']
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/blocks?head=1&id=0'
            - a paging property that matches the paging response
            - a data property that is a list of 1 dict
            - and that dict is a full block with an id of '0'
        """
        paging = Mocks.make_paging_response(0, 1)
        blocks = Mocks.make_blocks('0')
        self.stream.preset_response(head_id='1', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?id=0&head=1')
        self.stream.assert_valid_request_sent(
            head_id='1', block_ids=['0'], paging=Mocks.make_paging_controls())

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/blocks?head=1&id=0')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 1)
        self.assert_blocks_well_formed(response['data'], '0')
    async def test_batch_list_paginated(self):
        """Verifies GET /batches paginated by min id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 1, and 4 total resources
            - one batch with the id 'c'

        It should send a Protobuf request with:
            - paging controls with a count of 1, and a start_index of 1

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/batches?head=d&min=1&count=1'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 1 dict
            - and that dict is a full batch with the id 'c'
        """
        paging = Mocks.make_paging_response(1, 4)
        batches = Mocks.make_batches('c')
        self.connection.preset_response(head_id='d', paging=paging, batches=batches)

        response = await self.get_assert_200('/batches?min=1&count=1')
        controls = Mocks.make_paging_controls(1, start_index=1)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/batches?head=d&min=1&count=1')
        self.assert_has_valid_paging(response, paging,
                                     '/batches?head=d&min=2&count=1',
                                     '/batches?head=d&min=0&count=1')
        self.assert_has_valid_data_list(response, 1)
        self.assert_batches_well_formed(response['data'], 'c')
    async def test_state_list_with_address(self):
        """Verifies a GET /state works properly filtered by address.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 1 total resource
            - one leaf with addresses/data of: 'c': b'7'

        It should send a Protobuf request with:
            - an address property of 'c'
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2'
            - a link property that ends in '/state?head=2&min=0&count=1&address=c'
            - a paging property that matches the paging response
            - a data property that is a list of 1 leaf dict
            - one leaf that matches the Protobuf response
        """
        paging = Mocks.make_paging_response(0, 1)
        leaves = Mocks.make_leaves(c=b'7')
        self.connection.preset_response(head_id='2', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?address=c')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(address='c', paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2&address=c')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 1)
        self.assert_leaves_match(leaves, response['data'])
    async def test_batch_list_paginated_by_max_index(self):
        """Verifies GET /batches paginated by a max index works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - three batches with the ids 'd', 'c' and 'b'

        It should send a Protobuf request with:
            - paging controls with a count of 3, and an start_index of 0

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/batches?head=d&min=3&count=7'
            - paging that matches the response, with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids 'd', 'c', and 'b'
        """
        paging = Mocks.make_paging_response(0, 4)
        batches = Mocks.make_batches('d', 'c', 'b')
        self.connection.preset_response(head_id='d', paging=paging, batches=batches)

        response = await self.get_assert_200('/batches?max=2&count=7')
        controls = Mocks.make_paging_controls(3, start_index=0)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/batches?head=d&max=2&count=7')
        self.assert_has_valid_paging(response, paging,
                                     '/batches?head=d&min=3&count=7')
        self.assert_has_valid_data_list(response, 3)
        self.assert_batches_well_formed(response['data'], 'd', 'c', 'b')
    async def test_state_list_paginated(self):
        """Verifies GET /state paginated by min id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 1, and 4 total resources
            - one leaf of {'c': b'3'}

        It should send a Protobuf request with:
            - a paging controls with a count of 1, and a start_index of 1

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&min=1&count=1'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 1 dict
            - and that dict is a leaf that matches the one received
        """
        paging = Mocks.make_paging_response(1, 4)
        leaves = Mocks.make_leaves(c=b'3')
        self.connection.preset_response(head_id='d', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?min=1&count=1')
        controls = Mocks.make_paging_controls(1, start_index=1)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&min=1&count=1')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=2&count=1',
                                     '/state?head=d&min=0&count=1')
        self.assert_has_valid_data_list(response, 1)
        self.assert_leaves_match(leaves, response['data'])
    async def test_block_list_paginated_with_just_count(self):
        """Verifies GET /blocks paginated just by count works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with a start of 0, and 4 total resources
            - two blocks with the ids 'd' and 'c'

        It should send a Protobuf request with:
            - paging controls with a count of 2

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/blocks?head=d'
            - paging that matches the response with a next link
            - a data property that is a list of 2 dicts
            - and those dicts are full blocks with ids 'd' and 'c'
        """
        paging = Mocks.make_paging_response(0, 4)
        blocks = Mocks.make_blocks('d', 'c')
        self.connection.preset_response(head_id='d', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?count=2')
        controls = Mocks.make_paging_controls(2)
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/blocks?head=d&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/blocks?head=d&min=2&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_blocks_well_formed(response['data'], 'd', 'c')
    async def test_block_list(self):
        """Verifies a GET /blocks without parameters works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three blocks with ids '2', '1', and '0'

        It should send a Protobuf request with:
            - empty paging controls

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link property that ends in '/blocks?head=2'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full blocks with ids '2', '1', and '0'
        """
        paging = Mocks.make_paging_response(0, 3)
        blocks = Mocks.make_blocks('2', '1', '0')
        self.connection.preset_response(head_id='2', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/blocks?head=2')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_blocks_well_formed(response['data'], '2', '1', '0')
    async def test_block_list_with_ids(self):
        """Verifies GET /blocks with an id filter works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 2 total resources
            - two blocks with ids '0' and '2'

        It should send a Protobuf request with:
            - a block_ids property of ['0', '2']
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2', the latest
            - a link property that ends in '/blocks?head=2&id=0,2'
            - a paging property that matches the paging response
            - a data property that is a list of 2 dicts
            - and those dicts are full blocks with ids '0' and '2'
        """
        paging = Mocks.make_paging_response(0, 2)
        blocks = Mocks.make_blocks('0', '2')
        self.connection.preset_response(head_id='2', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?id=0,2')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(block_ids=['0', '2'], paging=controls)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/blocks?head=2&id=0,2')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 2)
        self.assert_blocks_well_formed(response['data'], '0', '2')
    async def test_state_list_with_head(self):
        """Verifies a GET /state works properly with head specified.

        It will receive a Protobuf response with:
            - a head id of '1'
            - two leaves with addresses/data of:
                * 'a': b'2'
                * 'b': b'4'

        It should send a Protobuf request with:
            - a head_id property of '1'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/state?head=1'
            - a data property that is a list of 2 leaf dicts
            - three leaves that match those in Protobuf response
        """
        leaves = Mocks.make_leaves(a=b'2', b=b'4')
        self.stream.preset_response(head_id='1', leaves=leaves)

        response = await self.get_json_assert_200('/state?head=1')
        self.stream.assert_valid_request_sent(head_id='1')

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/state?head=1')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
    async def test_state_list(self):
        """Verifies a GET /state without parameters works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - three leaves with addresses/data of:
                * 'a': b'3'
                * 'b': b'5'
                * 'c': b'7'

        It should send an empty Protobuf request of the correct type

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2'
            - a link property that ends in '/state?head=2'
            - a data property that is a list of 3 leaf dicts
            - three leaves that match those in Protobuf response
        """
        leaves = Mocks.make_leaves(a=b'3', b=b'5', c=b'7')
        self.stream.preset_response(head_id='2', leaves=leaves)

        response = await self.get_json_assert_200('/state')
        self.stream.assert_valid_request_sent()

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2')
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
    async def test_state_list_with_head_and_address(self):
        """Verifies GET /state works with a head and filtered by address.

        It will receive a Protobuf response with:
            - a head id of '1'
            - one leaf with addresses/data of: 'a': b'2'

        It should send a Protobuf request with:
            - a head_id property of '1'
            - an address property of 'a'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/state?head=1&address=a'
            - a data property that is a list of 1 leaf dict
            - one leaf that matches the Protobuf response
        """
        leaves = Mocks.make_leaves(a=b'2')
        self.stream.preset_response(head_id='1', leaves=leaves)

        response = await self.get_json_assert_200('/state?address=a&head=1')
        self.stream.assert_valid_request_sent(head_id='1', address='a')

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/state?head=1&address=a')
        self.assert_has_valid_data_list(response, 1)
        self.assert_leaves_match(leaves, response['data'])
예제 #29
0
    async def test_block_list_with_ids(self):
        """Verifies GET /blocks with the id parameter works properly.

        It should send a Protobuf request with:
            - a block_ids property of ['0', '2']

        It will receive a Protobuf response with:
            - a head id of '2'
            - two blocks with ids '0' and '2'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2', the latest
            - a link property that ends in '/blocks?head=2&id=0,2'
            - a data property that is a list of 2 dicts
            - and those dicts are full blocks with ids '0' and '2'
        """
        blocks = Mocks.make_blocks('0', '2')
        self.stream.preset_response(head_id='2', blocks=blocks)

        response = await self.get_json_assert_200('/blocks?id=0,2')
        self.stream.assert_valid_request_sent(block_ids=['0', '2'])

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/blocks?head=2&id=0,2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_blocks_well_formed(response['data'], '0', '2')
    async def test_state_list_with_bad_address(self):
        """Verifies a GET /state breaks properly filtered by a bad address.

        It will receive a Protobuf response with:
            - a status of NO_RESOURCE
            - a head id of '2'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2'
            - a link property that ends in '/state?head=2&address=bad'
            - a paging property with only a total_count of 0
            - a data property that is an empty list
        """
        paging = Mocks.make_paging_response(None, 0)
        self.stream.preset_response(
            self.status.NO_RESOURCE,
            head_id='2',
            paging=paging)
        response = await self.get_json_assert_200('/state?address=bad')

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2&address=bad')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 0)
예제 #31
0
    async def test_batch_list_with_head_and_ids(self):
        """Verifies GET /batches with head and id parameters works properly.

        It should send a Protobuf request with:
            - a head_id property of '1'
            - a batch_ids property of ['0']

        It will receive a Protobuf response with:
            - a head id of '1'
            - one batch with an id of '0'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/batches?head=1&id=0'
            - a data property that is a list of 1 dict
            - and that dict is a full batch with an id of '0'
        """
        batches = Mocks.make_batches('0')
        self.stream.preset_response(head_id='1', batches=batches)

        response = await self.get_json_assert_200('/batches?id=0&head=1')
        self.stream.assert_valid_request_sent(head_id='1', batch_ids=['0'])

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/batches?head=1&id=0')
        self.assert_has_valid_data_list(response, 1)
        self.assert_batches_well_formed(response['data'], '0')
예제 #32
0
    async def test_post_batch_with_wait(self):
        """Verifies a POST /batches can wait for commit properly.

        It will receive a Protobuf response with:
            - batch statuses of {'a': COMMITTED}

        It should send a Protobuf request with:
            - a batches property that matches the batches sent
            - a wait_for_commit property that is True
            - a timeout property of 4 (Rest Api default)

        It should send back a JSON response with:
            - a response status of 201
            - no data property
            - a link property that ends in '/batches?id=a'
        """
        batches = Mocks.make_batches('a')
        statuses = [BatchStatus(batch_id='a', status=BatchStatus.COMMITTED)]
        self.connection.preset_response(batch_statuses=statuses)

        request = await self.post_batches(batches, wait=True)
        self.connection.assert_valid_request_sent(batches=batches,
                                                  wait_for_commit=True,
                                                  timeout=4)
        self.assertEqual(201, request.status)

        response = await request.json()
        self.assert_has_valid_link(response, '/batches?id=a')
        self.assertNotIn('data', response)
예제 #33
0
    async def test_post_batch_with_timeout(self):
        """Verifies a POST /batches works when timed out while waiting.

        It will receive a Protobuf response with:
            - batch statuses of {'pending': PENDING}

        It should send a Protobuf request with:
            - a batches property that matches the batches sent
            - a wait_for_commit 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_status?id=pending'
            - a data property matching the batch statuses received
        """
        batches = Mocks.make_batches('pending')
        statuses = [
            BatchStatus(batch_id='pending', status=BatchStatus.PENDING)
        ]
        self.connection.preset_response(batch_statuses=statuses)

        request = await self.post_batches(batches, wait=True)
        self.connection.assert_valid_request_sent(batches=batches,
                                                  wait_for_commit=True,
                                                  timeout=4)
        self.assertEqual(202, request.status)

        response = await request.json()
        self.assert_has_valid_link(response, '/batch_status?id=pending&wait')
        self.assert_statuses_match(statuses, response['data'])
예제 #34
0
    async def test_batch_get(self):
        """Verifies a GET /batches/{batch_id} works properly.

        It should send a Protobuf request with:
            - a head_id property of '1'
            - a block_ids property of ['0']

        It will receive a Protobuf response with:
            - a head id of '1'
            - three batches with ids of '2', '1', and '0'

        It should send back a JSON response with:
            - a response status of 200
            - no head property
            - a link property that ends in '/batches/1'
            - a data property that is a full batch with an id of '1'
        """
        self.stream.preset_response(batch=Mocks.make_batches('1')[0])

        response = await self.get_json_assert_200('/batches/1')
        self.stream.assert_valid_request_sent(batch_id='1')

        self.assertNotIn('head', response)
        self.assert_has_valid_link(response, '/batches/1')
        self.assertIn('data', response)
        self.assert_batches_well_formed(response['data'], '1')
예제 #35
0
    async def test_batch_list(self):
        """Verifies a GET /batches without parameters works properly.

        It will receive a Protobuf response with:
            - a head id of '2'
            - three batches with ids of '2', '1', and '0'

        It should send an empty Protobuf request of the correct type

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2'
            - a link property that ends in '/batches?head=2'
            - a data property that is a list of 3 dicts
            - and those dicts are full batches with ids '2', '1', and '0'
        """
        batches = Mocks.make_batches('2', '1', '0')
        self.stream.preset_response(head_id='2', batches=batches)

        response = await self.get_json_assert_200('/batches')
        self.stream.assert_valid_request_sent()

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/batches?head=2')
        self.assert_has_valid_data_list(response, 3)
        self.assert_batches_well_formed(response['data'], '2', '1', '0')
예제 #36
0
    async def test_batch_list_with_head(self):
        """Verifies a GET /batches with a head parameter works properly.

        It will receive a Protobuf response with:
            - a head id of '1'
            - two batches with ids of 1' and '0'

        It should send a Protobuf request with:
            - a head_id property of '1'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/batches?head=1'
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids '1' and '0'
        """
        batches = Mocks.make_batches('1', '0')
        self.stream.preset_response(head_id='1', batches=batches)

        response = await self.get_json_assert_200('/batches?head=1')
        self.stream.assert_valid_request_sent(head_id='1')

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/batches?head=1')
        self.assert_has_valid_data_list(response, 2)
        self.assert_batches_well_formed(response['data'], '1', '0')
예제 #37
0
    async def test_txn_list_with_bad_ids(self):
        """Verifies GET /transactions with a bad id filter breaks properly.

        It will receive a Protobuf response with:
            - a status of NO_RESOURCE
            - a head id of '2'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2', the latest
            - a link property that ends in '/transactions?head=2&id=bad,notgood'
            - a paging property with only a total_count of 0
            - a data property that is an empty list
        """
        paging = Mocks.make_paging_response(None, 0)
        self.stream.preset_response(self.status.NO_RESOURCE,
                                    head_id='2',
                                    paging=paging)
        response = await self.get_json_assert_200(
            '/transactions?id=bad,notgood')

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
                                   '/transactions?head=2&id=bad,notgood')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 0)
    async def test_batch_list_with_bad_ids(self):
        """Verifies GET /batches with a bad id filter breaks properly.

        It will receive a Protobuf response with:
            - a status of NO_RESOURCE
            - a head id of '2'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '2', the latest
            - a link property that ends in '/batches?head=2&id=bad,notgood'
            - a paging property with only a total_count of 0
            - a data property that is an empty list
        """
        paging = Mocks.make_paging_response(None, 0)
        self.connection.preset_response(
            self.status.NO_RESOURCE,
            head_id='2',
            paging=paging)
        response = await self.get_assert_200('/batches?id=bad,notgood')

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/batches?head=2&id=bad,notgood')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 0)
예제 #39
0
    async def test_state_list_sorted_by_many_keys(self):
        """Verifies a GET /state can send proper sort parameters.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three leaves with addresses/data of:
                * 'c': b'7'
                * 'b': b'5'
                * 'a': b'3'

        It should send a Protobuf request with:
            - empty paging controls
            - multiple sort controls with:
                * a key of 'address' that is reversed
                * a key of 'value' that is sorted by length

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - link with '/state?head=2&sort=-address,value.length'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - three leaves that match those in Protobuf response
        """
        paging = Mocks.make_paging_response(0, 3)
        leaves = Mocks.make_leaves(c=b'7', b=b'5', a=b'3')
        self.connection.preset_response(head_id='2',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200(
            '/state?sort=-address,value.length')
        page_controls = Mocks.make_paging_controls()
        sorting = (Mocks.make_sort_controls('address', reverse=True) +
                   Mocks.make_sort_controls('value', compare_length=True))
        self.connection.assert_valid_request_sent(paging=page_controls,
                                                  sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
                                   '/state?head=2&sort=-address,value.length')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
예제 #40
0
    async def test_batch_list_sorted_by_many_keys(self):
        """Verifies a GET /batches can send proper sort parameters.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three batches with ids '2', '1', and '0'

        It should send a Protobuf request with:
            - empty paging controls
            - multiple sort controls with:
                * a key of 'header_signature' that is reversed
                * a key of 'transactions' that is sorted by length

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - link with '/batches?head=2&sort=-header_signature,transactions.length'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full batches with ids '2', '1', and '0'
        """
        paging = Mocks.make_paging_response(0, 3)
        batches = Mocks.make_batches('2', '1', '0')
        self.stream.preset_response(head_id='2',
                                    paging=paging,
                                    batches=batches)

        response = await self.get_assert_200(
            '/batches?sort=-header_signature,transactions.length')
        page_controls = Mocks.make_paging_controls()
        sorting = (
            Mocks.make_sort_controls('header_signature', reverse=True) +
            Mocks.make_sort_controls('transactions', compare_length=True))
        self.stream.assert_valid_request_sent(paging=page_controls,
                                              sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(
            response,
            '/batches?head=2&sort=-header_signature,transactions.length')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_batches_well_formed(response['data'], '2', '1', '0')
    async def test_state_list_sorted_by_many_keys(self):
        """Verifies a GET /state can send proper sort parameters.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three leaves with addresses/data of:
                * 'c': b'7'
                * 'b': b'5'
                * 'a': b'3'

        It should send a Protobuf request with:
            - empty paging controls
            - multiple sort controls with:
                * a key of 'address' that is reversed
                * a key of 'value' that is sorted by length

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - link with '/state?head=2&sort=-address,value.length'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - three leaves that match those in Protobuf response
        """
        paging = Mocks.make_paging_response(0, 3)
        leaves = Mocks.make_leaves(c=b'7', b=b'5', a=b'3')
        self.connection.preset_response(head_id='2', paging=paging, leaves=leaves)

        response = await self.get_assert_200(
            '/state?sort=-address,value.length')
        page_controls = Mocks.make_paging_controls()
        sorting = (Mocks.make_sort_controls('address', reverse=True) +
                   Mocks.make_sort_controls('value', compare_length=True))
        self.connection.assert_valid_request_sent(
            paging=page_controls,
            sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
            '/state?head=2&sort=-address,value.length')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
예제 #42
0
    async def test_txn_list_paginated_by_min_id(self):
        """Verifies GET /transactions paginated by a min id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with:
                * a start_index of 1
                * total_resources of 4
                * a previous_id of 'd'
            - three transactions with the ids 'c', 'b' and 'a'

        It should send a Protobuf request with:
            - paging controls with a count of 5, and a start_id of 'c'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/transactions?head=d&min=c&count=5'
            - paging that matches the response, with a previous link
            - a data property that is a list of 3 dicts
            - those dicts are full transactions with ids 'c', 'b', and 'a'
        """
        paging = Mocks.make_paging_response(1, 4, previous_id='d')
        self.stream.preset_response(head_id='d',
                                    paging=paging,
                                    transactions=Mocks.make_txns(
                                        'c', 'b', 'a'))

        response = await self.get_json_assert_200('/transactions?min=c&count=5'
                                                  )
        controls = Mocks.make_paging_controls(5, start_id='c')
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response,
                                   '/transactions?head=d&min=c&count=5')
        self.assert_has_valid_paging(
            response,
            paging,
            previous_link='/transactions?head=d&max=d&count=5')
        self.assert_has_valid_data_list(response, 3)
        self.assert_txns_well_formed(response['data'], 'c', 'b', 'a')
    async def test_batch_list_sorted_by_many_keys(self):
        """Verifies a GET /batches can send proper sort parameters.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three batches with ids '2', '1', and '0'

        It should send a Protobuf request with:
            - empty paging controls
            - multiple sort controls with:
                * a key of 'header_signature' that is reversed
                * a key of 'transactions' that is sorted by length

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - link with '/batches?head=2&sort=-header_signature,transactions.length'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full batches with ids '2', '1', and '0'
        """
        paging = Mocks.make_paging_response(0, 3)
        batches = Mocks.make_batches('2', '1', '0')
        self.connection.preset_response(head_id='2', paging=paging, batches=batches)

        response = await self.get_assert_200(
            '/batches?sort=-header_signature,transactions.length')
        page_controls = Mocks.make_paging_controls()
        sorting = (Mocks.make_sort_controls('header_signature', reverse=True) +
                   Mocks.make_sort_controls('transactions', compare_length=True))
        self.connection.assert_valid_request_sent(
            paging=page_controls,
            sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
            '/batches?head=2&sort=-header_signature,transactions.length')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_batches_well_formed(response['data'], '2', '1', '0')
예제 #44
0
    async def test_txn_list_paginated_by_max_id(self):
        """Verifies GET /transactions paginated by a max id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with:
                * a start_index of 1
                * a total_resources of 4
                * a previous_id of 'd'
                * a next_id of 'a'
            - two transactions with the ids 'c' and 'b'

        It should send a Protobuf request with:
            - paging controls with a count of 2, and an end_id of 'b'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/transactions?head=d&max=b&count=2'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 2 dicts
            - those dicts are full transactions with ids 'c' and 'b'
        """
        paging = Mocks.make_paging_response(1, 4, previous_id='d', next_id='a')
        self.connection.preset_response(
            head_id='d',
            paging=paging,
            transactions=Mocks.make_txns('c', 'b'))

        response = await self.get_assert_200('/transactions?max=b&count=2')
        controls = Mocks.make_paging_controls(2, end_id='b')
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/transactions?head=d&max=b&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/transactions?head=d&min=a&count=2',
                                     '/transactions?head=d&max=d&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_txns_well_formed(response['data'], 'c', 'b')
예제 #45
0
    async def test_batch_list_paginated_by_max_id(self):
        """Verifies GET /batches paginated by a max id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with:
                * a start_index of 1
                * a total_resources of 4
                * a previous_id of 'd'
                * a next_id of 'a'
            - two batches with the ids 'c' and 'b'

        It should send a Protobuf request with:
            - a paging controls with a count of 2 and an end_id of 'b'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/batches?head=d&max=b&count=2'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 2 dicts
            - and those dicts are full batches with ids 'c' and 'b'
        """
        paging = Mocks.make_paging_response(1, 4, previous_id='d', next_id='a')
        batches = Mocks.make_batches('c', 'b')
        self.stream.preset_response(head_id='d',
                                    paging=paging,
                                    batches=batches)

        response = await self.get_json_assert_200('/batches?max=b&count=2')
        controls = Mocks.make_paging_controls(2, end_id='b')
        self.stream.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/batches?head=d&max=b&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/batches?head=d&min=a&count=2',
                                     '/batches?head=d&max=d&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_batches_well_formed(response['data'], 'c', 'b')
예제 #46
0
    async def test_state_list_sorted(self):
        """Verifies GET /state can send proper sort controls.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three leaves with addresses/data of:
                * 'a': b'3'
                * 'b': b'5'
                * 'c': b'7'

        It should send a Protobuf request with:
            - empty paging controls
            - sort controls with a key of 'address'

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link property ending in '/state?head=2&sort=address'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - three leaves that match those in Protobuf response
        """
        paging = Mocks.make_paging_response(0, 3)
        leaves = Mocks.make_leaves(a=b'3', b=b'5', c=b'7')
        self.connection.preset_response(head_id='2',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200('/state?sort=address')
        page_controls = Mocks.make_paging_controls()
        sorting = Mocks.make_sort_controls('address')
        self.connection.assert_valid_request_sent(paging=page_controls,
                                                  sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2&sort=address')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
예제 #47
0
    async def test_state_list_paginated_by_max_id(self):
        """Verifies GET /state paginated by a max id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with:
                * a start_index of 1
                * a total_resources of 4
                * a previous_id of 'd'
                * a next_id of 'a'
            - two leaves of {'c': b'3'} and {'b': b'3'}

        It should send a Protobuf request with:
            - a paging controls with a count of 2 and an end_id of 'b'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&max=b&count=2'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(1, 4, previous_id='d', next_id='a')
        leaves = Mocks.make_leaves(c=b'3', b=b'2')
        self.connection.preset_response(head_id='d',
                                        paging=paging,
                                        leaves=leaves)

        response = await self.get_assert_200('/state?max=b&count=2')
        controls = Mocks.make_paging_controls(2, end_id='b')
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&max=b&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=a&count=2',
                                     '/state?head=d&max=d&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
예제 #48
0
    async def test_txn_list_with_head_and_ids(self):
        """Verifies GET /transactions with head and id parameters work properly.

        It should send a Protobuf request with:
            - a head_id property of '1'
            - a paging response with a start of 0, and 1 total resource
            - a transaction_ids property of ['0']

        It will receive a Protobuf response with:
            - a head id of '1'
            - one transaction with an id of '0'
            - empty paging controls

        It should send back a JSON response with:
            - a response status of 200
            - a head property of '1'
            - a link property that ends in '/transactions?head=1&id=0'
            - a paging property that matches the paging response
            - a data property that is a list of 1 dict
            - that dict is a full transaction with an id of '0'
        """
        paging = Mocks.make_paging_response(0, 1)
        self.connection.preset_response(
            head_id='1',
            paging=paging,
            transactions=Mocks.make_txns('0'))

        response = await self.get_assert_200('/transactions?id=0&head=1')
        controls = Mocks.make_paging_controls()
        self.connection.assert_valid_request_sent(
            head_id='1',
            transaction_ids=['0'],
            paging=controls)

        self.assert_has_valid_head(response, '1')
        self.assert_has_valid_link(response, '/transactions?head=1&id=0')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 1)
        self.assert_txns_well_formed(response['data'], '0')
    async def test_state_list_sorted(self):
        """Verifies GET /state can send proper sort controls.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three leaves with addresses/data of:
                * 'a': b'3'
                * 'b': b'5'
                * 'c': b'7'

        It should send a Protobuf request with:
            - empty paging controls
            - sort controls with a key of 'address'

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link property ending in '/state?head=2&sort=address'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - three leaves that match those in Protobuf response
        """
        paging = Mocks.make_paging_response(0, 3)
        leaves = Mocks.make_leaves(a=b'3', b=b'5', c=b'7')
        self.connection.preset_response(head_id='2', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?sort=address')
        page_controls = Mocks.make_paging_controls()
        sorting = Mocks.make_sort_controls('address')
        self.connection.assert_valid_request_sent(
            paging=page_controls,
            sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response, '/state?head=2&sort=address')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_leaves_match(leaves, response['data'])
    async def test_batch_list_sorted_with_nested_keys(self):
        """Verifies GET /batches can send proper sort controls with nested keys.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three batches with ids '0', '1', and '2'

        It should send a Protobuf request with:
            - empty paging controls
            - sort controls with keys of 'header' and 'signer_pubkey'

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link ending in '/batches?head=2&sort=header.signer_pubkey'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full batches with ids '0', '1', and '2'
        """
        paging = Mocks.make_paging_response(0, 3)
        batches = Mocks.make_batches('0', '1', '2')
        self.connection.preset_response(head_id='2', paging=paging, batches=batches)

        response = await self.get_assert_200(
            '/batches?sort=header.signer_pubkey')
        page_controls = Mocks.make_paging_controls()
        sorting = Mocks.make_sort_controls('header', 'signer_pubkey')
        self.connection.assert_valid_request_sent(
            paging=page_controls,
            sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
            '/batches?head=2&sort=header.signer_pubkey')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_batches_well_formed(response['data'], '0', '1', '2')
    async def test_state_list_paginated_by_max_id(self):
        """Verifies GET /state paginated by a max id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with:
                * a start_index of 1
                * a total_resources of 4
                * a previous_id of 'd'
                * a next_id of 'a'
            - two leaves of {'c': b'3'} and {'b': b'3'}

        It should send a Protobuf request with:
            - a paging controls with a count of 2 and an end_id of 'b'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/state?head=d&max=b&count=2'
            - paging that matches the response, with next and previous links
            - a data property that is a list of 2 dicts
            - and those dicts are leaves that match those received
        """
        paging = Mocks.make_paging_response(1, 4, previous_id='d', next_id='a')
        leaves = Mocks.make_leaves(c=b'3', b=b'2')
        self.connection.preset_response(head_id='d', paging=paging, leaves=leaves)

        response = await self.get_assert_200('/state?max=b&count=2')
        controls = Mocks.make_paging_controls(2, end_id='b')
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/state?head=d&max=b&count=2')
        self.assert_has_valid_paging(response, paging,
                                     '/state?head=d&min=a&count=2',
                                     '/state?head=d&max=d&count=2')
        self.assert_has_valid_data_list(response, 2)
        self.assert_leaves_match(leaves, response['data'])
    async def test_block_list_sorted_in_reverse(self):
        """Verifies a GET /blocks can send proper sort parameters.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three blocks with ids '2', '1', and '0'

        It should send a Protobuf request with:
            - empty paging controls
            - sort controls with a key of 'header_signature' that is reversed

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link property ending in '/blocks?head=2&sort=-header_signature'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full blocks with ids '2', '1', and '0'
        """
        paging = Mocks.make_paging_response(0, 3)
        blocks = Mocks.make_blocks('2', '1', '0')
        self.connection.preset_response(head_id='2', paging=paging, blocks=blocks)

        response = await self.get_assert_200('/blocks?sort=-header_signature')
        page_controls = Mocks.make_paging_controls()
        sorting = Mocks.make_sort_controls(
            'header_signature', reverse=True)
        self.connection.assert_valid_request_sent(
            paging=page_controls,
            sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
            '/blocks?head=2&sort=-header_signature')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_blocks_well_formed(response['data'], '2', '1', '0')
예제 #53
0
    async def test_txn_list_sorted_by_length(self):
        """Verifies a GET /transactions can send proper sort parameters.

        It will receive a Protobuf response with:
            - a head id of '2'
            - a paging response with a start of 0, and 3 total resources
            - three transactions with ids '0', '1', and '2'

        It should send a Protobuf request with:
            - empty paging controls
            - sort controls with a key of 'payload' sorted by length

        It should send back a JSON response with:
            - a status of 200
            - a head property of '2'
            - a link property ending in '/transactions?head=2&sort=payload.length'
            - a paging property that matches the paging response
            - a data property that is a list of 3 dicts
            - and those dicts are full transactions with ids '0', '1', and '2'
        """
        paging = Mocks.make_paging_response(0, 3)
        transactions = Mocks.make_txns('0', '1', '2')
        self.connection.preset_response(head_id='2', paging=paging, transactions=transactions)

        response = await self.get_assert_200('/transactions?sort=payload.length')
        page_controls = Mocks.make_paging_controls()
        sorting = Mocks.make_sort_controls('payload', compare_length=True)
        self.connection.assert_valid_request_sent(
            paging=page_controls,
            sorting=sorting)

        self.assert_has_valid_head(response, '2')
        self.assert_has_valid_link(response,
            '/transactions?head=2&sort=payload.length')
        self.assert_has_valid_paging(response, paging)
        self.assert_has_valid_data_list(response, 3)
        self.assert_txns_well_formed(response['data'], '0', '1', '2')
    async def test_batch_list_paginated_by_min_id(self):
        """Verifies GET /batches paginated by a min id works properly.

        It will receive a Protobuf response with:
            - a head id of 'd'
            - a paging response with:
                * a start_index of 1
                * total_resources of 4
                * a previous_id of 'd'
            - three batches with the ids 'c', 'b' and 'a'

        It should send a Protobuf request with:
            - paging controls with a count of 5, and a start_id of 'c'

        It should send back a JSON response with:
            - a response status of 200
            - a head property of 'd'
            - a link property that ends in '/batches?head=d&min=c&count=5'
            - paging that matches the response, with a previous link
            - a data property that is a list of 3 dicts
            - and those dicts are full batches with ids 'c', 'b', and 'a'
        """
        paging = Mocks.make_paging_response(1, 4, previous_id='d')
        batches = Mocks.make_batches('c', 'b', 'a')
        self.connection.preset_response(head_id='d', paging=paging, batches=batches)

        response = await self.get_assert_200('/batches?min=c&count=5')
        controls = Mocks.make_paging_controls(5, start_id='c')
        self.connection.assert_valid_request_sent(paging=controls)

        self.assert_has_valid_head(response, 'd')
        self.assert_has_valid_link(response, '/batches?head=d&min=c&count=5')
        self.assert_has_valid_paging(response, paging,
                                     previous_link='/batches?head=d&max=d&count=5')
        self.assert_has_valid_data_list(response, 3)
        self.assert_batches_well_formed(response['data'], 'c', 'b', 'a')
    async def test_batch_get(self):
        """Verifies a GET /batches/{batch_id} works properly.

        It should send a Protobuf request with:
            - a batch_id property of '1'

        It will receive a Protobuf response with:
            - a batch with an id of '1'

        It should send back a JSON response with:
            - a response status of 200
            - no head property
            - a link property that ends in '/batches/1'
            - a data property that is a full batch with an id of '1'
        """
        self.connection.preset_response(batch=Mocks.make_batches('1')[0])

        response = await self.get_assert_200('/batches/1')
        self.connection.assert_valid_request_sent(batch_id='1')

        self.assertNotIn('head', response)
        self.assert_has_valid_link(response, '/batches/1')
        self.assertIn('data', response)
        self.assert_batches_well_formed(response['data'], '1')