def test_entry_id(): from parsec.api.data.entry import EntryID, _RsEntryID, _PyEntryID assert EntryID is _RsEntryID ID = uuid4() py_ei = _PyEntryID(ID) rs_ei = EntryID(ID) assert str(py_ei) == str(rs_ei) assert repr(py_ei) == repr(rs_ei) assert py_ei.uuid == rs_ei.uuid assert py_ei.hex == rs_ei.hex assert py_ei.bytes == rs_ei.bytes assert str(EntryID.from_hex(str(ID))) == str(_PyEntryID.from_hex(str(ID))) assert str(EntryID.from_bytes(ID.bytes)) == str( _PyEntryID.from_bytes(ID.bytes)) with pytest.raises(ValueError) as excinfo: _PyEntryID(str(uuid4())) assert str(excinfo.value) == "Not a UUID" with pytest.raises(ValueError) as excinfo: EntryID(str(uuid4())) assert str(excinfo.value) == "Not a UUID"
async def test_upsert_file(alice_workspace): _update_file_mock = AsyncMock(spec=mock.Mock()) _create_path_mock = AsyncMock(spec=mock.Mock()) path = FsPath("/test") workspace_path = FsPath("/path_in_workspace") entry_id = EntryID() with mock.patch("parsec.core.cli.rsync._create_path", _create_path_mock): with mock.patch("parsec.core.cli.rsync._update_file", _update_file_mock): await rsync._upsert_file(entry_id, alice_workspace, path, workspace_path) _update_file_mock.assert_called_once_with(alice_workspace, entry_id, path, workspace_path) _create_path_mock.assert_not_called() _update_file_mock.reset_mock() entry_id = None with mock.patch("parsec.core.cli.rsync._create_path", _create_path_mock): with mock.patch("parsec.core.cli.rsync._update_file", _update_file_mock): await rsync._upsert_file(None, alice_workspace, path, workspace_path) _update_file_mock.assert_not_called() _create_path_mock.assert_called_once_with(alice_workspace, False, path, workspace_path)
async def test_get_or_create_directory(alice_workspace): load_manifest_mock = AsyncMock(spec=mock.Mock(), side_effect=lambda x: "load_manifest_mock") alice_workspace.remote_loader.load_manifest = load_manifest_mock _create_path_mock = AsyncMock(spec=mock.Mock(), side_effect=lambda *x: "_create_path_mock") with mock.patch("parsec.core.cli.rsync._create_path", _create_path_mock): entry_id = EntryID() res = await rsync._get_or_create_directory( entry_id, alice_workspace, FsPath("/test_directory"), FsPath("/path_in_workspace")) load_manifest_mock.assert_called_once_with(entry_id) _create_path_mock.assert_not_called() assert res == "load_manifest_mock" load_manifest_mock.reset_mock() with mock.patch("parsec.core.cli.rsync._create_path", _create_path_mock): res = await rsync._get_or_create_directory( None, alice_workspace, FsPath("/test_directory"), FsPath("/path_in_workspace")) load_manifest_mock.assert_not_called() _create_path_mock.assert_called_once_with(alice_workspace, True, FsPath("/test_directory"), FsPath("/path_in_workspace")) assert res == "_create_path_mock"
def test_local_device(): from parsec.core.types.local_device import _RsLocalDevice, LocalDevice, _PyLocalDevice assert LocalDevice is _RsLocalDevice def _assert_local_device_eq(py, rs): assert isinstance(py, _PyLocalDevice) assert isinstance(rs, _RsLocalDevice) assert py.organization_addr == rs.organization_addr assert py.device_id == rs.device_id assert py.device_label == rs.device_label assert py.human_handle == rs.human_handle assert py.signing_key == rs.signing_key assert py.private_key == rs.private_key assert py.profile == rs.profile assert py.user_manifest_id == rs.user_manifest_id assert py.user_manifest_key == rs.user_manifest_key assert py.local_symkey == rs.local_symkey assert py.is_admin == rs.is_admin assert py.is_outsider == rs.is_outsider assert py.slug == rs.slug assert py.slughash == rs.slughash assert py.root_verify_key == rs.root_verify_key assert py.organization_id == rs.organization_id assert py.device_name == rs.device_name assert py.user_id == rs.user_id assert py.verify_key == rs.verify_key assert py.public_key == rs.public_key assert py.user_display == rs.user_display assert py.short_user_display == rs.short_user_display assert py.device_display == rs.device_display signing_key = SigningKey.generate() kwargs = { "organization_addr": BackendOrganizationAddr.build( BackendAddr.from_url("parsec://foo"), organization_id=OrganizationID("org"), root_verify_key=signing_key.verify_key, ), "device_id": DeviceID.new(), "device_label": None, "human_handle": None, "signing_key": signing_key, "private_key": PrivateKey.generate(), "profile": UserProfile.ADMIN, "user_manifest_id": EntryID.new(), "user_manifest_key": SecretKey.generate(), "local_symkey": SecretKey.generate(), } py_ba = _PyLocalDevice(**kwargs) rs_ba = LocalDevice(**kwargs) _assert_local_device_eq(py_ba, rs_ba)
def new(cls: Type[WorkspaceEntryTypeVar], name: str) -> "WorkspaceEntry": now = pendulum_now() return WorkspaceEntry( name=name, id=EntryID(), key=SecretKey.generate(), encryption_revision=1, encrypted_on=now, role_cached_on=now, role=RealmRole.OWNER, )
def new(cls: Type[WorkspaceEntryTypeVar], name: EntryName, timestamp: DateTime) -> "WorkspaceEntry": assert isinstance(name, EntryName) return _PyWorkspaceEntry( name=name, id=EntryID.new(), key=SecretKey.generate(), encryption_revision=1, encrypted_on=timestamp, role_cached_on=timestamp, role=RealmRole.OWNER, )
async def test_sync_directory(alice_workspace): _get_or_create_directory_mock = AsyncMock( spec=mock.Mock(), side_effect=lambda *x: "folder_manifest_mock") _sync_directory_content_mock = AsyncMock(spec=mock.Mock()) _clear_directory_mock = AsyncMock(spec=mock.Mock()) entry_id = EntryID() path = FsPath("/test") workspace_path = FsPath("/path_in_workspace") with mock.patch("parsec.core.cli.rsync._get_or_create_directory", _get_or_create_directory_mock): with mock.patch("parsec.core.cli.rsync._sync_directory_content", _sync_directory_content_mock): with mock.patch("parsec.core.cli.rsync._clear_directory", _clear_directory_mock): await rsync._sync_directory(entry_id, alice_workspace, path, workspace_path) _get_or_create_directory_mock.assert_called_once_with( entry_id, alice_workspace, path, workspace_path) _sync_directory_content_mock.assert_called_once_with( workspace_path, path, alice_workspace, "folder_manifest_mock") _clear_directory_mock.assert_called_once_with( workspace_path, path, alice_workspace, "folder_manifest_mock") _get_or_create_directory_mock.reset_mock() _sync_directory_content_mock.reset_mock() _clear_directory_mock.reset_mock() with mock.patch("parsec.core.cli.rsync._get_or_create_directory", _get_or_create_directory_mock): with mock.patch("parsec.core.cli.rsync._sync_directory_content", _sync_directory_content_mock): with mock.patch("parsec.core.cli.rsync._clear_directory", _clear_directory_mock): await rsync._sync_directory(None, alice_workspace, path, workspace_path) _get_or_create_directory_mock.assert_called_once_with( None, alice_workspace, path, workspace_path) _sync_directory_content_mock.assert_called_once_with( workspace_path, path, alice_workspace, "folder_manifest_mock") _clear_directory_mock.assert_not_called()
async def test_update_file(alice_workspace): block_mock1 = mock.Mock() block_mock1.digest = b"block1" block_mock2 = mock.Mock() block_mock2.digest = b"block2" manifest_mock = mock.Mock() manifest_mock.blocks = [block_mock1, block_mock2] load_manifest_mock = AsyncMock(spec=mock.Mock, side_effect=lambda x: manifest_mock) alice_workspace.remote_loader.load_manifest = load_manifest_mock write_bytes_mock = AsyncMock(spec=mock.Mock) alice_workspace.write_bytes = write_bytes_mock sync_by_id_mock = AsyncMock(spec=mock.Mock) alice_workspace.sync_by_id = sync_by_id_mock HashDigest.from_data = mock.Mock(side_effect=lambda x: x) with mock.patch( "parsec.core.cli.rsync._chunks_from_path", AsyncMock(spec=mock.Mock, side_effect=[[b"block1", b"block2"]]), ): entry_id = EntryID() await rsync._update_file(alice_workspace, entry_id, FsPath("/src_file"), FsPath("/path_in_workspace")) rsync._chunks_from_path.assert_called_once_with(FsPath("/src_file")) load_manifest_mock.assert_called_once_with(entry_id) write_bytes_mock.assert_not_called() sync_by_id_mock.assert_called_once_with(entry_id, remote_changed=False, recursive=False) load_manifest_mock.reset_mock() sync_by_id_mock.reset_mock() with mock.patch( "parsec.core.cli.rsync._chunks_from_path", AsyncMock(spec=mock.Mock, side_effect=[[b"block1", b"block3"]]), ): await rsync._update_file(alice_workspace, entry_id, FsPath("/src_file"), FsPath("/path_in_workspace")) rsync._chunks_from_path.assert_called_once_with(FsPath("/src_file")) load_manifest_mock.assert_called_once_with(entry_id) write_bytes_mock.assert_called_once_with(FsPath("/path_in_workspace"), b"block3", len("block1")) sync_by_id_mock.assert_called_once_with(entry_id, remote_changed=False, recursive=False) load_manifest_mock.reset_mock() sync_by_id_mock.reset_mock() write_bytes_mock.reset_mock() with mock.patch( "parsec.core.cli.rsync._chunks_from_path", AsyncMock(spec=mock.Mock, side_effect=[[b"block3", b"block4"]]), ): await rsync._update_file(alice_workspace, entry_id, FsPath("/src_file"), FsPath("/path_in_workspace")) rsync._chunks_from_path.assert_called_once_with(FsPath("/src_file")) alice_workspace.remote_loader.load_manifest.assert_called_once_with( entry_id) write_bytes_mock.assert_has_calls([ mock.call(FsPath("/path_in_workspace"), b"block3", 0), mock.call(FsPath("/path_in_workspace"), b"block4", len("block3")), ]) sync_by_id_mock.assert_called_once_with(entry_id, remote_changed=False, recursive=False)