Skip to content

Commit

Permalink
Add tests for data source permission
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorGFM committed Sep 25, 2023
1 parent 29ca2f1 commit 9747f3b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
78 changes: 78 additions & 0 deletions cyral/data_source_cyral_permission_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cyral

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

var expectedPermissionNames = []string{
"Approval Management",
"Modify Policies",
"Modify Roles",
"Modify Sidecars and Repositories",
"Modify Users",
"Repo Crawler",
"View Audit Logs",
"View Datamaps",
"View Integrations",
"View Policies",
"View Roles",
"View Users",
"Modify Integrations",
}

func TestAccPermissionDataSource(t *testing.T) {
testSteps := []resource.TestStep{}
dataSourceName1 := "permissions_1"
testSteps = append(
testSteps,
[]resource.TestStep{
accTestStepPermissionDataSource_RetrieveAllPermissions(dataSourceName1),
}...,
)

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: testSteps,
})
}

func accTestStepPermissionDataSource_RetrieveAllPermissions(dataSourceName string) resource.TestStep {
dataSourceFullName := fmt.Sprintf("data.cyral_permission.%s", dataSourceName)
config := fmt.Sprintf(`
data "cyral_permission" "%s" {
}
`, dataSourceName)
var checks []resource.TestCheckFunc
for index, expectedPermissionName := range expectedPermissionNames {
checks = append(checks,
[]resource.TestCheckFunc{
resource.TestCheckResourceAttrSet(
dataSourceFullName,
fmt.Sprintf(
"%s.%d.%s",
PermissionDataSourcePermissionListKey,
index,
IDKey,
),
),
resource.TestCheckTypeSetElemNestedAttrs(
dataSourceFullName,
fmt.Sprintf("%s.*", PermissionDataSourcePermissionListKey),
map[string]string{NameKey: expectedPermissionName},
),
resource.TestCheckTypeSetElemNestedAttrs(
dataSourceFullName,
fmt.Sprintf("%s.*", PermissionDataSourcePermissionListKey),
map[string]string{DescriptionKey: expectedPermissionName},
),
}...,
)
}
return resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(checks...),
}
}
22 changes: 14 additions & 8 deletions cyral/resource_cyral_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (

func TestAccServiceAccountResource(t *testing.T) {
testSteps := []resource.TestStep{}
serviceAccountName1 := accTestName("service-account", "1")
resourceName1 := "service_account_1"
testSteps = append(
testSteps,
[]resource.TestStep{
accTestStepServiceAccountResource_RequiredArgumentDisplayName(serviceAccountName1),
accTestStepServiceAccountResource_RequiredArgumentPermissions(serviceAccountName1),
accTestStepServiceAccountResource_EmptyPermissions(serviceAccountName1),
accTestStepServiceAccountResource_SinglePermission(serviceAccountName1),
accTestStepServiceAccountResource_DuplicatedPermission(serviceAccountName1),
accTestStepServiceAccountResource_AllPermissions(serviceAccountName1),
accTestStepServiceAccountResource_UpdatedFields(serviceAccountName1),
accTestStepServiceAccountResource_RequiredArgumentDisplayName(resourceName1),
accTestStepServiceAccountResource_RequiredArgumentPermissions(resourceName1),
accTestStepServiceAccountResource_EmptyPermissions(resourceName1),
accTestStepServiceAccountResource_SinglePermission(resourceName1),
accTestStepServiceAccountResource_DuplicatedPermission(resourceName1),
accTestStepServiceAccountResource_AllPermissions(resourceName1),
accTestStepServiceAccountResource_UpdatedFields(resourceName1),
}...,
)

Expand Down Expand Up @@ -212,6 +212,12 @@ func getAccTestStepForServiceAccountResourceFullConfig(
fmt.Sprintf("%s.#", serviceAccountResourcePermissionIDsKey),
fmt.Sprintf("%d", len(permissionNames)),
),
resource.TestCheckResourceAttrPair(
resourceFullName,
IDKey,
resourceFullName,
serviceAccountResourceClientIDKey,
),
resource.TestCheckResourceAttrSet(
resourceFullName,
serviceAccountResourceClientIDKey,
Expand Down

0 comments on commit 9747f3b

Please sign in to comment.