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

(-)a/Koha/REST/V1.pm (+49 lines)
Lines 18-25 package Koha::REST::V1; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
use Mojo::Base 'Mojolicious';
19
use Mojo::Base 'Mojolicious';
20
20
21
use File::Find::Rule;
22
21
use C4::Auth qw( check_cookie_auth get_session );
23
use C4::Auth qw( check_cookie_auth get_session );
22
use C4::Context;
24
use C4::Context;
25
use Koha::Logger;
23
use Koha::Patrons;
26
use Koha::Patrons;
24
27
25
sub startup {
28
sub startup {
Lines 43-48 sub startup { Link Here
43
    # Force charset=utf8 in Content-Type header for JSON responses
46
    # Force charset=utf8 in Content-Type header for JSON responses
44
    $self->types->type(json => 'application/json; charset=utf8');
47
    $self->types->type(json => 'application/json; charset=utf8');
45
48
49
    $self->minifySwagger();
50
46
    my $secret_passphrase = C4::Context->config('api_secret_passphrase');
51
    my $secret_passphrase = C4::Context->config('api_secret_passphrase');
47
    if ($secret_passphrase) {
52
    if ($secret_passphrase) {
48
        $self->secrets([$secret_passphrase]);
53
        $self->secrets([$secret_passphrase]);
Lines 54-57 sub startup { Link Here
54
    });
59
    });
55
}
60
}
56
61
62
sub minifySwagger {
63
    my ($self, $swaggerPath) = @_;
64
65
    $swaggerPath = C4::Context->config("intranetdir").'/api/v1/' unless $swaggerPath;
66
    my $pathToMinifier = $swaggerPath.'minifySwagger.pl';
67
    my $pathToSwaggerJson = $swaggerPath.'swagger.json';
68
    my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json';
69
70
    if (-e $pathToSwaggerMinJson && not -w $pathToSwaggerMinJson
71
        || not -e $pathToSwaggerMinJson && not -w $swaggerPath) {
72
        my $user = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
73
        my $logger = Koha::Logger->get({ interface => 'intranet' });
74
        $logger->warn("User $user cannot write to $pathToSwaggerMinJson. ".
75
                      "Please give $user write-permission to $pathToSwaggerMinJson ".
76
                      "or run $pathToMinifier manually.");
77
        return;
78
    }
79
80
    if (_isMinificationRequired($swaggerPath, $pathToSwaggerJson,
81
                                $pathToSwaggerMinJson)) {
82
        my $output = `perl $pathToMinifier -s $pathToSwaggerJson -d $pathToSwaggerMinJson`;
83
        if ($output) {
84
            die $output;
85
        }
86
    }
87
}
88
89
sub _isMinificationRequired {
90
    my ($swaggerPath, $pathToSwaggerJson, $pathToSwaggerMinJson) = @_;
91
92
    return 1 unless -e $pathToSwaggerJson; # swagger.json exists?
93
    return 1 unless -e $pathToSwaggerMinJson; # swagger.min.json exists?
94
    my $latestModification = -C $pathToSwaggerJson;
95
96
    # Recursively go through specification files and find the last modified
97
    my $lastModifiedFile;
98
    my @files = File::Find::Rule->file()->name("*.json")->in($swaggerPath);
99
    foreach my $file (@files){
100
        next if $file eq $pathToSwaggerMinJson;
101
        $latestModification = -C $file if -C $file < $latestModification;
102
    }
103
    return 1 if $latestModification < -C $pathToSwaggerMinJson; # compare modification time
104
}
105
57
1;
106
1;
(-)a/t/api/v1/minifier.t (-1 / +147 lines)
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");

Return to bug 16212