diff --git a/lean/components/cloud/push_manager.py b/lean/components/cloud/push_manager.py index 617e9347..7a582508 100644 --- a/lean/components/cloud/push_manager.py +++ b/lean/components/cloud/push_manager.py @@ -141,10 +141,20 @@ def _push_project(self, project_path: Path, organization_id: str, encryption_act valid_project_name = expected_correct_project_path.relative_to(Path.cwd()).as_posix() self._logger.info(f"Renaming '{project_name}' to '{valid_project_name}'") self._project_manager.rename_project_and_contents(project_path, expected_correct_project_path) + old_project_path = project_path project_path = expected_correct_project_path project_name = valid_project_name project_config = self._project_config_manager.get_project_config(project_path) + # a key stored inside the project directory moved together with it + if local_encryption_key is not None: + moved_key = self._get_moved_path(local_encryption_key, old_project_path, project_path) + if moved_key != local_encryption_key: + local_encryption_key = moved_key + project_config.set("encryption-key-path", str(moved_key)) + if encryption_key is not None: + encryption_key = self._get_moved_path(encryption_key, old_project_path, project_path) + # Find the cloud project to push the files to if cloud_id is not None: # Project has cloud id which matches cloud project, update cloud project @@ -176,6 +186,19 @@ def _push_project(self, project_path: Path, organization_id: str, encryption_act self._push_metadata(project_path, cloud_project, encryption_action, encryption_key, force) return project_path + @staticmethod + def _get_moved_path(path: Path, old_directory: Path, new_directory: Path) -> Path: + """Returns the new location of a path after old_directory has been renamed to new_directory. + + Paths outside old_directory are returned unchanged. + """ + absolute_path = path if path.is_absolute() else Path.cwd() / path + try: + relative_path = absolute_path.relative_to(old_directory) + except ValueError: + return path + return new_directory / relative_path + def _get_files(self, project: Path, encryption_action: Optional[ActionType], encryption_key: Optional[Path]) -> List[Dict[str, str]]: """Pushes the files of a local project to the cloud. diff --git a/tests/commands/cloud/test_push.py b/tests/commands/cloud/test_push.py index e8765f50..c4f5ffa1 100644 --- a/tests/commands/cloud/test_push.py +++ b/tests/commands/cloud/test_push.py @@ -209,6 +209,42 @@ def test_cloud_push_sends_encrypted_files_and_turns_on_encryption_with_encrypted } api_client.projects.update.assert_called_once_with(1, **expected_arguments) +def test_cloud_push_follows_encryption_key_inside_project_when_push_renames_project() -> None: + create_fake_lean_cli_directory() + + project_path = Path.cwd() / "Python Project" + + encryption_file_path = project_path / "encryption.txt" + encryption_file_path.write_text("KtSwJtq5a4uuQmxbPqcCP3d8yMRz5TZxDBAKy7kGwPcvcvsNBdCprGYwSBN8ntJa5JNNYHTB2GrBpAbkA38kCdnceegffZH7") + key_hash = get_project_key_hash(encryption_file_path) + keys_api_data = {'keys': [{'name': 'test', 'hash': key_hash}]} + + api_client = mock.Mock() + api_client.encryption_keys.list = mock.MagicMock(return_value=keys_api_data) + # the cloud assigns a different name, so the push renames the local directory (and the key inside it) + cloud_project = create_api_project(1, "Renamed Project") + api_client.projects.create = mock.MagicMock(return_value=cloud_project) + api_client.projects.get = mock.MagicMock(return_value=cloud_project) + fake_cloud_files = [QCFullFile(name="file.py", content="testing", modified=datetime.now(), isLibrary=False)] + api_client.files.get_all = mock.MagicMock(return_value=fake_cloud_files) + + init_container(api_client_to_use=api_client) + + project_config = container.project_config_manager.get_project_config(project_path) + project_config.set("encrypted", True) + project_config.set("encryption-key-path", str(encryption_file_path)) + + result = CliRunner().invoke(lean, ["cloud", "push", "--project", project_path]) + + assert result.exit_code == 0 + api_client.projects.update.assert_called_once() + + new_project_path = Path.cwd() / "Renamed Project" + assert not project_path.exists() + new_config = container.project_config_manager.get_project_config(new_project_path) + assert Path(new_config.get("encryption-key-path")) == new_project_path / "encryption.txt" + + def test_cloud_push_aborts_sending_encrypted_files_when_local_file_encrypted_with_key_x_and_given_key_y() -> None: create_fake_lean_cli_directory()