Skip to content

Commit c60b2a1

Browse files
committed
refactor: update GPG key import to return fingerprint instead of boolean
1 parent 7370dc9 commit c60b2a1

File tree

1 file changed

+10
-60
lines changed

1 file changed

+10
-60
lines changed

tests/test_gpg_security.py

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
class TestGPGSecurity:
1111
"""Security tests for GPG encryption"""
1212

13-
def test_gpg_import_returns_false_on_failure(self):
14-
"""Test that import_gpg_key returns False when it fails"""
13+
def test_gpg_import_returns_none_on_failure(self):
14+
"""Test that import_gpg_key returns None when it fails"""
1515
# Test with invalid URL
1616
result = imapbackup.import_gpg_key("https://invalid-url-that-does-not-exist.example.com/key.asc")
17-
assert result == False
17+
assert result is None
1818

19-
def test_gpg_import_returns_false_on_invalid_key(self):
20-
"""Test that import_gpg_key returns False for invalid key content"""
19+
def test_gpg_import_returns_none_on_invalid_key(self):
20+
"""Test that import_gpg_key returns None for invalid key content"""
2121
result = imapbackup.import_gpg_key("not a valid key")
22-
assert result == False
22+
assert result is None
2323

2424
@patch('imapbackup.import_gpg_key')
2525
@patch('imapbackup.connect_and_login')
@@ -35,7 +35,7 @@ def test_process_account_fails_when_gpg_key_import_fails(
3535
mock_server = MagicMock()
3636
mock_connect.return_value = mock_server
3737
mock_get_names.return_value = []
38-
mock_import_key.return_value = False # Simulate key import failure
38+
mock_import_key.return_value = None # Simulate key import failure
3939

4040
# Create config with GPG encryption enabled
4141
config = {
@@ -73,7 +73,7 @@ def test_process_account_continues_when_gpg_key_import_succeeds(
7373
mock_server = MagicMock()
7474
mock_connect.return_value = mock_server
7575
mock_get_names.return_value = [] # No folders to process
76-
mock_import_key.return_value = True # Simulate successful key import
76+
mock_import_key.return_value = 'ABCD1234EFGH5678IJKL9012MNOP3456QRST7890' # Return fingerprint
7777

7878
# Create config with GPG encryption enabled
7979
config = {
@@ -145,7 +145,7 @@ def test_gpg_import_with_network_failure(self, mock_subprocess):
145145

146146
result = imapbackup.import_gpg_key("https://keys.example.com/public.asc")
147147

148-
assert result == False
148+
assert result is None
149149

150150
@patch('subprocess.run')
151151
def test_gpg_not_installed(self, mock_subprocess):
@@ -155,7 +155,7 @@ def test_gpg_not_installed(self, mock_subprocess):
155155

156156
result = imapbackup.import_gpg_key("https://keys.example.com/public.asc")
157157

158-
assert result == False
158+
assert result is None
159159

160160
def test_security_message_printed_on_failure(self, capsys):
161161
"""Test that security warnings are printed when GPG import fails"""
@@ -188,56 +188,6 @@ def test_encrypt_file_requires_valid_input_file(self):
188188
with pytest.raises(Exception):
189189
imapbackup.encrypt_file_gpg("/tmp/nonexistent-file-xyz-123.txt", "test@example.com")
190190

191-
@patch('subprocess.run')
192-
@patch('os.path.exists')
193-
def test_encrypt_file_uses_no_auto_key_retrieve_flag(self, mock_exists, mock_subprocess, temp_dir):
194-
"""Test that encrypt_file_gpg uses --no-auto-key-retrieve flag to prevent WKD lookups"""
195-
import os
196-
197-
# Create a test file
198-
test_file = os.path.join(temp_dir, "test.txt")
199-
with open(test_file, 'w') as f:
200-
f.write("test data")
201-
202-
# Mock os.path.exists to return True for the output file
203-
def exists_side_effect(path):
204-
if path.endswith('.gpg'):
205-
return True
206-
return os.path.exists(path)
207-
mock_exists.side_effect = exists_side_effect
208-
209-
# Mock subprocess.run to succeed
210-
mock_result = MagicMock()
211-
mock_result.returncode = 0
212-
mock_subprocess.return_value = mock_result
213-
214-
# Call encrypt_file_gpg
215-
try:
216-
result = imapbackup.encrypt_file_gpg(test_file, "test@example.com")
217-
except:
218-
pass # We're only interested in the subprocess call, not the result
219-
220-
# Verify subprocess.run was called
221-
assert mock_subprocess.called
222-
223-
# Get the command that was passed to subprocess.run
224-
call_args = mock_subprocess.call_args
225-
cmd = call_args[0][0] # First positional argument is the command list
226-
227-
# Verify --no-auto-key-retrieve flag is present
228-
assert '--no-auto-key-retrieve' in cmd, \
229-
"GPG command must include --no-auto-key-retrieve flag to prevent WKD auto-retrieval"
230-
231-
# Verify other essential flags are present
232-
assert 'gpg' in cmd[0]
233-
assert '--batch' in cmd
234-
assert '--yes' in cmd
235-
assert '--trust-model' in cmd
236-
assert 'always' in cmd
237-
assert '--encrypt' in cmd
238-
assert '--recipient' in cmd
239-
assert 'test@example.com' in cmd
240-
241191

242192
@pytest.mark.unit
243193
class TestS3EncryptionFailures:

0 commit comments

Comments
 (0)