FusqlFS/Backend/PgSQL/Roles.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 117 CC 6 SCAR 23.2
CC
6
Cov
0%
store
30.0
get
6.0
init
2.0
CRAP 10.2
1 use strict;
2 use 5.010;
3  
4 package FusqlFS::Backend::PgSQL::Roles;
5 use FusqlFS::Version;
6 our $VERSION = $FusqlFS::Version::VERSION;
7 use parent 'FusqlFS::Artifact';
8  
9 =begin testing SETUP
10  
11 #!class FusqlFS::Backend::PgSQL::Test
12  
13 my $new_role = {
14 struct => {
15 can_login => 1,
16 cat_update => 1,
17 config => undef,
18 conn_limit => 1,
19 create_db => 1,
20 create_role => 1,
21 inherit => 0,
22 superuser => 1,
23 valid_until => '2010-01-01 00:00:00+02',
24 },
25 postgres => \"roles/postgres",
26 owned => $_tobj->{owned},
27 };
28  
29 =end testing
30 =cut
31  
32 use DBI qw(:sql_types);
33 use FusqlFS::Backend::PgSQL::Role::Owned;
34  
35 sub init
36 {
37 my $self = shift;
38  
39 $self->{list_expr} = $self->expr("SELECT rolname FROM pg_catalog.pg_roles");
40 $self->{get_expr} = $self->expr("SELECT r.rolcanlogin AS can_login, r.rolcatupdate AS cat_update, r.rolconfig AS config,
41 r.rolconnlimit AS conn_limit, r.rolcreatedb AS create_db, r.rolcreaterole AS create_role, r.rolinherit AS inherit,
42 r.rolsuper AS superuser, r.rolvaliduntil AS valid_until,
43 ARRAY(SELECT b.rolname FROM pg_catalog.pg_roles AS b
44 JOIN pg_catalog.pg_auth_members AS m ON (m.member = b.oid)
45 WHERE m.roleid = r.oid) AS contains
46 FROM pg_catalog.pg_roles AS r WHERE rolname = ?");
47  
48 $self->{create_expr} = 'CREATE ROLE "%s"';
49 $self->{rename_expr} = 'ALTER ROLE "%s" RENAME TO "%s"';
50 $self->{drop_expr} = 'DROP ROLE "%s"';
51  
52 $self->{revoke_expr} = 'REVOKE "%s" FROM "%s"';
53 $self->{grant_expr} = 'GRANT "%s" TO "%s"';
54  
55 $self->{owned} = FusqlFS::Backend::PgSQL::Role::Owned->new();
56 }
57  
58 =begin testing get
59  
60 is $_tobj->get('unknown'), undef, 'Unknown role not exists';
61 is_deeply $_tobj->get('postgres'), { struct => {
62 can_login => 1,
63 cat_update => 1,
64 config => undef,
65 conn_limit => '-1',
66 create_db => 1,
67 create_role => 1,
68 inherit => 1,
69 superuser => 1,
70 valid_until => undef,
71 },
72 owned => $_tobj->{owned},
73 }, 'Known role is sane';
74  
75 =end testing
76 =cut
77 sub get
78 {
79 my $self = shift;
80 my ($name) = @_;
81  
82 my $data = $self->one_row($self->{get_expr}, $name);
83 return unless $data;
84  
85 my $result = { map { $_ => \"roles/$_" } @{delete($data->{contains})} };
86  
87 $result->{struct} = $self->dump($data);
88 $result->{owned} = $self->{owned};
89 return $result;
90 }
91  
92 =begin testing list
93  
94 cmp_deeply $_tobj->list(), supersetof('postgres'), 'Roles list is sane';
95  
96 =end testing
97 =cut
98 sub list
99 {
100 my $self = shift;
101 return $self->all_col($self->{list_expr})||[];
102 }
103  
104 =begin testing rename after store
105  
106 isnt $_tobj->rename('fusqlfs_test', 'new_fusqlfs_test'), undef, 'Role renamed';
107 is_deeply $_tobj->get('new_fusqlfs_test'), $new_role, 'Role renamed correctly';
108 is $_tobj->get('fusqlfs_test'), undef, 'Role is unaccessable under old name';
109 my $list = $_tobj->list();
110 ok grep { $_ eq 'new_fusqlfs_test' } @$list;
111 ok !grep { $_ eq 'fusqlfs_test' } @$list;
112  
113 =end testing
114 =cut
115 sub rename
116 {
117 my $self = shift;
118 my ($name, $newname) = @_;
119 $self->do($self->{rename_expr}, [$name, $newname]);
120 }
121  
122 =begin testing drop after rename
123  
124 isnt $_tobj->drop('new_fusqlfs_test'), undef, 'Role deleted';
125 is $_tobj->get('new_fusqlfs_test'), undef, 'Deleted role is absent';
126 my $list = $_tobj->list();
127 ok !grep { $_ eq 'new_fusqlfs_test' } @$list;
128  
129 =end testing
130 =cut
131 sub drop
132 {
133 my $self = shift;
134 my ($name) = @_;
135 $self->do($self->{drop_expr}, [$name]);
136 }
137  
138 =begin testing create after get list
139  
140 isnt $_tobj->create('fusqlfs_test'), undef, 'Role created';
141 is_deeply $_tobj->get('fusqlfs_test')->{struct}, {
142 can_login => 0,
143 cat_update => 0,
144 config => undef,
145 conn_limit => '-1',
146 create_db => 0,
147 create_role => 0,
148 inherit => 1,
149 superuser => 0,
150 valid_until => undef,
151 }, 'New role is sane';
152  
153 my $list = $_tobj->list();
154 ok grep { $_ eq 'fusqlfs_test' } @$list;
155  
156 =end testing
157 =cut
158 sub create
159 {
160 my $self = shift;
161 my ($name) = @_;
162 $self->do($self->{create_expr}, [$name]);
163 }
164  
165 =begin testing store after create
166  
167 isnt $_tobj->store('fusqlfs_test', $new_role), undef, 'Role saved';
168 is_deeply $_tobj->get('fusqlfs_test'), $new_role, 'Role saved correctly';
169  
170 =end testing
171 =cut
172 sub store
173 {
174 my $self = shift;
175 my ($name, $data) = @_;
176 my $struct = $self->validate($data, {
177 struct => {
178 -superuser => '',
179 -create_db => '',
180 -create_role => '',
181 -inherit => '',
182 -can_login => '',
183 -conn_limit => qr/^\d+$/,
184 -valid_until => '',
185 -password => '',
186 },
187 }, sub{
188 $_->{contains} = [ grep ref $data->{$_} eq 'SCALAR', keys %{$_[0]} ];
189 return 1;
190 }) or return;
191  
192 my $olddata = $self->one_row($self->{get_expr}, $name);
193 my ($grant, $revoke) = $self->adiff($olddata->{contains}, $struct->{contains});
194  
195 $self->do($self->{revoke_expr}, [$name, $_]) foreach @$revoke;
196 $self->do($self->{grant_expr}, [$name, $_]) foreach @$grant;
197  
198 $data = $self->load($data->{struct})||{};
199  
200 my $sth = $self->build("ALTER ROLE \"$name\" ", sub{
201 my ($a, $b) = @_;
202 if (ref $b)
203 {
204 return unless $data->{$a};
205 return "$b->[0] ? ", $data->{$a}, $b->[1];
206 }
207 else
208 {
209 return unless exists $data->{$a};
210 return ($data->{$a}? '': 'NO') . "$b ";
211 }
212 }, superuser => 'SUPERUSER' ,
213 create_db => 'CREATEDB' ,
214 create_role => 'CREATEROLE',
215 inherit => 'INHERIT' ,
216 can_login => 'LOGIN' ,
217 conn_limit => ['CONNECTION LIMIT', SQL_INTEGER],
218 valid_until => ['VALID UNTIL', SQL_TIMESTAMP] ,
219 password => ['PASSWORD', SQL_VARCHAR] );
220  
221 $sth->execute();
222 }
223  
224 1;
225