@@ -14,6 +14,15 @@ const mongooseFieldEncryption = require("../lib/mongoose-field-encryption").fiel
14
14
15
15
const uri = process . env . URI || "mongodb://127.0.0.1:27017/mongoose-field-encryption-test" ;
16
16
17
+ const postSchema = new Schema ( {
18
+ title : String ,
19
+ message : String ,
20
+ } ) ;
21
+
22
+ postSchema . plugin ( mongooseFieldEncryption , { fields : [ "message" ] , secret : "some secret key" } ) ;
23
+
24
+ const Post = mongoose . model ( "Post" , postSchema ) ;
25
+
17
26
describe ( "basic usage" , function ( ) {
18
27
this . timeout ( 5000 ) ;
19
28
@@ -25,6 +34,7 @@ describe("basic usage", function () {
25
34
} ) ;
26
35
27
36
setupMongoose ( mongoose ) ;
37
+
28
38
} ) ;
29
39
30
40
after ( function ( done ) {
@@ -34,15 +44,7 @@ describe("basic usage", function () {
34
44
} ) ;
35
45
36
46
it ( "should save a document" , function ( done ) {
37
- // given
38
- const postSchema = new Schema ( {
39
- title : String ,
40
- message : String ,
41
- } ) ;
42
-
43
- postSchema . plugin ( mongooseFieldEncryption , { fields : [ "message" ] , secret : "some secret key" } ) ;
44
-
45
- const Post = mongoose . model ( "Post" , postSchema ) ;
47
+
46
48
const post = new Post ( { title : "some text" , message : "hello all" } ) ;
47
49
48
50
// when
@@ -64,6 +66,29 @@ describe("basic usage", function () {
64
66
} ) ;
65
67
} ) ;
66
68
69
+ it ( "should save many documents" , function ( done ) {
70
+
71
+ const post = [ new Post ( { title : "some text" , message : "hello all" } ) ,
72
+ new Post ( { title : "some other text" , message : "hello many" } ) ,
73
+ new Post ( { title : "some other other text" , message : "aloha!" } ) ] ;
74
+
75
+ // when
76
+ Post . insertMany ( post , function ( err ) {
77
+ // then
78
+ if ( err ) {
79
+ return done ( err ) ;
80
+ }
81
+
82
+ expect ( post [ 0 ] . title ) . to . equal ( "some text" ) ;
83
+ expect ( post [ 0 ] . message ) . to . not . be . undefined ;
84
+ const split = post [ 0 ] . message . split ( ":" ) ;
85
+ expect ( split . length ) . to . equal ( 2 ) ;
86
+ expect ( post [ 0 ] . __enc_message ) . to . be . true ;
87
+
88
+ done ( ) ;
89
+ } ) ;
90
+ } ) ;
91
+
67
92
it ( "should search for a document on an encrypted field" , function ( done ) {
68
93
// given
69
94
const messageSchema = new Schema ( {
0 commit comments