12-save-changed-conf.t 6.01 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
1 2
# Verify that a modified configuration can be saved and that all changes are
# detected
3 4 5

use Test::More;
use strict;
6
use JSON;
Xavier Guimard's avatar
Xavier Guimard committed
7
require 't/test-lib.pm';
8

Xavier Guimard's avatar
Xavier Guimard committed
9
my $struct = 't/jsonfiles/12-modified.json';
10
my $confFiles = [ 't/conf/lmConf-1.json', 't/conf/lmConf-2.json' ];
11 12 13 14 15

sub body {
    return IO::File->new( $struct, 'r' );
}

16
# Delete lmConf-2.json if exists
17
eval { unlink $confFiles->[1]; };
Xavier Guimard's avatar
Xavier Guimard committed
18
mkdir 't/sessions';
19 20

my ( $res, $resBody );
21
ok( $res = &client->_post( '/confs/', 'cfgNum=1', &body, 'application/json' ),
Christophe Maudoux's avatar
Christophe Maudoux committed
22 23
    "Request succeed"
);
24
ok( $res->[0] == 200, "Result code is 200" );
Christophe Maudoux's avatar
Christophe Maudoux committed
25 26
ok( $resBody = from_json( $res->[2]->[0] ),
    "Result body contains JSON text" );
27
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" )
Christophe Maudoux's avatar
Christophe Maudoux committed
28
    or print STDERR Dumper($resBody);
29
ok( @{ $resBody->{details}->{__warnings__} } == 2,
Christophe Maudoux's avatar
Christophe Maudoux committed
30 31
    'JSON response contains 2 warnings'
) or print STDERR Dumper($resBody);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

foreach my $i (0 .. 1) {
    ok( $resBody->{details}->{__warnings__}->[$i]->{message}
            =~ /\b(unprotected|cross-domain-authentication)\b/,
        "Warning with 'unprotect', 'CDA' or 'retries' found"
    ) or print STDERR Dumper($resBody);
}





# ok( $resBody->{details}->{__warnings__}->[0]->{message}
#         =~ /\b(unprotected|cross-domain-authentication)\b/,
#     "Warning with 'unprotect' or 'CDA' found"
# ) or print STDERR Dumper($resBody);
# ok( $resBody->{details}->{__warnings__}->[1]->{message}
#         =~ /\b(unprotected|cross-domain-authentication)\b/,
#     "Warning with 'unprotect' or 'CDA' found"
# ) or print STDERR Dumper($resBody);
52
ok( -f $confFiles->[1], 'File is created' );
53
count(3);
54
my @changes = @{&changes};
Xavier Guimard's avatar
Xavier Guimard committed
55
my @cmsg    = @{ $resBody->{details}->{__changes__} };
56
my $bug;
Christophe Maudoux's avatar
Christophe Maudoux committed
57

Xavier Guimard's avatar
Xavier Guimard committed
58
while ( my $c = shift @{ $resBody->{details}->{__changes__} } ) {
59 60 61
    my $cmp1 = @changes;
    my $cmp2 = @cmsg;

62 63
    my @d1 = grep { $_->{key} eq $c->{key} } @changes;
    my @d2 = grep { $_->{key} eq $c->{key} } @cmsg;
64 65
    @changes = grep { $_->{key} ne $c->{key} } @changes;
    @cmsg    = grep { $_->{key} ne $c->{key} } @cmsg;
Xavier Guimard's avatar
tidy  
Xavier Guimard committed
66
    if ( $c->{key} eq 'applicationList' ) {
67 68 69
        pass qq("$c->{key}" found);
    }
    else {
Xavier Guimard's avatar
tidy  
Xavier Guimard committed
70
        ok( ( $cmp1 - @changes ) == ( $cmp2 - @cmsg ), qq("$c->{key}" found) )
Christophe Maudoux's avatar
Christophe Maudoux committed
71 72 73 74 75 76 77
            or print STDERR 'Expect: '
            . ( $cmp1 - @changes )
            . ', got: '
            . ( $cmp2 - @cmsg )
            . "\nExpect: "
            . Dumper( \@d1 ) . "Got: "
            . Dumper( \@d2 );
Xavier Guimard's avatar
tidy  
Xavier Guimard committed
78
    }
79 80 81 82 83
    count(1);
}
ok( !@changes, 'All changes detected' ) or $bug = 1;

if ($bug) {
84
    print STDERR 'Expected not found: '
Christophe Maudoux's avatar
Christophe Maudoux committed
85 86 87
        . Dumper( \@changes )
        . 'Changes announced and not found: '
        . Dumper( \@cmsg );
88 89 90 91
}

