Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when encrypting a file with chinese characters #496

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lean/commands/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def decrypt(project: Path,
source_files = project_manager.get_source_files(project)
try:
from lean.components.util.encryption_helper import get_decrypted_file_content_for_local_project
decrypted_data = get_decrypted_file_content_for_local_project(project,
decrypted_data = get_decrypted_file_content_for_local_project(project,
source_files, decryption_key, project_config_manager, organization_id)
except Exception as e:
raise RuntimeError(f"Could not decrypt project {project}: {e}")

for file, decrypted in zip(source_files, decrypted_data):
with open(file, 'w') as f:
with open(file, 'w', encoding="utf-8") as f:
f.write(decrypted)

# Mark the project as decrypted
Expand Down
2 changes: 1 addition & 1 deletion lean/commands/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def encrypt(project: Path,
except Exception as e:
raise RuntimeError(f"Could not encrypt project {project}: {e}")
for file, encrypted in zip(source_files, encrypted_data):
with open(file, 'w') as f:
with open(file, 'w', encoding="utf-8") as f:
f.write(encrypted)

# Mark the project as encrypted
Expand Down
4 changes: 2 additions & 2 deletions lean/components/util/encryption_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def get_decrypted_file_content_for_local_project(project: Path, source_files: Li
for file in source_files:
try:
# lets read and decrypt the file
with open(file, 'r') as f:
with open(file, 'r', encoding="utf-8") as f:
encrypted = f.read()
if not areProjectFilesAlreadyEncrypted:
decrypted = encrypted
Expand Down Expand Up @@ -131,7 +131,7 @@ def get_encrypted_file_content_for_local_project(project: Path, source_files: Li
for file in source_files:
try:
# lets read and decrypt the file
with open(file, 'r') as f:
with open(file, 'r', encoding= "utf-8") as f:
plain_text = f.read()
if areProjectFilesAlreadyEncrypted:
encrypted = plain_text
Expand Down
30 changes: 30 additions & 0 deletions tests/commands/test_decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,36 @@ def test_decrypt_decrypts_file_in_case_project_not_in_decrypt_state() -> None:
assert project_config.get("encrypted", False) == False
assert project_config.get("encryption-key-path", None) == None

def test_decrypt_decrypts_file_that_has_a_chinese_character() -> None:
create_fake_lean_cli_directory()

project_path = Path.cwd() / "Python Project"

source_files = container.project_manager.get_source_files(project_path)
file_contents_map = {file.name: file.read_text() for file in source_files}

encryption_file_path = project_path / "encryption_x.txt"
encryption_file_path.write_text("是一的", encoding="utf-8")

result = CliRunner().invoke(lean, ["encrypt", "Python Project", "--key", encryption_file_path])

project_config = container.project_config_manager.get_project_config(project_path)
assert project_config.get("encrypted", False) != False
assert project_config.get("encryption-key-path", None) != None
for file in source_files:
assert file_contents_map[file.name] != file.read_text()

result = CliRunner().invoke(lean, ["decrypt", "Python Project", "--key", encryption_file_path])

assert result.exit_code == 0

source_files = container.project_manager.get_source_files(project_path)
for file in source_files:
assert file_contents_map[file.name] == file.read_text()
project_config = container.project_config_manager.get_project_config(project_path)
assert project_config.get("encrypted", False) == False
assert project_config.get("encryption-key-path", None) == None


def test_decrypt_does_not_change_file_in_case_project_already_in_decrypt_state() -> None:
create_fake_lean_cli_directory()
Expand Down
69 changes: 68 additions & 1 deletion tests/commands/test_encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ def test_encrypt_encrypts_file_in_case_project_not_in_encrypt_state() -> None:
assert project_config.get("encrypted", False) == True
assert project_config.get("encryption-key-path", None) == str(encryption_file_path)

def test_encrypt_encrypts_file_with_chinese_characters() -> None:
create_fake_lean_cli_directory()

project_path = Path.cwd() / "Python Project"

encryption_file_path = project_path / "encryption_x.txt"
encryption_file_path.write_text("是一的", encoding="utf-8")

project_config = container.project_config_manager.get_project_config(project_path)
assert project_config.get("encrypted", False) == False
assert project_config.get("encryption-key-path", None) == None

result = CliRunner().invoke(lean, ["encrypt", "Python Project", "--key", encryption_file_path])

assert result.exit_code == 0

source_files = container.project_manager.get_source_files(project_path)
expected_encrypted_files = _get_expected_encrypted_files_content()
for file in source_files:
assert expected_encrypted_files["chinese_" + file.name].strip() == file.read_text().strip()
project_config = container.project_config_manager.get_project_config(project_path)
assert project_config.get("encrypted", False) == True
assert project_config.get("encryption-key-path", None) == str(encryption_file_path)


def test_encrypt_does_not_change_file_in_case_project_already_in_encrypt_state() -> None:
create_fake_lean_cli_directory()
Expand Down Expand Up @@ -167,6 +191,23 @@ def _get_expected_encrypted_files_content() -> dict:
4T/vRU2aSdisuNMdXyuF4OH7ZgBdUYaNtfxmuJlmS4tYsom5xJfxrEEGG203gq0ME5eZCmu4JlLbEo1w
L4u74Hsr4mWkJKbMJMcwW8ByRuy/VJiWW8JKIcoB0yHlwLJ/YoqMDF0BPG5i2EF0DXu1USNC/vE="""
,
"chinese_main.py":
"""/hBXLMAwLMr3D0WTwS5lbHxJquOwtubD1rwNFPyrPkaFfn4oJF/MlHR0+ibh0S/HmOfZy3bSaNUNyVlv
GwUc6BkTVduHfl0m0doinxtovFkLMi33PWYwTrdr7tXWoklzq+J+AyjmaYiJsN9GxJvUzM3fsvRR7+5h
6j16o+zI/PHntLsldC2+66e3E1yP+b6uYrOaqubIs6ORGV3G9oViLfKCADiHTRLH+7885cJxN1Is1wOE
5zIIe4DOBnq92XpazPM/1Lk3ECcu/bno2capvjRtqXZiINv7QGGtcHyZ5C4vanfE9KkUbXTaDBtxrxSH
jmI/kNo+2DFM9BQ9YWK8bBYBUEt6cBDnyM6yBC33QCoYVOm37p3lj5mZkeePPSMdLmXfi7tZ7iySuT49
/iWJ88rddbKVKU2qMtGjem5loKTOtKWoqd2R/fpOLAr+rH2D/Zz/hwomD+TdQ3DyWSYES3B9mMhk2Mpk
jtWkTi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