newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / acceptance / server / grant_spec.rb
1 require 'spec_helper_acceptance'
2
3 describe 'postgresql::server::grant:', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
4
5   let(:db) { 'grant_priv_test' }
6   let(:owner) { 'psql_grant_priv_owner' }
7   let(:user) { 'psql_grant_priv_tester' }
8   let(:password) { 'psql_grant_role_pw' }
9   let(:pp_install) { "class {'postgresql::server': }"}
10
11   let(:pp_setup) { pp_setup = <<-EOS.unindent
12     $db = #{db}
13     $owner = #{owner}
14     $user = #{user}
15     $password = #{password}
16
17     class { 'postgresql::server': }
18
19     postgresql::server::role { $owner:
20       password_hash => postgresql_password($owner, $password),
21     }
22
23     # Since we are not testing pg_hba or any of that, make a local user for ident auth
24     user { $owner:
25       ensure => present,
26     }
27
28     postgresql::server::database { $db:
29       owner   => $owner,
30       require => Postgresql::Server::Role[$owner],
31     }
32
33     # Create a user to grant privileges to
34     postgresql::server::role { $user:
35       db      => $db,
36       require => Postgresql::Server::Database[$db],
37     }
38
39     # Make a local user for ident auth
40     user { $user:
41       ensure => present,
42     }
43
44     # Grant them connect to the database
45     postgresql::server::database_grant { "allow connect for ${user}":
46       privilege => 'CONNECT',
47       db        => $db,
48       role      => $user,
49     }
50     EOS
51   }
52
53   context 'LANGUAGE' do
54     describe 'GRANT * ON LANGUAGE' do
55       #testing grants on language requires a superuser
56       let(:superuser) { 'postgres' }
57       let(:pp_lang) { pp_setup + <<-EOS.unindent
58
59           postgresql_psql { 'make sure plpgsql exists':
60             command   => 'CREATE LANGUAGE plpgsql',
61             db        => $db,
62             psql_user => '#{superuser}',
63             unless    => "SELECT 1 from pg_language where lanname = 'plpgsql'",
64             require   => Postgresql::Server::Database[$db],
65           }
66
67           postgresql::server::grant { 'grant usage on plpgsql':
68             psql_user     => '#{superuser}',
69             privilege     => 'USAGE',
70             object_type   => 'LANGUAGE',
71             object_name   => 'plpgsql',
72             role          => $user,
73             db            => $db,
74             require       => [ Postgresql_psql['make sure plpgsql exists'],
75                                Postgresql::Server::Role[$user], ]
76         }
77         EOS
78       }
79
80         it 'is expected to run idempotently' do
81           apply_manifest(pp_install)
82
83           #postgres version
84           result = shell('psql --version')
85           version = result.stdout.match(%r{\s(\d\.\d)})[1]
86
87           if version >= '8.4.0'
88             apply_manifest(pp_lang, :catch_failures => true)
89             apply_manifest(pp_lang, :catch_changes => true)
90           end
91         end
92
93         it 'is expected to GRANT USAGE ON LANGUAGE plpgsql to ROLE' do
94           result = shell('psql --version')
95           version = result.stdout.match(%r{\s(\d\.\d)})[1]
96
97           if version >= '8.4.0'
98             ## Check that the privilege was granted to the user
99             psql("-d #{db} --command=\"SELECT 1 WHERE has_language_privilege('#{user}', 'plpgsql', 'USAGE')\"", superuser) do |r|
100               expect(r.stdout).to match(/\(1 row\)/)
101               expect(r.stderr).to eq('')
102             end
103           end
104         end
105
106       let(:pp_onlyif) { pp_setup + <<-EOS.unindent
107           postgresql::server::grant { 'grant usage on BSql':
108             psql_user     => '#{superuser}',
109             privilege     => 'USAGE',
110             object_type   => 'LANGUAGE',
111             object_name   => 'bsql',
112             role          => $user,
113             db            => $db,
114             onlyif_exists => true,
115         }
116         EOS
117       }
118
119       #test onlyif_exists function
120       it 'is expected to not GRANT USAGE ON (dummy)LANGUAGE BSql to ROLE' do
121         apply_manifest(pp_install)
122
123         #postgres version
124         result = shell('psql --version')
125         version = result.stdout.match(%r{\s(\d\.\d)})[1]
126
127         if version >= '8.4.0'
128           apply_manifest(pp_onlyif, :catch_failures => true)
129           apply_manifest(pp_onlyif, :catch_changes => true)
130         end
131       end
132     end
133   end
134
135   context 'sequence' do
136     it 'should grant usage on a sequence to a user' do
137       begin
138         pp = pp_setup + <<-EOS.unindent
139
140           postgresql_psql { 'create test sequence':
141             command   => 'CREATE SEQUENCE test_seq',
142             db        => $db,
143             psql_user => $owner,
144             unless    => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq'",
145             require   => Postgresql::Server::Database[$db],
146           }
147
148           postgresql::server::grant { 'grant usage on test_seq':
149             privilege   => 'USAGE',
150             object_type => 'SEQUENCE',
151             object_name => 'test_seq',
152             db          => $db,
153             role        => $user,
154             require     => [ Postgresql_psql['create test sequence'],
155                              Postgresql::Server::Role[$user], ]
156           }
157         EOS
158
159         apply_manifest(pp_install, :catch_failures => true)
160
161         #postgres version
162         result = shell('psql --version')
163         version = result.stdout.match(%r{\s(\d\.\d)})[1]
164
165         if version >= '9.0'
166           apply_manifest(pp, :catch_failures => true)
167           apply_manifest(pp, :catch_changes => true)
168
169           ## Check that the privilege was granted to the user
170           psql("-d #{db} --command=\"SELECT 1 WHERE has_sequence_privilege('#{user}', 'test_seq', 'USAGE')\"", user) do |r|
171             expect(r.stdout).to match(/\(1 row\)/)
172             expect(r.stderr).to eq('')
173           end
174         end
175       end
176     end
177
178     it 'should grant update on a sequence to a user' do
179       begin
180         pp = pp_setup + <<-EOS.unindent
181
182           postgresql_psql { 'create test sequence':
183             command   => 'CREATE SEQUENCE test_seq',
184             db        => $db,
185             psql_user => $owner,
186             unless    => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq'",
187             require   => Postgresql::Server::Database[$db],
188           }
189
190           postgresql::server::grant { 'grant update on test_seq':
191             privilege   => 'UPDATE',
192             object_type => 'SEQUENCE',
193             object_name => 'test_seq',
194             db          => $db,
195             role        => $user,
196             require     => [ Postgresql_psql['create test sequence'],
197                              Postgresql::Server::Role[$user], ]
198           }
199         EOS
200
201         apply_manifest(pp_install, :catch_failures => true)
202
203         #postgres version
204         result = shell('psql --version')
205         version = result.stdout.match(%r{\s(\d\.\d)})[1]
206
207         if version >= '9.0'
208           apply_manifest(pp, :catch_failures => true)
209           apply_manifest(pp, :catch_changes => true)
210
211           ## Check that the privilege was granted to the user
212           psql("-d #{db} --command=\"SELECT 1 WHERE has_sequence_privilege('#{user}', 'test_seq', 'UPDATE')\"", user) do |r|
213             expect(r.stdout).to match(/\(1 row\)/)
214             expect(r.stderr).to eq('')
215           end
216         end
217       end
218     end
219   end
220
221   context 'all sequences' do
222     it 'should grant usage on all sequences to a user' do
223       begin
224         pp = pp_setup + <<-EOS.unindent
225
226           postgresql_psql { 'create test sequences':
227             command   => 'CREATE SEQUENCE test_seq2; CREATE SEQUENCE test_seq3;',
228             db        => $db,
229             psql_user => $owner,
230             unless    => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq2'",
231             require   => Postgresql::Server::Database[$db],
232           }
233
234           postgresql::server::grant { 'grant usage on all sequences':
235             privilege   => 'USAGE',
236             object_type => 'ALL SEQUENCES IN SCHEMA',
237             object_name => 'public',
238             db          => $db,
239             role        => $user,
240             require     => [ Postgresql_psql['create test sequences'],
241                              Postgresql::Server::Role[$user], ]
242           }
243         EOS
244
245         apply_manifest(pp_install, :catch_failures => true)
246
247         #postgres version
248         result = shell('psql --version')
249         version = result.stdout.match(%r{\s(\d\.\d)})[1]
250
251         if version >= '9.0'
252           apply_manifest(pp, :catch_failures => true)
253           apply_manifest(pp, :catch_changes => true)
254
255           ## Check that the privileges were granted to the user, this check is not available on version < 9.0
256           psql("-d #{db} --command=\"SELECT 1 WHERE has_sequence_privilege('#{user}', 'test_seq2', 'USAGE') AND has_sequence_privilege('#{user}', 'test_seq3', 'USAGE')\"", user) do |r|
257             expect(r.stdout).to match(/\(1 row\)/)
258             expect(r.stderr).to eq('')
259           end
260         end
261       end
262     end
263
264     it 'should grant update on all sequences to a user' do
265       begin
266         pp = pp_setup + <<-EOS.unindent
267
268           postgresql_psql { 'create test sequences':
269             command   => 'CREATE SEQUENCE test_seq2; CREATE SEQUENCE test_seq3;',
270             db        => $db,
271             psql_user => $owner,
272             unless    => "SELECT 1 FROM information_schema.sequences WHERE sequence_name = 'test_seq2'",
273             require   => Postgresql::Server::Database[$db],
274           }
275
276           postgresql::server::grant { 'grant usage on all sequences':
277             privilege   => 'UPDATE',
278             object_type => 'ALL SEQUENCES IN SCHEMA',
279             object_name => 'public',
280             db          => $db,
281             role        => $user,
282             require     => [ Postgresql_psql['create test sequences'],
283                              Postgresql::Server::Role[$user], ]
284           }
285         EOS
286
287         apply_manifest(pp_install, :catch_failures => true)
288
289         #postgres version
290         result = shell('psql --version')
291         version = result.stdout.match(%r{\s(\d\.\d)})[1]
292
293         if version >= '9.0'
294           apply_manifest(pp, :catch_failures => true)
295           apply_manifest(pp, :catch_changes => true)
296
297           ## Check that the privileges were granted to the user
298           psql("-d #{db} --command=\"SELECT 1 WHERE has_sequence_privilege('#{user}', 'test_seq2', 'UPDATE') AND has_sequence_privilege('#{user}', 'test_seq3', 'UPDATE')\"", user) do |r|
299             expect(r.stdout).to match(/\(1 row\)/)
300             expect(r.stderr).to eq('')
301           end
302         end
303       end
304     end
305   end
306 end