Skip to content

Commit fa61a7c

Browse files
committed
Code review fixes (2/x)
- Add missing fields
1 parent 664b2c2 commit fa61a7c

File tree

4 files changed

+104
-43
lines changed

4 files changed

+104
-43
lines changed

tests/encryption_/models.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
from django.db import models
22

33
from django_mongodb_backend.fields import (
4+
EncryptedBigIntegerField,
45
EncryptedBinaryField,
56
EncryptedBooleanField,
67
EncryptedCharField,
78
EncryptedDateField,
89
EncryptedDateTimeField,
910
EncryptedDecimalField,
11+
EncryptedDurationField,
1012
EncryptedEmailField,
1113
EncryptedFloatField,
1214
EncryptedGenericIPAddressField,
1315
EncryptedIntegerField,
16+
EncryptedPositiveBigIntegerField,
17+
EncryptedPositiveIntegerField,
18+
EncryptedPositiveSmallIntegerField,
1419
EncryptedSmallIntegerField,
1520
EncryptedTextField,
1621
EncryptedTimeField,
@@ -28,41 +33,48 @@ class Meta:
2833

2934

3035
class Appointment(QueryableEncryptionModelBase):
31-
time = EncryptedTimeField(queries=EQUALITY_QUERY)
36+
start_time = EncryptedTimeField(queries=EQUALITY_QUERY)
3237

3338

3439
class Billing(QueryableEncryptionModelBase):
3540
account_balance = EncryptedDecimalField(max_digits=10, decimal_places=2, queries=RANGE_QUERY)
41+
payment_duration = EncryptedDurationField(queries=RANGE_QUERY) # Duration for billing period
3642

3743

3844
class CreditCard(QueryableEncryptionModelBase):
39-
cc_type = EncryptedCharField(max_length=20, queries=EQUALITY_QUERY)
40-
cc_number = EncryptedIntegerField(queries=EQUALITY_QUERY)
45+
card_type = EncryptedCharField(max_length=20, queries=EQUALITY_QUERY)
46+
card_number = EncryptedIntegerField(queries=EQUALITY_QUERY)
47+
transaction_reference = EncryptedBigIntegerField(
48+
queries=EQUALITY_QUERY
49+
) # Simulating a long transaction ID
4150

4251

4352
class PatientPortalUser(QueryableEncryptionModelBase):
44-
ip_address = EncryptedGenericIPAddressField(queries=EQUALITY_QUERY)
45-
url = EncryptedURLField(queries=EQUALITY_QUERY)
53+
last_login_ip = EncryptedGenericIPAddressField(queries=EQUALITY_QUERY)
54+
profile_url = EncryptedURLField(queries=EQUALITY_QUERY)
4655

4756

4857
class PatientRecord(QueryableEncryptionModelBase):
4958
ssn = EncryptedCharField(max_length=11, queries=EQUALITY_QUERY)
5059
birth_date = EncryptedDateField(queries=RANGE_QUERY)
51-
profile_picture = EncryptedBinaryField(queries=EQUALITY_QUERY)
52-
patient_age = EncryptedSmallIntegerField(queries={**RANGE_QUERY, "min": 0, "max": 100})
60+
profile_picture_data = EncryptedBinaryField(queries=EQUALITY_QUERY)
61+
age = EncryptedSmallIntegerField(queries={**RANGE_QUERY, "min": 0, "max": 100})
5362
weight = EncryptedFloatField(queries=RANGE_QUERY)
63+
insurance_policy_number = EncryptedPositiveBigIntegerField(queries=EQUALITY_QUERY)
64+
emergency_contacts_count = EncryptedPositiveIntegerField(queries=EQUALITY_QUERY)
65+
completed_visits = EncryptedPositiveSmallIntegerField(queries=EQUALITY_QUERY)
5466

5567
# TODO: Embed Billing model
5668
# billing = EncryptedEmbeddedField(Billing)
5769

5870

5971
class Patient(QueryableEncryptionModelBase):
6072
patient_id = EncryptedIntegerField(queries=EQUALITY_QUERY)
61-
patient_name = EncryptedCharField(max_length=100)
62-
patient_notes = EncryptedTextField(queries=EQUALITY_QUERY)
73+
full_name = EncryptedCharField(max_length=100)
74+
notes = EncryptedTextField(queries=EQUALITY_QUERY)
6375
registration_date = EncryptedDateTimeField(queries=EQUALITY_QUERY)
6476
is_active = EncryptedBooleanField(queries=EQUALITY_QUERY)
65-
email = EncryptedEmailField(queries=EQUALITY_QUERY)
77+
contact_email = EncryptedEmailField(queries=EQUALITY_QUERY)
6678

6779
# TODO: Embed PatientRecord model
6880
# patient_record = EncryptedEmbeddedField(PatientRecord)

tests/encryption_/test_fields.py

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import date, datetime, time
1+
from datetime import date, datetime, time, timedelta
22

33
import pymongo
44
from bson.binary import Binary
@@ -20,31 +20,47 @@ class FieldTests(QueryableEncryptionTestCase):
2020
def setUp(self):
2121
Patient.objects.create(
2222
patient_id=1,
23-
patient_name="John Doe",
24-
patient_notes="patient notes " * 25,
23+
full_name="John Doe",
24+
notes="patient notes " * 25,
2525
registration_date=datetime(2023, 10, 1, 12, 0, 0),
2626
is_active=True,
27-
27+
contact_email="[email protected]",
2828
)
2929
PatientRecord.objects.create(
3030
ssn="123-45-6789",
3131
birth_date="1969-01-01",
32-
profile_picture=b"image data",
33-
patient_age=50,
32+
profile_picture_data=b"image data",
33+
age=50,
3434
weight=180.0,
35+
insurance_policy_number=98765432101234,
36+
emergency_contacts_count=2,
37+
completed_visits=3,
38+
)
39+
Billing.objects.create(
40+
account_balance=100.50,
41+
payment_duration=timedelta(days=30),
42+
)
43+
CreditCard.objects.create(
44+
card_type="Visa",
45+
card_number=1234567890123456,
46+
transaction_reference=98765432101234,
47+
)
48+
Appointment.objects.create(start_time="8:00")
49+
PatientPortalUser.objects.create(
50+
last_login_ip="127.0.0.1", profile_url="https://example.com"
3551
)
3652

3753
def test_binaryfield(self):
3854
self.assertEqual(
39-
PatientRecord.objects.get(profile_picture=b"image data").profile_picture, b"image data"
55+
PatientRecord.objects.get(profile_picture_data=b"image data").profile_picture_data,
56+
b"image data",
4057
)
4158

4259
def test_booleanfield(self):
4360
self.assertTrue(Patient.objects.get(patient_id=1).is_active)
4461

4562
def test_charfield(self):
46-
CreditCard.objects.create(cc_type="Visa", cc_number="1234567890123456")
47-
self.assertEqual(CreditCard.objects.get(cc_type="Visa").cc_type, "Visa")
63+
self.assertEqual(CreditCard.objects.get(card_type="Visa").card_type, "Visa")
4864
self.assertEqual(PatientRecord.objects.get(ssn="123-45-6789").ssn, "123-45-6789")
4965

5066
def test_datefield(self):
@@ -61,36 +77,67 @@ def test_datetimefield(self):
6177
)
6278

