1- import unittest
2- from unittest .mock import MagicMock , Mock , patch
1+ from unittest .mock import Mock , patch
32
43import pytest
54from human_protocol_sdk .constants import ChainId , Status
2019 FACTORY_ADDRESS ,
2120 JOB_LAUNCHER_ADDRESS ,
2221 RECORDING_ORACLE_ADDRESS ,
22+ REPUTATION_ORACLE_ADDRESS ,
2323 TOKEN_ADDRESS ,
2424)
2525
2626escrow_address = ESCROW_ADDRESS
2727
2828
29- class ServiceIntegrationTest (unittest .TestCase ):
30- def setUp (self ):
29+ class ServiceIntegrationTest :
30+ @pytest .fixture (autouse = True )
31+ def setup (self ):
3132 self .w3 = Mock ()
3233 self .w3 .eth .chain_id = ChainId .LOCALHOST .value
3334 self .escrow_data = EscrowData (
@@ -43,59 +44,30 @@ def setUp(self):
4344 token = TOKEN_ADDRESS ,
4445 total_funded_amount = 1000 ,
4546 created_at = "" ,
46- manifest_url = DEFAULT_MANIFEST_URL ,
4747 recording_oracle = RECORDING_ORACLE_ADDRESS ,
48+ reputation_oracle = REPUTATION_ORACLE_ADDRESS ,
4849 )
4950
50- def test_get_job_launcher_url (self ):
51+ @pytest .mark .parametrize (
52+ "get_url" , [get_reputation_oracle_url , get_job_launcher_url , get_recording_oracle_url ]
53+ )
54+ def test_get_oracle_url (self , get_url ):
5155 with (
5256 patch ("src.chain.kvstore.get_escrow" ) as mock_escrow ,
53- patch ("src.chain.kvstore.OperatorUtils.get_operator" ) as mock_operator ,
57+ patch ("src.chain.kvstore.get_web3" , return_value = self .w3 ),
58+ patch ("src.chain.kvstore.KVStoreClient.get" ) as mock_kvstore_get ,
5459 ):
5560 mock_escrow .return_value = self .escrow_data
56- mock_operator .return_value = MagicMock (webhook_url = DEFAULT_MANIFEST_URL )
57- recording_url = get_job_launcher_url (self .w3 .eth .chain_id , escrow_address )
58- assert recording_url == DEFAULT_MANIFEST_URL
59-
60- def test_get_job_launcher_url_invalid_escrow (self ):
61- with pytest .raises (EscrowClientError , match = "Invalid escrow address: invalid_address" ):
62- get_job_launcher_url (self .w3 .eth .chain_id , "invalid_address" )
63-
64- def test_get_job_launcher_url_invalid_recording_address (self ):
65- with (
66- patch ("src.chain.kvstore.get_escrow" ) as mock_escrow ,
67- patch ("src.chain.kvstore.OperatorUtils.get_operator" ) as mock_operator ,
68- ):
69- mock_escrow .return_value = self .escrow_data
70- mock_operator .return_value = MagicMock (webhook_url = "" )
71- recording_url = get_job_launcher_url (self .w3 .eth .chain_id , escrow_address )
72- assert recording_url == ""
73-
74- def test_get_recording_oracle_url (self ):
75- with (
76- patch ("src.chain.kvstore.get_escrow" ) as mock_escrow ,
77- patch ("src.chain.kvstore.OperatorUtils.get_operator" ) as mock_operator ,
78- ):
79- self .escrow_data .recording_oracle = RECORDING_ORACLE_ADDRESS
80- mock_escrow .return_value = self .escrow_data
81- mock_operator .return_value = MagicMock (webhook_url = DEFAULT_MANIFEST_URL )
82- recording_url = get_recording_oracle_url (self .w3 .eth .chain_id , escrow_address )
83- assert recording_url == DEFAULT_MANIFEST_URL
84-
85- def test_get_recording_oracle_url_invalid_escrow (self ):
61+ mock_kvstore_get .return_value = DEFAULT_MANIFEST_URL
62+ actual_url = get_url (self .w3 .eth .chain_id , escrow_address )
63+ assert actual_url == DEFAULT_MANIFEST_URL
64+
65+ @pytest .mark .parametrize (
66+ "get_url" , [get_reputation_oracle_url , get_job_launcher_url , get_recording_oracle_url ]
67+ )
68+ def test_get_oracle_url_invalid_escrow (self , get_url ):
8669 with pytest .raises (EscrowClientError , match = "Invalid escrow address: invalid_address" ):
87- get_recording_oracle_url (self .w3 .eth .chain_id , "invalid_address" )
88-
89- def test_get_recording_oracle_url_invalid_recording_address (self ):
90- with (
91- patch ("src.chain.kvstore.get_escrow" ) as mock_escrow ,
92- patch ("src.chain.kvstore.OperatorUtils.get_operator" ) as mock_operator ,
93- ):
94- self .escrow_data .recording_oracle = RECORDING_ORACLE_ADDRESS
95- mock_escrow .return_value = self .escrow_data
96- mock_operator .return_value = MagicMock (webhook_url = "" )
97- recording_url = get_recording_oracle_url (self .w3 .eth .chain_id , escrow_address )
98- assert recording_url == ""
70+ get_url (self .w3 .eth .chain_id , "invalid_address" )
9971
10072 def test_store_public_key (self ):
10173 PGP_PUBLIC_KEY_URL_1 = "http://pgp-public-key-url-1"
@@ -199,25 +171,3 @@ def set_file_url_and_hash(url: str, key: str):
199171 )
200172 == PGP_PUBLIC_KEY_URL_2
201173 )
202-
203- def test_get_reputation_oracle_url_config_url (self ):
204- with patch (
205- "src.chain.kvstore.Config.localhost.reputation_oracle_url" , DEFAULT_MANIFEST_URL
206- ):
207- reputation_url = get_reputation_oracle_url (self .w3 .eth .chain_id , escrow_address )
208- assert reputation_url == DEFAULT_MANIFEST_URL
209-
210- def test_get_reputation_oracle_url_from_escrow (self ):
211- with (
212- patch ("src.chain.kvstore.get_escrow" ) as mock_escrow ,
213- patch ("src.chain.kvstore.OperatorUtils.get_operator" ) as mock_operator ,
214- patch ("src.chain.kvstore.Config.localhost.reputation_oracle_url" , None ),
215- ):
216- mock_escrow .return_value = self .escrow_data
217- mock_operator .return_value = MagicMock (webhook_url = DEFAULT_MANIFEST_URL )
218- reputation_url = get_reputation_oracle_url (self .w3 .eth .chain_id , escrow_address )
219- assert reputation_url == DEFAULT_MANIFEST_URL
220-
221- def test_get_reputation_oracle_url_invalid_escrow (self ):
222- with pytest .raises (EscrowClientError , match = "Invalid escrow address: invalid address" ):
223- get_reputation_oracle_url (self .w3 .eth .chain_id , "invalid address" )
0 commit comments