Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 1; |
21 |
use Test::Mojo; |
22 |
|
23 |
use t::lib::TestBuilder; |
24 |
use t::lib::Mocks; |
25 |
|
26 |
use JSON qw(encode_json); |
27 |
|
28 |
use Koha::Edifact::Files; |
29 |
use Koha::Database; |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
my $builder = t::lib::TestBuilder->new; |
33 |
|
34 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
35 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
36 |
|
37 |
subtest 'list() tests' => sub { |
38 |
|
39 |
plan tests => 20; |
40 |
|
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
Koha::Edifact::Files->search->delete; |
44 |
|
45 |
my $librarian = $builder->build_object( |
46 |
{ |
47 |
class => 'Koha::Patrons', |
48 |
value => { flags => 2**11 } #acquisition |
49 |
} |
50 |
); |
51 |
my $password = 'thePassword123'; |
52 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
53 |
my $userid = $librarian->userid; |
54 |
|
55 |
my $patron = $builder->build_object( |
56 |
{ |
57 |
class => 'Koha::Patrons', |
58 |
value => { flags => 0 } |
59 |
} |
60 |
); |
61 |
|
62 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
63 |
my $unauth_userid = $patron->userid; |
64 |
|
65 |
## Authorized user tests |
66 |
# No acquisitions\/edifiles, so empty array should be returned |
67 |
$t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles")->status_is(200)->json_is( [] ); |
68 |
|
69 |
my $file = $builder->build_object( { class => 'Koha::Edifact::Files', value => { message_type => 'ORDER' } } ); |
70 |
|
71 |
# One file created, should get returned |
72 |
$t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles")->status_is(200)->json_is( [ $file->to_api ] ); |
73 |
|
74 |
my $another_file_ORDER = |
75 |
$builder->build_object( { class => 'Koha::Edifact::Files', value => { message_type => 'ORDER' } } ); |
76 |
my $another_file_QUOTE = |
77 |
$builder->build_object( { class => 'Koha::Edifact::Files', value => { message_type => 'QUOTE' } } ); |
78 |
|
79 |
# Two files created, they should both be returned |
80 |
$t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles")->status_is(200)->json_is( |
81 |
[ |
82 |
$file->to_api, |
83 |
$another_file_ORDER->to_api, |
84 |
$another_file_QUOTE->to_api |
85 |
] |
86 |
); |
87 |
|
88 |
# Filtering works, two files sharing type |
89 |
my $api_filter = encode_json( { 'me.type' => ['ORDER'] } ); |
90 |
$t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles?q=$api_filter")->status_is(200)->json_is( |
91 |
[ |
92 |
$file->to_api, |
93 |
$another_file_ORDER->to_api |
94 |
] |
95 |
); |
96 |
|
97 |
$api_filter = encode_json( { 'me.filename' => $file->filename } ); |
98 |
$t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles?q=$api_filter")->status_is(200) |
99 |
->json_is( [ $file->to_api ] ); |
100 |
|
101 |
# Warn on unsupported query parameter |
102 |
$api_filter = encode_json( { 'me.file_blah' => 'blah' } ); |
103 |
$t->get_ok("//$userid:$password@/api/v1/acquisitions/edifiles?file_blah=blah")->status_is(400) |
104 |
->json_is( [ { path => '/query/file_blah', message => 'Malformed query string' } ] ); |
105 |
|
106 |
# Unauthorized access |
107 |
$t->get_ok("//$unauth_userid:$password@/api/v1/acquisitions/edifiles")->status_is(403); |
108 |
|
109 |
$schema->storage->txn_rollback; |
110 |
}; |