View | Details | Raw Unified | Return to bug 28370
Collapse All | Expand All

(-)a/xt/api.t (-48 / +48 lines)
Lines 13-75 Link Here
13
# with Koha; if not, see <http://www.gnu.org/licenses>.
13
# with Koha; if not, see <http://www.gnu.org/licenses>.
14
14
15
use Modern::Perl;
15
use Modern::Perl;
16
use File::Slurp qw( read_file );
16
17
use JSON qw( from_json );
17
use Test::More tests => 2;
18
use Test::More tests => 4;
18
19
use Test::Mojo;
19
use Data::Dumper;
20
use Data::Dumper;
20
21
21
my $dh;
22
my $t    = Test::Mojo->new('Koha::REST::V1');
23
my $spec = $t->get_ok( '/api/v1/', 'Correctly fetched the spec' )->tx->res->json;
22
24
23
my $definitions_dir = 'api/v1/swagger/definitions';
25
my $paths = $spec->{paths};
24
opendir( $dh, $definitions_dir ) or die "$!";
26
25
my @files = readdir $dh;
27
my @missing_additionalProperties = ();
26
my @wrong_additionalProperties;
27
ok( @files, "making sure we found definitions files" );
28
for my $file (@files) {
29
    next unless $file =~ m|\.json$|;
30
    my $spec = from_json read_file("$definitions_dir/$file");
31
    if ( not exists $spec->{additionalProperties}
32
        or $spec->{additionalProperties} != 0 )
33
    {
34
        push @wrong_additionalProperties, { file => $file, };
35
    }
36
}
37
is( scalar @wrong_additionalProperties, 0 )
38
  or diag Dumper \@wrong_additionalProperties;
39
28
29
foreach my $route ( keys %{$paths} ) {
30
    foreach my $verb ( keys %{ $paths->{$route} } ) {
40
31
41
my $paths_dir = 'api/v1/swagger/paths';
32
        # p($paths->{$route}->{$verb});
42
opendir( $dh, $paths_dir ) or die "$!";
33
43
@files = readdir $dh;
34
        # check parameters []
44
@wrong_additionalProperties = ();
35
        foreach my $parameter ( @{ $paths->{$route}->{$verb}->{parameters} } ) {
45
ok(@files, "making sure we found paths files");
36
            if (   exists $parameter->{schema}
46
for my $file (@files) {
37
                && exists $parameter->{schema}->{type}
47
    next unless $file =~ m|\.json$|;
38
                && ref( $parameter->{schema}->{type} ) ne 'ARRAY'
48
    my $spec = from_json read_file("$paths_dir/$file");
39
                && $parameter->{schema}->{type} eq 'object' ) {
49
    for my $route ( keys %$spec ) {
40
50
        for my $method ( keys %{ $spec->{$route} } ) {
41
                # it is an object type definition
51
            next if $method ne 'post' && $method ne 'put';
42
                if ( not exists $parameter->{schema}->{additionalProperties} ) {
52
            for my $parameter ( @{ $spec->{$route}->{$method}->{parameters} } ) {
43
                    push @missing_additionalProperties,
53
                if ( exists $parameter->{schema} ) {
44
                      { type  => 'parameter',
45
                        route => $route,
46
                        verb  => $verb,
47
                        name  => $parameter->{name}
48
                      };
49
                }
50
            }
51
        }
54
52
55
                    # If it's a ref we inherit from the definition file
53
        # check responses  {}
56
                    next if exists $parameter->{schema}->{'$ref'};
54
        my $responses = $paths->{$route}->{$verb}->{responses};
55
        foreach my $response ( keys %{$responses} ) {
56
            if (   exists $responses->{$response}->{schema}
57
                && exists $responses->{$response}->{schema}->{type}
58
                && ref( $responses->{$response}->{schema}->{type} ) ne 'ARRAY'
59
                && $responses->{$response}->{schema}->{type} eq 'object' ) {
57
60
58
                    if ( not exists $parameter->{schema}->{additionalProperties}
61
                # it is an object type definition
59
                        or $parameter->{schema}->{additionalProperties} != 0 )
62
                if ( not exists $responses->{$response}->{schema}->{additionalProperties} ) {
60
                    {
63
                    push @missing_additionalProperties,
61
                        push @wrong_additionalProperties,
64
                      { type  => 'response',
62
                          {
65
                        route => $route,
63
                            file   => "$paths_dir/$file",
66
                        verb  => $verb,
64
                            route  => $route,
67
                        name  => $response
65
                            method => $method,
68
                      };
66
                          };
67
                    }
68
                }
69
                }
69
            }
70
            }
70
        }
71
        }
71
    }
72
    }
72
}
73
}
73
74
74
is( scalar @wrong_additionalProperties, 0 )
75
is( scalar @missing_additionalProperties, 0 )
75
  or diag Dumper \@wrong_additionalProperties;
76
  or diag Dumper \@missing_additionalProperties;
76
- 

Return to bug 28370