def test_get_blocks_unlimited(self): """ Testing whether the API takes no limit argument """ self.should_check_equality = False return self.do_request('trustchain/blocks/%s' % TrustChainBlock().public_key.encode("HEX"), expected_code=200)
def test_get_blocks_bad_limit_nan(self): """ Testing whether the API takes odd values for the limit """ self.should_check_equality = False return self.do_request('trustchain/blocks/%s?limit=bla' % TrustChainBlock().public_key.encode("HEX"), expected_code=400)
def test_get_bootstrap_identity_not_enough_tokens_2(self): """ Testing whether the API returns error 400 if bandwidth is to low when bootstrapping a new identity """ transaction = {'up': 0, 'down': 100, 'total_up': 0, 'total_down': 100} test_block = TrustChainBlock() test_block.transaction = transaction test_block.public_key = self.session.lm.triblerchain_community.my_peer.public_key.key_to_bin() self.session.lm.triblerchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap?amount=10', expected_code=400)
def test_get_blocks(self): """ Testing whether the API returns the correct blocks """ def verify_response(response): response_json = json.loads(response) self.assertEqual(len(response_json["blocks"]), 1) test_block = TrustChainBlock() self.session.lm.triblerchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/blocks/%s?limit=10' % test_block.public_key.encode("HEX"), expected_code=200).addCallback(verify_response)
def test_get_statistics(self): """ Testing whether the API returns the correct statistics """ block = TrustChainBlock() block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin( ) block.link_public_key = "deadbeef".decode("HEX") block.link_sequence_number = 21 block.type = 'tribler_bandwidth' block.transaction = { "up": 42, "down": 8, "total_up": 1024, "total_down": 2048, "type": "tribler_bandwidth" } block._transaction = encode(block.transaction) block.sequence_number = 3 block.previous_hash = "babecafe".decode("HEX") block.signature = "babebeef".decode("HEX") block.hash = block.calculate_hash() self.session.lm.trustchain_community.persistence.add_block(block) def verify_response(response): response_json = json.loads(response) self.assertTrue("statistics" in response_json) stats = response_json["statistics"] self.assertEqual( stats["id"], self.session.lm.trustchain_community.my_peer.public_key. key_to_bin().encode("HEX")) self.assertEqual(stats["total_blocks"], 3) self.assertEqual(stats["total_up"], 1024) self.assertEqual(stats["total_down"], 2048) self.assertEqual(stats["peers_that_pk_helped"], 1) self.assertEqual(stats["peers_that_helped_pk"], 1) self.assertIn("latest_block", stats) self.assertNotEqual(stats["latest_block"]["insert_time"], "") self.assertEqual(stats["latest_block"]["hash"], block.hash.encode("HEX")) self.assertEqual(stats["latest_block"]["link_public_key"], "deadbeef") self.assertEqual(stats["latest_block"]["link_sequence_number"], 21) self.assertEqual(stats["latest_block"]["up"], 42) self.assertEqual(stats["latest_block"]["down"], 8) self.should_check_equality = False return self.do_request('trustchain/statistics', expected_code=200).addCallback(verify_response)
def test_get_bootstrap_identity_partial_tokens(self): """ Testing whether the API return partial available credit when argument is supplied """ transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0} transaction2 = {'up': 50, 'down': 0} def verify_response(response): response_json = json.loads(response) self.assertEqual(response_json['transaction'], transaction2) test_block = TrustChainBlock() test_block.transaction = transaction test_block.public_key = self.session.lm.triblerchain_community.my_peer.public_key.key_to_bin() self.session.lm.triblerchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap?amount=50', expected_code=200).addCallback(verify_response)
def test_get_bootstrap_identity_all_tokens(self): """ Testing whether the API return all available tokens when no argument is supplied """ transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0} transaction2 = {'up': 100, 'down': 0} def verify_response(response): response_json = json.loads(response) self.assertEqual(response_json['transaction'], transaction2) test_block = TrustChainBlock() test_block.type = 'tribler_bandwidth' test_block.transaction = transaction test_block._transaction = encode(transaction) test_block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin( ) test_block.hash = test_block.calculate_hash() self.session.lm.trustchain_community.persistence.add_block(test_block) self.should_check_equality = False return self.do_request('trustchain/bootstrap', expected_code=200).addCallback(verify_response)