-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_encryption.py
156 lines (125 loc) · 6.83 KB
/
test_encryption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
"""
test_encryption.py: Unit tests for the encryption and secret phrase reading functionality.
This script tests two main functionalities:
1. Encrypting a patient ID using a secret phrase.
2. Reading a secret phrase from a file.
The test ensures that the encryption process is working as expected,
and that the secret phrase is correctly read from a temporary file.
Unit testing is achieved using the unittest framework.
The tests are run by executing this script directly.
Key Functions:
- test_encrypt_patient_id: Test the encryption of a patient ID.
- test_read_secret_phrase: Test the reading of a secret phrase from a temporary file.
Expected Usage:
- Run the test suite to verify the encryption and secret phrase reading functionality.
- Check the test results to ensure that the functions work as expected.
- Update the tests as needed to cover additional scenarios or edge cases.
Customization & Flexibility:
- The test cases can be extended to cover more encryption scenarios.
- Additional tests can be added to validate specific encryption or secret phrase reading scenarios.
- The test suite can be integrated into a continuous integration pipeline.
Performance & Compatibility:
- The tests are designed to be run in a local development environment.
- The test suite is compatible with Python 3.6+ and the unittest module.
- The tests are optimized for efficiency and reliability.
Best Practices & Maintenance:
- The test suite follows best practices for unit testing and validation.
- It provides comprehensive coverage of the encryption and secret phrase reading functionality.
- The tests are well-documented and can be easily maintained or extended.
Notes:
- This test suite is part of a larger data curation pipeline for medical imaging data.
- It is designed to validate the functionality of the encryption and secret phrase reading modules.
- The tests can be run automatically using a continuous integration service.
References:
- unittest module: https://docs.python.org/3/library/unittest.html
- hashlib module: https://docs.python.org/3/library/hashlib.html
"""
__author__ = "Francisco Maria Calisto"
__maintainer__ = "Francisco Maria Calisto"
__email__ = "francisco.calisto@tecnico.ulisboa.pt"
__license__ = "ACADEMIC & COMMERCIAL"
__version__ = "0.0.3" # Incremented version to reflect improvements
__status__ = "Development"
__credits__ = ["Carlos Santiago",
"Catarina Barata",
"Jacinto C. Nascimento",
"Diogo Araújo"]
import unittest # For creating and running test cases
import os # To handle file paths and environment variables
import tempfile # To create temporary files for testing
import logging # To log messages and steps in the testing process
import sys # To modify system path for module importing
# Add the project root directory to the system path for importing modules from 'src'
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
# Import the functions we are testing from the encryption module
from src.processing.encryption import encrypt_patient_id, read_secret_phrase
# Configure logging to log at the INFO level, including timestamps for each log
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
class TestEncryptionFunctions(unittest.TestCase):
"""
This class contains unit tests for the encryption module.
It ensures that patient ID encryption and secret phrase reading work correctly.
"""
def setUp(self):
"""
The setUp() method is executed before each test case.
It creates a temporary file with a mock secret phrase, which is used to simulate reading from a real secret file.
It also sets the SECRET_FILE_PATH environment variable to point to this temporary file.
"""
logging.info("Setting up temporary secret file for testing...")
# Create a temporary file to store the secret phrase
self.temp_secret_file = tempfile.NamedTemporaryFile(delete=False)
# Write the mock secret phrase into the file and flush the contents to disk
self.temp_secret_file.write(b"my_secret_phrase")
self.temp_secret_file.flush()
# Set the environment variable to point to the temporary file's path
os.environ["SECRET_FILE_PATH"] = self.temp_secret_file.name
logging.info(f"Temporary secret file created at: {self.temp_secret_file.name}")
def tearDown(self):
"""
The tearDown() method is executed after each test case.
It cleans up by removing the temporary secret file created during the setUp() phase.
"""
logging.info(f"Cleaning up temporary file: {self.temp_secret_file.name}")
if os.path.exists(self.temp_secret_file.name):
os.remove(self.temp_secret_file.name) # Remove the temporary file
logging.info("Temporary file removed successfully.")
def test_encrypt_patient_id(self):
"""
This test ensures that the encrypt_patient_id function:
1. Encrypts the patient ID.
2. The encrypted ID has the same length as the original patient ID.
"""
logging.info("Starting test: test_encrypt_patient_id")
# Define a mock patient ID for testing
test_patient_id = "12345678"
logging.info(f"Original Patient ID: {test_patient_id}")
# Encrypt the patient ID
encrypted_id = encrypt_patient_id(test_patient_id)
logging.info(f"Encrypted Patient ID: {encrypted_id}")
# Verify that the encrypted ID is different from the original
self.assertNotEqual(encrypted_id, test_patient_id, "Encrypted ID should not be the same as the original.")
# Verify that the encrypted ID has the same length as the original ID
self.assertEqual(len(encrypted_id), len(test_patient_id), "Encrypted ID should have the same length as the original.")
logging.info("Test passed: Encrypted ID differs from the original and has the correct length.")
def test_read_secret_phrase(self):
"""
This test checks that the read_secret_phrase function correctly reads the secret phrase from the temporary file.
"""
logging.info("Starting test: test_read_secret_phrase")
# Read the secret phrase from the temporary file
secret_phrase = read_secret_phrase(self.temp_secret_file.name)
logging.info(f"Secret phrase read: {secret_phrase}")
# Verify that the read secret phrase matches the expected value
self.assertEqual(secret_phrase, "my_secret_phrase", "Secret phrase read does not match the expected value.")
logging.info("Test passed: Secret phrase read matches the expected value.")
if __name__ == "__main__":
"""
Entry point of the script when executed directly.
It initializes logging and runs all the test cases within the TestEncryptionFunctions class.
"""
logging.info("Starting the encryption test suite...")
unittest.main() # Discover and run all test methods in the TestEncryptionFunctions class
logging.info("All encryption tests completed successfully.")
# End of file