| 1 |
# Verify that bas changes are detected |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use Test::More; |
| 5 |
use strict; |
| 6 |
use JSON; |
| 7 |
require 't/test-lib.pm'; |
| 8 |
|
| 9 |
my $struct = 't/jsonfiles/01-base-tree.json'; |
| 10 |
|
| 11 |
sub conf_with_key_nodes { |
| 12 |
my ($keynodes) = @_; |
| 13 |
return get_conf_body_from_fixture( |
| 14 |
't/jsonfiles/01-base-tree.json', |
| 15 |
sub { |
| 16 |
my $data = shift; |
| 17 |
splice( @$data, -1, 0, $keynodes ); |
| 18 |
} |
| 19 |
); |
| 20 |
} |
| 21 |
|
| 22 |
subtest "Add key to base conf" => sub { |
| 23 |
my ( $len, $body ) = conf_with_key_nodes( { |
| 24 |
"help" => "keys.html", |
| 25 |
"id" => "keyNodes", |
| 26 |
"nodes" => [ { |
| 27 |
"id" => "keyNodes/abc", |
| 28 |
"nodes" => [ { |
| 29 |
"data" => [ { |
| 30 |
"data" => "pv", |
| 31 |
"get" => "keyNodes/abc/keyPrivate", |
| 32 |
"id" => "keyNodes/abc/keyPrivate", |
| 33 |
"title" => "keyPrivate" |
| 34 |
}, |
| 35 |
{ |
| 36 |
"data" => "pass", |
| 37 |
"get" => "keyNodes/abc/keyPrivatePwd", |
| 38 |
"id" => "keyNodes/abc/keyPrivatePwd", |
| 39 |
"title" => "keyPrivatePwd" |
| 40 |
}, |
| 41 |
{ |
| 42 |
"data" => "pub", |
| 43 |
"get" => "keyNodes/abc/keyPublic", |
| 44 |
"id" => "keyNodes/abc/keyPublic", |
| 45 |
"title" => "keyPublic" |
| 46 |
} |
| 47 |
], |
| 48 |
"get" => [ |
| 49 |
"keyNodes/abc/keyPrivate", |
| 50 |
"keyNodes/abc/keyPrivatePwd", |
| 51 |
"keyNodes/abc/keyPublic" |
| 52 |
], |
| 53 |
"id" => "keyNodes/abc/KeyMaterial", |
| 54 |
"title" => "KeyMaterial", |
| 55 |
"type" => "RSACertKey" |
| 56 |
}, |
| 57 |
{ |
| 58 |
"help" => "keys.html#options", |
| 59 |
"id" => "keyOptions", |
| 60 |
"nodes" => [ { |
| 61 |
"data" => "aaazz", |
| 62 |
"get" => "keyNodes/abc/keyId", |
| 63 |
"id" => "keyNodes/abc/keyId", |
| 64 |
"title" => "keyId" |
| 65 |
}, |
| 66 |
{ |
| 67 |
"data" => "mycomment", |
| 68 |
"get" => "keyNodes/abc/keyComment", |
| 69 |
"id" => "keyNodes/abc/keyComment", |
| 70 |
"title" => "keyComment", |
| 71 |
} |
| 72 |
], |
| 73 |
"title" => "keyOptions", |
| 74 |
"type" => "simpleInputContainer" |
| 75 |
} |
| 76 |
], |
| 77 |
"template" => "keyNode", |
| 78 |
"title" => "abc", |
| 79 |
"type" => "keyNode" |
| 80 |
} |
| 81 |
], |
| 82 |
"template" => "key", |
| 83 |
"title" => "keyNodes", |
| 84 |
"type" => "keyNodeContainer" |
| 85 |
} |
| 86 |
); |
| 87 |
|
| 88 |
my ( $res, $resBody ); |
| 89 |
ok( |
| 90 |
$res = &client->_post( |
| 91 |
'/confs/', 'cfgNum=1', $body, 'application/json', $len |
| 92 |
), |
| 93 |
"Request succeed" |
| 94 |
); |
| 95 |
ok( $res->[0] == 200, "Result code is 200" ); |
| 96 |
ok( |
| 97 |
$resBody = from_json( $res->[2]->[0] ), |
| 98 |
"Result body contains JSON text" |
| 99 |
); |
| 100 |
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" ) |
| 101 |
or print STDERR Dumper($res); |
| 102 |
|
| 103 |
ok( $res = &client->_get( '/confs/latest', 'full=1' ), 'Get saved conf' ); |
| 104 |
ok( |
| 105 |
$resBody = from_json( $res->[2]->[0] ), |
| 106 |
"Result body contains JSON text" |
| 107 |
); |
| 108 |
is( $resBody->{cfgNum}, 2, "New config was saved" ); |
| 109 |
|
| 110 |
is_deeply( |
| 111 |
$resBody->{keys}, |
| 112 |
{ |
| 113 |
'abc' => { |
| 114 |
'keyPrivate' => 'pv', |
| 115 |
'keyPrivatePwd' => 'pass', |
| 116 |
'keyPublic' => 'pub', |
| 117 |
'keyId' => 'aaazz', |
| 118 |
'keyComment' => 'mycomment', |
| 119 |
} |
| 120 |
} |
| 121 |
); |
| 122 |
}; |
| 123 |
|
| 124 |
subtest "Posting the same config does not trigger a save" => sub { |
| 125 |
my ( $len, $body ) = conf_with_key_nodes( { |
| 126 |
"help" => "keys.html", |
| 127 |
"id" => "keyNodes", |
| 128 |
"nodes" => [ { |
| 129 |
"id" => "keyNodes/abc", |
| 130 |
"nodes" => [ { |
| 131 |
"data" => [ { |
| 132 |
"data" => "pv", |
| 133 |
"get" => "keyNodes/abc/keyPrivate", |
| 134 |
"id" => "keyNodes/abc/keyPrivate", |
| 135 |
"title" => "keyPrivate" |
| 136 |
}, |
| 137 |
{ |
| 138 |
"data" => "pass", |
| 139 |
"get" => "keyNodes/abc/keyPrivatePwd", |
| 140 |
"id" => "keyNodes/abc/keyPrivatePwd", |
| 141 |
"title" => "keyPrivatePwd" |
| 142 |
}, |
| 143 |
{ |
| 144 |
"data" => "pub", |
| 145 |
"get" => "keyNodes/abc/keyPublic", |
| 146 |
"id" => "keyNodes/abc/keyPublic", |
| 147 |
"title" => "keyPublic" |
| 148 |
} |
| 149 |
], |
| 150 |
"get" => [ |
| 151 |
"keyNodes/abc/keyPrivate", |
| 152 |
"keyNodes/abc/keyPrivatePwd", |
| 153 |
"keyNodes/abc/keyPublic" |
| 154 |
], |
| 155 |
"id" => "keyNodes/abc/KeyMaterial", |
| 156 |
"title" => "KeyMaterial", |
| 157 |
"type" => "RSACertKey" |
| 158 |
}, |
| 159 |
{ |
| 160 |
"help" => "keys.html#options", |
| 161 |
"id" => "keyOptions", |
| 162 |
"nodes" => [ { |
| 163 |
"data" => "aaazz", |
| 164 |
"get" => "keyNodes/abc/keyId", |
| 165 |
"id" => "keyNodes/abc/keyId", |
| 166 |
"title" => "keyId" |
| 167 |
}, |
| 168 |
{ |
| 169 |
"data" => 'mycomment', |
| 170 |
"get" => "keyNodes/abc/keyComment", |
| 171 |
"id" => "keyNodes/abc/keyComment", |
| 172 |
"title" => "keyComment", |
| 173 |
} |
| 174 |
], |
| 175 |
"title" => "keyOptions", |
| 176 |
"type" => "simpleInputContainer" |
| 177 |
} |
| 178 |
], |
| 179 |
"template" => "keyNode", |
| 180 |
"title" => "abc", |
| 181 |
"type" => "keyNode" |
| 182 |
} |
| 183 |
], |
| 184 |
"template" => "key", |
| 185 |
"title" => "keyNodes", |
| 186 |
"type" => "keyNodeContainer" |
| 187 |
} |
| 188 |
); |
| 189 |
|
| 190 |
my ( $res, $resBody ); |
| 191 |
ok( |
| 192 |
$res = &client->_post( |
| 193 |
'/confs/', 'cfgNum=2', $body, 'application/json', $len |
| 194 |
), |
| 195 |
"Request succeed" |
| 196 |
); |
| 197 |
ok( $res->[0] == 200, "Result code is 200" ); |
| 198 |
ok( |
| 199 |
$resBody = from_json( $res->[2]->[0] ), |
| 200 |
"Result body contains JSON text" |
| 201 |
); |
| 202 |
is( $resBody->{result}, 0, "No save was done" ); |
| 203 |
is( $resBody->{message}, '__confNotChanged__', "Correct message" ); |
| 204 |
|
| 205 |
ok( $res = &client->_get( '/confs/latest', 'full=1' ), 'Get saved conf' ); |
| 206 |
ok( |
| 207 |
$resBody = from_json( $res->[2]->[0] ), |
| 208 |
"Result body contains JSON text" |
| 209 |
); |
| 210 |
is( $resBody->{cfgNum}, 2, "New config was not saved" ); |
| 211 |
|
| 212 |
is_deeply( |
| 213 |
$resBody->{keys}, |
| 214 |
{ |
| 215 |
'abc' => { |
| 216 |
'keyPrivate' => 'pv', |
| 217 |
'keyPrivatePwd' => 'pass', |
| 218 |
'keyPublic' => 'pub', |
| 219 |
'keyId' => 'aaazz', |
| 220 |
'keyComment' => 'mycomment', |
| 221 |
} |
| 222 |
} |
| 223 |
); |
| 224 |
}; |
| 225 |
|
| 226 |
subtest "Add one more key" => sub { |
| 227 |
my ( $len, $body ) = conf_with_key_nodes( { |
| 228 |
"help" => "keys.html", |
| 229 |
"id" => "keyNodes", |
| 230 |
"nodes" => [ { |
| 231 |
"_nodes" => [ { |
| 232 |
"get" => [ |
| 233 |
"keyNodes/abc/keyPrivate", |
| 234 |
"keyNodes/abc/keyPrivatePwd", |
| 235 |
"keyNodes/abc/keyPublic" |
| 236 |
], |
| 237 |
"id" => "keyNodes/abc/KeyMaterial", |
| 238 |
"title" => "KeyMaterial", |
| 239 |
"type" => "RSACertKey" |
| 240 |
}, |
| 241 |
{ |
| 242 |
"_nodes" => [ { |
| 243 |
"get" => "keyNodes/abc/keyId", |
| 244 |
"id" => "keyNodes/abc/keyId", |
| 245 |
"title" => "keyId" |
| 246 |
}, |
| 247 |
{ |
| 248 |
"get" => "keyNodes/abc/keyComment", |
| 249 |
"id" => "keyNodes/abc/keyComment", |
| 250 |
"title" => "keyComment", |
| 251 |
} |
| 252 |
], |
| 253 |
"help" => "keys.html#options", |
| 254 |
"id" => "keyOptions", |
| 255 |
"title" => "keyOptions", |
| 256 |
"type" => "simpleInputContainer" |
| 257 |
} |
| 258 |
], |
| 259 |
"id" => "keyNodes/abc", |
| 260 |
"template" => "keyNode", |
| 261 |
"title" => "abc", |
| 262 |
"type" => "keyNode" |
| 263 |
}, |
| 264 |
{ |
| 265 |
"id" => "keyNodes/new__key3", |
| 266 |
"nodes" => [ { |
| 267 |
"data" => [ { |
| 268 |
"data" => "pv2", |
| 269 |
"get" => "keyNodes/new__key3/keyPrivate", |
| 270 |
"id" => "keyNodes/new__key3/keyPrivate", |
| 271 |
"title" => "keyPrivate" |
| 272 |
}, |
| 273 |
{ |
| 274 |
"get" => "keyNodes/new__key3/keyPrivatePwd", |
| 275 |
"id" => "keyNodes/new__key3/keyPrivatePwd", |
| 276 |
"title" => "keyPrivatePwd" |
| 277 |
}, |
| 278 |
{ |
| 279 |
"data" => "pub2", |
| 280 |
"get" => "keyNodes/new__key3/keyPublic", |
| 281 |
"id" => "keyNodes/new__key3/keyPublic", |
| 282 |
"title" => "keyPublic" |
| 283 |
} |
| 284 |
], |
| 285 |
"get" => [ |
| 286 |
"keyNodes/new__key3/keyPrivate", |
| 287 |
"keyNodes/new__key3/keyPrivatePwd", |
| 288 |
"keyNodes/new__key3/keyPublic" |
| 289 |
], |
| 290 |
"id" => "keyNodes/new__key3/KeyMaterial", |
| 291 |
"title" => "KeyMaterial", |
| 292 |
"type" => "RSACertKey" |
| 293 |
}, |
| 294 |
{ |
| 295 |
"help" => "keys.html#options", |
| 296 |
"id" => "keyOptions", |
| 297 |
"nodes" => [ { |
| 298 |
"data" => "k3", |
| 299 |
"get" => "keyNodes/new__key3/keyId", |
| 300 |
"id" => "keyNodes/new__key3/keyId", |
| 301 |
"title" => "keyId" |
| 302 |
}, |
| 303 |
{ |
| 304 |
"get" => "keyNodes/new__key3/keyComment", |
| 305 |
"id" => "keyNodes/new__key3/keyComment", |
| 306 |
"title" => "keyComment", |
| 307 |
} |
| 308 |
], |
| 309 |
"title" => "keyOptions", |
| 310 |
"type" => "simpleInputContainer" |
| 311 |
} |
| 312 |
], |
| 313 |
"title" => "key3", |
| 314 |
"type" => "keyNode" |
| 315 |
} |
| 316 |
], |
| 317 |
"template" => "key", |
| 318 |
"title" => "keyNodes", |
| 319 |
"type" => "keyNodeContainer" |
| 320 |
}, |
| 321 |
); |
| 322 |
|
| 323 |
my ( $res, $resBody ); |
| 324 |
ok( |
| 325 |
$res = &client->_post( |
| 326 |
'/confs/', 'cfgNum=2', $body, 'application/json', $len |
| 327 |
), |
| 328 |
"Request succeed" |
| 329 |
); |
| 330 |
ok( $res->[0] == 200, "Result code is 200" ); |
| 331 |
ok( |
| 332 |
$resBody = from_json( $res->[2]->[0] ), |
| 333 |
"Result body contains JSON text" |
| 334 |
); |
| 335 |
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" ) |
| 336 |
or print STDERR Dumper($res); |
| 337 |
|
| 338 |
ok( $res = &client->_get( '/confs/latest', 'full=1' ), 'Get saved conf' ); |
| 339 |
ok( |
| 340 |
$resBody = from_json( $res->[2]->[0] ), |
| 341 |
"Result body contains JSON text" |
| 342 |
); |
| 343 |
is( $resBody->{cfgNum}, 3, "New config was saved" ); |
| 344 |
|
| 345 |
is_deeply( |
| 346 |
$resBody->{keys}, |
| 347 |
{ |
| 348 |
'abc' => { |
| 349 |
'keyPrivate' => 'pv', |
| 350 |
'keyPrivatePwd' => 'pass', |
| 351 |
'keyPublic' => 'pub', |
| 352 |
'keyId' => 'aaazz', |
| 353 |
'keyComment' => 'mycomment', |
| 354 |
}, |
| 355 |
'key3' => { |
| 356 |
'keyId' => 'k3', |
| 357 |
'keyPrivate' => 'pv2', |
| 358 |
'keyPublic' => 'pub2' |
| 359 |
} |
| 360 |
} |
| 361 |
); |
| 362 |
}; |
| 363 |
|
| 364 |
subtest "Test REST server" => sub { |
| 365 |
my ( $res, $resBody ); |
| 366 |
ok( $res = &client->_get('/confs/latest/keyNodes/abc/keyPrivatePwd'), |
| 367 |
"Request succeed" ); |
| 368 |
ok( |
| 369 |
$resBody = from_json( $res->[2]->[0] ), |
| 370 |
"Result body contains JSON text" |
| 371 |
); |
| 372 |
is( $resBody->{value}, "pass" ); |
| 373 |
|
| 374 |
ok( $res = &client->_get('/confs/latest/keyNodes/key3/keyId'), |
| 375 |
"Request succeed" ); |
| 376 |
ok( |
| 377 |
$resBody = from_json( $res->[2]->[0] ), |
| 378 |
"Result body contains JSON text" |
| 379 |
); |
| 380 |
is( $resBody->{value}, "k3" ); |
| 381 |
}; |
| 382 |
|
| 383 |
subtest "Delete one key" => sub { |
| 384 |
my ( $len, $body ) = conf_with_key_nodes( { |
| 385 |
"help" => "keys.html", |
| 386 |
"id" => "keyNodes", |
| 387 |
"nodes" => [ { |
| 388 |
"_nodes" => [ { |
| 389 |
"get" => [ |
| 390 |
"keyNodes/abc/keyPrivate", |
| 391 |
"keyNodes/abc/keyPrivatePwd", |
| 392 |
"keyNodes/abc/keyPublic" |
| 393 |
], |
| 394 |
"id" => "keyNodes/abc/KeyMaterial", |
| 395 |
"title" => "KeyMaterial", |
| 396 |
"type" => "RSACertKey" |
| 397 |
}, |
| 398 |
{ |
| 399 |
"_nodes" => [ { |
| 400 |
"get" => "keyNodes/abc/keyId", |
| 401 |
"id" => "keyNodes/abc/keyId", |
| 402 |
"title" => "keyId" |
| 403 |
}, |
| 404 |
{ |
| 405 |
"get" => "keyNodes/abc/keyComment", |
| 406 |
"id" => "keyNodes/abc/keyComment", |
| 407 |
"title" => "keyComment", |
| 408 |
} |
| 409 |
], |
| 410 |
"help" => "keys.html#options", |
| 411 |
"id" => "keyOptions", |
| 412 |
"title" => "keyOptions", |
| 413 |
"type" => "simpleInputContainer" |
| 414 |
} |
| 415 |
], |
| 416 |
"id" => "keyNodes/abc", |
| 417 |
"template" => "keyNode", |
| 418 |
"title" => "abc", |
| 419 |
"type" => "keyNode" |
| 420 |
}, |
| 421 |
], |
| 422 |
"template" => "key", |
| 423 |
"title" => "keyNodes", |
| 424 |
"type" => "keyNodeContainer" |
| 425 |
}, |
| 426 |
); |
| 427 |
|
| 428 |
my ( $res, $resBody ); |
| 429 |
ok( |
| 430 |
$res = &client->_post( |
| 431 |
'/confs/', 'cfgNum=3', $body, 'application/json', $len |
| 432 |
), |
| 433 |
"Request succeed" |
| 434 |
); |
| 435 |
ok( $res->[0] == 200, "Result code is 200" ); |
| 436 |
ok( |
| 437 |
$resBody = from_json( $res->[2]->[0] ), |
| 438 |
"Result body contains JSON text" |
| 439 |
); |
| 440 |
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" ) |
| 441 |
or print STDERR Dumper($res); |
| 442 |
|
| 443 |
ok( $res = &client->_get( '/confs/latest', 'full=1' ), 'Get saved conf' ); |
| 444 |
ok( |
| 445 |
$resBody = from_json( $res->[2]->[0] ), |
| 446 |
"Result body contains JSON text" |
| 447 |
); |
| 448 |
is( $resBody->{cfgNum}, 4, "New config was saved" ); |
| 449 |
|
| 450 |
is_deeply( |
| 451 |
$resBody->{keys}, |
| 452 |
{ |
| 453 |
'abc' => { |
| 454 |
'keyPrivate' => 'pv', |
| 455 |
'keyPrivatePwd' => 'pass', |
| 456 |
'keyPublic' => 'pub', |
| 457 |
'keyId' => 'aaazz', |
| 458 |
'keyComment' => 'mycomment', |
| 459 |
}, |
| 460 |
} |
| 461 |
); |
| 462 |
}; |
| 463 |
|
| 464 |
subtest "Empty keyNodes deletes all keys" => sub { |
| 465 |
my ( $len, $body ) = conf_with_key_nodes( { |
| 466 |
"help" => "keys.html", |
| 467 |
"id" => "keyNodes", |
| 468 |
"nodes" => [], |
| 469 |
"template" => "key", |
| 470 |
"title" => "keyNodes", |
| 471 |
"type" => "keyNodeContainer" |
| 472 |
} |
| 473 |
); |
| 474 |
|
| 475 |
my ( $res, $resBody ); |
| 476 |
ok( |
| 477 |
$res = &client->_post( |
| 478 |
'/confs/', 'cfgNum=4', $body, 'application/json', $len |
| 479 |
), |
| 480 |
"Request succeed" |
| 481 |
); |
| 482 |
ok( $res->[0] == 200, "Result code is 200" ); |
| 483 |
ok( |
| 484 |
$resBody = from_json( $res->[2]->[0] ), |
| 485 |
"Result body contains JSON text" |
| 486 |
); |
| 487 |
ok( $resBody->{result} == 1, "JSON response contains \"result:1\"" ) |
| 488 |
or print STDERR Dumper($res); |
| 489 |
|
| 490 |
ok( $res = &client->_get( '/confs/latest', 'full=1' ), 'Get saved conf' ); |
| 491 |
ok( |
| 492 |
$resBody = from_json( $res->[2]->[0] ), |
| 493 |
"Result body contains JSON text" |
| 494 |
); |
| 495 |
is( $resBody->{cfgNum}, 5, "New config was saved" ); |
| 496 |
|
| 497 |
is_deeply( $resBody->{keys}, {} ); |
| 498 |
}; |
| 499 |
|
| 500 |
done_testing(); |