#print STDERR Dumper(\@changes,\@cmsg);

92
count(6);
93

94
# TODO: check result of this
Xavier Guimard's avatar
Xavier Guimard committed
95 96
ok( $res = &client->jsonResponse('/diff/1/2'), 'Diff called' );
my ( @c1, @c2 );
Xavier Guimard's avatar
Xavier Guimard committed
97 98
ok( ( @c1 = sort keys %{ $res->[0] } ), 'diff() detects changes in conf 1' );
ok( ( @c2 = sort keys %{ $res->[1] } ), 'diff() detects changes in conf 2' );
99
ok( @c1 == 11, '11 keys changed in conf 1' )
Christophe Maudoux's avatar
Christophe Maudoux committed
100
    or print STDERR "Expect: 11 keys, get: " . join( ', ', @c1 ) . "\n";
Xavier Guimard's avatar
Xavier Guimard committed
101
ok( @c2 == 14, '14 keys changed or created in conf 2' )
Christophe Maudoux's avatar
Christophe Maudoux committed
102
    or print STDERR "Expect: 14 keys, get: " . join( ',', @c2 ) . "\n";
103

Xavier Guimard's avatar
Xavier Guimard committed
104
count(5);
105

106
unlink $confFiles->[1];
Christophe Maudoux's avatar
Christophe Maudoux committed
107

108
#eval { rmdir 't/sessions'; };
109 110
done_testing( count() );

111 112 113
# Remove sessions directory
`rm -rf t/sessions`;

114 115
sub changes {
    return [
Christophe Maudoux's avatar
Christophe Maudoux committed
116
        {   'key' => 'portal',
117 118 119
            'new' => 'http://auth2.example.com/',
            'old' => 'http://auth.example.com/'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
120
        {   'new' => 0,
121 122 123
            'old' => 1,
            'key' => 'portalDisplayLogout'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
124 125
        {   'key' =>
                'applicationList, Sample applications, Application Test 1, uri',
126 127 128
            'old' => 'http://test1.example.com/',
            'new' => 'http://testex.example.com/'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
129
        {   'new' => 'Application Test 3',
130 131
            'key' => 'applicationList, Sample applications'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
132
        {   'new' => 'Changes in cat(s)/app(s)',
133
            'key' => 'applicationList',
134
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
135
        {   'key' => 'applicationList',
136 137 138
            'old' => 'Documentation',
            'new' => 'Administration',
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
139
        {   'key' => 'applicationList',
140 141 142
            'old' => 'Administration',
            'new' => 'Sample applications',
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
143
        {   'key' => 'applicationList',
144 145
            'old' => 'Sample applications',
            'new' => 'Documentation',
146
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
147
        {   'key' => 'userDB',
148 149 150
            'new' => 'LDAP',
            'old' => 'Demo'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
151
        {   'key' => 'passwordDB',
152 153 154
            'new' => 'LDAP',
            'old' => 'Demo'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
155
        {   'key' => 'openIdSPList',
156 157
            'new' => '1;bad.com'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
158
        {   'new' => 'Uid',
159 160
            'key' => 'exportedVars'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
161 162
        {   'key' =>
                'locationRules, test1.example.com, (?#Logout comment)^/logout',
163 164 165
            'new' => 'logout_sso',
            'old' => undef
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
166
        {   'old' => '^/logout',
167 168
            'key' => 'locationRules, test1.example.com'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
169
        {   'key' => 'locationRules, test3.example.com, ^/logout',
170 171 172
            'new' => 'logout_sso',
            'old' => undef
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
173
        {   'key' => 'locationRules, test3.example.com, default',
174 175 176
            'old' => undef,
            'new' => 'accept'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
177
        {   'key' => 'locationRules',
178 179
            'new' => 'test3.example.com'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
180
        {   'key' => 'exportedHeaders, test3.example.com, Auth-User',
181 182 183
            'old' => undef,
            'new' => '$uid'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
184
        {   'new' => 'test3.example.com',
185 186
            'key' => 'exportedHeaders'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
187
        {   'key' => 'locationRules, test.ex.com, default',
188 189 190
            'old' => undef,
            'new' => 'deny'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
191
        {   'key' => 'locationRules',
192 193
            'new' => 'test.ex.com'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
194
        {   'key' => 'virtualHosts',
195 196 197
            'new' => 'test3.example.com',
            'old' => 'test2.example.com'
        },
Christophe Maudoux's avatar
Christophe Maudoux committed
198
        {   'key' => 'virtualHosts',
199 200 201 202
            'old' => 'test2.example.com'
        }
    ];
}