12a41e2bfc468966e4356cf2804e3a6ed78b27ac
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / spec / unit / defines / server / grant_role_spec.rb
1 require 'spec_helper'
2
3 describe 'postgresql::server::grant_role', :type => :define do
4   let :pre_condition do
5     "class { 'postgresql::server': }"
6   end
7
8   let :facts do
9     {:osfamily => 'Debian',
10      :operatingsystem => 'Debian',
11      :operatingsystemrelease => '6.0',
12      :kernel => 'Linux', :concat_basedir => tmpfilename('postgis'),
13      :id => 'root',
14      :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
15     }
16   end
17
18   let (:title) { 'test' }
19
20   let (:params) { {
21     :group => 'my_group',
22     :role  => 'my_role',
23   } }
24
25   context "with mandatory arguments only" do
26     it {
27       is_expected.to contain_postgresql_psql("grant_role:#{title}").with({
28         :command => "GRANT \"#{params[:group]}\" TO \"#{params[:role]}\"",
29         :unless  => "SELECT 1 WHERE EXISTS (SELECT 1 FROM pg_roles AS r_role JOIN pg_auth_members AS am ON r_role.oid = am.member JOIN pg_roles AS r_group ON r_group.oid = am.roleid WHERE r_group.rolname = '#{params[:group]}' AND r_role.rolname = '#{params[:role]}') = true",
30       }).that_requires('Class[postgresql::server]')
31     }
32   end
33
34   context "with db arguments" do
35     let (:params) { super().merge({
36       :psql_db   => 'postgres',
37       :psql_user => 'postgres',
38       :port      => '5432',
39     }) }
40
41     it {
42       is_expected.to contain_postgresql_psql("grant_role:#{title}").with({
43         :command => "GRANT \"#{params[:group]}\" TO \"#{params[:role]}\"",
44         :unless  => "SELECT 1 WHERE EXISTS (SELECT 1 FROM pg_roles AS r_role JOIN pg_auth_members AS am ON r_role.oid = am.member JOIN pg_roles AS r_group ON r_group.oid = am.roleid WHERE r_group.rolname = '#{params[:group]}' AND r_role.rolname = '#{params[:role]}') = true",
45         :db        => params[:psql_db],
46         :psql_user => params[:psql_user],
47         :port      => params[:port],
48       }).that_requires('Class[postgresql::server]')
49     }
50   end
51
52   context "with ensure => absent" do
53     let (:params) { super().merge({
54       :ensure   => 'absent',
55     }) }
56
57     it {
58       is_expected.to contain_postgresql_psql("grant_role:#{title}").with({
59         :command => "REVOKE \"#{params[:group]}\" FROM \"#{params[:role]}\"",
60         :unless  => "SELECT 1 WHERE EXISTS (SELECT 1 FROM pg_roles AS r_role JOIN pg_auth_members AS am ON r_role.oid = am.member JOIN pg_roles AS r_group ON r_group.oid = am.roleid WHERE r_group.rolname = '#{params[:group]}' AND r_role.rolname = '#{params[:role]}') != true",
61       }).that_requires('Class[postgresql::server]')
62     }
63   end
64
65   context "with user defined" do
66     let :pre_condition do
67       "class { 'postgresql::server': }
68 postgresql::server::role { '#{params[:role]}': }"
69     end
70
71     it {
72       is_expected.to contain_postgresql_psql("grant_role:#{title}").that_requires("Postgresql::Server::Role[#{params[:role]}]")
73     }
74     it {
75       is_expected.not_to contain_postgresql_psql("grant_role:#{title}").that_requires("Postgresql::Server::Role[#{params[:group]}]")
76     }
77   end
78
79   context "with group defined" do
80     let :pre_condition do
81       "class { 'postgresql::server': }
82 postgresql::server::role { '#{params[:group]}': }"
83     end
84
85     it {
86       is_expected.to contain_postgresql_psql("grant_role:#{title}").that_requires("Postgresql::Server::Role[#{params[:group]}]")
87     }
88     it {
89       is_expected.not_to contain_postgresql_psql("grant_role:#{title}").that_requires("Postgresql::Server::Role[#{params[:role]}]")
90     }
91   end
92
93   context "with connect_settings" do
94     let (:params) { super().merge({
95       :connect_settings => { 'PGHOST' => 'postgres-db-server' },
96     }) }
97
98     it {
99       is_expected.to contain_postgresql_psql("grant_role:#{title}").with_connect_settings( { 'PGHOST' => 'postgres-db-server' } )
100     }
101     it {
102       is_expected.not_to contain_postgresql_psql("grant_role:#{title}").that_requires('Class[postgresql::server]')
103     }
104   end
105 end