Add test script
This commit is contained in:
144
tests/test_api.py
Normal file
144
tests/test_api.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for the GPG API using python-gpg library
|
||||
"""
|
||||
|
||||
from app import UPLOAD_DIR, GPG_KEYRING_DIR, app
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
import gnupg
|
||||
|
||||
|
||||
class TestGPGAPI(unittest.TestCase):
|
||||
"""Test class for GPG API endpoints"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Set up test fixtures before each test method."""
|
||||
cls.test_name = "Test User"
|
||||
cls.test_email = "test@example.com"
|
||||
cls.test_passphrase = "testpassphrase123"
|
||||
cls.app = app.test_client() # Create a test client for the Flask app
|
||||
cls.app.testing = True
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
"""Clean up after all tests in this class."""
|
||||
print("\nTearing down test class...")
|
||||
try:
|
||||
if os.path.exists(UPLOAD_DIR):
|
||||
shutil.rmtree(UPLOAD_DIR) # Remove directory and all contents
|
||||
if os.path.exists(GPG_KEYRING_DIR):
|
||||
shutil.rmtree(GPG_KEYRING_DIR) # Remove directory and all contents
|
||||
except OSError as e:
|
||||
print(f"Warning: Failed to clean up directories: {e}")
|
||||
print("Test class teardown complete.")
|
||||
|
||||
def test_1_key_generation(self):
|
||||
"""Test key generation"""
|
||||
print("\nTesting key generation...")
|
||||
|
||||
key_data = {
|
||||
"name": "Test User",
|
||||
"email": self.test_email,
|
||||
"comment": "Test Key",
|
||||
"passphrase": self.test_passphrase,
|
||||
}
|
||||
|
||||
response = self.app.post("/api/generate-key", json=key_data)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_2_export_key(self):
|
||||
"""Test key export"""
|
||||
print("\nTesting key export...")
|
||||
response = self.app.post(
|
||||
"/api/download/public-key", json={"email": self.test_email}
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_3_encrypt_file(self):
|
||||
"""Test file encryption using the API"""
|
||||
print("\nTesting file encryption...")
|
||||
|
||||
# Test file path
|
||||
test_file_path = Path(__file__).parent / "resources" / "testing_file.txt"
|
||||
if not test_file_path.exists():
|
||||
print(f"Warning: Test file not found at {test_file_path}")
|
||||
self.skipTest("Test file not found")
|
||||
|
||||
# Encrypt file using the API
|
||||
with open(test_file_path, "rb") as f:
|
||||
data = {"email": self.test_email, "file": (f, "testing_file.txt")}
|
||||
response = self.app.post("/api/encrypt-file", data=data)
|
||||
|
||||
if response.status_code != 200:
|
||||
print(f"Encryption Status: {response.status_code}")
|
||||
print(f"Response: {response.get_json()}")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Save the encrypted file for the decrypt test
|
||||
encrypted_file_path = (
|
||||
Path(__file__).parent / "resources" / "testing_encrypted.gpg"
|
||||
)
|
||||
with open(encrypted_file_path, "wb") as f:
|
||||
f.write(response.data)
|
||||
|
||||
return encrypted_file_path
|
||||
|
||||
def test_4_decrypt_file(self):
|
||||
"""Test file decryption"""
|
||||
print("\nTesting file decryption...")
|
||||
|
||||
# First encrypt a file using the API
|
||||
encrypted_file_path = self.test_3_encrypt_file()
|
||||
|
||||
# Now decrypt it using the decrypt endpoint
|
||||
with open(encrypted_file_path, "rb") as f:
|
||||
data = {
|
||||
"passphrase": self.test_passphrase,
|
||||
"file": (f, "api_encrypted.gpg"),
|
||||
}
|
||||
response = self.app.post("/api/upload-decrypt", data=data)
|
||||
|
||||
print(f"Decryption Status: {response.status_code}")
|
||||
if response.status_code != 200:
|
||||
print(f"Response: {response.get_json()}")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
# tests = [
|
||||
# test_setup,
|
||||
# test_key_generation,
|
||||
# test_list_keys,
|
||||
# test_export_key,
|
||||
# test_text_encryption,
|
||||
# ]
|
||||
#
|
||||
# passed = 0
|
||||
# for test in tests:
|
||||
# try:
|
||||
# if test():
|
||||
# passed += 1
|
||||
# print("✓ PASSED")
|
||||
# else:
|
||||
# print("✗ FAILED")
|
||||
# except Exception as e:
|
||||
# print(f"✗ ERROR: {e}")
|
||||
# print("-" * 50)
|
||||
#
|
||||
# print(f"\nTest Results: {passed}/{len(tests)} tests passed")
|
||||
#
|
||||
# if passed == len(tests):
|
||||
# print("🎉 All tests passed!")
|
||||
# else:
|
||||
# print("❌ Some tests failed")
|
||||
#
|
||||
#
|
||||
# if __name__ == "__main__":
|
||||
# sys.exit(main())
|
||||
Reference in New Issue
Block a user