Esempio n. 1
0
def test_move_from_storage_service(mocker, src_path, dst_path, package,
                                   encrypt_ret, expect):
    mocker.patch("locations.models.gpg._get_gpg_version",
                 return_value=GPG_VERSION)
    orig_pkg_key = package and package.encryption_key_fingerprint
    if isinstance(encrypt_ret, Exception):
        mocker.patch.object(gpg, "_gpg_encrypt", side_effect=encrypt_ret)
    else:
        mocker.patch.object(gpg, "_gpg_encrypt", return_value=encrypt_ret)
    gpg_space = gpg.GPG(key=SOME_FINGERPRINT, space=space.Space())
    mocker.patch.object(gpg_space.space, "create_local_directory")
    mocker.patch.object(gpg_space.space, "move_rsync")
    encryption_event = 42
    mocker.patch(
        "locations.models.gpg.premis.create_encryption_event",
        return_value=encryption_event,
    )
    if expect == "success":
        ret = gpg_space.move_from_storage_service(src_path,
                                                  dst_path,
                                                  package=package)
        if package.should_have_pointer_file():
            assert ret.events == [encryption_event]
            assert callable(ret.composition_level_updater)
            assert ret.inhibitors[0][0] == "inhibitors"
        else:
            assert ret is None
        if orig_pkg_key != gpg_space.key:
            assert package.encryption_key_fingerprint == gpg_space.key
            assert package.save_called == 1
    else:
        with pytest.raises(gpg.GPGException) as excinfo:
            gpg_space.move_from_storage_service(src_path,
                                                dst_path,
                                                package=package)
        if package:
            assert excinfo.value == encrypt_ret
        else:
            assert str(excinfo.value) == "GPG spaces can only contain packages"
    if package:
        gpg_space.space.create_local_directory.assert_called_once_with(
            dst_path)
        gpg_space.space.move_rsync.assert_any_call(src_path,
                                                   dst_path,
                                                   try_mv_local=True)
        if expect != "success":
            gpg_space.space.move_rsync.assert_any_call(dst_path,
                                                       src_path,
                                                       try_mv_local=True)
        gpg._gpg_encrypt.assert_called_once_with(dst_path, gpg_space.key)
Esempio n. 2
0
def test_move_to_storage_service(mocker, src_path, dst_path, src_exists1,
                                 src_exists2, encr_path, expect):
    gpg_space = gpg.GPG(key=SOME_FINGERPRINT, space=space.Space())
    mocker.patch.object(gpg_space.space, "create_local_directory")
    mocker.patch.object(gpg_space.space, "move_rsync")
    mocker.patch.object(gpg, "_gpg_decrypt")
    mocker.patch.object(gpg, "_gpg_encrypt")
    mocker.patch.object(gpg,
                        "_encr_path2key_fingerprint",
                        return_value=SOME_FINGERPRINT)
    mocker.patch.object(gpg, "_get_encrypted_path", return_value=encr_path)
    mocker.patch.object(os.path,
                        "exists",
                        side_effect=(src_exists1, src_exists2))
    if expect == "success":
        ret = gpg_space.move_to_storage_service(src_path, dst_path, None)
        assert ret is None
    else:
        with pytest.raises(gpg.GPGException) as excinfo:
            gpg_space.move_to_storage_service(src_path, dst_path, None)
        if not encr_path:
            assert ("Unable to move {}; this file/dir does not exist;"
                    " nor is it in an encrypted directory.".format(src_path) ==
                    str(excinfo.value))
        if not src_exists2:
            assert ("Unable to move {}; this file/dir does not"
                    " exist, not even in encrypted directory"
                    " {}.".format(src_path, encr_path) == str(excinfo.value))
    if src_exists2 and encr_path:
        gpg_space.space.move_rsync.assert_called_once_with(src_path, dst_path)
    else:
        assert not gpg_space.space.move_rsync.called
    if src_exists1:
        gpg._gpg_decrypt.assert_called_once_with(dst_path)
        assert not gpg._gpg_encrypt.called
    else:
        gpg._get_encrypted_path.assert_called_once_with(src_path)
        if encr_path:
            gpg._gpg_encrypt.assert_called_once_with(encr_path,
                                                     SOME_FINGERPRINT)
            gpg._gpg_decrypt.assert_called_once_with(encr_path)
    gpg_space.space.create_local_directory.assert_called_once_with(dst_path)