示例#1
0
def test_fs_error_during_checksum(monkeypatch):
    cli = MinidClient()
    hasher_inst = Mock()
    hasher_inst.update.side_effect = Exception
    monkeypatch.setattr(hashlib, 'sha256', Mock(return_value=hasher_inst))
    with pytest.raises(MinidException):
        cli.check(TEST_CHECKSUM_FILE)
示例#2
0
def test_custom_authorizer():
    inst_authorizer = Mock()
    mc = MinidClient(authorizer=inst_authorizer)
    assert mc.authorizer == inst_authorizer

    settr_authorizer = Mock()
    mc.authorizer = settr_authorizer
    assert mc.authorizer == settr_authorizer
示例#3
0
def test_update(mock_identifiers_client, mocked_checksum, logged_in):
    cli = MinidClient()
    cli.update('hdl:20.500.12633/mock-hdl',
               title='foo.txt',
               locations=['http://example.com'])
    mock_identifiers_client.update_identifier.assert_called_with(
        'hdl:20.500.12633/mock-hdl',
        metadata={'title': 'foo.txt'},
        location=['http://example.com'])
示例#4
0
def test_update_translates_hdls(mock_identifiers_client, logged_in):
    cli = MinidClient()

    cli.update('minid:first', replaces='minid:second', replaced_by='minid:thd')
    expected = call('hdl:20.500.12582/first',
                    replaces='hdl:20.500.12582/second',
                    replaced_by='hdl:20.500.12582/thd',
                    metadata={})
    assert mock_identifiers_client.update_identifier.called
    assert mock_identifiers_client.update_identifier.call_args == expected
示例#5
0
def test_rfm_register_replaces_changed(logged_in, mock_get_identifier,
                                       mock_gcs_register,
                                       mock_identifier_response,
                                       mock_gcs_update):
    mock_identifier_response.data = mock_identifier_response.data[
        'identifiers'][0]
    mock_get_identifier.get_identifier.return_value = mock_identifier_response
    rfm_record = {
        "length": 47,
        "filename": "test_document.txt",
        "url": "hdl:20.500.12633/foo-identifier",
        "sha256": "checksum_has_changed"
    }
    cli = MinidClient()
    cli.register_rfm(rfm_record, True, update_if_exists=True)
    # Mocked identifiers should match exactly the batch registered identifiers
    assert mock_gcs_register.call_count == 1
    assert mock_gcs_update.call_count == 0
示例#6
0
def test_register(mock_identifiers_client, mocked_checksum, logged_in):
    cli = MinidClient()
    cli.register('foo.txt')

    expected = {
        'checksums': [{
            'function': 'sha256',
            'value': 'mock_checksum'
        }],
        'metadata': {
            '_profile': 'erc',
            'erc.what': 'foo.txt'
        },
        'location': [],
        'namespace': MinidClient.NAMESPACE,
        'visible_to': ['public']
    }
    assert expected in mock_identifiers_client.create_identifier.call_args
示例#7
0
def test_register_replaces(mock_identifiers_client, logged_in):
    cli = MinidClient()
    checksums = [{'function': 'sha256', 'value': 'mock_checksum'}]
    cli.register(checksums, title='foo.txt', replaces='minid:123456')

    expected = {
        'checksums': [{
            'function': 'sha256',
            'value': 'mock_checksum'
        }],
        'metadata': {
            'title': 'foo.txt'
        },
        'location': [],
        'namespace': MinidClient.IDENTIFIERS_NAMESPACE,
        'visible_to': ['public'],
        'replaces': 'hdl:20.500.12582/123456'
    }
    assert expected in mock_identifiers_client.create_identifier.call_args
示例#8
0
def test_rfm_register_updates_existing(logged_in, mock_get_identifier,
                                       mock_gcs_register,
                                       mock_identifier_response,
                                       mock_gcs_update):
    mock_identifier_response.data = mock_identifier_response.data[
        'identifiers'][0]
    mock_get_identifier.get_identifier.return_value = mock_identifier_response
    rfm_record = {
        "length":
        47,
        "filename":
        "test_document.txt",
        "url":
        "hdl:20.500.12633/foo-identifier",
        "sha256":
        "f92d11e4316ac9f282571338dba4df819203639ff5cf8d32225d857828189998"
    }
    cli = MinidClient()
    cli.register_rfm(rfm_record, True, update_if_exists=True)
    # Mocked identifiers should match exactly the batch registered identifiers
    assert mock_gcs_register.call_count == 0
    assert mock_gcs_update.call_count == 1
示例#9
0
def test_is_logged_in(monkeypatch):
    mc = MinidClient()
    monkeypatch.setattr(mc.native_client, 'get_authorizers_by_scope',
                        Mock(side_effect=fair_research_login.LoadError()))
    mc._authorizer = None
    assert mc.is_logged_in() is False
    mc._authorizer = Mock()
    assert mc.is_logged_in() is True
示例#10
0
def test_register_unsupported_checksum(mock_identifiers_client, logged_in):
    cli = MinidClient()
    checksums = [{
        'function': 'sha256',
        'value': 'mock_checksum'
    }, {
        'function': 'NOT_REAL',
        'value': 'irrelevant!'
    }]
    cli.register(checksums, title='foo.txt')

    expected = {
        'checksums': [{
            'function': 'sha256',
            'value': 'mock_checksum'
        }],
        'metadata': {
            'title': 'foo.txt'
        },
        'location': [],
        'namespace': MinidClient.IDENTIFIERS_NAMESPACE,
        'visible_to': ['public'],
    }
    assert expected in mock_identifiers_client.create_identifier.call_args
