11-save-changed-conf-with-confirmation.t 6.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Verify that a modified configuration can be saved and that all changes are
# detected

use Test::More;
use strict;
use JSON;
require 't/test-lib.pm';

my $struct = 't/jsonfiles/13-modified-with-confirmation.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]; };
mkdir 't/sessions';

my ( $res, $resBody );
ok( $res = &client->_post( '/confs/', 'cfgNum=1', &body, 'application/json' ),
    "Request succeed"
);
ok( $res->[0] == 200, "Result code is 200" );
ok( $resBody = from_json( $res->[2]->[0] ),
    "Result body contains JSON text" );

ok( $resBody->{result} == 0, "JSON response contains \"result:0\"" )
    or print STDERR Dumper($resBody);

ok( $resBody->{needConfirm} == 1, "JSON response contains \"needConfirm:1\"" )
    or print STDERR Dumper($resBody);

ok( @{ $resBody->{details}->{__warnings__} } == 3,
    'JSON response contains 3 warnings'
) or print STDERR Dumper($resBody);

foreach my $i (0 .. 2) {
    ok( $resBody->{details}->{__warnings__}->[$i]->{message}
            =~ /\b(unprotected|cross-domain-authentication|retries)\b/,
        "Warning with 'unprotect', 'CDA' or 'retries' found"
    ) or print STDERR Dumper($resBody);
}
44
count(4);
45
46
47
48
49
50
51
52
53
ok( @{ $resBody->{details}->{__needConfirmation__} } == 1,
    'JSON response contains 1 needConfirmation'
) or print STDERR Dumper($resBody);

ok( $resBody->{details}->{__needConfirmation__}->[0]->{message}
        =~ /\bplugin is enabled without CSRF Token or Captcha required\b/,
    "Warning with confirmation needed found"
) or print STDERR Dumper($resBody);

54

55
ok( @{ $resBody->{details}->{__changes__} } == 22,
56
57
    'JSON response contains 24 changes'
) or print STDERR Dumper($resBody);
58
#print STDERR Dumper($resBody);
59
60
61
62
63
ok( $res = &client->_post( '/confs/', 'cfgNum=1&force=1', &body, 'application/json' ),
    "Request succeed"
);

ok( -f $confFiles->[1], 'File is created' );
64
count(5);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
my @changes = @{&changes};
my @cmsg    = @{ $resBody->{details}->{__changes__} };
my $bug;

while ( my $c = shift @{ $resBody->{details}->{__changes__} } ) {
    my $cmp1 = @changes;
    my $cmp2 = @cmsg;

    my @d1 = grep { $_->{key} eq $c->{key} } @changes;
    my @d2 = grep { $_->{key} eq $c->{key} } @cmsg;
    @changes = grep { $_->{key} ne $c->{key} } @changes;
    @cmsg    = grep { $_->{key} ne $c->{key} } @cmsg;
    if ( $c->{key} eq 'applicationList' ) {
        pass qq("$c->{key}" found);
    }
    else {
        ok( ( $cmp1 - @changes ) == ( $cmp2 - @cmsg ), qq("$c->{key}" found) )
            or print STDERR 'Expect: '
            . ( $cmp1 - @changes )
            . ', got: '
            . ( $cmp2 - @cmsg )
            . "\nExpect: "
            . Dumper( \@d1 ) . "Got: "
            . Dumper( \@d2 );
    }
    count(1);
}
ok( !@changes, 'All changes detected' ) or $bug = 1;

if ($bug) {
    print STDERR 'Expected not found: '
        . Dumper( \@changes )
        . 'Changes announced and not found: '
        . Dumper( \@cmsg );
}

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

count(6);

# TODO: check result of this
ok( $res = &client->jsonResponse('/diff/1/2'), 'Diff called' );
my ( @c1, @c2 );
ok( ( @c1 = sort keys %{ $res->[0] } ), 'diff() detects changes in conf 1' );
ok( ( @c2 = sort keys %{ $res->[1] } ), 'diff() detects changes in conf 2' );
ok( @c1 == 12, '11 keys changed in conf 1' )
    or print STDERR "Expect: 12 keys, get: " . join( ', ', @c1 ) . "\n";
ok( @c2 == 16, '14 keys changed or created in conf 2' )
    or print STDERR "Expect: 16 keys, get: " . join( ',', @c2 ) . "\n";

count(5);

unlink $confFiles->[1];

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

# Remove sessions directory
`rm -rf t/sessions`;

sub changes {
    return [
        {   'key' => 'portal',
            'new' => 'http://auth2.example.com/',
            'old' => 'http://auth.example.com/'
        },
        {   'new' => 0,
            'old' => 1,
            'key' => 'portalDisplayLogout'
        },
        {   'key' =>
                'applicationList, Sample applications, Application Test 1, uri',
            'old' => 'http://test1.example.com/',
            'new' => 'http://testex.example.com/'
        },
        {   'new' => 'Application Test 3',
            'key' => 'applicationList, Sample applications'
        },
        {   'new' => 'Changes in cat(s)/app(s)',
            'key' => 'applicationList',
        },
        {   'key' => 'applicationList',
            'old' => 'Documentation',
            'new' => 'Administration',
        },
        {   'key' => 'applicationList',
            'old' => 'Administration',
            'new' => 'Sample applications',
        },
        {   'key' => 'applicationList',
            'old' => 'Sample applications',
            'new' => 'Documentation',
        },
        {   'key' => 'userDB',
            'new' => 'LDAP',
            'old' => 'Demo'
        },
        {   'key' => 'passwordDB',
            'new' => 'LDAP',
            'old' => 'Demo'
        },
        {   'key' => 'openIdSPList',
            'new' => '1;bad.com'
        },
        {   'new' => 'Uid',
            'key' => 'exportedVars'
        },
        {   'key' =>
                'locationRules, test1.example.com, (?#Logout comment)^/logout',
            'new' => 'logout_sso',
            'old' => undef
        },
        {   'old' => '^/logout',
            'key' => 'locationRules, test1.example.com'
        },
        {   'key' => 'locationRules, test3.example.com, ^/logout',
            'new' => 'logout_sso',
            'old' => undef
        },
        {   'key' => 'locationRules, test3.example.com, default',
            'old' => undef,
            'new' => 'accept'
        },
        {   'key' => 'locationRules',
            'new' => 'test3.example.com'
        },
        {   'key' => 'exportedHeaders, test3.example.com, Auth-User',
            'old' => undef,
            'new' => '$uid'
        },
        {   'new' => 'test3.example.com',
            'key' => 'exportedHeaders'
        },
        {   'key' => 'locationRules, test.ex.com, default',
            'old' => undef,
            'new' => 'deny'
        },
        {   'key' => 'locationRules',
            'new' => 'test.ex.com'
        },
        {   'key' => 'virtualHosts',
            'new' => 'test3.example.com',
            'old' => 'test2.example.com'
        },
        {   'key' => 'virtualHosts',
            'old' => 'test2.example.com'
        },
        {
            'new' => 1,
            'key' => 'portalDisplayResetPassword',
            'old' => 0
        },
        {
            'new' => 0,
            'key' => 'captcha_mail_enabled',
            'old' => '1'
        } 
    ];
}