Skip to content

Commit

Permalink
chore: update storage service tests (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleythedeveloper authored Sep 24, 2024
1 parent f668aaa commit f883129
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 63 deletions.
44 changes: 17 additions & 27 deletions packages/vc-test-suite/__tests__/storage.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ describe('decryptData', () => {
tag: 'mockTag',
};
const mockKey = 'mockKey';
const mockDecryptedBase64 = 'ZGVjcnlwdGVkIGRhdGE='; // base64 for "decrypted data"
const mockDecrypted = 'decrypted data';
const mockDecryptedJSON = '{"key": "value"}';
const mockDecryptedObject = { key: 'value' };
const mockDecryptedNonJSON = 'non-JSON string';

beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -34,48 +35,37 @@ describe('decryptData', () => {
jest.restoreAllMocks();
});

it('should return decrypted data when given valid input', () => {
it('should return parsed JSON object when decrypted data is valid JSON', () => {
(crypto.createDecipheriv as jest.Mock).mockReturnValue({
setAuthTag: jest.fn(),
update: jest.fn().mockReturnValue(mockDecryptedBase64),
update: jest.fn().mockReturnValue(mockDecryptedJSON),
final: jest.fn().mockReturnValue(''),
});

const result = decryptData(mockEncryptedData, mockKey);
expect(result).toBe(mockDecrypted);
expect(result).toEqual(mockDecryptedObject);
});

it('should throw an error if decryption fails', () => {
it('should return decrypted string when decrypted data is not valid JSON', () => {
(crypto.createDecipheriv as jest.Mock).mockReturnValue({
setAuthTag: jest.fn(),
update: jest.fn(),
final: jest.fn().mockImplementation(() => {
throw new Error('Decryption failed');
}),
update: jest.fn().mockReturnValue(mockDecryptedNonJSON),
final: jest.fn().mockReturnValue(''),
});

expect(() => decryptData(mockEncryptedData, mockKey)).toThrow('Decryption failed');
const result = decryptData(mockEncryptedData, mockKey);
expect(result).toBe(mockDecryptedNonJSON);
});

it('should return the original string for non-base64 decryption result', () => {
const nonBase64String = 'not a base64 string';
it('should throw an error if decryption fails', () => {
(crypto.createDecipheriv as jest.Mock).mockReturnValue({
setAuthTag: jest.fn(),
update: jest.fn().mockReturnValue(nonBase64String),
final: jest.fn().mockReturnValue(''),
});

const originalBufferFrom = Buffer.from;
jest.spyOn(Buffer, 'from').mockImplementation((input: any, encoding?: string) => {
if (input === nonBase64String && encoding === 'base64') {
throw new Error('Invalid base64 string');
}
return originalBufferFrom(input);
update: jest.fn(),
final: jest.fn().mockImplementation(() => {
throw new Error('Decryption failed');
}),
});

const result = decryptData(mockEncryptedData, mockKey);

expect(result).toBe(nonBase64String);
expect(Buffer.from).toHaveBeenCalledWith(nonBase64String, 'base64');
expect(() => decryptData(mockEncryptedData, mockKey)).toThrow('Decryption failed');
});
});
5 changes: 2 additions & 3 deletions packages/vc-test-suite/tests/Storage/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ interface EncryptedData {
* @param {string} key - Decryption key in hexadecimal format
* @returns {string} - Decrypted data
*/
export function decryptData({ cipherText, iv, tag }: EncryptedData, key: string): string {
export function decryptData({ cipherText, iv, tag }: EncryptedData, key: string): Record<string, any> | string {
const decipher = crypto.createDecipheriv('aes-256-gcm', Buffer.from(key, 'hex'), Buffer.from(iv, 'base64'));
decipher.setAuthTag(Buffer.from(tag, 'base64'));
let decrypted = decipher.update(cipherText, 'base64', 'utf8');
decrypted += decipher.final('utf8');

// Try to decode the base64 string, if it fails, return the original string
try {
return Buffer.from(decrypted, 'base64').toString('utf8');
return JSON.parse(decrypted);
} catch (error) {
return decrypted;
}
Expand Down
60 changes: 27 additions & 33 deletions packages/vc-test-suite/tests/Storage/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,15 @@ describe('Storage Service', function () {
expect(fetchResponse.data).to.have.property('iv');
expect(fetchResponse.data).to.have.property('tag');
expect(fetchResponse.data).to.have.property('type');
expect(fetchResponse.data.type).to.equal('AES-GCM');

const decryptedData = JSON.parse(
decryptData(
{
cipherText: fetchResponse.data.cipherText,
iv: fetchResponse.data.iv,
tag: fetchResponse.data.tag,
},
response.data.key,
),
expect(fetchResponse.data.type).to.equal('aes-256-gcm');

const decryptedData = decryptData(
{
cipherText: fetchResponse.data.cipherText,
iv: fetchResponse.data.iv,
tag: fetchResponse.data.tag,
},
response.data.key,
);

expect(decryptedData).to.deep.equal(payload.data);
Expand Down Expand Up @@ -199,17 +197,15 @@ describe('Storage Service', function () {
expect(fetchResponse.data).to.have.property('iv');
expect(fetchResponse.data).to.have.property('tag');
expect(fetchResponse.data).to.have.property('type');
expect(fetchResponse.data.type).to.equal('AES-GCM');

const decryptedData = JSON.parse(
decryptData(
{
cipherText: fetchResponse.data.cipherText,
iv: fetchResponse.data.iv,
tag: fetchResponse.data.tag,
},
storeResponse.data.key,
),
expect(fetchResponse.data.type).to.equal('aes-256-gcm');

const decryptedData = decryptData(
{
cipherText: fetchResponse.data.cipherText,
iv: fetchResponse.data.iv,
tag: fetchResponse.data.tag,
},
storeResponse.data.key,
);

expect(decryptedData).to.deep.equal(payload.data);
Expand Down Expand Up @@ -270,17 +266,15 @@ describe('Storage Service', function () {
expect(fetchResponse.data).to.have.property('iv');
expect(fetchResponse.data).to.have.property('tag');
expect(fetchResponse.data).to.have.property('type');
expect(fetchResponse.data.type).to.equal('AES-GCM');

const decryptedData = JSON.parse(
decryptData(
{
cipherText: fetchResponse.data.cipherText,
iv: fetchResponse.data.iv,
tag: fetchResponse.data.tag,
},
response1.data.key,
),
expect(fetchResponse.data.type).to.equal('aes-256-gcm');

const decryptedData = decryptData(
{
cipherText: fetchResponse.data.cipherText,
iv: fetchResponse.data.iv,
tag: fetchResponse.data.tag,
},
response1.data.key,
);

expect(decryptedData).to.deep.equal(payload1.data);
Expand Down

0 comments on commit f883129

Please sign in to comment.