12-save-changed-conf.t 5.36 KB
Newer Older
Yadd's avatar
Yadd 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;
Yadd's avatar
Yadd committed
6
use JSON;
Yadd's avatar
Yadd committed
7
require 't/test-lib.pm';
8

Yadd's avatar
Yadd 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]; };
Yadd's avatar
Yadd committed
18
mkdir 't/sessions';
19
20

my ( $res, $resBody );
Yadd's avatar
Yadd committed
21
ok( $res = &client->_post( '/confs/', 'cfgNum=1', &body, 'application/json' ),
22
23
    "Request succeed" );
ok( $res->[0] == 200, "Result code is 200" );
Yadd's avatar
Yadd committed
24
ok( $resBody = from_json( $res->[2]->[0] ), "Result body contains JSON text" );
25
26
27
28
29
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" )
  or print STDERR Dumper($resBody);
ok( -f $confFiles->[1], 'File is created' );

my @changes = @{&changes};
Yadd's avatar
Yadd committed
30
my @cmsg    = @{ $resBody->{details}->{__changes__} };
Yadd's avatar
Yadd committed
31
my $bug;
Yadd's avatar
Yadd committed
32
while ( my $c = shift @{ $resBody->{details}->{__changes__} } ) {
Yadd's avatar
Yadd committed
33
34
35
    my $cmp1 = @changes;
    my $cmp2 = @cmsg;

36
37
    my @d1 = grep { $_->{key} eq $c->{key} } @changes;
    my @d2 = grep { $_->{key} eq $c->{key} } @cmsg;
38
39
    @changes = grep { $_->{key} ne $c->{key} } @changes;
    @cmsg    = grep { $_->{key} ne $c->{key} } @cmsg;
Yadd's avatar
tidy    
Yadd committed
40
    if ( $c->{key} eq 'applicationList' ) {
41
42
43
        pass qq("$c->{key}" found);
    }
    else {
Yadd's avatar
tidy    
Yadd committed
44
45
46
47
48
49
50
51
52
        ok( ( $cmp1 - @changes ) == ( $cmp2 - @cmsg ), qq("$c->{key}" found) )
          or print STDERR 'Expect: '
          . ( $cmp1 - @changes )
          . ', got: '
          . ( $cmp2 - @cmsg )
          . "\nExpect: "
          . Dumper( \@d1 ) . "Got: "
          . Dumper( \@d2 );
    }
Yadd's avatar
Yadd committed
53
54
55
56
57
    count(1);
}
ok( !@changes, 'All changes detected' ) or $bug = 1;

if ($bug) {
58
59
60
61
    print STDERR 'Expected not found: '
      . Dumper( \@changes )
      . 'Changes announced and not found: '
      . Dumper( \@cmsg );
62
63
64
65
}

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

66
count(6);
67

68
# TODO: check result of this
Yadd's avatar
Yadd committed
69
70
ok( $res = &client->jsonResponse('/diff/1/2'), 'Diff called' );
my ( @c1, @c2 );
Yadd's avatar
Yadd committed
71
72
ok( ( @c1 = sort keys %{ $res->[0] } ), 'diff() detects changes in conf 1' );
ok( ( @c2 = sort keys %{ $res->[1] } ), 'diff() detects changes in conf 2' );
Yadd's avatar
Yadd committed
73
ok( @c1 == 11, '11 keys changed in conf 1' )
Yadd's avatar
Yadd committed
74
  or print STDERR "Expect: 11 keys, get: " . join( ', ', @c1 ) . "\n";
Yadd's avatar
Yadd committed
75
ok( @c2 == 14, '14 keys changed or created in conf 2' )
Yadd's avatar
Yadd committed
76
  or print STDERR "Expect: 14 keys, get: " . join( ',', @c2 ) . "\n";
77

Yadd's avatar
Yadd committed
78
count(5);
79

80
unlink $confFiles->[1];
Yadd's avatar
Yadd committed
81
eval { rmdir 't/sessions'; };
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
done_testing( count() );

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'
        },
Yadd's avatar
Yadd committed
106
        {
107
            'new' => 'Changes in cat(s)/app(s)',
Yadd's avatar
Yadd committed
108
            'key' => 'applicationList',
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        },
        {
            'key' => 'applicationList',
            'old' => 'Documentation',
            'new' => 'Administration',
        },
        {
            'key' => 'applicationList',
            'old' => 'Administration',
            'new' => 'Sample applications',
        },
        {
            'key' => 'applicationList',
            'old' => 'Sample applications',
            'new' => 'Documentation',
Yadd's avatar
Yadd committed
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
        {
            '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'
        },
185
186
187
188
189
        {
            'key' => 'virtualHosts',
            'new' => 'test3.example.com',
            'old' => 'test2.example.com'
        },
190
191
192
193
194
195
        {
            'key' => 'virtualHosts',
            'old' => 'test2.example.com'
        }
    ];
}