6379
def test_decimalfield(self):
64-
Billing.objects.create(account_balance=100.50)
6580
self.assertTrue(Billing.objects.filter(account_balance__gte=100.0).exists())
6681

82+
def test_durationfield(self):
83+
self.assertTrue(Billing.objects.filter(payment_duration__gte=timedelta(days=15)).exists())
84+
6785
def test_emailfield(self):
6886
self.assertEqual(
69-
Patient.objects.get(email="[email protected]").email, "[email protected]"
87+
Patient.objects.get(contact_email="[email protected]").contact_email,
88+
7089
)
7190

7291
def test_floatfield(self):
7392
self.assertTrue(PatientRecord.objects.filter(weight__gte=175.0).exists())
7493

7594
def test_integerfield(self):
76-
CreditCard.objects.create(cc_type="Visa", cc_number="1234567890123456")
7795
self.assertEqual(
78-
CreditCard.objects.get(cc_number=1234567890123456).cc_number, 1234567890123456
96+
CreditCard.objects.get(card_number=1234567890123456).card_number, 1234567890123456
97+
)
98+
self.assertEqual(
99+
PatientRecord.objects.get(emergency_contacts_count=2).emergency_contacts_count, 2
100+
)
101+
102+
def test_positive_bigintegerfield(self):
103+
self.assertEqual(
104+
PatientRecord.objects.get(
105+
insurance_policy_number=98765432101234
106+
).insurance_policy_number,
107+
98765432101234,
108+
)
109+
110+
def test_positive_integerfield(self):
111+
self.assertEqual(
112+
PatientRecord.objects.get(emergency_contacts_count=2).emergency_contacts_count, 2
113+
)
114+
115+
def test_positive_smallintegerfield(self):
116+
self.assertEqual(PatientRecord.objects.get(completed_visits=3).completed_visits, 3)
117+
118+
def test_bigintegerfield(self):
119+
self.assertEqual(
120+
CreditCard.objects.get(transaction_reference=98765432101234).transaction_reference,
121+
98765432101234,
79122
)
80123

