Skip to content

Commit faf061a

Browse files
add comprehensive schema type testing too.
1 parent 2bc5dde commit faf061a

File tree

1 file changed

+122
-45
lines changed

1 file changed

+122
-45
lines changed

test/encrypted_schema.test.js

Lines changed: 122 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,58 +61,135 @@ describe('encrypted schema declaration', function() {
6161
});
6262

6363
describe('Tests that fields of valid schema types can be declared as encrypted schemas', function() {
64-
describe('when a schema is instantiated with a nested encrypted schema', function() {
65-
let schema;
66-
beforeEach(function() {
67-
const encryptedSchema = new Schema({
68-
encrypted: {
69-
type: String, encrypt: { keyId: KEY_ID, algorithm }
70-
}
71-
}, { encryptionType: 'csfle' });
72-
schema = new Schema({
73-
field: encryptedSchema
74-
}, { encryptionType: 'csfle' });
75-
});
76-
77-
78-
it('then the schema has a nested property that is encrypted', function() {
79-
assert.ok(schemaHasEncryptedProperty(schema, ['field', 'encrypted']));
80-
});
81-
});
82-
83-
describe('when a schema is instantiated with a nested schema object', function() {
84-
let schema;
85-
beforeEach(function() {
86-
schema = new Schema({
87-
field: {
64+
const tests = {
65+
'nested schema for csfle':
66+
{
67+
schemaFactory: () => {
68+
const encryptedSchema = new Schema({
8869
encrypted: {
8970
type: String, encrypt: { keyId: KEY_ID, algorithm }
9071
}
72+
}, { encryptionType: 'csfle' });
73+
return new Schema({
74+
field: encryptedSchema
75+
}, { encryptionType: 'csfle' });
76+
}, predicate: (schema) => assert.ok(schemaHasEncryptedProperty(schema, ['field', 'encrypted'])),
77+
schemaMap: {
78+
bsonType: 'object',
79+
properties: {
80+
field: {
81+
bsonType: 'object',
82+
properties: {
83+
encrypted: { encrypt: { bsonType: 'string', algorithm, keyId: KEY_ID } }
84+
}
85+
}
9186
}
92-
}, { encryptionType: 'csfle' });
93-
});
94-
95-
it('then the schema has a nested property that is encrypted', function() {
96-
assert.ok(schemaHasEncryptedProperty(schema, ['field', 'encrypted']));
97-
});
98-
});
99-
100-
describe('when a schema is instantiated as an Array', function() {
101-
let schema;
102-
beforeEach(function() {
103-
schema = new Schema({
104-
encrypted: {
105-
type: [Number],
106-
encrypt: { keyId: KEY_ID, algorithm }
87+
}
88+
},
89+
'nested schema for qe': {
90+
schemaFactory: () => {
91+
const encryptedSchema = new Schema({
92+
encrypted: {
93+
type: String, encrypt: { keyId: KEY_ID }
94+
}
95+
}, { encryptionType: 'qe' });
96+
return new Schema({
97+
field: encryptedSchema
98+
}, { encryptionType: 'qe' });
99+
}, predicate: (schema) => assert.ok(schemaHasEncryptedProperty(schema, ['field', 'encrypted'])),
100+
encryptedFields: {
101+
fields: [
102+
{ path: 'field.encrypted', keyId: KEY_ID, bsonType: 'string' }
103+
]
104+
}
105+
},
106+
'nested object for csfle':
107+
{
108+
schemaFactory: () => {
109+
return new Schema({
110+
field: {
111+
encrypted: {
112+
type: String, encrypt: { keyId: KEY_ID, algorithm }
113+
}
114+
}
115+
}, { encryptionType: 'csfle' });
116+
}, predicate: (schema) => assert.ok(schemaHasEncryptedProperty(schema, ['field', 'encrypted'])),
117+
schemaMap: {
118+
bsonType: 'object',
119+
properties: {
120+
field: {
121+
bsonType: 'object',
122+
properties: {
123+
encrypted: { encrypt: { bsonType: 'string', algorithm, keyId: KEY_ID } }
124+
}
125+
}
107126
}
108-
}, { encryptionType: 'csfle' });
109-
});
127+
}
128+
},
129+
'nested object for qe': {
130+
schemaFactory: () => {
131+
return new Schema({
132+
field: {
133+
encrypted: {
134+
type: String, encrypt: { keyId: KEY_ID }
135+
}
136+
}
137+
}, { encryptionType: 'qe' });
138+
}, predicate: (schema) => assert.ok(schemaHasEncryptedProperty(schema, ['field', 'encrypted'])),
139+
encryptedFields: {
140+
fields: [
141+
{ path: 'field.encrypted', keyId: KEY_ID, bsonType: 'string' }
142+
]
143+
}
144+
},
145+
'schema with encrypted array for csfle': {
146+
schemaFactory: () => {
147+
return new Schema({
148+
encrypted: {
149+
type: [Number],
150+
encrypt: { keyId: KEY_ID, algorithm }
151+
}
152+
}, { encryptionType: 'csfle' });
153+
}, predicate: (schema) => assert.ok(schemaHasEncryptedProperty(schema, ['encrypted'])),
154+
schemaMap: {
155+
bsonType: 'object',
156+
properties: {
157+
encrypted: {
158+
encrypt: {
159+
bsonType: 'array',
160+
keyId: KEY_ID,
161+
algorithm
162+
}
163+
}
164+
}
165+
}
166+
},
167+
'schema with encrypted array for qe': {
168+
schemaFactory: () => {
169+
return new Schema({
170+
encrypted: {
171+
type: [Number],
172+
encrypt: { keyId: KEY_ID }
173+
}
174+
}, { encryptionType: 'qe' });
175+
}, predicate: (schema) => assert.ok(schemaHasEncryptedProperty(schema, ['encrypted'])),
176+
encryptedFields: {
177+
fields: [
178+
{ path: 'encrypted', keyId: KEY_ID, bsonType: 'array' }
179+
]
180+
}
181+
}
182+
};
110183

111-
it('then the schema has a nested property that is encrypted', function() {
112-
assert.ok(schemaHasEncryptedProperty(schema, 'encrypted'));
113-
});
114-
});
184+
for (const [description, { schemaFactory, predicate, schemaMap, encryptedFields }] of Object.entries(tests)) {
185+
it(description, function() {
186+
const schema = schemaFactory();
187+
predicate(schema);
115188

189+
schemaMap && assert.deepEqual(schema._buildSchemaMap(), schemaMap);
190+
encryptedFields && assert.deepEqual(schema._buildEncryptedFields(), encryptedFields);
191+
});
192+
}
116193
});
117194

118195
describe('invalid schema types for encrypted schemas', function() {

0 commit comments

Comments
 (0)