Skip to content

Commit

Permalink
changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Jan 9, 2024
1 parent b3f933a commit 1f8ebb6
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 deletions pkg/resources/grant_privileges_to_database_role_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,57 @@ func TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase(t *testing.T) {
})
}

func TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase_PrivilegesReversed(t *testing.T) {
name := "test_database_role_name"
configVariables := config.Variables{
"name": config.StringVariable(name),
"privileges": config.ListVariable(
config.StringVariable(string(sdk.AccountObjectPrivilegeUsage)),
config.StringVariable(string(sdk.AccountObjectPrivilegeModify)),
config.StringVariable(string(sdk.AccountObjectPrivilegeCreateSchema)),
),
"database": config.StringVariable(acc.TestDatabaseName),
"with_grant_option": config.BoolVariable(true),
}
resourceName := "snowflake_grant_privileges_to_database_role.test"

databaseRoleName := sdk.NewDatabaseObjectIdentifier(acc.TestDatabaseName, name).FullyQualifiedName()
databaseName := sdk.NewAccountObjectIdentifier(acc.TestDatabaseName).FullyQualifiedName()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: testAccCheckDatabaseRolePrivilegesRevoked,
Steps: []resource.TestStep{
{
PreConfig: func() { createDatabaseRoleOutsideTerraform(t, name) },
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase"),
ConfigVariables: configVariables,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "database_role_name", databaseRoleName),
resource.TestCheckResourceAttr(resourceName, "privileges.#", "3"),
resource.TestCheckResourceAttr(resourceName, "privileges.0", string(sdk.AccountObjectPrivilegeCreateSchema)),
resource.TestCheckResourceAttr(resourceName, "privileges.1", string(sdk.AccountObjectPrivilegeModify)),
resource.TestCheckResourceAttr(resourceName, "privileges.2", string(sdk.AccountObjectPrivilegeUsage)),
resource.TestCheckResourceAttr(resourceName, "on_database", databaseName),
resource.TestCheckResourceAttr(resourceName, "with_grant_option", "true"),
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s|true|false|CREATE SCHEMA,MODIFY,USAGE|OnDatabase|%s", databaseRoleName, databaseName)),
),
},
{
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_GrantPrivilegesToDatabaseRole_OnDatabase"),
ConfigVariables: configVariables,
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAcc_GrantPrivilegesToDatabaseRole_OnSchema(t *testing.T) {
name := "test_database_role_name"
configVariables := config.Variables{
Expand Down Expand Up @@ -641,19 +692,6 @@ func TestAcc_GrantPrivilegesToDatabaseRole_AlwaysApply(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s|false|false|ALL|OnDatabase|%s", databaseRoleName, databaseName)),
),
},
{
ConfigDirectory: config.TestNameDirectory(),
ConfigVariables: configVariables(false),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "always_apply", "false"),
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s|false|false|ALL|OnDatabase|%s", databaseRoleName, databaseName)),
),
},
{
ConfigDirectory: config.TestNameDirectory(),
ConfigVariables: configVariables(true),
Expand Down Expand Up @@ -765,7 +803,10 @@ func queriedPrivilegesContainAtLeast(databaseRoleName sdk.DatabaseObjectIdentifi
for _, grant := range grants {
grantedPrivileges = append(grantedPrivileges, grant.Privilege)
}
if len(grantedPrivileges) < len(privileges) {
notAllPrivilegesInGrantedPrivileges := slices.ContainsFunc(privileges, func(privilege string) bool {
return !slices.Contains(grantedPrivileges, privilege)
})
if notAllPrivilegesInGrantedPrivileges {
return fmt.Errorf("not every privilege from the list: %v was found in grant privileges: %v, for database role name: %s", privileges, grantedPrivileges, databaseRoleName.FullyQualifiedName())
}

Expand Down

0 comments on commit 1f8ebb6

Please sign in to comment.