81124
def test_ipaddressfield(self):
82-
PatientPortalUser.objects.create(ip_address="127.0.0.1", url="https://example.com")
83125
self.assertEqual(
84-
PatientPortalUser.objects.get(ip_address="127.0.0.1").ip_address, "127.0.0.1"
126+
PatientPortalUser.objects.get(last_login_ip="127.0.0.1").last_login_ip, "127.0.0.1"
85127
)
86128

87129
def test_smallintegerfield(self):
88-
self.assertTrue(PatientRecord.objects.filter(patient_age__gte=40).exists())
89-
self.assertFalse(PatientRecord.objects.filter(patient_age__gte=80).exists())
130+
self.assertTrue(PatientRecord.objects.filter(age__gte=40).exists())
131+
self.assertFalse(PatientRecord.objects.filter(age__gte=80).exists())
132+
133+
def test_textfield(self):
134+
self.assertEqual(
135+
Patient.objects.get(notes="patient notes " * 25).notes,
136+
"patient notes " * 25,
137+
)
90138

91139
def test_timefield(self):
92-
Appointment.objects.create(time="8:00")
93-
self.assertEqual(Appointment.objects.get(time="8:00").time, time(8, 0))
140+
self.assertEqual(Appointment.objects.get(start_time="8:00").start_time, time(8, 0))
94141

95142
def test_encrypted_patient_record_in_encrypted_database(self):
96143
patients = connections["encrypted"].database.encryption__patient.find()
@@ -102,16 +149,7 @@ def test_encrypted_patient_record_in_unencrypted_database(self):
102149
conn_params = connections["encrypted"].get_connection_params()
103150
db_name = settings.DATABASES["encrypted"]["NAME"]
104151
if conn_params.pop("auto_encryption_opts", False):
105-
# Call MongoClient instead of get_new_connection because
106-
# get_new_connection will return the encrypted connection
107-
# from the connection pool.
108152
with pymongo.MongoClient(**conn_params) as new_connection:
109153
patientrecords = new_connection[db_name].encryption__patientrecord.find()
110154
ssn = patientrecords[0]["ssn"]
111155
self.assertTrue(isinstance(ssn, Binary))
112-
113-
def test_textfield(self):
114-
self.assertEqual(
115-
Patient.objects.get(patient_notes="patient notes " * 25).patient_notes,
116-
"patient notes " * 25,
117-
)

tests/encryption_/test_management.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,30 @@ class CommandTests(TransactionTestCase):
2929
},
3030
{
3131
"bsonType": "binData",
32-
"path": "profile_picture",
32+
"path": "profile_picture_data",
3333
"queries": {"queryType": "equality"},
3434
},
3535
{
3636
"bsonType": "int",
37-
"path": "patient_age",
37+
"path": "age",
3838
"queries": {"queryType": "range", "max": 100, "min": 0},
3939
},
4040
{
4141
"bsonType": "double",
4242
"path": "weight",
4343
"queries": {"queryType": "range"},
4444
},
45+
{
46+
"bsonType": "long",
47+
"path": "insurance_policy_number",
48+
"queries": {"queryType": "equality"},
49+
},
50+
{
51+
"bsonType": "long",
52+
"path": "emergency_contacts_count",
53+
"queries": {"queryType": "equality"},
54+
},
55+
{"bsonType": "int", "path": "completed_visits", "queries": {"queryType": "equality"}},
4556
]
4657
}
4758

tests/encryption_/test_schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def test_get_encrypted_fields_map(self):
2727
},
2828
{
2929
"bsonType": "string",
30-
"path": "patient_name",
30+
"path": "full_name",
3131
},
3232
{
3333
"bsonType": "string",
34-
"path": "patient_notes",
34+
"path": "notes",
3535
"queries": {"queryType": "equality"},
3636
},
3737
{
@@ -46,7 +46,7 @@ def test_get_encrypted_fields_map(self):
4646
},
4747
{
4848
"bsonType": "string",
49-
"path": "email",
49+
"path": "contact_email",
5050
"queries": {"queryType": "equality"},
5151
},
5252
]

0 commit comments

Comments
 (0)