Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update storage tests #95

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions packages/vc-test-suite/__tests__/storage.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ describe('decryptData', () => {
tag: 'mockTag',
};
const mockKey = 'mockKey';
const mockDecryptedBase64 = 'ZGVjcnlwdGVkIGRhdGE='; // base64 for "decrypted data"
const mockDecrypted = 'decrypted data';

beforeEach(() => {
jest.clearAllMocks();
});

afterEach(() => {
jest.restoreAllMocks();
});

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

it('should return decrypted data when given valid input', () => {
const result = decryptData(mockEncryptedData, mockKey);
expect(result).toBe(mockDecrypted);
});
Expand All @@ -50,4 +56,26 @@ describe('decryptData', () => {

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

it('should return the original string for non-base64 decryption result', () => {
const nonBase64String = 'not a base64 string';
(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);
});

const result = decryptData(mockEncryptedData, mockKey);

expect(result).toBe(nonBase64String);
expect(Buffer.from).toHaveBeenCalledWith(nonBase64String, 'base64');
});
});
18 changes: 12 additions & 6 deletions packages/vc-test-suite/tests/Storage/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ interface EncryptedData {
/**
* Decrypts given data using AES-GCM
* @param {object} encryptedData - Object containing cipherText, IV, and tag
* @param {string} key - Decryption key
* @param {string} key - Decryption key in hexadecimal format
* @returns {string} - Decrypted data
*/
export function decryptData({ cipherText, iv, tag }: EncryptedData, key: string) {
const decipher = crypto.createDecipheriv('aes-256-gcm', Buffer.from(key, 'hex'), Buffer.from(iv, 'hex'));
decipher.setAuthTag(Buffer.from(tag, 'hex'));
let decrypted = decipher.update(cipherText, 'hex', 'utf8');
export function decryptData({ cipherText, iv, tag }: EncryptedData, key: string): 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');
return decrypted;

// Try to decode the base64 string, if it fails, return the original string
try {
return Buffer.from(decrypted, 'base64').toString('utf8');
} catch (error) {
return decrypted;
}
}
54 changes: 34 additions & 20 deletions packages/vc-test-suite/tests/Storage/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ describe('Storage Service', function () {
const payload1 = buildPayload(
{
id: guid,
data: 'First credential',
data: { test: 'First credential' },
},
additionalPayload,
);
const payload2 = buildPayload(
{
id: guid,
data: 'Second credential',
data: { test: 'Second credential' },
},
additionalPayload,
);
Expand All @@ -68,14 +68,21 @@ describe('Storage Service', function () {

expect(response1.status).to.equal(201);

const response2 = await request({
url: buildUrl(url, additionalParams),
method: 'POST',
headers: headers,
data: payload2,
});

expect(response2.status).to.equal(409);
let response2;
try {
response2 = await request({
url: buildUrl(url, additionalParams),
method: 'POST',
headers: headers,
data: payload2,
});
} catch (error: any) {
expect(error?.response?.status).to.equal(409);
}

if (response2) {
expect(response2.status).to.not.equal(201);
}

const fetchResponse = await request({
url: buildUrl(response1.data.uri, additionalParams),
Expand Down Expand Up @@ -213,14 +220,14 @@ describe('Storage Service', function () {
const payload1 = buildPayload(
{
id: guid,
data: 'First encrypted credential',
data: { test: 'First encrypted credential' },
},
additionalPayload,
);
const payload2 = buildPayload(
{
id: guid,
data: 'Second encrypted credential',
data: { test: 'Second encrypted credential' },
},
additionalPayload,
);
Expand All @@ -236,14 +243,21 @@ describe('Storage Service', function () {
expect(response1.data).to.have.property('uri');
expect(response1.data).to.have.property('key');

const response2 = await request({
url: buildUrl(encryptionUrl, additionalParams),
method: 'POST',
headers: headers,
data: payload2,
});

expect(response2.status).to.equal(409);
let response2;
try {
response2 = await request({
url: buildUrl(encryptionUrl, additionalParams),
method: 'POST',
headers: headers,
data: payload2,
});
} catch (error: any) {
expect(error?.response?.status).to.equal(409);
}

if (response2) {
expect(response2.status).to.not.equal(201);
}

const fetchResponse = await request({
url: buildUrl(response1.data.uri, additionalParams),
Expand Down
Loading