Bug 28370: Add tests
[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 use File::Slurp qw( read_file );
17 use JSON qw( from_json );
18 use Test::More tests => 4;
19 use Data::Dumper;
20
21 my $dh;
22
23 my $definitions_dir = 'api/v1/swagger/definitions';
24 opendir( $dh, $definitions_dir ) or die "$!";
25 my @files = readdir $dh;
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
40
41 my $paths_dir = 'api/v1/swagger/paths';
42 opendir( $dh, $paths_dir ) or die "$!";
43 @files = readdir $dh;
44 @wrong_additionalProperties = ();
45 ok(@files, "making sure we found paths files");
46 for my $file (@files) {
47     next unless $file =~ m|\.json$|;
48     my $spec = from_json read_file("$paths_dir/$file");
49     for my $route ( keys %$spec ) {
50         for my $method ( keys %{ $spec->{$route} } ) {
51             next if $method ne 'post' && $method ne 'put';
52             for my $parameter ( @{ $spec->{$route}->{$method}->{parameters} } ) {
53                 if ( exists $parameter->{schema} ) {
54
55                     # If it's a ref we inherit from the definition file
56                     next if exists $parameter->{schema}->{'$ref'};
57
58                     if ( not exists $parameter->{schema}->{additionalProperties}
59                         or $parameter->{schema}->{additionalProperties} != 0 )
60                     {
61                         push @wrong_additionalProperties,
62                           {
63                             file   => "$paths_dir/$file",
64                             route  => $route,
65                             method => $method,
66                           };
67                     }
68                 }
69             }
70         }
71     }
72 }
73
74 is( scalar @wrong_additionalProperties, 0 )
75   or diag Dumper \@wrong_additionalProperties;