Skip to content

Commit

Permalink
updated IAM tests for missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Dec 11, 2024
1 parent 7494212 commit 80cbe67
Showing 1 changed file with 15 additions and 56 deletions.
71 changes: 15 additions & 56 deletions pkg/clients/iam/iam_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ var _ = Describe("IAMClient", func() {
})

It("should skip deletion if inline policy does not exist", func(ctx SpecContext) {
mockIAM.GetUserFunc = func(ctx context.Context, input *iam.GetUserInput, opts ...func(*iam.Options)) (*iam.GetUserOutput, error) {
return &iam.GetUserOutput{}, nil
}
mockIAM.DeleteUserPolicyFunc = func(ctx context.Context, input *iam.DeleteUserPolicyInput, opts ...func(*iam.Options)) (*iam.DeleteUserPolicyOutput, error) {
return nil, &types.NoSuchEntityException{}
}
Expand All @@ -297,9 +294,6 @@ var _ = Describe("IAMClient", func() {
})

It("should return an error if deleting inline policy fails", func(ctx SpecContext) {
mockIAM.GetUserFunc = func(ctx context.Context, input *iam.GetUserInput, opts ...func(*iam.Options)) (*iam.GetUserOutput, error) {
return &iam.GetUserOutput{}, nil
}
mockIAM.DeleteUserPolicyFunc = func(ctx context.Context, input *iam.DeleteUserPolicyInput, opts ...func(*iam.Options)) (*iam.DeleteUserPolicyOutput, error) {
return nil, fmt.Errorf("simulated DeleteUserPolicy failure")
}
Expand All @@ -313,21 +307,6 @@ var _ = Describe("IAMClient", func() {
})

It("should successfully delete all access keys for the user", func(ctx SpecContext) {
mockIAM.GetUserFunc = func(ctx context.Context, input *iam.GetUserInput, opts ...func(*iam.Options)) (*iam.GetUserOutput, error) {
return &iam.GetUserOutput{}, nil
}
mockIAM.ListAccessKeysFunc = func(ctx context.Context, input *iam.ListAccessKeysInput, opts ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
return &iam.ListAccessKeysOutput{
AccessKeyMetadata: []types.AccessKeyMetadata{
{AccessKeyId: aws.String("key-1")},
{AccessKeyId: aws.String("key-2")},
},
}, nil
}
mockIAM.DeleteAccessKeyFunc = func(ctx context.Context, input *iam.DeleteAccessKeyInput, opts ...func(*iam.Options)) (*iam.DeleteAccessKeyOutput, error) {
return &iam.DeleteAccessKeyOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client.IAMService = mockIAM

Expand Down Expand Up @@ -359,16 +338,6 @@ var _ = Describe("IAMClient", func() {
})

It("should successfully delete the user", func(ctx SpecContext) {
mockIAM.GetUserFunc = func(ctx context.Context, input *iam.GetUserInput, opts ...func(*iam.Options)) (*iam.GetUserOutput, error) {
return &iam.GetUserOutput{}, nil
}
mockIAM.ListAccessKeysFunc = func(ctx context.Context, input *iam.ListAccessKeysInput, opts ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
return &iam.ListAccessKeysOutput{}, nil
}
mockIAM.DeleteUserFunc = func(ctx context.Context, input *iam.DeleteUserInput, opts ...func(*iam.Options)) (*iam.DeleteUserOutput, error) {
return &iam.DeleteUserOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client.IAMService = mockIAM

Expand All @@ -377,12 +346,6 @@ var _ = Describe("IAMClient", func() {
})

It("should return an error if deleting user fails", func(ctx SpecContext) {
mockIAM.GetUserFunc = func(ctx context.Context, input *iam.GetUserInput, opts ...func(*iam.Options)) (*iam.GetUserOutput, error) {
return &iam.GetUserOutput{}, nil
}
mockIAM.ListAccessKeysFunc = func(ctx context.Context, input *iam.ListAccessKeysInput, opts ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
return &iam.ListAccessKeysOutput{}, nil
}
mockIAM.DeleteUserFunc = func(ctx context.Context, input *iam.DeleteUserInput, opts ...func(*iam.Options)) (*iam.DeleteUserOutput, error) {
return nil, fmt.Errorf("simulated DeleteUser failure")
}
Expand Down Expand Up @@ -422,18 +385,6 @@ var _ = Describe("IAMClient", func() {
})

It("should successfully delete all access keys", func(ctx SpecContext) {
mockIAM.ListAccessKeysFunc = func(ctx context.Context, input *iam.ListAccessKeysInput, opts ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
return &iam.ListAccessKeysOutput{
AccessKeyMetadata: []types.AccessKeyMetadata{
{AccessKeyId: aws.String("key-1")},
{AccessKeyId: aws.String("key-2")},
},
}, nil
}
mockIAM.DeleteAccessKeyFunc = func(ctx context.Context, input *iam.DeleteAccessKeyInput, opts ...func(*iam.Options)) (*iam.DeleteAccessKeyOutput, error) {
return &iam.DeleteAccessKeyOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client.IAMService = mockIAM

Expand All @@ -442,13 +393,6 @@ var _ = Describe("IAMClient", func() {
})

It("should return an error if deleting any access key fails", func(ctx SpecContext) {
mockIAM.ListAccessKeysFunc = func(ctx context.Context, input *iam.ListAccessKeysInput, opts ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
return &iam.ListAccessKeysOutput{
AccessKeyMetadata: []types.AccessKeyMetadata{
{AccessKeyId: aws.String("key-1")},
},
}, nil
}
mockIAM.DeleteAccessKeyFunc = func(ctx context.Context, input *iam.DeleteAccessKeyInput, opts ...func(*iam.Options)) (*iam.DeleteAccessKeyOutput, error) {
return nil, fmt.Errorf("simulated DeleteAccessKey failure")
}
Expand Down Expand Up @@ -485,5 +429,20 @@ var _ = Describe("IAMClient", func() {
Expect(err).To(BeNil())
})

It("should return an error if listing access keys fails", func(ctx SpecContext) {
mockIAM := &mock.MockIAMClient{}
mockIAM.ListAccessKeysFunc = func(ctx context.Context, input *iam.ListAccessKeysInput, opts ...func(*iam.Options)) (*iam.ListAccessKeysOutput, error) {
return nil, fmt.Errorf("simulated ListAccessKeys failure")
}

client := &iamclient.IAMClient{
IAMService: mockIAM,
}

err := client.DeleteAllAccessKeys(ctx, "test-user")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to list access keys for IAM user test-user"))
Expect(err.Error()).To(ContainSubstring("simulated ListAccessKeys failure"))
})
})
})

0 comments on commit 80cbe67

Please sign in to comment.