@@ -140,6 +140,58 @@ describe("Utils", () => {
140140 const res = utils . jsonToString ( '{"field": "value"}' ) ;
141141 assert . strictEqual ( res , '{"field":"value"}' ) ;
142142 } ) ;
143+
144+ it ( "string from string" , ( ) => {
145+ const res = utils . jsonToString ( 'value' ) ;
146+ assert . strictEqual ( res , 'value' ) ;
147+ } ) ;
148+
149+ it ( "string from string object" , ( ) => {
150+ const res = utils . jsonToString ( new String ( "value" ) ) ;
151+ assert . strictEqual ( res , 'value' ) ;
152+ } ) ;
153+ } ) ;
154+
155+ describe ( "#stringToJson" , ( ) => {
156+ it ( "when null" , ( ) => {
157+ assert . throws ( ( ) => {
158+ utils . stringToJson ( null ) ;
159+ } ) ;
160+ } ) ;
161+
162+ it ( "when undefined" , ( ) => {
163+ assert . throws ( ( ) => {
164+ utils . stringToJson ( undefined ) ;
165+ } ) ;
166+ } ) ;
167+
168+ it ( "when empty obj" , ( ) => {
169+ const res = utils . stringToJson ( '{}' ) ;
170+ assert . strictEqual ( JSON . stringify ( res ) , JSON . stringify ( { } ) ) ;
171+ } ) ;
172+
173+ it ( "when empty str" , ( ) => {
174+ assert . throws ( ( ) => utils . stringToJson ( "" ) ) ;
175+ } ) ;
176+
177+ it ( "when Json object" , ( ) => {
178+ assert . throws ( ( ) => utils . stringToJson ( { field : "value" } ) ) ;
179+ } ) ;
180+
181+ it ( "string from string" , ( ) => {
182+ const res = utils . stringToJson ( 'value' ) ;
183+ assert . strictEqual ( res , "value" ) ;
184+ } ) ;
185+
186+ it ( "string from string object" , ( ) => {
187+ const res = utils . stringToJson ( new String ( "value" ) ) ;
188+ assert . strictEqual ( res , "value" ) ;
189+ } ) ;
190+
191+ it ( "correct json object from string" , ( ) => {
192+ const res = utils . stringToJson ( '{"field": "value"}' ) ;
193+ assert . strictEqual ( JSON . stringify ( res ) , JSON . stringify ( { field : 'value' } ) ) ;
194+ } ) ;
143195 } ) ;
144196
145197 describe ( "#mutateObjectProperty" , ( ) => {
@@ -314,6 +366,93 @@ describe("Utils", () => {
314366 } ) ;
315367 } ) ;
316368
369+ describe ( "#addEncryptedDataToBody" , ( ) => {
370+ it ( "encrypting first level field" , ( ) => {
371+ const body = {
372+ firstLevelField : "secret" ,
373+ anotherFirstLevelField : "value"
374+ } ;
375+ const expected = JSON . stringify ( {
376+ anotherFirstLevelField : "value" ,
377+ encryptedData : "changed"
378+ } ) ;
379+ const value = { encryptedData : "changed" } ;
380+ const path = { element : 'firstLevelField' , obj : 'encryptedData' } ;
381+ utils . addEncryptedDataToBody ( value , path , "encryptedData" , body ) ;
382+ assert . strictEqual ( JSON . stringify ( body ) , expected ) ;
383+ } ) ;
384+
385+ it ( "encryptedValueFieldName is not in the path" , ( ) => {
386+ const body = {
387+ field : "secret" ,
388+ anotherField : "value"
389+ } ;
390+ const expected = JSON . stringify ( {
391+ anotherField : "value" ,
392+ encryptedField : {
393+ encryptedData : "changed"
394+ }
395+ } ) ;
396+ const value = { encryptedData : "changed" } ;
397+ const path = { element : 'field' , obj : 'encryptedField' } ;
398+ utils . addEncryptedDataToBody ( value , path , "encryptedData" , body ) ;
399+ assert . strictEqual ( JSON . stringify ( body ) , expected ) ;
400+ } ) ;
401+
402+ it ( "encrypting to existing field" , ( ) => {
403+ const body = {
404+ field : "secret" ,
405+ anotherField : "value"
406+ } ;
407+ const expected = JSON . stringify ( {
408+ anotherField : "value" ,
409+ field : "changed"
410+ } ) ;
411+ const value = { field : "changed" } ;
412+ const path = { element : 'field' , obj : 'field' } ;
413+ utils . addEncryptedDataToBody ( value , path , "field" , body ) ;
414+ assert . strictEqual ( JSON . stringify ( body ) , expected ) ;
415+ } ) ;
416+
417+ it ( "encrypting to existing subfield" , ( ) => {
418+ const body = {
419+ field : {
420+ subField : "secret"
421+ } ,
422+ anotherField : "value"
423+ } ;
424+ const expected = JSON . stringify ( {
425+ field : {
426+ subField : "changed"
427+ } ,
428+ anotherField : "value"
429+ } ) ;
430+ const value = { subField : "changed" } ;
431+ const path = { element : 'field.subField' , obj : 'field' } ;
432+ utils . addEncryptedDataToBody ( value , path , "subField" , body ) ;
433+ assert . strictEqual ( JSON . stringify ( body ) , expected ) ;
434+ } ) ;
435+
436+ it ( "encrypting to new subfield" , ( ) => {
437+ const body = {
438+ field : {
439+ subField : "secret"
440+ } ,
441+ anotherField : "value"
442+ } ;
443+ const expected = JSON . stringify ( {
444+ field : {
445+ encryptedData : "changed"
446+ } ,
447+ anotherField : "value"
448+ } ) ;
449+ const value = { encryptedData : "changed" } ;
450+ const path = { element : 'field.subField' , obj : 'field' } ;
451+ utils . addEncryptedDataToBody ( value , path , "encryptedData" , body ) ;
452+ assert . strictEqual ( JSON . stringify ( body ) , expected ) ;
453+ } ) ;
454+ } ) ;
455+
317456 describe ( "#getPrivateKey12" , ( ) => {
318457 it ( "empty alias" , ( ) => {
319458 assert . throws ( ( ) => {
0 commit comments