newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / unit / defines / server / grant_spec.rb
1 require 'spec_helper'
2
3 describe 'postgresql::server::grant', :type => :define do
4   let :facts do
5     {
6       :osfamily => 'Debian',
7       :operatingsystem => 'Debian',
8       :operatingsystemrelease => '6.0',
9       :kernel => 'Linux',
10       :concat_basedir => tmpfilename('contrib'),
11       :id => 'root',
12       :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
13     }
14   end
15
16   let :title do
17     'test'
18   end
19
20   context 'plain' do
21     let :params do
22       {
23         :db => 'test',
24         :role => 'test',
25       }
26     end
27
28     let :pre_condition do
29       "class {'postgresql::server':}"
30     end
31
32     it { is_expected.to contain_postgresql__server__grant('test') }
33   end
34
35   context 'sequence' do
36     let :params do
37       {
38         :db => 'test',
39         :role => 'test',
40         :privilege => 'usage',
41         :object_type => 'sequence',
42       }
43     end
44
45     let :pre_condition do
46       "class {'postgresql::server':}"
47     end
48
49     it { is_expected.to contain_postgresql__server__grant('test') }
50     it { is_expected.to contain_postgresql_psql('grant:test').with(
51       {
52         'command' => /GRANT USAGE ON SEQUENCE "test" TO\s* "test"/m,
53         'unless'  => /SELECT 1 WHERE has_sequence_privilege\('test',\s* 'test', 'USAGE'\)/m,
54       }
55     ) }
56   end
57
58   context 'all sequences' do
59     let :params do
60       {
61         :db => 'test',
62         :role => 'test',
63         :privilege => 'usage',
64         :object_type => 'all sequences in schema',
65         :object_name => 'public',
66       }
67     end
68
69     let :pre_condition do
70       "class {'postgresql::server':}"
71     end
72
73     it { is_expected.to contain_postgresql__server__grant('test') }
74     it { is_expected.to contain_postgresql_psql('grant:test').with(
75       {
76         'command' => /GRANT USAGE ON ALL SEQUENCES IN SCHEMA "public" TO\s* "test"/m,
77         'unless'  => /SELECT 1 FROM \(\s*SELECT sequence_name\s* FROM information_schema\.sequences\s* WHERE sequence_schema='public'\s* EXCEPT DISTINCT\s* SELECT object_name as sequence_name\s* FROM .* WHERE .*grantee='test'\s* AND object_schema='public'\s* AND privilege_type='USAGE'\s*\) P\s* HAVING count\(P\.sequence_name\) = 0/m,
78       }
79     ) }
80   end
81
82   context "with specific db connection settings - default port" do
83     let :params do
84       {
85         :db => 'test',
86         :role => 'test',
87         :connect_settings => { 'PGHOST'    => 'postgres-db-server',
88                                'DBVERSION' => '9.1', },
89       }
90     end
91
92     let :pre_condition do
93       "class {'postgresql::server':}"
94     end
95
96     it { is_expected.to contain_postgresql__server__grant('test') }
97     it { is_expected.to contain_postgresql_psql("grant:test").with_connect_settings( { 'PGHOST'    => 'postgres-db-server','DBVERSION' => '9.1' } ).with_port( 5432 ) }
98   end
99
100   context "with specific db connection settings - including port" do
101     let :params do
102       {
103         :db => 'test',
104         :role => 'test',
105         :connect_settings => { 'PGHOST'    => 'postgres-db-server',
106                                'DBVERSION' => '9.1',
107                                'PGPORT'    => '1234', },
108       }
109     end
110
111     let :pre_condition do
112       "class {'postgresql::server':}"
113     end
114
115     it { is_expected.to contain_postgresql__server__grant('test') }
116     it { is_expected.to contain_postgresql_psql("grant:test").with_connect_settings( { 'PGHOST'    => 'postgres-db-server','DBVERSION' => '9.1','PGPORT'    => '1234' } ) }
117   end
118
119   context "with specific db connection settings - port overriden by explicit parameter" do
120     let :params do
121       {
122         :db => 'test',
123         :role => 'test',
124         :connect_settings => { 'PGHOST'    => 'postgres-db-server',
125                                'DBVERSION' => '9.1',
126              'PGPORT'    => '1234', },
127         :port => 5678,
128       }
129     end
130
131     let :pre_condition do
132       "class {'postgresql::server':}"
133     end
134
135     it { is_expected.to contain_postgresql__server__grant('test') }
136     it { is_expected.to contain_postgresql_psql("grant:test").with_connect_settings( { 'PGHOST'    => 'postgres-db-server','DBVERSION' => '9.1','PGPORT'    => '1234' } ).with_port( '5678' ) }
137   end
138
139   context 'invalid objectype' do
140     let :params do
141       {
142         :db => 'test',
143         :role => 'test',
144         :privilege => 'usage',
145         :object_type => 'invalid',
146       }
147     end
148
149     let :pre_condition do
150       "class {'postgresql::server':}"
151     end
152
153     it { is_expected.to compile.and_raise_error(/"INVALID" does not match/) }
154   end
155 end