lemonldap-ng/lemonldap-ng-manager/t/12-save-changed-conf.t

251 lines
7.0 KiB
Perl
Raw Normal View History

2016-01-07 12:40:24 +01:00
# Verify that a modified configuration can be saved and that all changes are
# detected
use Test::More;
use strict;
2016-01-13 20:47:56 +01:00
use JSON;
2015-05-14 08:45:03 +02:00
require 't/test-lib.pm';
2019-07-02 20:03:40 +02:00
my $struct = 't/jsonfiles/12-modified.json';
my $confFiles = [ 't/conf/lmConf-1.json', 't/conf/lmConf-2.json' ];
sub body {
return IO::File->new( $struct, 'r' );
}
# Delete lmConf-2.json if exists
eval { unlink $confFiles->[1]; };
2017-01-27 10:39:58 +01:00
mkdir 't/sessions';
my ( $res, $resBody );
2016-01-01 20:55:35 +01:00
ok( $res = &client->_post( '/confs/', 'cfgNum=1', &body, 'application/json' ),
2019-02-05 23:12:17 +01:00
"Request succeed" );
2022-02-16 17:43:29 +01:00
ok( $res->[0] == 200, "Result code is 200" );
2019-02-05 23:12:17 +01:00
ok( $resBody = from_json( $res->[2]->[0] ), "Result body contains JSON text" );
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" )
2019-02-05 23:12:17 +01:00
or print STDERR Dumper($resBody);
ok(
2020-02-20 23:34:02 +01:00
$resBody->{details}->{__warnings__}
and @{ $resBody->{details}->{__warnings__} } == 2,
2018-12-01 22:40:22 +01:00
'JSON response contains 2 warnings'
) or print STDERR Dumper($resBody);
2018-12-29 18:50:30 +01:00
2019-02-05 23:12:17 +01:00
foreach my $i ( 0 .. 1 ) {
ok(
$resBody->{details}->{__warnings__}->[$i]->{message} =~
/\b(unprotected|cross-domain-authentication)\b/,
2018-12-29 18:50:30 +01:00
"Warning with 'unprotect', 'CDA' or 'retries' found"
) or print STDERR Dumper($resBody);
}
2019-02-05 23:12:17 +01:00
ok(
2020-02-20 23:34:02 +01:00
$resBody->{details}->{__changes__}
and @{ $resBody->{details}->{__changes__} } == 24,
2019-10-05 11:59:52 +02:00
'JSON response contains 24 changes'
2018-12-29 22:04:44 +01:00
) or print STDERR Dumper($resBody);
2020-02-20 23:34:02 +01:00
ok( $resBody->{details}->{__changes__}->[23]->{confCompacted} == 1,
'Conf. has been compacted' )
or print STDERR Dumper($resBody);
2019-02-05 23:12:17 +01:00
2020-02-20 23:34:02 +01:00
my @removedKeys = split /; /,
$resBody->{details}->{__changes__}->[23]->{removedKeys};
ok( @removedKeys == 60, 'All removed keys found' )
or print STDERR Dumper( \@removedKeys );
2018-12-29 22:04:44 +01:00
#print STDERR Dumper($resBody);
ok( -f $confFiles->[1], 'File is created' );
count(6);
my @changes = @{&changes};
2015-12-25 11:45:51 +01:00
my @cmsg = @{ $resBody->{details}->{__changes__} };
my $bug;
2018-12-01 22:37:10 +01:00
2015-12-25 11:45:51 +01:00
while ( my $c = shift @{ $resBody->{details}->{__changes__} } ) {
my $cmp1 = @changes;
my $cmp2 = @cmsg;
2019-09-16 17:43:27 +02:00
@changes = grep { ( $_->{key} || '' ) ne ( $c->{key} || '' ) } @changes;
@cmsg = grep { ( $_->{key} || '' ) ne ( $c->{key} || '' ) } @cmsg;
if ( $c->{key} and $c->{key} eq 'applicationList' ) {
pass qq("$c->{key}" found);
2019-09-16 17:43:27 +02:00
count(1);
}
2019-09-16 17:43:27 +02:00
elsif ( $c->{key} ) {
2018-08-30 07:40:51 +02:00
ok( ( $cmp1 - @changes ) == ( $cmp2 - @cmsg ), qq("$c->{key}" found) )
2019-02-05 23:12:17 +01:00
or print STDERR 'Expect: '
. ( $cmp1 - @changes )
. ', got: '
. ( $cmp2 - @cmsg )
2019-09-16 17:43:27 +02:00
. "\nChanges "
. Dumper( \@changes )
. "Cmsg: "
. Dumper( \@cmsg );
count(1);
2018-08-30 07:40:51 +02:00
}
}
ok( !@changes, 'All changes detected' ) or $bug = 1;
if ($bug) {
print STDERR 'Expected not found: '
2019-02-05 23:12:17 +01:00
. Dumper( \@changes )
. 'Changes announced and not found: '
. Dumper( \@cmsg );
}
count(6);
# TODO: check result of this
2016-03-20 23:35:14 +01:00
ok( $res = &client->jsonResponse('/diff/1/2'), 'Diff called' );
my ( @c1, @c2 );
2017-01-12 07:05:06 +01:00
ok( ( @c1 = sort keys %{ $res->[0] } ), 'diff() detects changes in conf 1' );
ok( ( @c2 = sort keys %{ $res->[1] } ), 'diff() detects changes in conf 2' );
2017-03-31 18:15:38 +02:00
ok( @c1 == 11, '11 keys changed in conf 1' )
2019-02-05 23:12:17 +01:00
or print STDERR "Expect: 11 keys, get: " . join( ', ', @c1 ) . "\n";
2019-10-05 11:59:52 +02:00
ok( @c2 == 15, '15 keys changed or created in conf 2' )
or print STDERR "Expect: 15 keys, get: " . join( ',', @c2 ) . "\n";
2016-03-20 23:35:14 +01:00
count(5);
2020-02-20 23:34:02 +01:00
ok( $res = &client->jsonResponse('/confs/latest'), 'Get last config metadata' );
2019-09-16 20:52:00 +02:00
ok( $res->{prev} == 1, ' Get previous configuration' );
2019-09-16 17:56:16 +02:00
count(2);
unlink $confFiles->[1];
2018-12-01 22:37:10 +01:00
2018-12-01 22:36:03 +01:00
#eval { rmdir 't/sessions'; };
done_testing( count() );
2018-12-01 22:36:03 +01:00
# Remove sessions directory
`rm -rf t/sessions`;
sub changes {
2020-02-20 23:34:02 +01:00
return [ {
2019-02-05 23:12:17 +01:00
'key' => 'portal',
'new' => 'http://auth2.example.com/',
'old' => 'http://auth.example.com/'
},
2019-02-05 23:12:17 +01:00
{
'new' => 0,
'old' => 1,
'key' => 'portalDisplayLogout'
},
2019-02-05 23:12:17 +01:00
{
'key' =>
'applicationList, Sample applications, Application Test 1, uri',
'old' => 'http://test1.example.com/',
'new' => 'http://testex.example.com/'
},
2019-02-05 23:12:17 +01:00
{
'new' => 'Application Test 3',
'key' => 'applicationList, Sample applications'
},
2019-02-05 23:12:17 +01:00
{
'new' => 'Changes in cat(s)/app(s)',
'key' => 'applicationList',
},
2019-02-05 23:12:17 +01:00
{
'key' => 'applicationList',
'old' => 'Documentation',
'new' => 'Administration',
},
2019-02-05 23:12:17 +01:00
{
'key' => 'applicationList',
'old' => 'Administration',
'new' => 'Sample applications',
},
2019-02-05 23:12:17 +01:00
{
'key' => 'applicationList',
'old' => 'Sample applications',
'new' => 'Documentation',
},
2019-02-05 23:12:17 +01:00
{
'key' => 'userDB',
'new' => 'LDAP',
'old' => 'Demo'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'passwordDB',
'new' => 'LDAP',
'old' => 'Demo'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'openIdSPList',
'new' => '1;bad.com'
},
2019-02-05 23:12:17 +01:00
{
'new' => 'Uid',
'key' => 'exportedVars'
},
2019-02-05 23:12:17 +01:00
{
'key' =>
'locationRules, test1.example.com, (?#Logout comment)^/logout',
'new' => 'logout_sso',
'old' => undef
},
2019-02-05 23:12:17 +01:00
{
'old' => '^/logout',
'key' => 'locationRules, test1.example.com'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'locationRules, test3.example.com, ^/logout',
'new' => 'logout_sso',
'old' => undef
},
2019-02-05 23:12:17 +01:00
{
'key' => 'locationRules, test3.example.com, default',
'old' => undef,
'new' => 'accept'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'locationRules',
'new' => 'test3.example.com'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'exportedHeaders, test3.example.com, Auth-User',
'old' => undef,
'new' => '$uid'
},
2019-06-07 11:25:56 +02:00
{
'key' => 'exportedHeaders, test3.example.com, cipherId',
'old' => undef,
'new' => 'encrypt($uid)'
},
{
'key' => 'exportedHeaders, test3.example.com, encodeId',
'old' => undef,
'new' => 'encode_base64($uid)'
},
2019-02-05 23:12:17 +01:00
{
'new' => 'test3.example.com',
'key' => 'exportedHeaders'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'locationRules, test.ex.com, default',
'old' => undef,
'new' => 'deny'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'locationRules',
'new' => 'test.ex.com'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'virtualHosts',
'new' => 'test3.example.com',
'old' => 'test2.example.com'
},
2019-02-05 23:12:17 +01:00
{
'key' => 'virtualHosts',
'old' => 'test2.example.com'
2019-09-01 21:41:41 +02:00
},
{
'confCompacted' => '1',
2019-09-16 17:43:27 +02:00
'removedKeys' => 'some; keys'
2019-10-05 11:59:52 +02:00
},
{
'key' => 'cookieExpiration',
'old' => undef,
'new' => '10'
},
];
}