test_uri.py (4270B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 7 # external imports 8 from chainlib.eth.unittest.ethtester import EthTesterCase 9 from chainlib.connection import RPCConnection 10 from chainlib.eth.nonce import RPCNonceOracle 11 from chainlib.eth.address import to_checksum_address 12 from chainlib.eth.tx import ( 13 receipt, 14 transaction, 15 TxFormat, 16 ) 17 from chainlib.eth.contract import ( 18 abi_decode_single, 19 ABIContractType, 20 ) 21 from chainlib.eth.address import is_same_address 22 from chainlib.error import JSONRPCException 23 from chainlib.eth.constant import ZERO_ADDRESS 24 from hexathon import ( 25 add_0x, 26 strip_0x, 27 ) 28 from chainlib.eth.tx import TxFormat 29 from chainlib.eth.contract import ABIContractEncoder 30 31 32 # local imports 33 from craft_nft import CraftNFT 34 from craft_nft.unittest import TestCraftNFT 35 from craft_nft.error import InvalidBatchError 36 from craft_nft.eth import ABIContractType 37 from craft_nft.nft import to_batch_key 38 39 logging.basicConfig(level=logging.DEBUG) 40 logg = logging.getLogger() 41 42 testdir = os.path.dirname(__file__) 43 44 hash_of_foo = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae' 45 hash_of_bar = 'fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9' 46 47 48 class TestURI(TestCraftNFT): 49 50 def test_base_uri(self): 51 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 52 c = CraftNFT(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 53 54 o = c.to_uri(self.address, hash_of_foo, sender_address=self.accounts[0]) 55 r = self.rpc.do(o) 56 uri = c.parse_uri(r) 57 self.assertEqual('sha256:' + hash_of_foo, uri) 58 59 60 def test_explicit_url(self): 61 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 62 c = CraftNFT(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 63 64 (tx_hash_hex, o) = c.set_base_url(self.address, self.accounts[0], 'http://localhost') 65 self.rpc.do(o) 66 o = receipt(tx_hash_hex) 67 r = self.conn.do(o) 68 self.assertEqual(r['status'], 1) 69 70 o = c.to_url(self.address, hash_of_foo, sender_address=self.accounts[0]) 71 r = self.rpc.do(o) 72 uri = c.parse_uri(r) 73 self.assertEqual('http://localhost/' + hash_of_foo, uri) 74 75 o = c.token_uri(self.address, int(hash_of_foo, 16), sender_address=self.accounts[0]) 76 with self.assertRaises(JSONRPCException): 77 self.rpc.do(o) 78 uri = c.parse_uri(r) 79 self.assertEqual('http://localhost/' + hash_of_foo, uri) 80 81 (tx_hash_hex, o) = c.allocate(self.address, self.accounts[0], hash_of_foo, amount=2) 82 self.rpc.do(o) 83 o = c.token_uri(self.address, int(hash_of_foo, 16), sender_address=self.accounts[0]) 84 self.rpc.do(o) 85 uri = c.parse_uri(r) 86 self.assertEqual('http://localhost/' + hash_of_foo, uri) 87 88 (tx_hash_hex, o) = c.set_base_url(self.address, self.accounts[0], 'https://example.org/') 89 self.rpc.do(o) 90 o = receipt(tx_hash_hex) 91 r = self.conn.do(o) 92 self.assertEqual(r['status'], 1) 93 94 o = c.to_url(self.address, hash_of_bar, sender_address=self.accounts[0]) 95 r = self.rpc.do(o) 96 uri = c.parse_uri(r) 97 self.assertEqual('https://example.org/' + hash_of_bar, uri) 98 99 100 def test_batch_to_uri(self): 101 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 102 c = CraftNFT(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 103 104 (tx_hash_hex, o) = c.allocate(self.address, self.accounts[0], hash_of_foo, amount=1000) 105 self.rpc.do(o) 106 o = receipt(tx_hash_hex) 107 r = self.conn.do(o) 108 self.assertEqual(r['status'], 1) 109 110 (tx_hash_hex, o) = c.mint_to(self.address, self.accounts[0], self.accounts[9], hash_of_foo, 0, index=666) 111 self.rpc.do(o) 112 o = receipt(tx_hash_hex) 113 r = self.conn.do(o) 114 self.assertEqual(r['status'], 1) 115 token_batch_id = hash_of_foo[:64-16] + '000000000000029a' 116 117 o = c.token_uri(self.address, int(token_batch_id, 16), sender_address=self.accounts[0]) 118 r = self.rpc.do(o) 119 uri = c.parse_uri(r) 120 self.assertEqual(hash_of_foo, uri) 121 122 123 if __name__ == '__main__': 124 unittest.main()