Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB committed Jan 29, 2025
1 parent 7fa6616 commit 616ce14
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
12 changes: 11 additions & 1 deletion test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
ValidateOpts,
BufferToBinary
} from 'mongoose';
import { Binary } from 'mongodb';
import { Binary, BSON } from 'mongodb';
import { IsPathRequired } from '../../types/inferschematype';
import { expectType, expectError, expectAssignable } from 'tsd';
import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype';
Expand Down Expand Up @@ -591,6 +591,16 @@ const batchSchema2 = new Schema({ name: String }, { discriminatorKey: 'kind', st
} } });
batchSchema2.discriminator('event', eventSchema2);


function encryptionType() {
const keyId = new BSON.UUID();
expectError<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'newFakeEncryptionType' }));
expectError<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 1 }));

expectType<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'queryableEncryption' }));
expectType<Schema>(new Schema({ name: { type: String, encrypt: { keyId } } }, { encryptionType: 'csfle' }));
}

function gh11828() {
interface IUser {
name: string;
Expand Down
37 changes: 37 additions & 0 deletions test/types/schemaTypeOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BSON } from 'mongodb';
import {
AnyArray,
Schema,
Expand Down Expand Up @@ -74,3 +75,39 @@ function defaultOptions() {
expectType<Record<string, any>>(new Schema.Types.Subdocument('none').defaultOptions);
expectType<Record<string, any>>(new Schema.Types.UUID('none').defaultOptions);
}

function encrypt() {
const keyId = new BSON.UUID();

new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'Indexed' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'Unindexed' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: 'Range' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, algorithm: undefined };

// qe + valid queries
new SchemaTypeOptions<string>()['encrypt'] = { keyId, queries: 'equality' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, queries: 'range' };
new SchemaTypeOptions<string>()['encrypt'] = { keyId, queries: undefined };

// empty object
expectError<SchemaTypeOptions<string>['encrypt']>({});

// invalid keyId
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId: 'fakeId' });

// missing keyId
expectError<SchemaTypeOptions<string>['encrypt']>({ queries: 'equality' });
expectError<SchemaTypeOptions<string>['encrypt']>({ algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' });

// invalid algorithm
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId, algorithm: 'SHA_FAKE_ALG' });

// invalid queries
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId, queries: 'fakeQueryOption' });

// invalid input option
expectError<SchemaTypeOptions<string>['encrypt']>({ keyId, invalidKey: 'fakeKeyOption' });
}
3 changes: 3 additions & 0 deletions types/schemaoptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ declare module 'mongoose' {
*/
overwriteModels?: boolean;

/**
* Required when the schema is encrypted.
*/
encryptionType?: 'csfle' | 'queryableEncryption';
}

Expand Down
39 changes: 16 additions & 23 deletions types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,22 @@ declare module 'mongoose' {

[other: string]: any;

encrypt?: EncryptSchemaTypeOptions;
encrypt?: {
/** The id of the dataKey to use for encryption */
keyId: BSON.UUID;

/**
* Specifies the type of queries that the field can be queried on for Queryable Encryption.
* Required when `SchemaOptions.encryptionType` is 'queryableEncryption'
*/
queries?: 'equality' | 'range';

/**
* The algorithm to use for encryption.
* Required when `SchemaOptions.encryptionType` is 'csfle'
*/
algorithm?: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' | 'AEAD_AES_256_CBC_HMAC_SHA_512-Random' | 'Indexed' | 'Unindexed' | 'Range';
};
}

interface Validator<DocType = any> {
Expand All @@ -222,28 +237,6 @@ declare module 'mongoose' {

type ValidatorFunction<DocType = any> = (this: DocType, value: any, validatorProperties?: Validator) => any;

export interface EncryptSchemaTypeOptions {
/** The id of the dataKey to use for encryption */
keyId: BSON.UUID;

/**
* Specifies the type of queries that the field can be queried on for Queryable Encryption.
* Required when `SchemaOptions.encryptionType` is 'queryableEncryption'
*/
queries?: 'equality' | 'range';

/**
* The algorithm to use for encryption.
* Required when `SchemaOptions.encryptionType` is 'csfle'
*/
algorithm?:
| 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
| 'AEAD_AES_256_CBC_HMAC_SHA_512-Random'
| 'Indexed'
| 'Unindexed'
| 'Range';
}

class SchemaType<T = any, DocType = any> {
/** SchemaType constructor */
constructor(path: string, options?: AnyObject, instance?: string);
Expand Down

0 comments on commit 616ce14

Please sign in to comment.