Skip to content

Commit

Permalink
Add unit test for readCredentialsGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
vicentepinto98 committed Oct 10, 2023
1 parent d2ace73 commit e1b6d88
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ var (
_ resource.ResourceWithImportState = &credentialsGroupResource{}
)

// needed for testing
type objectStorageClient interface {
GetCredentialsGroupsExecute(ctx context.Context, projectId string) (*objectstorage.GetCredentialsGroupsResponse, error)
}

type Model struct {
Id types.String `tfsdk:"id"` // needed by TF
CredentialsGroupId types.String `tfsdk:"credentials_group_id"`
Expand Down Expand Up @@ -302,14 +307,14 @@ func (r *credentialsGroupResource) enableProject(ctx context.Context, model *Mod

// readCredentialsGroups gets all the existing credentials groups for the specified project,
// finds the credentials group that is being read and updates the state. If the credentials group cannot be found, it throws an error
func readCredentialsGroups(ctx context.Context, model *Model, projectId string, client *objectstorage.APIClient) error {
func readCredentialsGroups(ctx context.Context, model *Model, projectId string, client objectStorageClient) error {
found := false

if model.CredentialsGroupId.ValueString() == "" && model.Name.ValueString() == "" {
return fmt.Errorf("missing configuration: either name or credentials group id must be provided")
}

credentialsGroupsResp, err := client.GetCredentialsGroups(ctx, projectId).Execute()
credentialsGroupsResp, err := client.GetCredentialsGroupsExecute(ctx, projectId)
if err != nil {
return fmt.Errorf("getting credentials groups: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package objectstorage

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -9,6 +11,19 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

type objectStorageClientMocked struct {
getCredentialsGroupsFails bool
getCredentialsGroupsResponse *objectstorage.GetCredentialsGroupsResponse
}

func (c *objectStorageClientMocked) GetCredentialsGroupsExecute(ctx context.Context, projectId string) (*objectstorage.GetCredentialsGroupsResponse, error) {

Check failure on line 19 in stackit/internal/services/objectstorage/credentialsgroup/resource_test.go

View workflow job for this annotation

GitHub Actions / CI

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
if c.getCredentialsGroupsFails {
return c.getCredentialsGroupsResponse, fmt.Errorf("get credentials groups failed")
}

return c.getCredentialsGroupsResponse, nil
}

func TestMapFields(t *testing.T) {
tests := []struct {
description string
Expand Down Expand Up @@ -99,3 +114,135 @@ func TestMapFields(t *testing.T) {
})
}
}

func TestReadCredentialsGroups(t *testing.T) {
tests := []struct {
description string
mockedResp *objectstorage.GetCredentialsGroupsResponse
expected Model
getCredentialsGroupsFails bool
isValid bool
}{
{
"default_values",
&objectstorage.GetCredentialsGroupsResponse{
CredentialsGroups: &[]objectstorage.CredentialsGroup{
{
CredentialsGroupId: utils.Ptr("cid"),
},
},
},
Model{
Id: types.StringValue("pid,cid"),
Name: types.StringNull(),
ProjectId: types.StringValue("pid"),
CredentialsGroupId: types.StringValue("cid"),
URN: types.StringNull(),
},
false,
true,
},
{
"simple_values",
&objectstorage.GetCredentialsGroupsResponse{
CredentialsGroups: &[]objectstorage.CredentialsGroup{
{
CredentialsGroupId: utils.Ptr("cid"),
DisplayName: utils.Ptr("name"),
Urn: utils.Ptr("urn"),
},
},
},
Model{
Id: types.StringValue("pid,cid"),
Name: types.StringValue("name"),
ProjectId: types.StringValue("pid"),
CredentialsGroupId: types.StringValue("cid"),
URN: types.StringValue("urn"),
},
false,
true,
},
{
"empty_credentials_groups",
&objectstorage.GetCredentialsGroupsResponse{
CredentialsGroups: &[]objectstorage.CredentialsGroup{},
},
Model{},
false,
false,
},
{
"nil_credentials_groups",
&objectstorage.GetCredentialsGroupsResponse{
CredentialsGroups: nil,
},
Model{},
false,
false,
},
{
"nil_response",
nil,
Model{},
false,
false,
},
{
"non_matching_credentials_group",
&objectstorage.GetCredentialsGroupsResponse{
CredentialsGroups: &[]objectstorage.CredentialsGroup{
{
CredentialsGroupId: utils.Ptr("other_id"),
DisplayName: utils.Ptr("name"),
Urn: utils.Ptr("urn"),
},
},
},
Model{},
false,
false,
},
{
"error_response",
&objectstorage.GetCredentialsGroupsResponse{
CredentialsGroups: &[]objectstorage.CredentialsGroup{
{
CredentialsGroupId: utils.Ptr("other_id"),
DisplayName: utils.Ptr("name"),
Urn: utils.Ptr("urn"),
},
},
},
Model{},
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {

Check failure on line 223 in stackit/internal/services/objectstorage/credentialsgroup/resource_test.go

View workflow job for this annotation

GitHub Actions / CI

empty-lines: extra empty line at the start of a block (revive)

client := &objectStorageClientMocked{
getCredentialsGroupsFails: tt.getCredentialsGroupsFails,
getCredentialsGroupsResponse: tt.mockedResp,
}
model := &Model{
ProjectId: tt.expected.ProjectId,
CredentialsGroupId: tt.expected.CredentialsGroupId,
}
err := readCredentialsGroups(context.Background(), model, "pid", client)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(model, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
})
}
}

0 comments on commit e1b6d88

Please sign in to comment.