Skip to content

Commit 4dc10ed

Browse files
Merge pull request #65 from skyflowapi/release/23.8.2
SK-942/Release/23.8.2
2 parents e8c98cb + 42a5287 commit 4dc10ed

File tree

20 files changed

+4300
-3629
lines changed

20 files changed

+4300
-3629
lines changed

__tests__/core-utils/element-validations.test.js

Lines changed: 725 additions & 0 deletions
Large diffs are not rendered by default.

__tests__/core-utils/reveal.test.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Skyflow from '../../src/core/Skyflow';
55
import {
66
formatRecordsForClient,
77
formatRecordsForIframe,
8+
fetchRecordsGET,
89
fetchRecordsByTokenId,
910
} from '../../src/core-utils/reveal';
1011
import * 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+
});
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import * as revealUtils from '../../src/core-utils/reveal';
2+
import Skyflow from '../../src/core/Skyflow';
3+
import SkyflowContainer from '../../src/core/SkyflowContainer';
4+
import { RedactionType } from '../../src/utils/constants';
5+
import { parameterizedString } from '../../src/utils/logs-helper';
6+
import SKYFLOW_ERROR_CODE from '../../src/utils/skyflow-error-code';
7+
8+
const testSkyflowClient = new Skyflow({
9+
vaultID: '1234',
10+
vaultURL: 'https://validurl.com',
11+
getBearerToken: () => Promise.resolve('valid_auth_token'),
12+
});
13+
14+
describe('test SkyflowContiner class', () => {
15+
let skyflowContainer;
16+
beforeEach(() => {
17+
skyflowContainer = new SkyflowContainer(testSkyflowClient);
18+
});
19+
20+
it('should be an instance of SkyflowContainer class', () => {
21+
expect(skyflowContainer).toBeInstanceOf(SkyflowContainer);
22+
});
23+
});
24+
25+
const getRequestIDs = {
26+
records: [
27+
{
28+
ids: ['id1'],
29+
table: 'pii_fields',
30+
redaction: RedactionType.PLAIN_TEXT,
31+
},
32+
],
33+
};
34+
35+
const invalidGetRequest = {
36+
records: [
37+
{
38+
ids: ['id1'],
39+
table: 'pii_fields',
40+
},
41+
],
42+
};
43+
44+
const getSuccessRecord = {
45+
fields: {
46+
cvv: 123,
47+
id: 'id1',
48+
name: 'name',
49+
},
50+
table: 'pii_fields',
51+
};
52+
53+
const getErrorRecord = {
54+
error: {
55+
code: 404,
56+
description: 'No records found requestId - 3wq45w8-2fni-33fd-vt62-3rdsbe45',
57+
},
58+
ids: ['id1'],
59+
};
60+
61+
describe('test get method of SkyflowContainer class', () => {
62+
let skyflowContainer;
63+
beforeEach(() => {
64+
skyflowContainer = new SkyflowContainer(testSkyflowClient);
65+
});
66+
67+
it('should throw error in case of invalid input', (done) => {
68+
jest
69+
.spyOn(revealUtils, 'fetchRecordsGET')
70+
.mockResolvedValue({ records: [getSuccessRecord] });
71+
72+
skyflowContainer.get(invalidGetRequest).catch((err) => {
73+
expect(err?.errors[0]?.description).toEqual(
74+
parameterizedString(
75+
SKYFLOW_ERROR_CODE.MISSING_REDACTION_IN_GET.description,
76+
0
77+
)
78+
);
79+
done();
80+
});
81+
});
82+
83+
it('should return response with records in case of success', (done) => {
84+
jest
85+
.spyOn(revealUtils, 'fetchRecordsGET')
86+
.mockResolvedValue({ records: [getSuccessRecord] });
87+
88+
skyflowContainer
89+
.get(getRequestIDs)
90+
.then((res) => {
91+
expect(res.records.length).toBe(1);
92+
expect(res.errors).toBe(undefined);
93+
expect(res.records[0]).toEqual(getSuccessRecord);
94+
done();
95+
})
96+
.catch((err) => {
97+
done(err);
98+
});
99+
});
100+
101+
it('should return response with records and errors in case of partial success', (done) => {
102+
jest.spyOn(revealUtils, 'fetchRecordsGET').mockRejectedValue({
103+
records: [getSuccessRecord],
104+
errors: [getErrorRecord],
105+
});
106+
107+
skyflowContainer
108+
.get(getRequestIDs)
109+
.then(
110+
(res) => {},
111+
(err) => {
112+
expect(err.records.length).toBe(1);
113+
expect(err.errors.length).toBe(1);
114+
expect(err.records[0]).toEqual(getSuccessRecord);
115+
expect(err.errors[0]).toEqual(getErrorRecord);
116+
done();
117+
}
118+
)
119+
.catch((err) => {
120+
done(err);
121+
});
122+
});
123+
124+
it('should return response with errors in case of failure', (done) => {
125+
jest
126+
.spyOn(revealUtils, 'fetchRecordsGET')
127+
.mockRejectedValue({ errors: [getErrorRecord] });
128+
129+
skyflowContainer
130+
.get(getRequestIDs)
131+
.then(
132+
(res) => {},
133+
(err) => {
134+
expect(err.records).toBe(undefined);
135+
expect(err.errors.length).toBe(1);
136+
expect(err.errors[0]).toEqual(getErrorRecord);
137+
done();
138+
}
139+
)
140+
.catch((err) => {
141+
done(err);
142+
});
143+
});
144+
});

__tests__/hooks/useSkyflow.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { renderHook } from '@testing-library/react-native';
2+
import useSkyflow from '../../src/hooks/useSkyflow';
3+
import SkyflowContainer from '../../src/core/SkyflowContainer';
4+
5+
describe('test useSkyflow hook', () => {
6+
it('should return SkyflowContainer instance with pure methods', () => {
7+
const { result } = renderHook(() => useSkyflow());
8+
expect(result.current).toBeInstanceOf(SkyflowContainer);
9+
});
10+
});

0 commit comments

Comments
 (0)