Skip to content

Commit

Permalink
refactor: Regenerate IAM client
Browse files Browse the repository at this point in the history
  • Loading branch information
godu committed Mar 8, 2024
1 parent 81ef1d8 commit 14068eb
Showing 1 changed file with 98 additions and 104 deletions.
202 changes: 98 additions & 104 deletions packages/client-iam/test/IAM.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
CreateRoleCommand,
type AddClientIDToOpenIDConnectProviderCommandInput,
AddClientIDToOpenIDConnectProviderCommand,
IAMClient,
CreateRoleCommandInput,
} from "@aws-sdk/client-iam";
import { mockClient } from "aws-sdk-client-mock";
import * as Effect from "effect/Effect";
Expand All @@ -21,29 +21,21 @@ import {

import "aws-sdk-client-mock-jest";

const iamMock = mockClient(IAMClient);
const { createRole } = Effect.serviceFunctions(IAMService);
const clientMock = mockClient(IAMClient);

describe("IAMClientImpl", () => {
it("default", async () => {
iamMock.reset().on(CreateRoleCommand).resolves({});

const args: CreateRoleCommandInput = {
RoleName: "test",
AssumeRolePolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "Statement1",
Effect: "Allow",
Principal: {},
Action: "sts:AssumeRole",
},
],
}),
};

const program = createRole(args);
clientMock
.reset()
.on(AddClientIDToOpenIDConnectProviderCommand)
.resolves({});

const args =
{} as unknown as AddClientIDToOpenIDConnectProviderCommandInput;

const program = Effect.flatMap(IAMService, (service) =>
service.addClientIDToOpenIDConnectProvider(args),
);

const result = await pipe(
program,
Expand All @@ -52,29 +44,28 @@ describe("IAMClientImpl", () => {
);

expect(result).toEqual(Exit.succeed({}));
expect(iamMock).toHaveReceivedCommandTimes(CreateRoleCommand, 1);
expect(iamMock).toHaveReceivedCommandWith(CreateRoleCommand, args);
expect(clientMock).toHaveReceivedCommandTimes(
AddClientIDToOpenIDConnectProviderCommand,
1,
);
expect(clientMock).toHaveReceivedCommandWith(
AddClientIDToOpenIDConnectProviderCommand,
args,
);
});

it("configurable", async () => {
iamMock.reset().on(CreateRoleCommand).resolves({});

const args: CreateRoleCommandInput = {
RoleName: "test",
AssumeRolePolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "Statement1",
Effect: "Allow",
Principal: {},
Action: "sts:AssumeRole",
},
],
}),
};

const program = createRole(args);
clientMock
.reset()
.on(AddClientIDToOpenIDConnectProviderCommand)
.resolves({});

const args =
{} as unknown as AddClientIDToOpenIDConnectProviderCommandInput;

const program = Effect.flatMap(IAMService, (service) =>
service.addClientIDToOpenIDConnectProvider(args),
);

const IAMClientConfigLayer = Layer.succeed(IAMClientInstanceConfig, {
region: "eu-central-1",
Expand All @@ -90,29 +81,28 @@ describe("IAMClientImpl", () => {
);

expect(result).toEqual(Exit.succeed({}));
expect(iamMock).toHaveReceivedCommandTimes(CreateRoleCommand, 1);
expect(iamMock).toHaveReceivedCommandWith(CreateRoleCommand, args);
expect(clientMock).toHaveReceivedCommandTimes(
AddClientIDToOpenIDConnectProviderCommand,
1,
);
expect(clientMock).toHaveReceivedCommandWith(
AddClientIDToOpenIDConnectProviderCommand,
args,
);
});

it("base", async () => {
iamMock.reset().on(CreateRoleCommand).resolves({});

const args: CreateRoleCommandInput = {
RoleName: "test",
AssumeRolePolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "Statement1",
Effect: "Allow",
Principal: {},
Action: "sts:AssumeRole",
},
],
}),
};

const program = createRole(args);
clientMock
.reset()
.on(AddClientIDToOpenIDConnectProviderCommand)
.resolves({});

const args =
{} as unknown as AddClientIDToOpenIDConnectProviderCommandInput;

const program = Effect.flatMap(IAMService, (service) =>
service.addClientIDToOpenIDConnectProvider(args),
);

const IAMClientInstanceLayer = Layer.succeed(
IAMClientInstance,
Expand All @@ -129,29 +119,28 @@ describe("IAMClientImpl", () => {
);

expect(result).toEqual(Exit.succeed({}));
expect(iamMock).toHaveReceivedCommandTimes(CreateRoleCommand, 1);
expect(iamMock).toHaveReceivedCommandWith(CreateRoleCommand, args);
expect(clientMock).toHaveReceivedCommandTimes(
AddClientIDToOpenIDConnectProviderCommand,
1,
);
expect(clientMock).toHaveReceivedCommandWith(
AddClientIDToOpenIDConnectProviderCommand,
args,
);
});

it("extended", async () => {
iamMock.reset().on(CreateRoleCommand).resolves({});

const args: CreateRoleCommandInput = {
RoleName: "test",
AssumeRolePolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "Statement1",
Effect: "Allow",
Principal: {},
Action: "sts:AssumeRole",
},
],
}),
};

const program = createRole(args);
clientMock
.reset()
.on(AddClientIDToOpenIDConnectProviderCommand)
.resolves({});

const args =
{} as unknown as AddClientIDToOpenIDConnectProviderCommandInput;

const program = Effect.flatMap(IAMService, (service) =>
service.addClientIDToOpenIDConnectProvider(args),
);

const IAMClientInstanceLayer = Layer.effect(
IAMClientInstance,
Expand All @@ -172,29 +161,28 @@ describe("IAMClientImpl", () => {
);

expect(result).toEqual(Exit.succeed({}));
expect(iamMock).toHaveReceivedCommandTimes(CreateRoleCommand, 1);
expect(iamMock).toHaveReceivedCommandWith(CreateRoleCommand, args);
expect(clientMock).toHaveReceivedCommandTimes(
AddClientIDToOpenIDConnectProviderCommand,
1,
);
expect(clientMock).toHaveReceivedCommandWith(
AddClientIDToOpenIDConnectProviderCommand,
args,
);
});

it("fail", async () => {
iamMock.reset().on(CreateRoleCommand).rejects(new Error("test"));

const args: CreateRoleCommandInput = {
RoleName: "test",
AssumeRolePolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Sid: "Statement1",
Effect: "Allow",
Principal: {},
Action: "sts:AssumeRole",
},
],
}),
};

const program = createRole(args, { requestTimeout: 1000 });
clientMock
.reset()
.on(AddClientIDToOpenIDConnectProviderCommand)
.rejects(new Error("test"));

const args =
{} as unknown as AddClientIDToOpenIDConnectProviderCommandInput;

const program = Effect.flatMap(IAMService, (service) =>
service.addClientIDToOpenIDConnectProvider(args),
);

const result = await pipe(
program,
Expand All @@ -212,7 +200,13 @@ describe("IAMClientImpl", () => {
}),
),
);
expect(iamMock).toHaveReceivedCommandTimes(CreateRoleCommand, 1);
expect(iamMock).toHaveReceivedCommandWith(CreateRoleCommand, args);
expect(clientMock).toHaveReceivedCommandTimes(
AddClientIDToOpenIDConnectProviderCommand,
1,
);
expect(clientMock).toHaveReceivedCommandWith(
AddClientIDToOpenIDConnectProviderCommand,
args,
);
});
});

0 comments on commit 14068eb

Please sign in to comment.