Debian Codegraph @

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