示例#11
0
def test_register(mock_identifiers_client, mocked_checksum, logged_in,
                  mock_get_cached_created_by, monkeypatch):
    stat = Mock()
    stat.return_value.st_size = 21
    monkeypatch.setattr(os, 'stat', stat)
    cli = MinidClient()
    cli.register_file('foo.txt')

    expected = {
        'checksums': [{
            'function': 'sha256',
            'value': 'mock_checksum'
        }],
        'metadata': {
            'title': 'foo.txt',
            'length': 21,
            'created_by': 'Test User',
        },
        'location': [],
        'namespace': MinidClient.IDENTIFIERS_NAMESPACE,
        'visible_to': ['public'],
        'replaces': None,
    }
    assert expected in mock_identifiers_client.create_identifier.call_args
示例#12
0
def test_is_stream(mock_streamed_rfm):
    with open('magic_file.json', 'r') as manifest:
        assert MinidClient._is_stream(manifest) is True
示例#13
0
def test_read_manifest_entries(mock_rfm, mock_rfm_filename):
    read_rfm = list(MinidClient.read_manifest_entries(mock_rfm_filename))
    assert read_rfm == mock_rfm
示例#14
0
def test_get_cached_created_by_is_cached(mock_globus_sdk_auth,
                                         mock_fair_research_login):
    mc = MinidClient()
    mc.get_cached_created_by()
    mc.get_cached_created_by()
    assert mock_globus_sdk_auth.call_count == 1
示例#15
0
def test_empty_dir():
    # Prehashed sum with openssl, file contents "12345"
    with pytest.raises(MinidException):
        MinidClient.compute_checksum(EMPTY_DIR)
示例#16
0
def test_update_invalid_args(mock_identifiers_client, logged_in):
    cli = MinidClient()
    with pytest.raises(MinidException):
        cli.update('hdl:20.500.12633/1234567', non_existant_arg='foobar')
示例#17
0
def test_client_creates_config_dir_if_not_exists(monkeypatch):
    mkdir = Mock()
    monkeypatch.setattr(os, 'mkdir', mkdir)
    monkeypatch.setattr(os.path, 'exists', Mock(return_value=False))
    MinidClient()
    assert mkdir.called
示例#18
0
def test_minid_logout_after_logout(mock_load_tokens_error, mock_auth_logout):
    mc = MinidClient()
    mc.logout()
    assert not mock_auth_logout.called
示例#19
0
def test_minid_logout(mock_load_tokens, mock_auth_logout):
    mc = MinidClient()
    mc.logout()
    assert mock_auth_logout.called
示例#20
0
def test_checksumming_file_does_not_exist():
    cli = MinidClient()
    with pytest.raises(MinidException):
        cli.check('does_not_exist.txt')
示例#21
0
def test_check_by_checksum(mock_identifiers_client):
    cli = MinidClient()
    cli.check(TEST_CHECKSUM_FILE)
    mock_identifiers_client.get_identifier_by_checksum.assert_called_with(
        TEST_CHECKSUM_VALUE, )
示例#22
0
def test_check(mock_identifiers_client, mocked_checksum):
    cli = MinidClient()
    cli.check('hdl:20.500.12633/mock-hdl')
    mock_identifiers_client.get_identifier.assert_called_with(
        'hdl:20.500.12633/mock-hdl', )
示例#23
0
def test_batch_register(logged_in, mock_rfm, mock_rfm_filename,
                        mock_gcs_register, mock_gcs_get_by_checksum):
    mock_gcs_get_by_checksum.return_value.data['identifiers'] = []
    cli = MinidClient()
    cli.batch_register(mock_rfm_filename, True)
    assert mock_gcs_register.call_count == len(mock_rfm)
示例#24
0
def test_is_not_stream(mock_rfm_filename):
    with open(mock_rfm_filename, 'r') as manifest:
        assert MinidClient._is_stream(manifest) is False
示例#25
0
def test_read_manifest_entries_streamed(mock_streamed_rfm, mock_rfm,
                                        monkeypatch):
    monkeypatch.setattr(MinidClient, '_is_stream', Mock(return_value=True))
    read_rfm = list(MinidClient.read_manifest_entries('rfm.json'))
    assert len(read_rfm) == len(mock_rfm)
    assert read_rfm == mock_rfm
示例#26
0
def test_unrecognized_algorithm():
    with pytest.raises(MinidException):
        MinidClient.get_algorithm('not_elliptical_enough')
示例#27
0
def test_compute_checksum():
    # Prehashed sum with openssl, file contents "12345"
    checksum = MinidClient.compute_checksum(TEST_CHECKSUM_FILE)
    assert checksum == TEST_CHECKSUM_VALUE
示例#28
0
def test_validate_checksums_without_common_algs_returns_false(
        mock_identifier_response):
    checksums = mock_identifier_response['identifiers'][0]['checksums']
    assert MinidClient.validate_checksums(checksums, []) is False
示例#29
0
def test_minid_sdk_login(mock_load_tokens):
    mc = MinidClient()
    mc.login()
    assert mock_load_tokens.called
示例#30
0
def test_load_logged_out_authorizer(logged_out):
    assert MinidClient().identifiers_client.authorizer is None