Skip to content

Commit

Permalink
chore: added test for cancellation to VaultsSecretsRemove
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Jan 13, 2025
1 parent a027966 commit 4780cd8
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import os from 'os';
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
import { DB } from '@matrixai/db';
import { RPCClient } from '@matrixai/rpc';
import { ErrorRPCTimedOut } from '@matrixai/rpc/dist/errors';
import { WebSocketClient } from '@matrixai/ws';
import TaskManager from '@/tasks/TaskManager';
import ACL from '@/acl/ACL';
@@ -2463,6 +2464,62 @@ describe('vaultsSecretsRemove', () => {
vaultsErrors.ErrorVaultsVaultUndefined,
);
});
test('should fail when cancelled', async () => {
// Inducing a cancellation by a timeout
const response = await rpcClient.methods.vaultsSecretsRemove({
timer: 100,
});
// Read response
const consumeP = async () => {
for await (const _ of response.readable) {
// Consume values
}
};
await expect(consumeP()).rejects.toThrow(ErrorRPCTimedOut);
});
test('should cancel in the midst of an operation', async () => {
// Create secrets
const secretName = 'test-secret1';
const vaultId = await vaultManager.createVault('test-vault');
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.writeF(async (efs) => {
await efs.writeFile(secretName, secretName);
});
});
// Inducing a cancellation by a timeout
const response = await rpcClient.methods.vaultsSecretsRemove({
timer: 100,
});
// Header message
const writer = response.writable.getWriter();
await writer.write({
type: 'VaultNamesHeaderMessage',
vaultNames: [vaultIdEncoded],
});
// Set a timeout so that the method will execute after RPC timeout
setTimeout(async () => {
// Content messages
await writer.write({
type: 'SecretIdentifierMessage',
nameOrId: vaultIdEncoded,
secretName: secretName,
});
await writer.close();
// Read response
const consumeP = async () => {
for await (const _ of response.readable) {
// Consume values
}
};
await expect(consumeP()).rejects.toThrow(ErrorRPCTimedOut);
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.readF(async (efs) => {
expect(await efs.exists(secretName)).toBeTruthy();
});
});
}, 150);
});
test('fails deleting vault root', async () => {
// Create secrets
const secretName = 'test-secret1';

0 comments on commit 4780cd8

Please sign in to comment.