@@ -5,6 +5,7 @@ import Skyflow from '../../src/core/Skyflow';
55import {
66 formatRecordsForClient ,
77 formatRecordsForIframe ,
8+ fetchRecordsGET ,
89 fetchRecordsByTokenId ,
910} from '../../src/core-utils/reveal' ;
1011import * as ClientModule from '../../src/core-utils/client' ;
@@ -455,3 +456,180 @@ describe('test fetchRecordsByTokenId', () => {
455456 } ) ;
456457 } ) ;
457458} ) ;
459+
460+ const getRecordID = {
461+ ids : [ 'id1' ] ,
462+ table : 'pii_fields' ,
463+ redaction : RedactionType . PLAIN_TEXT ,
464+ } ;
465+
466+ const getRecordColumn = {
467+ table : 'pii_fields' ,
468+ redaction : RedactionType . PLAIN_TEXT ,
469+ columnName : 'column-name' ,
470+ columnValues : [ 'value1' ] ,
471+ } ;
472+
473+ const optionsFalse = { tokens : false } ;
474+ const optionsTrue = { tokens : true } ;
475+
476+ const invalidGetRequest = {
477+ records : [
478+ {
479+ ids : [ 'invalid_id1' ] ,
480+ table : 'pii_fields' ,
481+ } ,
482+ ] ,
483+ } ;
484+
485+ const getErrorResponse = {
486+ error : {
487+ code : 404 ,
488+ description : 'No records found requestId - 3wq45w8-2fni-33fd-vt62-3rdsbe45' ,
489+ } ,
490+ ids : [ 'id1' ] ,
491+ } ;
492+
493+ const getSuccessResponse = {
494+ records : [
495+ {
496+ fields : {
497+ cvv : 123 ,
498+ id : 'id1' ,
499+ name : 'name' ,
500+ } ,
501+ table : 'pii_fields' ,
502+ } ,
503+ ] ,
504+ } ;
505+
506+ describe ( 'fetchRecordGET fn test' , ( ) => {
507+ beforeEach ( ( ) => {
508+ jest . clearAllMocks ( ) ;
509+ jest . resetAllMocks ( ) ;
510+ } ) ;
511+
512+ it ( 'should throw error for invalid access token' , ( done ) => {
513+ const testSkyflowClient = new Skyflow ( {
514+ vaultID : '1234' ,
515+ vaultURL : 'https://url.com' ,
516+ getBearerToken : ( ) => Promise . reject ( 'valid_token' ) ,
517+ } ) ;
518+
519+ jest
520+ . spyOn ( testSkyflowClient , 'getAccessToken' )
521+ . mockRejectedValue ( 'Invalid Access Token' ) ;
522+
523+ fetchRecordsGET (
524+ testSkyflowClient ,
525+ [ getRecordID , getRecordColumn ] ,
526+ optionsFalse
527+ ) . catch ( ( err ) => {
528+ expect ( err ) . toEqual ( 'Invalid Access Token' ) ;
529+ done ( ) ;
530+ } ) ;
531+ } ) ;
532+
533+ it ( 'should reject promise in case of error' , ( done ) => {
534+ jest . spyOn ( ClientModule , 'default' ) . mockImplementation ( ( ) => ( {
535+ request : ( ) => Promise . reject ( getErrorResponse ) ,
536+ } ) ) ;
537+
538+ const testSkyflowClient = new Skyflow ( {
539+ vaultID : '1234' ,
540+ vaultURL : 'https://url.com' ,
541+ getBearerToken : ( ) => Promise . resolve ( 'valid_token' ) ,
542+ } ) ;
543+
544+ jest
545+ . spyOn ( testSkyflowClient , 'getAccessToken' )
546+ . mockResolvedValue ( 'valid token' ) ;
547+
548+ fetchRecordsGET ( testSkyflowClient , invalidGetRequest . records , optionsTrue )
549+ . then (
550+ ( res ) => { } ,
551+ ( err ) => {
552+ expect ( err . errors . length ) . toBe ( 1 ) ;
553+ expect ( err . records ) . toBe ( undefined ) ;
554+ expect ( err . errors [ 0 ] . error . code ) . toBe ( 404 ) ;
555+ expect ( err . errors [ 0 ] . error . description ) . toBe (
556+ getErrorResponse . error . description
557+ ) ;
558+ done ( ) ;
559+ }
560+ )
561+ . catch ( ( err ) => {
562+ done ( err ) ;
563+ } ) ;
564+ } ) ;
565+
566+ it ( 'should give success reponse in case of success' , ( done ) => {
567+ jest . spyOn ( ClientModule , 'default' ) . mockImplementation ( ( ) => ( {
568+ request : ( ) => new Promise . resolve ( getSuccessResponse ) ,
569+ } ) ) ;
570+
571+ const testSkyflowClient = new Skyflow ( {
572+ vaultID : '1234' ,
573+ vaultURL : 'https://url.com' ,
574+ getBearerToken : ( ) => Promise . resolve ( 'valid_token' ) ,
575+ } ) ;
576+
577+ jest
578+ . spyOn ( testSkyflowClient , 'getAccessToken' )
579+ . mockResolvedValue ( 'valid token' ) ;
580+
581+ fetchRecordsGET (
582+ testSkyflowClient ,
583+ [ getRecordID , getRecordColumn ] ,
584+ optionsFalse
585+ )
586+ . then ( ( res ) => {
587+ expect ( res . errors ) . toBe ( undefined ) ;
588+ expect ( res . records . length ) . toBe ( 2 ) ;
589+ done ( ) ;
590+ } )
591+ . catch ( ( err ) => {
592+ done ( err ) ;
593+ } ) ;
594+ } ) ;
595+
596+ it ( 'should reject promise in case of partial success' , ( done ) => {
597+ jest . spyOn ( ClientModule , 'default' ) . mockImplementation ( ( ) => ( {
598+ request : ( requestInput ) => {
599+ return new Promise ( ( resolve , reject ) => {
600+ if ( requestInput . url . includes ( 'column_name=column-name' ) )
601+ resolve ( getSuccessResponse ) ;
602+ else reject ( getErrorResponse ) ;
603+ } ) ;
604+ } ,
605+ } ) ) ;
606+
607+ const testSkyflowClient = new Skyflow ( {
608+ vaultID : '1234' ,
609+ vaultURL : 'https://url.com' ,
610+ getBearerToken : ( ) => Promise . resolve ( 'valid_token' ) ,
611+ } ) ;
612+
613+ jest
614+ . spyOn ( testSkyflowClient , 'getAccessToken' )
615+ . mockResolvedValue ( 'valid token' ) ;
616+
617+ const getRequestRecords = [
618+ { ...invalidGetRequest . records [ 0 ] } ,
619+ getRecordColumn ,
620+ ] ;
621+
622+ fetchRecordsGET ( testSkyflowClient , getRequestRecords , optionsFalse )
623+ . then (
624+ ( res ) => { } ,
625+ ( err ) => {
626+ expect ( err . errors . length ) . toBe ( 1 ) ;
627+ expect ( err . records . length ) . toBe ( 1 ) ;
628+ done ( ) ;
629+ }
630+ )
631+ . catch ( ( err ) => {
632+ done ( err ) ;
633+ } ) ;
634+ } ) ;
635+ } ) ;
0 commit comments