Bug 30194: (follow-up) Fix xt/api.t
[koha.git] / xt / api.t
1 # This file is part of Koha.
2 #
3 # Koha is free software; you can redistribute it and/or modify it under the
4 # terms of the GNU General Public License as published by the Free Software
5 # Foundation; either version 3 of the License, or (at your option) any later
6 # version.
7 #
8 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with Koha; if not, see <http://www.gnu.org/licenses>.
14
15 use Modern::Perl;
16
17 use Test::More tests => 2;
18
19 use Test::Mojo;
20 use Data::Dumper;
21
22 my $t    = Test::Mojo->new('Koha::REST::V1');
23 my $spec = $t->get_ok( '/api/v1/', 'Correctly fetched the spec' )->tx->res->json;
24
25 my $paths = $spec->{paths};
26
27 my @missing_additionalProperties = ();
28
29 foreach my $route ( keys %{$paths} ) {
30     foreach my $verb ( keys %{ $paths->{$route} } ) {
31
32         # p($paths->{$route}->{$verb});
33
34         # check parameters []
35         foreach my $parameter ( @{ $paths->{$route}->{$verb}->{parameters} } ) {
36             if (   exists $parameter->{schema}
37                 && exists $parameter->{schema}->{type}
38                 && ref( $parameter->{schema}->{type} ) ne 'ARRAY'
39                 && $parameter->{schema}->{type} eq 'object' ) {
40
41                 # it is an object type definition
42                 if ( $parameter->{name} ne 'query' # our query parameter is under-specified
43                     and not exists $parameter->{schema}->{additionalProperties} ) {
44                     push @missing_additionalProperties,
45                       { type  => 'parameter',
46                         route => $route,
47                         verb  => $verb,
48                         name  => $parameter->{name}
49                       };
50                 }
51             }
52         }
53
54         # check responses  {}
55         my $responses = $paths->{$route}->{$verb}->{responses};
56         foreach my $response ( keys %{$responses} ) {
57             if (   exists $responses->{$response}->{schema}
58                 && exists $responses->{$response}->{schema}->{type}
59                 && ref( $responses->{$response}->{schema}->{type} ) ne 'ARRAY'
60                 && $responses->{$response}->{schema}->{type} eq 'object' ) {
61
62                 # it is an object type definition
63                 if ( not exists $responses->{$response}->{schema}->{additionalProperties} ) {
64                     push @missing_additionalProperties,
65                       { type  => 'response',
66                         route => $route,
67                         verb  => $verb,
68                         name  => $response
69                       };
70                 }
71             }
72         }
73     }
74 }
75
76 is( scalar @missing_additionalProperties, 0 )
77   or diag Dumper \@missing_additionalProperties;