Skip to content

Commit

Permalink
test(unit): fix unittest for DeleteCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
joerncodes committed Oct 23, 2023
1 parent 43238a0 commit d4663a7
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 58 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"@types/node": "^18.16.0",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"aws-sdk-client-mock": "^3.0.0",
"aws-sdk-client-mock-jest": "^3.0.0",
"eslint": "^8.39.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
Expand Down Expand Up @@ -63,7 +65,6 @@
"figlet": "^1.6.0",
"husky": "^8.0.3",
"prompt-sync": "^4.2.0",
"read": "isaacs/read",
"source-map-support": "^0.5.21"
}
}
10 changes: 5 additions & 5 deletions src/requests/DeleteRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class DeleteRequest extends BaseRequest {

const command = new DeleteSecretCommand(payload);

return this.getClient()
.send(command)
.catch((error: any) => {
new TypedErrorHandler().handleError(error, mapping);
});
try {
return await this.getClient().send(command);
} catch (error: any) {
new TypedErrorHandler().handleError(error, mapping);
}
}
}

Expand Down
67 changes: 24 additions & 43 deletions test/unit/commands/DeleteCommand.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import { SecretsManagerClient, DeleteSecretCommandInput } from "@aws-sdk/client-secrets-manager";
import { DeleteSecretCommand, SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
import DeleteCommand from "../../../src/commands/DeleteCommand";
import DeleteRequest from "../../../src/requests/DeleteRequest";
import DateFormatter from "../../../src/utils/DateFormatter";
import {mockClient} from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';

class ResourceNotFoundException extends Error {
public __type: string = "ResourceNotFoundException";
}

describe("DeleteCommand", () => {
it("will schedule a secret for deletion", async () => {
const deleteCommand = new DeleteCommand({ key: "hello-world" });
const spy = jest.spyOn(DeleteRequest.prototype, "execute");
const deleteRequestSpy = jest.spyOn(DeleteRequest.prototype, "execute");
const deleteCommand = new DeleteCommand({ key: "hello-world" });

const secretsManagerMock = mockClient(SecretsManagerClient);

const now = new Date();
const now = new Date();

spy.mockImplementation(() => {
return new Promise((resolve, reject) => {
resolve({$metadata: {}, DeletionDate: now})
});
});
const dateString = DateFormatter.formatDate(now);
const successFixture = {$metadata: {}, DeletionDate: now};

const dateString = DateFormatter.formatDate(new Date());
describe("DeleteCommand", () => {
beforeEach(() => {
secretsManagerMock.reset();
});
it("will schedule a secret for deletion", async () => {
secretsManagerMock.on(DeleteSecretCommand).resolves(successFixture);

const result = await deleteCommand.execute();
expect(result).toContain("hush-hello-world");
expect(result).toContain(dateString);
spy.mockReset();
deleteRequestSpy.mockReset();
});

it("will give a helpful error message if the secret can't be found", async () => {
const deleteCommand = new DeleteCommand({ key: "hello-world" });
const spy = jest.spyOn(SecretsManagerClient.prototype, "send");

spy.mockImplementation(() => {
return new Promise((resolve, reject) => {
reject(new ResourceNotFoundException());
});
});
secretsManagerMock.on(DeleteSecretCommand).rejects(new ResourceNotFoundException());

expect.assertions(3);
try {
Expand All @@ -46,43 +43,27 @@ describe("DeleteCommand", () => {
expect(error.toString()).toContain("hush-hello-world");
expect(error.toString()).toContain("could not be deleted because it was not found.");
}
spy.mockReset();
});

it("will just display other errors", async () => {
const deleteCommand = new DeleteCommand({key: "hello-world"});
const spy = jest.spyOn(SecretsManagerClient.prototype, "send");

spy.mockImplementation(() => {
return new Promise((resolve, reject) => {
reject(new Error("Hello world"));
});
});
secretsManagerMock.on(DeleteSecretCommand).rejects(new Error("Hello world"));

expect.assertions(1);
try {
await deleteCommand.execute();
} catch(error: any) {
expect(error.toString()).toBe("Error: Hello world");
}

spy.mockReset();
});

it("can use the force option to delete a secret without scheduling for deletion", async () => {
const deleteCommand = new DeleteCommand({ key: "hello-world", force: true});
const spy = jest.spyOn(SecretsManagerClient.prototype, "send");
spy.mockReset();
secretsManagerMock.on(DeleteSecretCommand).resolves(successFixture);

spy.mockImplementation(() => {
return new Promise((resolve, reject) => {
resolve({$metadata: {}})
});
});
const result = await deleteCommand.execute();
expect(spy).toHaveBeenCalled();
const forceCommand = new DeleteCommand({ key: "hello-world", force: true });
const result = await forceCommand.execute();

expect(secretsManagerMock).toHaveReceivedCommandTimes(DeleteSecretCommand, 1);
expect(result).toContain("hush-hello-world");
expect(result).toContain("successfully deleted.");
spy.mockReset();
});
});
Loading

0 comments on commit d4663a7

Please sign in to comment.