| 1 |
# Test viewer API |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Lemonldap::NG::Manager::Conf::Diff; |
| 5 |
use Test::More; |
| 6 |
use strict; |
| 7 |
use IO::String; |
| 8 |
use JSON qw(from_json); |
| 9 |
use Storable qw(dclone); |
| 10 |
|
| 11 |
require 't/test-lib.pm'; |
| 12 |
|
| 13 |
# Load lemonldap-ng-noDiff.ini |
| 14 |
my $client; |
| 15 |
ok( |
| 16 |
$client = LLNG::Manager::Test->new( |
| 17 |
ini => { |
| 18 |
viewerAllowDiff => 0, |
| 19 |
viewerHiddenKeys => |
| 20 |
"samlIDPMetaDataNodes samlSPMetaDataNodes portalDisplayLogout", |
| 21 |
viewerAllowBrowser => '$env->{REMOTE_ADDR} ne "127.0.0.1"', |
| 22 |
} |
| 23 |
), |
| 24 |
'Client object' |
| 25 |
); |
| 26 |
|
| 27 |
sub getDiff { |
| 28 |
my ( |
| 29 |
$application_list_old, $application_list_new, |
| 30 |
$other_keys_old, $other_keys_new |
| 31 |
) = @_; |
| 32 |
return [ |
| 33 |
$client->p->Lemonldap::NG::Manager::Conf::Diff::diff( { |
| 34 |
applicationList => $application_list_old, |
| 35 |
%{ $other_keys_old // {} } |
| 36 |
}, |
| 37 |
{ |
| 38 |
applicationList => $application_list_new, |
| 39 |
%{ $other_keys_new // {} } |
| 40 |
}, |
| 41 |
) |
| 42 |
]; |
| 43 |
} |
| 44 |
my $baseconfig = { |
| 45 |
'1sample' => { |
| 46 |
'applicationtest1' => { |
| 47 |
'options' => { |
| 48 |
'name' => 'Application Test 1', |
| 49 |
'uri' => 'https://test.internal/' |
| 50 |
}, |
| 51 |
'order' => 15, |
| 52 |
'type' => 'application' |
| 53 |
}, |
| 54 |
'type' => 'category', |
| 55 |
'catname' => 'Sample applications', |
| 56 |
'order' => 1, |
| 57 |
}, |
| 58 |
}; |
| 59 |
|
| 60 |
subtest "Add a category" => sub { |
| 61 |
my $diff = getDiff( |
| 62 |
$baseconfig, |
| 63 |
{ |
| 64 |
%$baseconfig, |
| 65 |
'2new' => { |
| 66 |
'mynewapp1' => { |
| 67 |
'options' => { |
| 68 |
'name' => 'My new app 1', |
| 69 |
'uri' => 'https://test.internal/' |
| 70 |
}, |
| 71 |
'order' => 15, |
| 72 |
'type' => 'application' |
| 73 |
}, |
| 74 |
'type' => 'category', |
| 75 |
'catname' => 'New applications', |
| 76 |
'order' => 1, |
| 77 |
}, |
| 78 |
} |
| 79 |
); |
| 80 |
is_deeply( |
| 81 |
$diff, |
| 82 |
[ |
| 83 |
undef, |
| 84 |
{ |
| 85 |
'applicationList' => { |
| 86 |
'New applications' => { |
| 87 |
'My new app 1' => { |
| 88 |
'options' => { |
| 89 |
'name' => 'My new app 1', |
| 90 |
'uri' => 'https://test.internal/' |
| 91 |
}, |
| 92 |
'order' => 15, |
| 93 |
'type' => 'application' |
| 94 |
}, |
| 95 |
'catname' => 'New applications', |
| 96 |
'type' => 'category' |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
], |
| 101 |
"Expected result" |
| 102 |
); |
| 103 |
}; |
| 104 |
|
| 105 |
subtest "Delete a category" => sub { |
| 106 |
my $diff = getDiff( { |
| 107 |
%$baseconfig, |
| 108 |
'2new' => { |
| 109 |
'mynewapp1' => { |
| 110 |
'options' => { |
| 111 |
'name' => 'My new app 1', |
| 112 |
'uri' => 'https://test.internal/' |
| 113 |
}, |
| 114 |
'order' => 15, |
| 115 |
'type' => 'application' |
| 116 |
}, |
| 117 |
'type' => 'category', |
| 118 |
'catname' => 'New applications', |
| 119 |
'order' => 1, |
| 120 |
}, |
| 121 |
}, |
| 122 |
$baseconfig |
| 123 |
); |
| 124 |
is_deeply( |
| 125 |
$diff, |
| 126 |
[ { |
| 127 |
'applicationList' => { |
| 128 |
'New applications' => { |
| 129 |
'My new app 1' => { |
| 130 |
'options' => { |
| 131 |
'name' => 'My new app 1', |
| 132 |
'uri' => 'https://test.internal/' |
| 133 |
}, |
| 134 |
'order' => 15, |
| 135 |
'type' => 'application' |
| 136 |
}, |
| 137 |
'catname' => 'New applications', |
| 138 |
'type' => 'category' |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
], |
| 143 |
"Expected result" |
| 144 |
); |
| 145 |
}; |
| 146 |
|
| 147 |
subtest "Add an application" => sub { |
| 148 |
my $oldconfig = dclone($baseconfig); |
| 149 |
my $newconfig = dclone($baseconfig); |
| 150 |
$newconfig->{'1sample'}->{newapp} = { |
| 151 |
'options' => { |
| 152 |
'name' => 'A new app', |
| 153 |
'uri' => 'https://test.internal/' |
| 154 |
}, |
| 155 |
'order' => 15, |
| 156 |
'type' => 'application' |
| 157 |
}; |
| 158 |
|
| 159 |
my $diff = getDiff( $oldconfig, $newconfig ); |
| 160 |
is_deeply( |
| 161 |
$diff, |
| 162 |
[ |
| 163 |
undef, |
| 164 |
{ |
| 165 |
'applicationList' => { |
| 166 |
'Sample applications' => { |
| 167 |
'A new app' => { |
| 168 |
'options' => { |
| 169 |
'name' => 'A new app', |
| 170 |
'uri' => 'https://test.internal/' |
| 171 |
}, |
| 172 |
'order' => 15, |
| 173 |
'type' => 'application' |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
], |
| 179 |
"Expected diff" |
| 180 |
); |
| 181 |
}; |
| 182 |
|
| 183 |
subtest "Remove an application" => sub { |
| 184 |
my $oldconfig = dclone($baseconfig); |
| 185 |
my $newconfig = dclone($baseconfig); |
| 186 |
$oldconfig->{'1sample'}->{newapp} = { |
| 187 |
'options' => { |
| 188 |
'name' => 'A new app', |
| 189 |
'uri' => 'https://test.internal/' |
| 190 |
}, |
| 191 |
'order' => 15, |
| 192 |
'type' => 'application' |
| 193 |
}; |
| 194 |
|
| 195 |
my $diff = getDiff( $oldconfig, $newconfig ); |
| 196 |
is_deeply( |
| 197 |
$diff, |
| 198 |
[ { |
| 199 |
'applicationList' => { |
| 200 |
'Sample applications' => { |
| 201 |
'A new app' => { |
| 202 |
'options' => { |
| 203 |
'name' => 'A new app', |
| 204 |
'uri' => 'https://test.internal/' |
| 205 |
}, |
| 206 |
'order' => 15, |
| 207 |
'type' => 'application' |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
], |
| 213 |
"Expected diff" |
| 214 |
); |
| 215 |
}; |
| 216 |
|
| 217 |
subtest "Modify an application (option)" => sub { |
| 218 |
my $oldconfig = dclone($baseconfig); |
| 219 |
my $newconfig = dclone($baseconfig); |
| 220 |
$newconfig->{'1sample'}->{applicationtest1}->{options}->{uri} = |
| 221 |
"http://new.uri/"; |
| 222 |
|
| 223 |
my $diff = getDiff( $oldconfig, $newconfig ); |
| 224 |
is_deeply( |
| 225 |
$diff, |
| 226 |
[ { |
| 227 |
'applicationList' => { |
| 228 |
'Sample applications' => { |
| 229 |
'Application Test 1' => { |
| 230 |
'options' => { |
| 231 |
'uri' => 'https://test.internal/' |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
}, |
| 237 |
{ |
| 238 |
'applicationList' => { |
| 239 |
'Sample applications' => { |
| 240 |
'Application Test 1' => { |
| 241 |
'options' => { |
| 242 |
'uri' => 'http://new.uri/' |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
], |
| 249 |
"Expected diff" |
| 250 |
); |
| 251 |
}; |
| 252 |
|
| 253 |
subtest "Modify an application (name)" => sub { |
| 254 |
my $oldconfig = dclone($baseconfig); |
| 255 |
my $newconfig = dclone($baseconfig); |
| 256 |
$newconfig->{'1sample'}->{applicationtest1}->{options}->{name} = |
| 257 |
"New application name"; |
| 258 |
|
| 259 |
my $diff = getDiff( $oldconfig, $newconfig ); |
| 260 |
is_deeply( |
| 261 |
$diff, |
| 262 |
[ { |
| 263 |
'applicationList' => { |
| 264 |
'Sample applications' => { |
| 265 |
'New application name' => { |
| 266 |
'options' => { |
| 267 |
'name' => 'Application Test 1' |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
}, |
| 273 |
{ |
| 274 |
'applicationList' => { |
| 275 |
'Sample applications' => { |
| 276 |
'New application name' => { |
| 277 |
'options' => { |
| 278 |
'name' => 'New application name' |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
], |
| 285 |
"Expected diff" |
| 286 |
); |
| 287 |
}; |
| 288 |
|
| 289 |
subtest "Multiple changes" => sub { |
| 290 |
my $oldconfig = dclone($baseconfig); |
| 291 |
my $newconfig = dclone($baseconfig); |
| 292 |
|
| 293 |
$oldconfig->{'1sample'}->{applicationtest1}->{options}->{oldoption} = "old"; |
| 294 |
$oldconfig->{'1sample'}->{oldapp} = { |
| 295 |
'options' => { |
| 296 |
'name' => 'Application to be removed', |
| 297 |
'uri' => 'https://old.example.com' |
| 298 |
}, |
| 299 |
'order' => 15, |
| 300 |
'type' => 'application' |
| 301 |
}; |
| 302 |
|
| 303 |
$newconfig->{'1sample'}->{applicationtest1}->{options}->{newoption} = "new"; |
| 304 |
$newconfig->{'1sample'}->{newapp} = { |
| 305 |
'options' => { |
| 306 |
'name' => 'A new app', |
| 307 |
'uri' => 'https://test.internal/' |
| 308 |
}, |
| 309 |
'order' => 15, |
| 310 |
'type' => 'application' |
| 311 |
}; |
| 312 |
|
| 313 |
my $diff = getDiff( $oldconfig, $newconfig ); |
| 314 |
is_deeply( |
| 315 |
$diff, |
| 316 |
[ { |
| 317 |
'applicationList' => { |
| 318 |
'Sample applications' => { |
| 319 |
'Application Test 1' => { |
| 320 |
'options' => { |
| 321 |
'newoption' => undef, |
| 322 |
'oldoption' => 'old' |
| 323 |
}, |
| 324 |
}, |
| 325 |
'Application to be removed' => { |
| 326 |
'options' => { |
| 327 |
'name' => 'Application to be removed', |
| 328 |
'uri' => 'https://old.example.com' |
| 329 |
}, |
| 330 |
'order' => 15, |
| 331 |
'type' => 'application' |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
}, |
| 336 |
{ |
| 337 |
'applicationList' => { |
| 338 |
'Sample applications' => { |
| 339 |
'A new app' => { |
| 340 |
'options' => { |
| 341 |
'name' => 'A new app', |
| 342 |
'uri' => 'https://test.internal/' |
| 343 |
}, |
| 344 |
'order' => 15, |
| 345 |
'type' => 'application' |
| 346 |
}, |
| 347 |
'Application Test 1' => { |
| 348 |
'options' => { |
| 349 |
'newoption' => 'new', |
| 350 |
'oldoption' => undef |
| 351 |
}, |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
], |
| 357 |
"Expected diff" |
| 358 |
); |
| 359 |
}; |
| 360 |
|
| 361 |
subtest "Multiple applications with the same name" => sub { |
| 362 |
my $appList = { |
| 363 |
'1sample' => { |
| 364 |
'applicationtest1' => { |
| 365 |
'options' => { |
| 366 |
'description' => 'Application Test 1', |
| 367 |
'display' => 'auto', |
| 368 |
'logo' => 'network.png', |
| 369 |
'name' => 'Application Test 1', |
| 370 |
'tooltip' => 'New app tooltip', |
| 371 |
'uri' => 'https://test.internal/' |
| 372 |
}, |
| 373 |
'order' => 15, |
| 374 |
'type' => 'application' |
| 375 |
}, |
| 376 |
'appli1' => { |
| 377 |
'options' => { |
| 378 |
'description' => 'Application Test 1', |
| 379 |
'display' => 'auto', |
| 380 |
'logo' => 'network.png', |
| 381 |
'name' => 'Application Test 1', |
| 382 |
'tooltip' => 'New app tooltip', |
| 383 |
'uri' => 'https://test.example.com/' |
| 384 |
}, |
| 385 |
'order' => 16, |
| 386 |
'type' => 'application' |
| 387 |
}, |
| 388 |
'type' => 'category', |
| 389 |
'catname' => 'Sample applications', |
| 390 |
'order' => 1, |
| 391 |
}, |
| 392 |
}; |
| 393 |
my $diff = |
| 394 |
getDiff( $appList, $appList, { mykey => 1 }, { mykey => 2 } ); |
| 395 |
is_deeply( |
| 396 |
$diff, |
| 397 |
[ { |
| 398 |
'mykey' => 1 |
| 399 |
}, |
| 400 |
{ |
| 401 |
'mykey' => 2 |
| 402 |
} |
| 403 |
], |
| 404 |
"Diff only exposes mykey" |
| 405 |
); |
| 406 |
}; |
| 407 |
|
| 408 |
done_testing(); |
| 409 |
|