Debian Codegraph @

lemonldap-ng-manager/t/11-save-changed-conf-with-confirmation.t

raw
1 # Verify that a modified configuration can be saved and that all changes are
2 # detected
3
4 use warnings;
5 use Test::More;
6 use strict;
7 use JSON;
8 require 't/test-lib.pm';
9
10 my $struct = 't/jsonfiles/11-modified-with-confirmation.json';
11 my $confFiles =
12 [ "$main::tmpdir/conf/lmConf-1.json", "$main::tmpdir/conf/lmConf-2.json" ];
13
14 my ( $res, $resBody );
15 my ( $len, $body ) = substitute_io_handle($struct);
16 ok(
17 $res =
18 &client->_post( '/confs/', 'cfgNum=1', $body, 'application/json', $len ),
19 "Request succeed"
20 );
21 ok( $res->[0] == 200, "Result code is 200" );
22 ok( $resBody = from_json( $res->[2]->[0] ), "Result body contains JSON text" );
23
24 ok( $resBody->{result} == 0, "JSON response contains \"result:0\"" )
25 or print STDERR Dumper($resBody);
26 ok( $resBody->{needConfirm} == 1, "JSON response contains \"needConfirm:1\"" )
27 or print STDERR Dumper($resBody);
28 ok(
29 @{ $resBody->{details}->{__warnings__} } >= 3,
30 'JSON response contains 4 warnings'
31 ) or print STDERR Dumper($resBody);
32 count(6);
33
34 my $found = 0;
35
36 foreach ( @{ $resBody->{details}->{__warnings__} } ) {
37 $found++
38 if $_->{message} =~
39 /\b(cross-domain-authentication|retries|__badExpressionAssignment__)\b/;
40 }
41 ok( $found >= 2, "Warning with 'CDA', 'assignment' or 'retries' found" )
42 or print STDERR Dumper($resBody);
43 count(1);
44
45 ok(
46 @{ $resBody->{details}->{__needConfirmation__} } == 1,
47 'JSON response contains 1 needConfirmation'
48 ) or print STDERR Dumper($resBody);
49 ok(
50 @{ $resBody->{details}->{__changes__} } == 24,
51 'JSON response contains 24 changes'
52 ) or print STDERR Dumper($resBody);
53
54 #print STDERR Dumper($resBody);
55 ( $len, $body ) = substitute_io_handle($struct);
56 ok(
57 $res = &client->_post(
58 '/confs/', 'cfgNum=1&force=1', $body, 'application/json', $len
59 ),
60 "Request succeed"
61 );
62 ok( -f $confFiles->[1], 'File is created' );
63 count(4);
64
65 my @changes = @{&changes};
66 my @cmsg = @{ $resBody->{details}->{__changes__} };
67 my $bug;
68
69 while ( my $c = shift @{ $resBody->{details}->{__changes__} } ) {
70 my $cmp1 = @changes;
71 my $cmp2 = @cmsg;
72
73 @changes = grep { ( $_->{key} || '' ) ne ( $c->{key} || '' ) } @changes;
74 @cmsg = grep { ( $_->{key} || '' ) ne ( $c->{key} || '' ) } @cmsg;
75 if ( $c->{key} and $c->{key} eq 'applicationList' ) {
76 pass qq("$c->{key}" found);
77 count(1);
78 }
79 elsif ( $c->{key} ) {
80 ok( ( $cmp1 - @changes ) == ( $cmp2 - @cmsg ), qq("$c->{key}" found) )
81 or print STDERR 'Expect: '
82 . ( $cmp1 - @changes )
83 . ', got: '
84 . ( $cmp2 - @cmsg )
85 . "\nChanges "
86 . Dumper( \@changes )
87 . "Cmsg: "
88 . Dumper( \@cmsg );
89 count(1);
90 }
91 }
92 ok( !@changes, 'All changes detected' ) or $bug = 1;
93 count(1);
94 if ($bug) {
95 print STDERR 'Expected not found: '
96 . Dumper( \@changes )
97 . 'Changes announced and not found: '
98 . Dumper( \@cmsg );
99 }
100
101 #print STDERR Dumper(\@changes,\@cmsg);
102
103 # TODO: check result of this
104 ok( $res = &client->jsonResponse('/diff/1/2'), 'Diff called' );
105 my ( @c1, @c2 );
106 ok( ( @c1 = sort keys %{ $res->[0] } ), 'diff() detects changes in conf 1' );
107 ok( ( @c2 = sort keys %{ $res->[1] } ), 'diff() detects changes in conf 2' );
108 ok( @c1 == 12, '12 keys changed in conf 1' )
109 or print STDERR "Expect: 12 keys, get: " . join( ', ', @c1 ) . "\n";
110 ok( @c2 == 17, '17 keys changed or created in conf 2' )
111 or print STDERR "Expect: 17 keys, get: " . join( ',', @c2 ) . "\n";
112 count(5);
113
114 unlink $confFiles->[1];
115
116 #eval { rmdir$main::tmpdir; };
117 done_testing( count() );
118
119 sub changes {
120 return [ {
121 'key' => 'portal',
122 'new' => 'http://auth2.example.com/',
123 'old' => 'http://auth.example.com/'
124 },
125 {
126 'new' => 0,
127 'old' => 1,
128 'key' => 'portalDisplayLogout'
129 },
130 {
131 'key' =>
132 'applicationList, Sample applications, Application Test 1, uri',
133 'old' => 'http://test1.example.com/',
134 'new' => 'http://testex.example.com/'
135 },
136 {
137 'new' => 'Application Test 3',
138 'key' => 'applicationList, Sample applications'
139 },
140 {
141 'new' => 'Changes in cat(s)/app(s)',
142 'key' => 'applicationList',
143 },
144 {
145 'key' => 'applicationList',
146 'old' => 'Documentation',
147 'new' => 'Administration',
148 },
149 {
150 'key' => 'applicationList',
151 'old' => 'Administration',
152 'new' => 'Sample applications',
153 },
154 {
155 'key' => 'applicationList',
156 'old' => 'Sample applications',
157 'new' => 'Documentation',
158 },
159 {
160 'key' => 'userDB',
161 'new' => 'LDAP',
162 'old' => 'Demo'
163 },
164 {
165 'key' => 'passwordDB',
166 'new' => 'LDAP',
167 'old' => 'Demo'
168 },
169 {
170 'key' => 'openIdSPList',
171 'new' => '1;bad.com'
172 },
173 {
174 'new' => 'Uid',
175 'key' => 'exportedVars'
176 },
177 {
178 'key' =>
179 'locationRules, test1.example.com, (?#Logout comment)^/logout',
180 'new' => 'logout_sso',
181 'old' => undef
182 },
183 {
184 'old' => '^/logout',
185 'key' => 'locationRules, test1.example.com'
186 },
187 {
188 'key' => 'locationRules, test3.example.com, ^/logout',
189 'new' => 'logout_sso',
190 'old' => undef
191 },
192 {
193 'key' => 'locationRules, test3.example.com, default',
194 'old' => undef,
195 'new' => 'accept'
196 },
197 {
198 'key' => 'locationRules',
199 'new' => 'test3.example.com'
200 },
201 {
202 'key' => 'exportedHeaders, test3.example.com, Auth-User',
203 'old' => undef,
204 'new' => '$uid'
205 },
206 {
207 'new' => 'test3.example.com',
208 'key' => 'exportedHeaders'
209 },
210 {
211 'key' => 'locationRules, test.ex.com, default',
212 'old' => undef,
213 'new' => 'deny'
214 },
215 {
216 'key' => 'locationRules',
217 'new' => 'test.ex.com'
218 },
219 {
220 'key' => 'virtualHosts',
221 'new' => 'test3.example.com',
222 'old' => 'test2.example.com'
223 },
224 {
225 'key' => 'virtualHosts',
226 'old' => 'test2.example.com'
227 },
228 {
229 'new' => 1,
230 'key' => 'portalDisplayResetPassword',
231 'old' => 0
232 },
233 {
234 'new' => 0,
235 'key' => 'captcha_mail_enabled',
236 'old' => '1'
237 },
238 {
239 'confCompacted' => '1',
240 'removedKeys' => 'some; keys'
241 },
242 {
243 'new' => 1,
244 'key' => 'avoidAssignment',
245 'old' => '0'
246 }
247 ];
248 }