|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
| 3 |
# Copyright (C) 2016 KohaSuomi |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use File::Copy qw/copy/; |
| 23 |
use File::Temp; |
| 24 |
use File::Path qw/make_path rmtree/; |
| 25 |
|
| 26 |
use Test::More tests => 13; |
| 27 |
|
| 28 |
use C4::Context; |
| 29 |
use Koha::REST::V1; |
| 30 |
|
| 31 |
my $oldSwagger_title = "Swagger Sample App"; |
| 32 |
my $newSwagger_title = "Koha REST API"; |
| 33 |
|
| 34 |
# Construct an example swagger.json |
| 35 |
my $swaggerJsonTest = |
| 36 |
qq ({ |
| 37 |
"swagger": "2.0", |
| 38 |
"info": { |
| 39 |
"title": "$oldSwagger_title", |
| 40 |
"version": "1" |
| 41 |
}, |
| 42 |
"paths": {}, |
| 43 |
"definitions": {}, |
| 44 |
"parameters": {} |
| 45 |
}); |
| 46 |
|
| 47 |
my $swaggerJsonTestModified = |
| 48 |
qq ({ |
| 49 |
"swagger": "2.0", |
| 50 |
"info": { |
| 51 |
"title": "$newSwagger_title", |
| 52 |
"version": "1" |
| 53 |
}, |
| 54 |
"paths": {}, |
| 55 |
"definitions": { |
| 56 |
"\$ref": "definitions.json" |
| 57 |
}, |
| 58 |
"parameters": {} |
| 59 |
}); |
| 60 |
my $swaggerJsonTestDefinitions = |
| 61 |
qq ({ |
| 62 |
"test": { |
| 63 |
"type": "object", |
| 64 |
"properties": { |
| 65 |
"id": { |
| 66 |
"type": "integer", |
| 67 |
"description": "test integer" |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
}); |
| 72 |
|
| 73 |
# Path containing actual swagger specification files |
| 74 |
my $real_swaggerPath = C4::Context->config("intranetdir").'/api/v1/'; |
| 75 |
|
| 76 |
# Create a tmp directory where we can modify swagger.json |
| 77 |
my $swaggerPath = File::Temp->newdir()."/"; |
| 78 |
make_path($swaggerPath); |
| 79 |
|
| 80 |
# Copy minifySwagger.pl to this location |
| 81 |
copy($real_swaggerPath.'minifySwagger.pl', $swaggerPath); |
| 82 |
|
| 83 |
my $pathToMinifier = $swaggerPath.'minifySwagger.pl'; |
| 84 |
my $pathToSwaggerJson = $swaggerPath.'swagger.json'; |
| 85 |
my $pathToSwaggerDefinitionsJson = $swaggerPath.'definitions.json'; |
| 86 |
my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json'; |
| 87 |
|
| 88 |
is(-e $pathToSwaggerJson, undef, "swagger.json does not yet exist"); |
| 89 |
is(-e $pathToSwaggerMinJson, undef, "swagger.min.json does not yet exist"); |
| 90 |
is(-e $pathToSwaggerDefinitionsJson, undef, "definitions.json does not yet exist"); |
| 91 |
ok(-w $swaggerPath, "$swaggerPath is writeable"); |
| 92 |
|
| 93 |
my $exists = -e $pathToSwaggerJson; |
| 94 |
# Create swagger.json test file |
| 95 |
open my $fh, '>', $pathToSwaggerJson; |
| 96 |
print $fh $swaggerJsonTest; |
| 97 |
close $fh; |
| 98 |
|
| 99 |
ok(-e $pathToSwaggerJson, "swagger.json created!"); |
| 100 |
|
| 101 |
Koha::REST::V1::minifySwagger(undef, $swaggerPath); |
| 102 |
ok(-e $pathToSwaggerMinJson, "swagger.min.json created!"); |
| 103 |
my $lastmodified = -C $pathToSwaggerMinJson; |
| 104 |
|
| 105 |
# Read swagger.min.json |
| 106 |
require Swagger2; |
| 107 |
my $swaggerMin = Swagger2->new($pathToSwaggerMinJson); |
| 108 |
$swaggerMin = $swaggerMin->expand; |
| 109 |
my $oldSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'}; |
| 110 |
is($oldSwaggerTitle, $oldSwagger_title, "old title found"); |
| 111 |
|
| 112 |
# Modify swagger.json |
| 113 |
sleep(1); # make sure modification time differs from swagger.min.json |
| 114 |
open $fh, '>', $pathToSwaggerJson; |
| 115 |
print $fh $swaggerJsonTestModified; |
| 116 |
close $fh; |
| 117 |
|
| 118 |
# Add definitions.json |
| 119 |
sleep(1); # make sure modification time differs from swagger.min.json |
| 120 |
open $fh, '>', $pathToSwaggerDefinitionsJson; |
| 121 |
print $fh $swaggerJsonTestDefinitions; |
| 122 |
close $fh; |
| 123 |
|
| 124 |
ok(-e $pathToSwaggerDefinitionsJson, "definitions.json created!"); |
| 125 |
|
| 126 |
Koha::REST::V1::minifySwagger(undef, $swaggerPath); |
| 127 |
|
| 128 |
ok($lastmodified > -C $pathToSwaggerMinJson, |
| 129 |
"swagger.min.json modified after editing swagger.json"); |
| 130 |
$lastmodified = -C $pathToSwaggerMinJson; |
| 131 |
|
| 132 |
# Re-read swagger.min.json |
| 133 |
$swaggerMin = Swagger2->new($pathToSwaggerMinJson); |
| 134 |
$swaggerMin = $swaggerMin->expand; |
| 135 |
my $newSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'}; |
| 136 |
is($newSwaggerTitle, $newSwagger_title, "new title found"); # was minified |
| 137 |
is($swaggerMin->{'api_spec'}->{'data'}->{'definitions'} |
| 138 |
->{'test'}->{'properties'}->{'id'}->{'description'}, |
| 139 |
"test integer", "test integer found!"); |
| 140 |
|
| 141 |
Koha::REST::V1::minifySwagger(undef, $swaggerPath); |
| 142 |
ok($lastmodified == -C $pathToSwaggerMinJson, |
| 143 |
"swagger.min.json not modified because no modifications to swagger.json"); |
| 144 |
|
| 145 |
# Cleanup |
| 146 |
rmtree($swaggerPath) if $swaggerPath ne $real_swaggerPath; |
| 147 |
is(-e $swaggerPath, undef, "tmp files deleted"); |