|
| 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 | +const getRequestIDs = { |
| 15 | + records: [ |
| 16 | + { |
| 17 | + ids: ['id1'], |
| 18 | + table: 'pii_fields', |
| 19 | + redaction: RedactionType.PLAIN_TEXT, |
| 20 | + }, |
| 21 | + ], |
| 22 | +}; |
| 23 | + |
| 24 | +const getRequestCVs = { |
| 25 | + records: [ |
| 26 | + { |
| 27 | + table: 'pii_fields', |
| 28 | + redaction: RedactionType.PLAIN_TEXT, |
| 29 | + columnName: 'name', |
| 30 | + columnValues: ['name', 'anotherName'], |
| 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 getSuccessRecord1 = { |
| 54 | + fields: { |
| 55 | + cvv: 321, |
| 56 | + id: 'id2', |
| 57 | + name: 'anotherName', |
| 58 | + }, |
| 59 | + table: 'pii_fields', |
| 60 | +}; |
| 61 | + |
| 62 | +const getErrorRecord = { |
| 63 | + error: { |
| 64 | + code: 404, |
| 65 | + description: 'No records found requestId - 3wq45w8-2fni-33fd-vt62-3rdsbe45', |
| 66 | + }, |
| 67 | + ids: ['id1'], |
| 68 | +}; |
| 69 | + |
| 70 | +describe('test get method of SkyflowContainer class', () => { |
| 71 | + let skyflowContainer; |
| 72 | + beforeEach(() => { |
| 73 | + skyflowContainer = new SkyflowContainer(testSkyflowClient); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should throw error in case of invalid input', (done) => { |
| 77 | + jest |
| 78 | + .spyOn(revealUtils, 'fetchRecordsGET') |
| 79 | + .mockResolvedValue({ records: [getSuccessRecord] }); |
| 80 | + |
| 81 | + skyflowContainer.get(invalidGetRequest).catch((err) => { |
| 82 | + expect(err?.errors[0]?.description).toEqual( |
| 83 | + parameterizedString( |
| 84 | + SKYFLOW_ERROR_CODE.MISSING_REDACTION_IN_GET.description, |
| 85 | + 0 |
| 86 | + ) |
| 87 | + ); |
| 88 | + done(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return response with records in case of success by skyflow ids', (done) => { |
| 93 | + jest |
| 94 | + .spyOn(revealUtils, 'fetchRecordsGET') |
| 95 | + .mockResolvedValue({ records: [getSuccessRecord] }); |
| 96 | + |
| 97 | + skyflowContainer |
| 98 | + .get(getRequestIDs) |
| 99 | + .then((res) => { |
| 100 | + expect(res.records.length).toBe(1); |
| 101 | + expect(res.errors).toBe(undefined); |
| 102 | + expect(res.records[0]).toEqual(getSuccessRecord); |
| 103 | + done(); |
| 104 | + }) |
| 105 | + .catch((err) => { |
| 106 | + done(err); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should return response with records in case of success by column values', (done) => { |
| 111 | + jest |
| 112 | + .spyOn(revealUtils, 'fetchRecordsGET') |
| 113 | + .mockResolvedValue({ records: [getSuccessRecord, getSuccessRecord1] }); |
| 114 | + |
| 115 | + skyflowContainer |
| 116 | + .get(getRequestCVs) |
| 117 | + .then((res) => { |
| 118 | + expect(res.records.length).toBe(2); |
| 119 | + expect(res.errors).toBe(undefined); |
| 120 | + expect(res.records[0]).toEqual(getSuccessRecord); |
| 121 | + expect(res.records[1]).toEqual(getSuccessRecord1); |
| 122 | + done(); |
| 123 | + }) |
| 124 | + .catch((err) => { |
| 125 | + done(err); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return response with records and errors in case of partial success', (done) => { |
| 130 | + jest.spyOn(revealUtils, 'fetchRecordsGET').mockRejectedValue({ |
| 131 | + records: [getSuccessRecord], |
| 132 | + errors: [getErrorRecord], |
| 133 | + }); |
| 134 | + |
| 135 | + skyflowContainer |
| 136 | + .get(getRequestIDs) |
| 137 | + .then( |
| 138 | + (res) => {}, |
| 139 | + (err) => { |
| 140 | + expect(err.records.length).toBe(1); |
| 141 | + expect(err.errors.length).toBe(1); |
| 142 | + expect(err.records[0]).toEqual(getSuccessRecord); |
| 143 | + expect(err.errors[0]).toEqual(getErrorRecord); |
| 144 | + done(); |
| 145 | + } |
| 146 | + ) |
| 147 | + .catch((err) => { |
| 148 | + done(err); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return response with errors in case of failure', (done) => { |
| 153 | + jest |
| 154 | + .spyOn(revealUtils, 'fetchRecordsGET') |
| 155 | + .mockRejectedValue({ errors: [getErrorRecord] }); |
| 156 | + |
| 157 | + skyflowContainer |
| 158 | + .get(getRequestIDs) |
| 159 | + .then( |
| 160 | + (res) => {}, |
| 161 | + (err) => { |
| 162 | + expect(err.records).toBe(undefined); |
| 163 | + expect(err.errors.length).toBe(1); |
| 164 | + expect(err.errors[0]).toEqual(getErrorRecord); |
| 165 | + done(); |
| 166 | + } |
| 167 | + ) |
| 168 | + .catch((err) => { |
| 169 | + done(err); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments