@@ -5,9 +5,136 @@ import (
5
5
"testing"
6
6
7
7
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8
+ "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8
9
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10
+ "github.com/lib/pq"
9
11
)
10
12
13
+ func TestCreateGrantQuery (t * testing.T ) {
14
+ var databaseName = "foo"
15
+ var roleName = "bar"
16
+
17
+ cases := []struct {
18
+ resource * schema.ResourceData
19
+ privileges []string
20
+ expected string
21
+ }{
22
+ {
23
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
24
+ "object_type" : "table" ,
25
+ "schema" : databaseName ,
26
+ "role" : roleName ,
27
+ }),
28
+ privileges : []string {"SELECT" },
29
+ expected : fmt .Sprintf ("GRANT SELECT ON ALL TABLES IN SCHEMA %s TO %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
30
+ },
31
+ {
32
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
33
+ "object_type" : "sequence" ,
34
+ "schema" : databaseName ,
35
+ "role" : roleName ,
36
+ }),
37
+ privileges : []string {"SELECT" },
38
+ expected : fmt .Sprintf ("GRANT SELECT ON ALL SEQUENCES IN SCHEMA %s TO %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
39
+ },
40
+ {
41
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
42
+ "object_type" : "TABLE" ,
43
+ "schema" : databaseName ,
44
+ "role" : roleName ,
45
+ "with_grant_option" : true ,
46
+ }),
47
+ privileges : []string {"SELECT" , "INSERT" , "UPDATE" },
48
+ expected : fmt .Sprintf ("GRANT SELECT,INSERT,UPDATE ON ALL TABLES IN SCHEMA %s TO %s WITH GRANT OPTION" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
49
+ },
50
+ {
51
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
52
+ "object_type" : "database" ,
53
+ "database" : databaseName ,
54
+ "role" : roleName ,
55
+ }),
56
+ privileges : []string {"CREATE" },
57
+ expected : fmt .Sprintf ("GRANT CREATE ON DATABASE %s TO %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
58
+ },
59
+ {
60
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
61
+ "object_type" : "database" ,
62
+ "database" : databaseName ,
63
+ "role" : roleName ,
64
+ }),
65
+ privileges : []string {"CREATE" , "CONNECT" },
66
+ expected : fmt .Sprintf ("GRANT CREATE,CONNECT ON DATABASE %s TO %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
67
+ },
68
+ {
69
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
70
+ "object_type" : "DATABASE" ,
71
+ "database" : databaseName ,
72
+ "role" : roleName ,
73
+ "with_grant_option" : true ,
74
+ }),
75
+ privileges : []string {"ALL PRIVILEGES" },
76
+ expected : fmt .Sprintf ("GRANT ALL PRIVILEGES ON DATABASE %s TO %s WITH GRANT OPTION" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
77
+ },
78
+ }
79
+
80
+ for _ , c := range cases {
81
+ out := createGrantQuery (c .resource , c .privileges )
82
+ if out != c .expected {
83
+ t .Fatalf ("Error matching output and expected: %#v vs %#v" , out , c .expected )
84
+ }
85
+ }
86
+ }
87
+
88
+ func TestCreateRevokeQuery (t * testing.T ) {
89
+ var databaseName = "foo"
90
+ var roleName = "bar"
91
+
92
+ cases := []struct {
93
+ resource * schema.ResourceData
94
+ expected string
95
+ }{
96
+ {
97
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
98
+ "object_type" : "table" ,
99
+ "schema" : databaseName ,
100
+ "role" : roleName ,
101
+ }),
102
+ expected : fmt .Sprintf ("REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA %s FROM %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
103
+ },
104
+ {
105
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
106
+ "object_type" : "sequence" ,
107
+ "schema" : databaseName ,
108
+ "role" : roleName ,
109
+ }),
110
+ expected : fmt .Sprintf ("REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA %s FROM %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
111
+ },
112
+ {
113
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
114
+ "object_type" : "database" ,
115
+ "database" : databaseName ,
116
+ "role" : roleName ,
117
+ }),
118
+ expected : fmt .Sprintf ("REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
119
+ },
120
+ {
121
+ resource : schema .TestResourceDataRaw (t , resourcePostgreSQLGrant ().Schema , map [string ]interface {}{
122
+ "object_type" : "DATABASE" ,
123
+ "database" : databaseName ,
124
+ "role" : roleName ,
125
+ }),
126
+ expected : fmt .Sprintf ("REVOKE ALL PRIVILEGES ON DATABASE %s FROM %s" , pq .QuoteIdentifier (databaseName ), pq .QuoteIdentifier (roleName )),
127
+ },
128
+ }
129
+
130
+ for _ , c := range cases {
131
+ out := createRevokeQuery (c .resource )
132
+ if out != c .expected {
133
+ t .Fatalf ("Error matching output and expected: %#v vs %#v" , out , c .expected )
134
+ }
135
+ }
136
+ }
137
+
11
138
func TestAccPostgresqlGrant (t * testing.T ) {
12
139
skipIfNotAcc (t )
13
140
@@ -84,3 +211,45 @@ func TestAccPostgresqlGrant(t *testing.T) {
84
211
},
85
212
})
86
213
}
214
+
215
+ func TestAccPostgresqlGrantDatabase (t * testing.T ) {
216
+ skipIfNotAcc (t )
217
+
218
+ // We have to create the database outside of resource.Test
219
+ // because we need to create tables to assert that grant are correctly applied
220
+ // and we don't have this resource yet
221
+ dbSuffix , teardown := setupTestDatabase (t , true , true )
222
+ defer teardown ()
223
+
224
+ dbName , roleName := getTestDBNames (dbSuffix )
225
+ var testGrantSelect = fmt .Sprintf (`
226
+ resource "postgresql_grant" "test" {
227
+ database = "%s"
228
+ role = "%s"
229
+ schema = "test_schema"
230
+ object_type = "database"
231
+ privileges = ["CONNECT", "CREATE"]
232
+ with_grant_option = true
233
+ }
234
+ ` , dbName , roleName )
235
+
236
+ resource .Test (t , resource.TestCase {
237
+ PreCheck : func () {
238
+ testAccPreCheck (t )
239
+ testCheckCompatibleVersion (t , featurePrivileges )
240
+ },
241
+ Providers : testAccProviders ,
242
+ Steps : []resource.TestStep {
243
+ {
244
+ Config : testGrantSelect ,
245
+ Check : resource .ComposeTestCheckFunc (
246
+ resource .TestCheckResourceAttr ("postgresql_grant.test" , "privileges.#" , "2" ),
247
+ resource .TestCheckResourceAttr ("postgresql_grant.test" , "with_grant_option" , "true" ),
248
+ func (* terraform.State ) error {
249
+ return testCheckDatabasesPrivileges (t , dbSuffix , []string {"CONNECT" })
250
+ },
251
+ ),
252
+ },
253
+ },
254
+ })
255
+ }
0 commit comments