|
1 |
| -from datetime import datetime, time |
| 1 | +from datetime import datetime |
2 | 2 |
|
3 |
| -import pymongo |
4 |
| -from bson.binary import Binary |
5 |
| -from django.conf import settings |
6 |
| -from django.db import connections, models |
7 | 3 | from django.test import TransactionTestCase, override_settings
|
8 | 4 |
|
9 |
| -from django_mongodb_backend.fields import EncryptedFieldMixin |
10 |
| - |
11 | 5 | from .models import (
|
12 | 6 | Appointment,
|
13 | 7 | Billing,
|
14 |
| - EncryptedNumbers, |
15 | 8 | Patient,
|
16 | 9 | PatientPortalUser,
|
17 | 10 | PatientRecord,
|
18 | 11 | )
|
19 | 12 | from .routers import TestEncryptedRouter
|
20 | 13 |
|
21 | 14 |
|
22 |
| -class EncryptedDurationField(EncryptedFieldMixin, models.DurationField): |
23 |
| - """ |
24 |
| - Unsupported by MongoDB when used with Queryable Encryption. |
25 |
| - Included in tests until fix or wontfix. |
26 |
| - """ |
27 |
| - |
28 |
| - |
29 |
| -class EncryptedSlugField(EncryptedFieldMixin, models.SlugField): |
30 |
| - """ |
31 |
| - Unsupported by MongoDB when used with Queryable Encryption. |
32 |
| - Included in tests until fix or wontfix. |
33 |
| - """ |
34 |
| - |
35 |
| - |
36 | 15 | @override_settings(DATABASE_ROUTERS=[TestEncryptedRouter()])
|
37 |
| -class EncryptedFieldTests(TransactionTestCase): |
| 16 | +class QueryableEncryptionTestCase(TransactionTestCase): |
38 | 17 | databases = {"default", "encrypted"}
|
39 | 18 | available_apps = ["encryption_"]
|
40 | 19 |
|
@@ -65,142 +44,3 @@ def setUp(self):
|
65 | 44 |
|
66 | 45 | # TODO: Embed billing and patient_record models in patient model
|
67 | 46 | # then add tests
|
68 |
| - |
69 |
| - def test_get_encrypted_fields_map(self): |
70 |
| - """Test class method called by schema editor |
71 |
| - and management command to get encrypted fields map for |
72 |
| - `create_encrypted_collection` and `auto_encryption_opts` respectively. |
73 |
| - There are no data keys in the results. |
74 |
| -
|
75 |
| - Data keys for the schema editor are created by |
76 |
| - `create_encrypted_collection` and data keys for the |
77 |
| - management command are created by the management command |
78 |
| - using code similar to the code in `create_encrypted_collection` |
79 |
| - in Pymongo. |
80 |
| - """ |
81 |
| - expected_encrypted_fields_map = { |
82 |
| - "fields": [ |
83 |
| - { |
84 |
| - "bsonType": "int", |
85 |
| - "path": "patient_id", |
86 |
| - "queries": {"queryType": "equality"}, |
87 |
| - }, |
88 |
| - { |
89 |
| - "bsonType": "string", |
90 |
| - "path": "patient_name", |
91 |
| - }, |
92 |
| - { |
93 |
| - "bsonType": "string", |
94 |
| - "path": "patient_notes", |
95 |
| - "queries": {"queryType": "equality"}, |
96 |
| - }, |
97 |
| - { |
98 |
| - "bsonType": "date", |
99 |
| - "path": "registration_date", |
100 |
| - "queries": {"queryType": "equality"}, |
101 |
| - }, |
102 |
| - { |
103 |
| - "bsonType": "bool", |
104 |
| - "path": "is_active", |
105 |
| - "queries": {"queryType": "equality"}, |
106 |
| - }, |
107 |
| - { |
108 |
| - "bsonType": "string", |
109 |
| - "path": "email", |
110 |
| - "queries": {"queryType": "equality"}, |
111 |
| - }, |
112 |
| - ] |
113 |
| - } |
114 |
| - connection = connections["encrypted"] |
115 |
| - auto_encryption_opts = getattr(connection.connection._options, "auto_encryption_opts", None) |
116 |
| - with connection.schema_editor() as editor: |
117 |
| - client = connection.connection |
118 |
| - encrypted_fields_map = editor._get_encrypted_fields_map( |
119 |
| - self.patient, client, auto_encryption_opts |
120 |
| - ) |
121 |
| - for field in encrypted_fields_map["fields"]: |
122 |
| - # Remove data keys from the output; they are expected to differ |
123 |
| - field.pop("keyId", None) |
124 |
| - self.assertEqual( |
125 |
| - encrypted_fields_map, |
126 |
| - expected_encrypted_fields_map, |
127 |
| - ) |
128 |
| - |
129 |
| - def test_appointment(self): |
130 |
| - self.assertEqual(Appointment.objects.get(time="8:00").time, time(8, 0)) |
131 |
| - |
132 |
| - def test_billing(self): |
133 |
| - self.assertEqual( |
134 |
| - Billing.objects.get(cc_number=1234567890123456).cc_number, 1234567890123456 |
135 |
| - ) |
136 |
| - self.assertEqual(Billing.objects.get(cc_type="Visa").cc_type, "Visa") |
137 |
| - self.assertTrue(Billing.objects.filter(account_balance__gte=100.0).exists()) |
138 |
| - |
139 |
| - def test_patientportaluser(self): |
140 |
| - self.assertEqual( |
141 |
| - PatientPortalUser.objects.get(ip_address="127.0.0.1").ip_address, "127.0.0.1" |
142 |
| - ) |
143 |
| - |
144 |
| - def test_patientrecord(self): |
145 |
| - self.assertEqual(PatientRecord.objects.get(ssn="123-45-6789").ssn, "123-45-6789") |
146 |
| - with self.assertRaises(PatientRecord.DoesNotExist): |
147 |
| - PatientRecord.objects.get(ssn="000-00-0000") |
148 |
| - self.assertTrue(PatientRecord.objects.filter(birth_date__gte="1969-01-01").exists()) |
149 |
| - self.assertEqual( |
150 |
| - PatientRecord.objects.get(ssn="123-45-6789").profile_picture, b"image data" |
151 |
| - ) |
152 |
| - self.assertTrue(PatientRecord.objects.filter(patient_age__gte=40).exists()) |
153 |
| - self.assertFalse(PatientRecord.objects.filter(patient_age__gte=80).exists()) |
154 |
| - self.assertTrue(PatientRecord.objects.filter(weight__gte=175.0).exists()) |
155 |
| - |
156 |
| - # Test encrypted patient record in unencrypted database. |
157 |
| - conn_params = connections["encrypted"].get_connection_params() |
158 |
| - db_name = settings.DATABASES["encrypted"]["NAME"] |
159 |
| - if conn_params.pop("auto_encryption_opts", False): |
160 |
| - # Call MongoClient instead of get_new_connection because |
161 |
| - # get_new_connection will return the encrypted connection |
162 |
| - # from the connection pool. |
163 |
| - with pymongo.MongoClient(**conn_params) as new_connection: |
164 |
| - patientrecords = new_connection[db_name].encryption__patientrecord.find() |
165 |
| - ssn = patientrecords[0]["ssn"] |
166 |
| - self.assertTrue(isinstance(ssn, Binary)) |
167 |
| - |
168 |
| - def test_patient(self): |
169 |
| - self.assertEqual( |
170 |
| - Patient.objects.get(patient_notes="patient notes " * 25).patient_notes, |
171 |
| - "patient notes " * 25, |
172 |
| - ) |
173 |
| - self.assertEqual( |
174 |
| - Patient.objects.get( |
175 |
| - registration_date=datetime(2023, 10, 1, 12, 0, 0) |
176 |
| - ).registration_date, |
177 |
| - datetime(2023, 10, 1, 12, 0, 0), |
178 |
| - ) |
179 |
| - self.assertTrue(Patient.objects.get(patient_id=1).is_active) |
180 |
| - self.assertEqual( |
181 |
| - Patient. objects. get( email="[email protected]"). email, "[email protected]" |
182 |
| - ) |
183 |
| - |
184 |
| - # Test decrypted patient record in encrypted database. |
185 |
| - patients = connections["encrypted"].database.encryption__patient.find() |
186 |
| - self.assertEqual(len(list(patients)), 1) |
187 |
| - records = connections["encrypted"].database.encryption__patientrecord.find() |
188 |
| - self.assertTrue("__safeContent__" in records[0]) |
189 |
| - |
190 |
| - def test_numeric_fields(self): |
191 |
| - """ |
192 |
| - Fields that have not been tested elsewhere. |
193 |
| - """ |
194 |
| - EncryptedNumbers.objects.create( |
195 |
| - pos_bigint=1000000, |
196 |
| - pos_smallint=12345, |
197 |
| - smallint=-12345, |
198 |
| - ) |
199 |
| - |
200 |
| - obj = EncryptedNumbers.objects.get(pos_bigint=1000000) |
201 |
| - obj = EncryptedNumbers.objects.get(pos_smallint=12345) |
202 |
| - obj = EncryptedNumbers.objects.get(smallint=-12345) |
203 |
| - |
204 |
| - self.assertEqual(obj.pos_bigint, 1000000) |
205 |
| - self.assertEqual(obj.pos_smallint, 12345) |
206 |
| - self.assertEqual(obj.smallint, -12345) |
0 commit comments