Esempio n. 1
0
def test_unsafe_operations(boto3):
    s3_client = boto3.client.return_value
    repo = MinioRepo(CONNECTION_DATA)

    # testing unsafe clear method
    s3_client.list_objects.return_value = {
        'Contents': [{
            'Key': 'a'
        }, {
            'Key': 'b'
        }],
        'IsTruncated': False
    }
    # deleted 2 objects
    assert repo._unsafe_method__clear() == 2
    s3_client.list_objects.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    # deleted only objects from the response
    assert s3_client.delete_object.call_count == 2
    assert s3_client.delete_object.call_args_list == [
        mock.call(Bucket=CONNECTION_DATA['bucket'], Key='a'),
        mock.call(Bucket=CONNECTION_DATA['bucket'], Key='b')
    ]
    boto3.reset_mock()
    # testing unsafe is empty
    assert not repo.is_empty()
    s3_client.list_objects.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    boto3.reset_mock()

    s3_client.list_objects.return_value['Contents'] = []
    assert repo.is_empty()
    s3_client.list_objects.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    boto3.reset_mock()
Esempio n. 2
0
def test_initialization(boto3):
    # test missing connection data keys errors
    for key in CONNECTION_DATA.keys():
        missing_key_connection_data = {**CONNECTION_DATA}
        del missing_key_connection_data[key]
        with pytest.raises(KeyError) as einfo:
            MinioRepo(missing_key_connection_data)
        assert str(einfo.value) == str(KeyError(key))

    # test initialization
    s3_client = boto3.client.return_value
    # initialization without ssl
    MinioRepo(CONNECTION_DATA)
    boto3.client.assert_called_once_with('s3', **AWS_CONNECTION_DATA)
    s3_client.head_bucket.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    s3_client.create_bucket.assert_not_called()
    boto3.reset_mock()

    # initialization with ssl
    MinioRepo(CONNECTION_DATA_SSL)
    boto3.client.assert_called_once_with('s3', **AWS_CONNECTION_DATA_SSL)
    s3_client.head_bucket.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    s3_client.create_bucket.assert_not_called()
    boto3.reset_mock()

    # test initialization with default bucket
    missing_bucket_connection_data = {**CONNECTION_DATA}
    del missing_bucket_connection_data['bucket']

    class DefaultBucketRepo(MinioRepo):
        DEFAULT_BUCKET = 'default_bucket'

    DefaultBucketRepo(missing_bucket_connection_data)

    boto3.client.assert_called_once_with('s3', **AWS_CONNECTION_DATA)
    s3_client.head_bucket.assert_called_once_with(
        Bucket=DefaultBucketRepo.DEFAULT_BUCKET)
    s3_client.create_bucket.assert_not_called()
    boto3.reset_mock()

    # testing bucket not found
    s3_client.head_bucket.side_effect = Exception()
    MinioRepo(CONNECTION_DATA)
    boto3.client.assert_called_once_with('s3', **AWS_CONNECTION_DATA)
    s3_client.head_bucket.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    s3_client.create_bucket.assert_called_once_with(
        Bucket=CONNECTION_DATA['bucket'])
    boto3.reset_mock()
Esempio n. 3
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'])
Esempio n. 4
0
def test_aws_connection_args():
    assert MinioRepo._aws_connection_data(
        CONNECTION_DATA) == AWS_CONNECTION_DATA
    assert MinioRepo._aws_connection_data(
        CONNECTION_DATA_SSL) == AWS_CONNECTION_DATA_SSL
Esempio n. 5
0
def test_unsafe_operations_not_allowed(boto3):
    repo = MinioRepo(CONNECTION_DATA)
    with pytest.raises(RuntimeError):
        repo._unsafe_method__clear()
Esempio n. 6
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)
Esempio n. 7
0
def test_unsafe_operations_not_allowed(boto3):
    repo = MinioRepo(CONNECTION_DATA)
    with pytest.raises(RuntimeError):
        repo._unsafe_clear_for_test()
    with pytest.raises(RuntimeError):
        repo._unsafe_is_empty_for_test()