Skip to content

Commit 9926fff

Browse files
authored
postgresql_default_privileges_test: Add test for with_grant_option (#63)
1 parent 2d2e9fb commit 9926fff

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

postgresql/resource_postgresql_default_privileges.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func resourcePostgreSQLDefaultPrivilegesRead(db *DBConnection, d *schema.Resourc
9797

9898
func resourcePostgreSQLDefaultPrivilegesCreate(db *DBConnection, d *schema.ResourceData) error {
9999

100+
if d.Get("with_grant_option").(bool) && strings.ToLower(d.Get("role").(string)) == "public" {
101+
return fmt.Errorf("with_grant_option cannot be true for role 'public'")
102+
}
103+
100104
if err := validatePrivileges(d); err != nil {
101105
return err
102106
}

postgresql/resource_postgresql_default_privileges_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func TestAccPostgresqlDefaultPrivileges(t *testing.T) {
2323
// Set default privileges to the test role then to public (i.e.: everyone)
2424
for _, role := range []string{roleName, "public"} {
2525
t.Run(role, func(t *testing.T) {
26+
withGrant := true
27+
if role == "public" {
28+
withGrant = false
29+
}
2630

2731
// We set PGUSER as owner as he will create the test table
2832
var tfConfig = fmt.Sprintf(`
@@ -32,9 +36,10 @@ resource "postgresql_default_privileges" "test_ro" {
3236
role = "%s"
3337
schema = "test_schema"
3438
object_type = "table"
39+
with_grant_option = %t
3540
privileges = %%s
3641
}
37-
`, dbName, config.Username, role)
42+
`, dbName, config.Username, role, withGrant)
3843

3944
resource.Test(t, resource.TestCase{
4045
PreCheck: func() {
@@ -56,6 +61,7 @@ resource "postgresql_default_privileges" "test_ro" {
5661
return testCheckTablesPrivileges(t, dbName, roleName, tables, []string{"SELECT"})
5762
},
5863
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "object_type", "table"),
64+
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "with_grant_option", fmt.Sprintf("%t", withGrant)),
5965
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "privileges.#", "1"),
6066
resource.TestCheckResourceAttr("postgresql_default_privileges.test_ro", "privileges.3138006342", "SELECT"),
6167
),

0 commit comments

Comments
 (0)