5
0
mirror of git://git.proxmox.com/git/pve-common.git synced 2024-12-22 21:33:47 +03:00
pve-common/test/api_parameter_test.pl
Dominik Csapak 07f136d636 JSONSchema: add support for array parameter in api calls, cli and config
a few things were missing for it to work:
* on the cli, we have to get the option as an array if the type is an
  array
* the untainting must be done recursively, otherwise, the regex matching
  converts an array hash into the string 'ARRAY(0x123412341234)'
* JSONSchema::parse_config did not handle array formats specially, but
  we want to allow to specify them multiple time
* the biggest point: in the RESTHandler, to be compatible with the
  current gui behavior, we have to rewrite two parameter types:
  - when the api defines a '-list' format for a string type, but we get
    a list (because of the changes in http-server), we join the list
    with a comma into a string
  - when the api defines an 'array' type, but we get a scalar value,
    wrap the value in an array (because for www-form-urlencoded, you
    cannot send an array with a single value) add tests for this
    behavior, some of which we want to deprecate and remove in the
    future

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
2023-06-07 13:10:50 +02:00

101 lines
1.9 KiB
Perl
Executable File

#!/usr/bin/perl
package PVE::TestAPIParameters;
# Tests the automatic conversion of -list and array parameter types
use strict;
use warnings;
use lib '../src';
use PVE::RESTHandler;
use PVE::JSONSchema;
use Test::More;
use base qw(PVE::RESTHandler);
my $setup = [
{
name => 'list-format-with-list',
parameter => {
type => 'string',
format => 'pve-configid-list',
},
value => "foo,bar",
'value-expected' => "foo,bar",
},
{
name => 'array-format-with-array',
parameter => {
type => 'array',
items => {
type => 'string',
format => 'pve-configid',
},
},
value => ['foo', 'bar'],
'value-expected' => ['foo', 'bar'],
},
# TODO: below behaviour should be deprecated with 9.x and fail with 10.x
{
name => 'list-format-with-alist',
parameter => {
type => 'string',
format => 'pve-configid-list',
},
value => "foo\0bar",
'value-expected' => "foo\0bar",
},
{
name => 'array-format-with-non-array',
parameter => {
type => 'array',
items => {
type => 'string',
format => 'pve-configid',
},
},
value => "foo",
'value-expected' => ['foo'],
},
{
name => 'list-format-with-array',
parameter => {
type => 'string',
format => 'pve-configid-list',
},
value => ['foo', 'bar'],
'value-expected' => "foo,bar",
},
];
for my $data ($setup->@*) {
__PACKAGE__->register_method({
name => $data->{name},
path => $data->{name},
method => 'POST',
parameters => {
additionalProperties => 0,
properties => {
param => $data->{parameter},
},
},
returns => { type => 'null' },
code => sub {
my ($param) = @_;
return $param->{param};
}
});
my ($handler, $info) = __PACKAGE__->find_handler('POST', $data->{name});
my $param = {
param => $data->{value},
};
my $res = $handler->handle($info, $param);
is_deeply($res, $data->{'value-expected'}, $data->{name});
}
done_testing();