Skip to content

Commit

Permalink
chore: cleaned up tests and VaultsSecretsRemove
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Dec 2, 2024
1 parent 84226e8 commit 6cf41b7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
22 changes: 15 additions & 7 deletions src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
SecretsRemoveHeaderMessage,
SecretIdentifierMessageTagged,
SuccessOrErrorMessage,
VaultNamesHeaderMessage,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import type { FileSystemWritable } from '../../vaults/types';
Expand Down Expand Up @@ -34,18 +35,20 @@ class VaultsSecretsRemove extends DuplexHandler<
): AsyncGenerator<ClientRPCResponseResult<SuccessOrErrorMessage>> {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
const vaultAcquires: Array<ResourceAcquire<FileSystemWritable>> = [];
// Extracts the header message from the iterator
const headerMessage = await (async () => {
const iterator = input[Symbol.asyncIterator]();
const header = (await iterator.next()).value;
if (header.type === 'VaultNamesHeaderMessage') {
if (header == null) throw new clientErrors.ErrorClientInvalidHeader();
return header;
const header: VaultNamesHeaderMessage | SecretIdentifierMessageTagged = (
await iterator.next()
).value;
if (header == null || header.type !== 'VaultNamesHeaderMessage') {
throw new clientErrors.ErrorClientInvalidHeader();
}
return header;
})();
// Create an array of write acquires
await db.withTransactionF(async (tran) => {
const vaultAcquires = await db.withTransactionF(async (tran) => {
const vaultAcquires: Array<ResourceAcquire<FileSystemWritable>> = [];
for (const vaultName of headerMessage.vaultNames) {
const vaultIdFromName = await vaultManager.getVaultId(vaultName, tran);
const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(vaultName);
Expand All @@ -60,6 +63,7 @@ class VaultsSecretsRemove extends DuplexHandler<
);
vaultAcquires.push(acquire);
}
return vaultAcquires;
});
// Acquire all locks in parallel and perform all operations at once
yield* withG(
Expand All @@ -72,7 +76,11 @@ class VaultsSecretsRemove extends DuplexHandler<
}
for await (const message of input) {
// Ignoring any header messages
if (message.type !== 'SecretIdentifierMessage') continue;
if (message.type === 'VaultNamesHeaderMessage') {
throw new clientErrors.ErrorClientInvalidHeader(
'The header message cannot be sent multiple times',
);
}
const efs = vaultMap.get(message.nameOrId);
if (efs == null) {
throw new vaultsErrors.ErrorVaultsVaultUndefined(
Expand Down
2 changes: 1 addition & 1 deletion src/git/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ async function listObjects({
}
return;
default:
utils.never();
utils.never('Invalid type');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function getDefaultNodePath(): string | undefined {
return p;
}

function never(message?: string): never {
function never(message: string): never {
throw new utilsErrors.ErrorUtilsUndefinedBehaviour(message);
}

Expand Down
2 changes: 1 addition & 1 deletion src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class VaultInternal {
}
// The returned transaction can be undefined, too. We won't handle those
// cases.
if (tran == null) utils.never();
if (tran == null) utils.never('Acquired transactions cannot be null');
await tran.lock(
[...this.vaultMetadataDbPath, VaultInternal.dirtyKey].join(''),
);
Expand Down
35 changes: 24 additions & 11 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ describe('vaultsSecretsMkdir', () => {
const vaultName = 'test-vault';
const vaultId = await vaultManager.createVault(vaultName);
const dirPath = 'dir/dir1/dir2';
// Attempt to make directories
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({
Expand All @@ -1461,7 +1462,7 @@ describe('vaultsSecretsMkdir', () => {
metadata: { options: { recursive: true } },
});
await writer.close();

// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.type).toEqual('success');
}
Expand All @@ -1476,13 +1477,15 @@ describe('vaultsSecretsMkdir', () => {
const vaultId = await vaultManager.createVault(vaultName);
const encodeVaultId = vaultsUtils.encodeVaultId(vaultId);
const dirPath = 'dir/dir1/dir2';
// Attempt to make directories
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({ nameOrId: encodeVaultId, dirName: dirPath });
await writer.close();
// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('ENOENT');
expect(data.reason).toEqual(dirPath);
}
Expand Down Expand Up @@ -1543,17 +1546,21 @@ describe('vaultsSecretsMkdir', () => {
// Attempt to make directories
const response = await rpcClient.methods.vaultsSecretsMkdir();
const writer = response.writable.getWriter();
await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath1 });
await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 });
await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath3 });
await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 });
await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath1 });
await writer.close();
// Check if the operation concluded as expected
let successCount = 0;
for await (const data of response.readable) {
if (data.type === 'error') {
expect(data.code).toEqual('ENOENT');
expect(data.reason).toEqual(dirPath3);
} else {
successCount++;
}
}
expect(successCount).toEqual(2);
await vaultManager.withVaults(
[vaultId1, vaultId2],
async (vault1, vault2) => {
Expand Down Expand Up @@ -1585,7 +1592,7 @@ describe('vaultsSecretsMkdir', () => {
// Check if the operation concluded as expected
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('EEXIST');
expect(data.reason).toEqual(dirPath);
}
Expand Down Expand Up @@ -1728,7 +1735,9 @@ describe('vaultsSecretsCat', () => {
// Read response
for await (const data of response.readable) {
expect(data.type).toEqual('success');
if (data.type !== 'success') utils.never();
if (data.type !== 'success') {
utils.never("Type is asserted to be 'success'");
}
expect(data.secretContent).toEqual(secretContent);
}
});
Expand All @@ -1747,7 +1756,7 @@ describe('vaultsSecretsCat', () => {
// Read response
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('ENOENT');
expect(data.reason).toEqual(secretName);
}
Expand All @@ -1773,7 +1782,7 @@ describe('vaultsSecretsCat', () => {
// Read response
for await (const data of response.readable) {
expect(data.type).toEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toEqual('EISDIR');
expect(data.reason).toEqual(secretName);
}
Expand Down Expand Up @@ -1803,7 +1812,9 @@ describe('vaultsSecretsCat', () => {
let totalContent = '';
for await (const data of response.readable) {
expect(data.type).toEqual('success');
if (data.type !== 'success') utils.never();
if (data.type !== 'success') {
utils.never("Type is asserted to be 'success'");
}
totalContent += data.secretContent;
}
expect(totalContent).toEqual(`${secretContent1}${secretContent2}`);
Expand Down Expand Up @@ -1845,7 +1856,9 @@ describe('vaultsSecretsCat', () => {
let totalContent = '';
for await (const data of response.readable) {
expect(data.type).toEqual('success');
if (data.type !== 'success') utils.never();
if (data.type !== 'success') {
utils.never("Type is asserted to be 'success'");
}
totalContent += data.secretContent;
}
expect(totalContent).toEqual(
Expand Down Expand Up @@ -2397,7 +2410,7 @@ describe('vaultsSecretsRemove', () => {
for await (const data of response.readable) {
loopRun = true;
expect(data.type).toStrictEqual('error');
if (data.type !== 'error') utils.never();
if (data.type !== 'error') utils.never("Type is asserted to be 'error'");
expect(data.code).toStrictEqual('EINVAL');
}
// Check
Expand Down

0 comments on commit 6cf41b7

Please sign in to comment.