Skip to content

Commit f31c705

Browse files
committed
Improved tests
1 parent 4ddfdf8 commit f31c705

File tree

1 file changed

+224
-57
lines changed

1 file changed

+224
-57
lines changed

tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

Lines changed: 224 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,80 @@ namespace MongoDB.Driver.Tests.Encryption
2626
public class CsfleSchemaBuilderTests
2727
{
2828
private const string _keyIdString = "6f4af470-00d1-401f-ac39-f45902a0c0c8";
29-
private static Guid _keyIdExample = Guid.Parse(_keyIdString); //TODO Check if I should remove this
29+
private static Guid _keyId = Guid.Parse(_keyIdString);
30+
31+
[Fact]
32+
public void BasicPropertyTest()
33+
{
34+
const string collectionName = "medicalRecords.patients";
35+
36+
var builder = CsfleSchemaBuilder.Create(schemaBuilder =>
37+
{
38+
schemaBuilder.Encrypt<Patient>(collectionName, builder =>
39+
{
40+
builder
41+
.EncryptMetadata(keyId: _keyId)
42+
.Property(p => p.MedicalRecords, BsonType.Array,
43+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
44+
.Property("bloodType", BsonType.String,
45+
algorithm: CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
46+
.Property(p => p.Ssn, BsonType.Int32,
47+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
48+
.Property(p => p.Insurance, innerBuilder =>
49+
{
50+
innerBuilder
51+
.Property(i => i.PolicyNumber, BsonType.Int32,
52+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic);
53+
});
54+
} );
55+
});
56+
57+
var expected = new Dictionary<string, string>
58+
{
59+
[collectionName] = """
60+
{
61+
"bsonType": "object",
62+
"encryptMetadata": {
63+
"keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
64+
},
65+
"properties": {
66+
"insurance": {
67+
"bsonType": "object",
68+
"properties": {
69+
"policyNumber": {
70+
"encrypt": {
71+
"bsonType": "int",
72+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
73+
}
74+
}
75+
}
76+
},
77+
"medicalRecords": {
78+
"encrypt": {
79+
"bsonType": "array",
80+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
81+
}
82+
},
83+
"bloodType": {
84+
"encrypt": {
85+
"bsonType": "string",
86+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
87+
}
88+
},
89+
"ssn": {
90+
"encrypt": {
91+
"bsonType": "int",
92+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
93+
}
94+
}
95+
},
96+
}
97+
"""
98+
};
99+
100+
AssertOutcomeBuilder(builder, expected);
101+
}
102+
30103

31104
[Theory]
32105
[InlineData(
@@ -148,23 +221,25 @@ public void EncryptedCollection_PatternPropertyNested_works_as_expected()
148221
{
149222
"bsonType": "object",
150223
"patternProperties": {
151-
"bsonType": "object",
152-
"encryptMetadata": {
153-
"keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
154-
},
155-
"properties": {
156-
"policyNumber": {
157-
"encrypt": {
158-
"bsonType": "int",
159-
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
224+
"insurance": {
225+
"bsonType": "object",
226+
"encryptMetadata": {
227+
"keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
228+
},
229+
"properties": {
230+
"policyNumber": {
231+
"encrypt": {
232+
"bsonType": "int",
233+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
234+
}
160235
}
161-
}
162-
},
163-
"patternProperties": {
164-
"randomRegex*": {
165-
"encrypt": {
166-
"bsonType": "string",
167-
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
236+
},
237+
"patternProperties": {
238+
"randomRegex*": {
239+
"encrypt": {
240+
"bsonType": "string",
241+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
242+
}
168243
}
169244
}
170245
}
@@ -192,39 +267,34 @@ public void EncryptedCollection_PatternPropertyNestedWithString_works_as_expecte
192267
});
193268

194269
var expected = """
195-
{
196-
"bsonType": "object",
197-
"patternProperties": {
198-
"bsonType": "object",
199-
"encryptMetadata": {
200-
"keyId": [
201-
{
202-
"$binary": {
203-
"base64": "b0r0cADRQB+sOfRZAqDAyA==",
204-
"subType": "04"
205-
}
206-
}
207-
]
208-
},
209-
"properties": {
210-
"policyNumber": {
211-
"encrypt": {
212-
"bsonType": "int",
213-
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
214-
}
215-
}
216-
},
217-
"patternProperties": {
218-
"randomRegex*": {
219-
"encrypt": {
220-
"bsonType": "string",
221-
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
222-
}
223-
}
224-
}
225-
}
226-
}
227-
""";
270+
{
271+
"bsonType": "object",
272+
"patternProperties": {
273+
"insurance": {
274+
"bsonType": "object",
275+
"encryptMetadata": {
276+
"keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
277+
},
278+
"properties": {
279+
"policyNumber": {
280+
"encrypt": {
281+
"bsonType": "int",
282+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
283+
}
284+
}
285+
},
286+
"patternProperties": {
287+
"randomRegex*": {
288+
"encrypt": {
289+
"bsonType": "string",
290+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
291+
}
292+
}
293+
}
294+
}
295+
}
296+
}
297+
""";
228298

229299
AssertOutcomeCollectionBuilder(builder, expected);
230300
}
@@ -337,6 +407,105 @@ public void EncryptedCollection_PropertyWithString_works_as_expected(BsonType bs
337407
AssertOutcomeCollectionBuilder(builder, expected);
338408
}
339409

410+
[Fact]
411+
public void EncryptedCollection_PropertyNested_works_as_expected()
412+
{
413+
Guid? keyId = Guid.Parse(_keyIdString);
414+
var builder = new EncryptedCollectionBuilder<Patient>();
415+
416+
builder.Property(p => p.Insurance, innerBuilder =>
417+
{
418+
innerBuilder
419+
.EncryptMetadata(keyId)
420+
.Property("policyNumber", BsonType.Int32,
421+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
422+
.PatternProperty("randomRegex*", BsonType.String,
423+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
424+
});
425+
426+
var expected = """
427+
{
428+
"bsonType": "object",
429+
"properties": {
430+
"insurance": {
431+
"bsonType": "object",
432+
"encryptMetadata": {
433+
"keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
434+
},
435+
"properties": {
436+
"policyNumber": {
437+
"encrypt": {
438+
"bsonType": "int",
439+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
440+
}
441+
}
442+
},
443+
"patternProperties": {
444+
"randomRegex*": {
445+
"encrypt": {
446+
"bsonType": "string",
447+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
448+
}
449+
}
450+
}
451+
}
452+
}
453+
}
454+
""";
455+
456+
AssertOutcomeCollectionBuilder(builder, expected);
457+
}
458+
459+
[Fact]
460+
public void EncryptedCollection_PropertyNestedWithString_works_as_expected()
461+
{
462+
Guid? keyId = Guid.Parse(_keyIdString);
463+
var builder = new EncryptedCollectionBuilder<Patient>();
464+
465+
builder.Property<Insurance>("insurance", innerBuilder =>
466+
{
467+
innerBuilder
468+
.EncryptMetadata(keyId)
469+
.Property("policyNumber", BsonType.Int32,
470+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic)
471+
.PatternProperty("randomRegex*", BsonType.String,
472+
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random);
473+
});
474+
475+
var expected = """
476+
{
477+
"bsonType": "object",
478+
"properties": {
479+
"insurance": {
480+
"bsonType": "object",
481+
"encryptMetadata": {
482+
"keyId": [{ "$binary" : { "base64" : "b0r0cADRQB+sOfRZAqDAyA==", "subType" : "04" } }]
483+
},
484+
"properties": {
485+
"policyNumber": {
486+
"encrypt": {
487+
"bsonType": "int",
488+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
489+
}
490+
}
491+
},
492+
"patternProperties": {
493+
"randomRegex*": {
494+
"encrypt": {
495+
"bsonType": "string",
496+
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Random"
497+
}
498+
}
499+
}
500+
}
501+
}
502+
}
503+
""";
504+
505+
AssertOutcomeCollectionBuilder(builder, expected);
506+
}
507+
508+
340509
[Fact]
341510
public void BasicPropertyTest()
342511
{
@@ -347,7 +516,7 @@ public void BasicPropertyTest()
347516
schemaBuilder.Encrypt<Patient>(collectionName, builder =>
348517
{
349518
builder
350-
.EncryptMetadata(keyId: _keyIdExample)
519+
.EncryptMetadata(keyId: _keyId)
351520
.Property(p => p.MedicalRecords, BsonType.Array,
352521
CsfleEncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random)
353522
.Property("bloodType", BsonType.String,
@@ -502,12 +671,10 @@ private void AssertOutcomeCollectionBuilder<T>(EncryptedCollectionBuilder<T> bui
502671
* - *Property with multiple bsonType
503672
* - *Pattern property with string
504673
* - *Pattern property with multiple bsonType
505-
*
506-
* - Nested property with expression
507-
* - Nested property with string
508-
509-
* - Nested pattern property with expression
510-
* - Nested pattern property with string
674+
* - *Nested property with expression
675+
* - *Nested property with string
676+
* - *Nested pattern property with expression
677+
* - *Nested pattern property with string
511678
*
512679
* - Multiple types in schema
513680
* - Property and pattern property together

0 commit comments

Comments
 (0)