Exemplo n.º 1
0
def Contract(web3: web3.Web3 = None,
             repo: MinioRepo = None,
             network_id: str = None,
             artifact_key: str = None) -> web3.eth.Contract:
    artifact = json.loads(repo.get_object_content(artifact_key))
    return web3.eth.contract(
        address=artifact['networks'][network_id]['address'],
        abi=artifact['abi'])
Exemplo n.º 2
0
def test_operations(boto3):

    CONTENT = "dummy_content"
    CONTENT_BYTES = CONTENT.encode('utf-8')

    class DummyFileLikeObj():
        def read(self):
            return CONTENT

    s3_client = boto3.client.return_value
    # testing successful get
    repo = MinioRepo(CONNECTION_DATA)
    s3_client.get_object.return_value = {'Body': DummyFileLikeObj()}
    assert CONTENT == repo.get_object_content('path_to_object')
    boto3.reset_mock()

    # testing successful put
    repo.put_message_related_object('AU', URI_LEN_25, '/metadata.json',
                                    CONTENT)
    s3_client.put_object.assert_called_once()

    args, kwargs = s3_client.put_object.call_args_list[0]
    assert kwargs['Bucket'] == CONNECTION_DATA['bucket']
    assert kwargs['ContentLength'] == len(CONTENT_BYTES)
    assert kwargs['Key'] == f'AU/{URI_LEN_5}/{URI_LEN_10*2}/metadata.json'
    assert isinstance(kwargs.get('Body'), BytesIO)
    assert kwargs['Body'].read() == CONTENT_BYTES

    boto3.reset_mock()

    # testing invalid sender
    with pytest.raises(ValueError):
        repo.put_message_related_object('Au', URI_LEN_25, '/metadata.json',
                                        CONTENT)
    with pytest.raises(ValueError):
        repo.put_message_related_object('AUU', URI_LEN_25, '/metadata.json',
                                        CONTENT)

    # testing put object with clear path. Path will be chunked in the process
    repo.put_object(clean_path=URI_LEN_25, content_body=CONTENT)
    s3_client.put_object.assert_called_once()

    args, kwargs = s3_client.put_object.call_args_list[0]
    assert kwargs['Bucket'] == CONNECTION_DATA['bucket']
    assert kwargs['ContentLength'] == len(CONTENT_BYTES)
    assert kwargs['Key'] == f'{URI_LEN_5}/{URI_LEN_10*2}'
    assert isinstance(kwargs.get('Body'), BytesIO)
    assert kwargs['Body'].read() == CONTENT_BYTES

    boto3.reset_mock()

    # testing put object with chunked path. Path should not be chunked in the process
    repo.put_object(chunked_path=URI_LEN_25, content_body=CONTENT)
    s3_client.put_object.assert_called_once()

    args, kwargs = s3_client.put_object.call_args_list[0]
    assert kwargs['Bucket'] == CONNECTION_DATA['bucket']
    assert kwargs['ContentLength'] == len(CONTENT_BYTES)
    assert kwargs['Key'] == URI_LEN_25
    assert isinstance(kwargs.get('Body'), BytesIO)
    assert kwargs['Body'].read() == CONTENT_BYTES

    boto3.reset_mock()

    # testing both chunked and clear path error
    with pytest.raises(TypeError):
        repo.put_object(chunked_path=URI_LEN_25,
                        clean_path=URI_LEN_25,
                        content_body=CONTENT)
    with pytest.raises(TypeError):
        repo.put_object(content_body=CONTENT)