async def test_block_list_paginated_by_start_id(self): """Verifies GET /blocks paginated by a start id works properly. It will receive a Protobuf response with: - a head id of ID_D - a paging response with start of 0x0003 and limit of 5 - three blocks with the ids ID_C, ID_B and ID_A It should send a Protobuf request with: - paging controls with a limit of 5, and a startof ID_C It should send back a JSON response with: - a response status of 200 - a head property of ID_D - a link property that ends in '/blocks?head={}&start=0x0003&limit=5'.format(ID_C) - paging that matches the response, with a previous link - a data property that is a list of 3 dicts - and those dicts are full blocks with ids ID_C, ID_B, and ID_A """ paging = Mocks.make_paging_response("", "0x0003", 5) blocks = Mocks.make_blocks(ID_C, ID_B, ID_A) self.connection.preset_response( head_id=ID_D, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?start=0x0003&limit=5') controls = Mocks.make_paging_controls(5, start="0x0003") self.connection.assert_valid_request_sent(paging=controls) self.assert_has_valid_head(response, ID_D) self.assert_has_valid_link( response, '/blocks?head={}&start=0x0003&limit=5'.format(ID_D)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 3) self.assert_blocks_well_formed(response['data'], ID_C, ID_B, ID_A)
async def test_block_list_paginated_without_limit(self): """Verifies GET /blocks paginated without limit works properly. It will receive a Protobuf response with: - a head id of ID_D - a paging response with start of 0x0002 and limit of 100 - two blocks with the ids ID_B and ID_A It should send a Protobuf request with: - paging controls with a start of 2 It should send back a JSON response with: - a response status of 200 - a head property of ID_D - a link property that ends in '/blocks?head={}&start=0x0002&limit=100'.format(ID_D) - 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 ID_D and ID_C """ paging = Mocks.make_paging_response("", "0x0002", DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_B, ID_A) self.connection.preset_response( head_id=ID_D, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?start=0x0002') controls = Mocks.make_paging_controls(None, start="0x0002") self.connection.assert_valid_request_sent(paging=controls) self.assert_has_valid_head(response, ID_D) self.assert_has_valid_link( response, '/blocks?head={}&start=0x0002&limit=100'.format(ID_D)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 2) self.assert_blocks_well_formed(response['data'], ID_B, ID_A)
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 ID_C - a paging reponse with start of ID_C and limit of 100 - three blocks with ids ID_C, ID_B, and ID_A 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 ID_C - a link property that ends in '/blocks?head={}&start={}&limit=100'.format(ID_C, ID_C)) - 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 ID_C, ID_B, and ID_A """ paging = Mocks.make_paging_response("", ID_C, DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_C, ID_B, ID_A) self.connection.preset_response( head_id=ID_C, 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, ID_C) self.assert_has_valid_link( response, '/blocks?head={}&start={}&limit=100'.format(ID_C, ID_C)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 3) self.assert_blocks_well_formed(response['data'], ID_C, ID_B, ID_A)
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 ID_B - an empty paging response - one block with an id of ID_A It should send a Protobuf request with: - a head_id property of ID_B - a block_ids property of [ID_A] - empty paging controls It should send back a JSON response with: - a response status of 200 - a head property of ID_B - a link property that ends in '/blocks?head={}&id={}'.format(ID_B, ID_A) - 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 ID_A """ paging = Mocks.make_paging_response("", ID_B, DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_A) self.connection.preset_response( head_id=ID_B, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?id={}&head={}'.format( ID_A, ID_B)) self.connection.assert_valid_request_sent( head_id=ID_B, block_ids=[ID_A], paging=Mocks.make_paging_controls()) self.assert_has_valid_head(response, ID_B) self.assert_has_valid_link( response, '/blocks?head={ID_B}&start={ID_B}&limit=100&id={ID_A}'.format( ID_B=ID_B, ID_A=ID_A)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 1) self.assert_blocks_well_formed(response['data'], ID_A)
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 ID_D - a paging response with a next of '0x0002', start of 0x0003 and limit of 1 - one block with the id ID_C It should send a Protobuf request with: - paging controls with a limit of 1, and a start_id of start='0x0003' It should send back a JSON response with: - a response status of 200 - a head property of ID_D - a link property that ends in '/blocks?head={}&start=1&limit=1'.format(ID_D) - 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 ID_C """ # Block list only returns a next id paging = Mocks.make_paging_response('0x0002', "0x0003", 1) blocks = Mocks.make_blocks(ID_C) self.connection.preset_response( head_id=ID_D, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?start=0x0003&limit=1') controls = Mocks.make_paging_controls(1, start='0x0003') self.connection.assert_valid_request_sent(paging=controls) self.assert_has_valid_head(response, ID_D) self.assert_has_valid_link( response, '/blocks?head={}&start=0x0003&limit=1'.format(ID_D)) self.assert_has_valid_paging( response, paging, '/blocks?head={}&start=0x0002&limit=1'.format(ID_D)) self.assert_has_valid_data_list(response, 1) self.assert_blocks_well_formed(response['data'], ID_C)
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 ID_C - an empty paging response - two blocks with ids ID_A and ID_C It should send a Protobuf request with: - a block_ids property of [ID_A, ID_C] - empty paging controls It should send back a JSON response with: - a response status of 200 - a head property of ID_C, the latest - a link property that ends in '/blocks?head={}&start={}&limit=100&id={},{}' .format(ID_C, ID_C, ID_A, ID_C)) - 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 ID_A and ID_C """ paging = Mocks.make_paging_response("", ID_C, DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_A, ID_C) self.connection.preset_response( head_id=ID_C, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?id={},{}'.format( ID_A, ID_C)) controls = Mocks.make_paging_controls() self.connection.assert_valid_request_sent( block_ids=[ID_A, ID_C], paging=controls) self.assert_has_valid_head(response, ID_C) link = '/blocks?head={ID_C}&start={ID_C}&limit=100&id={ID_A},{ID_C}' self.assert_has_valid_link( response, link.format(ID_C=ID_C, ID_A=ID_A)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 2) self.assert_blocks_well_formed(response['data'], ID_A, ID_C)
async def test_block_list_paginated_with_just_limit(self): """Verifies GET /blocks paginated just by limit works properly. It will receive a Protobuf response with: - a head id of ID_D - a paging response with a next of 0x0002, start of 0x004 and limit of 2 - two blocks with the ids ID_D and ID_C It should send a Protobuf request with: - paging controls with a limit of 2 It should send back a JSON response with: - a response status of 200 - a head property of ID_D - a link property that ends in '/blocks?head={}&start=0x0004&limit=2'.format(ID_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 ID_D and ID_C """ # Block list only returns a next id paging = Mocks.make_paging_response('0x0002', '0x0004', 2) blocks = Mocks.make_blocks(ID_D, ID_C) self.connection.preset_response( head_id=ID_D, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?limit=2') controls = Mocks.make_paging_controls(2) self.connection.assert_valid_request_sent(paging=controls) self.assert_has_valid_head(response, ID_D) self.assert_has_valid_link( response, '/blocks?head={}&start=0x0004&limit=2'.format(ID_D)) self.assert_has_valid_paging( response, paging, '/blocks?head={}&start=0x0002&limit=2'.format(ID_D)) self.assert_has_valid_data_list(response, 2) self.assert_blocks_well_formed(response['data'], ID_D, ID_C)
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 ID_C - a paging response with start ID_C and limit of 100 - three blocks with ids ID_C, ID_B, and ID_A 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 ID_C - a link property ending in '/blocks?head={}&start={}&limit=100&reverse'.format(ID_C, ID_C) - 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 ID_C, ID_B, and ID_A """ paging = Mocks.make_paging_response("", ID_C, DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_C, ID_B, ID_A) self.connection.preset_response( head_id=ID_C, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?reverse') page_controls = Mocks.make_paging_controls() sorting = Mocks.make_sort_controls('block_num', reverse=True) self.connection.assert_valid_request_sent( paging=page_controls, sorting=sorting) self.assert_has_valid_head(response, ID_C) self.assert_has_valid_link( response, '/blocks?head={ID_C}&start={ID_C}&limit=100&reverse'.format( ID_C=ID_C)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 3) self.assert_blocks_well_formed(response['data'], ID_C, ID_B, ID_A)
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' """ # Block list only returns a next id paging = Mocks.make_paging_response(None, 4, next_id='0x0002') 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=0x0002&count=2') self.assert_has_valid_data_list(response, 2) self.assert_blocks_well_formed(response['data'], 'd', 'c')
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.connection.preset_response(head_id='1', paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?id=0&head=1') self.connection.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_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.connection.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.connection.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_block_list_paginated_by_min_id(self): """Verifies GET /blocks 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 blocks 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 '/blocks?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 blocks with ids 'c', 'b', and 'a' """ paging = Mocks.make_paging_response(1, 4, previous_id='d') blocks = Mocks.make_blocks('c', 'b', 'a') self.connection.preset_response(head_id='d', paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?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, '/blocks?head=d&min=c&count=5') self.assert_has_valid_paging(response, paging, previous_link='/blocks?head=d&max=d&count=5') self.assert_has_valid_data_list(response, 3) self.assert_blocks_well_formed(response['data'], 'c', 'b', 'a')
async def test_block_list_paginated_by_start_id(self): """Verifies GET /blocks paginated by a start id works properly. It will receive a Protobuf response with: - a head id of ID_D - a paging response with start of 0x0003 and limit of 5 - three blocks with the ids ID_C, ID_B and ID_A It should send a Protobuf request with: - paging controls with a limit of 5, and a startof ID_C It should send back a JSON response with: - a response status of 200 - a head property of ID_D - a link property that ends in '/blocks?head={}&start=0x0003&limit=5'.format(ID_C) - paging that matches the response, with a previous link - a data property that is a list of 3 dicts - and those dicts are full blocks with ids ID_C, ID_B, and ID_A """ paging = Mocks.make_paging_response("", "0x0003", 5) blocks = Mocks.make_blocks(ID_C, ID_B, ID_A) self.connection.preset_response(head_id=ID_D, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?start=0x0003&limit=5') controls = Mocks.make_paging_controls(5, start="0x0003") self.connection.assert_valid_request_sent(paging=controls) self.assert_has_valid_head(response, ID_D) self.assert_has_valid_link( response, '/blocks?head={}&start=0x0003&limit=5'.format(ID_D)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 3) self.assert_blocks_well_formed(response['data'], ID_C, ID_B, ID_A)
async def test_block_list_paginated_without_limit(self): """Verifies GET /blocks paginated without limit works properly. It will receive a Protobuf response with: - a head id of ID_D - a paging response with start of 0x0002 and limit of 100 - two blocks with the ids ID_B and ID_A It should send a Protobuf request with: - paging controls with a start of 2 It should send back a JSON response with: - a response status of 200 - a head property of ID_D - a link property that ends in '/blocks?head={}&start=0x0002&limit=100'.format(ID_D) - 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 ID_D and ID_C """ paging = Mocks.make_paging_response("", "0x0002", DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_B, ID_A) self.connection.preset_response(head_id=ID_D, paging=paging, blocks=blocks) response = await self.get_assert_200('/blocks?start=0x0002') controls = Mocks.make_paging_controls(None, start="0x0002") self.connection.assert_valid_request_sent(paging=controls) self.assert_has_valid_head(response, ID_D) self.assert_has_valid_link( response, '/blocks?head={}&start=0x0002&limit=100'.format(ID_D)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 2) self.assert_blocks_well_formed(response['data'], ID_B, ID_A)
async def test_block_get(self): """Verifies a GET /blocks/{block_id} works properly. It should send a Protobuf request with: - a block_id property of ID_B It will receive a Protobuf response with: - a block with an id of ID_B It should send back a JSON response with: - a response status of 200 - no head property - a link property that ends in '/blocks/{}'.format(ID_B) - a data property that is a full block with an id of ID_A """ self.connection.preset_response(block=Mocks.make_blocks(ID_B)[0]) response = await self.get_assert_200('/blocks/{}'.format(ID_B)) self.connection.assert_valid_request_sent(block_id=ID_B) self.assertNotIn('head', response) self.assert_has_valid_link(response, '/blocks/{}'.format(ID_B)) self.assertIn('data', response) self.assert_blocks_well_formed(response['data'], ID_B)
async def test_block_get(self): """Verifies a GET /blocks/{block_id} works properly. It should send a Protobuf request with: - a block_id property of '1' It will receive a Protobuf response with: - a block 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 '/blocks/1' - a data property that is a full block with an id of '0' """ self.connection.preset_response(block=Mocks.make_blocks('1')[0]) response = await self.get_assert_200('/blocks/1') self.connection.assert_valid_request_sent(block_id='1') self.assertNotIn('head', response) self.assert_has_valid_link(response, '/blocks/1') self.assertIn('data', response) self.assert_blocks_well_formed(response['data'], '1')
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_block_list(self): """Verifies a GET /blocks without parameters works properly. It will receive a Protobuf response with: - a head id of ID_C - a paging reponse with start of ID_C and limit of 100 - three blocks with ids ID_C, ID_B, and ID_A 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 ID_C - a link property that ends in '/blocks?head={}&start={}&limit=100'.format(ID_C, ID_C)) - 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 ID_C, ID_B, and ID_A """ paging = Mocks.make_paging_response("", ID_C, DEFAULT_LIMIT) blocks = Mocks.make_blocks(ID_C, ID_B, ID_A) self.connection.preset_response(head_id=ID_C, 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, ID_C) self.assert_has_valid_link( response, '/blocks?head={}&start={}&limit=100'.format(ID_C, ID_C)) self.assert_has_valid_paging(response, paging) self.assert_has_valid_data_list(response, 3) self.assert_blocks_well_formed(response['data'], ID_C, ID_B, ID_A)