3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 1;
23 use t::lib::TestBuilder;
26 use Koha::ERM::Documents;
29 use MIME::Base64 qw( decode_base64 );
30 use Koha::ERM::Licenses;
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 my $t = Test::Mojo->new('Koha::REST::V1');
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
38 subtest 'get() tests' => sub {
42 $schema->storage->txn_begin;
44 my $document = $builder->build_object(
46 class => 'Koha::ERM::Documents',
48 file_content => '123',
54 my $librarian = $builder->build_object(
56 class => 'Koha::Patrons',
57 value => { flags => 2**28 }
60 my $password = 'thePassword123';
61 $librarian->set_password( { password => $password, skip_validation => 1 } );
62 my $userid = $librarian->userid;
64 my $patron = $builder->build_object(
66 class => 'Koha::Patrons',
67 value => { flags => 0 }
71 $patron->set_password( { password => $password, skip_validation => 1 } );
72 my $unauth_userid = $patron->userid;
74 # This document exists, should get returned
75 $t->get_ok( "//$userid:$password@/api/v1/erm/documents/"
76 . $document->document_id
77 . "/file/content" )->status_is(200)->json_is('123');
79 # Create a document through a license, gets returned
80 my $license = $builder->build_object( { class => 'Koha::ERM::Licenses' } );
85 file_content => '321',
86 file_name => '321.jpeg'
89 file_content => '456',
90 file_name => '456.jpeg'
94 my @documents = $license->documents->as_list;
95 my $license_document_id = $documents[0]->document_id;
97 $t->get_ok( "//$userid:$password@/api/v1/erm/documents/"
98 . $license_document_id
99 . "/file/content" )->status_is(200)
100 ->content_is( decode_base64('321') );
102 # Delete a document through a license, no longer exists
103 my $deleted_document_id = $license_document_id;
104 my $remaining_document_id = $documents[1]->document_id;
109 document_id => $remaining_document_id,
110 file_content => '456',
111 file_name => '456.jpeg'
116 $t->get_ok( "//$userid:$password@/api/v1/erm/documents/"
117 . $deleted_document_id
118 . "/file/content" )->status_is(404);
120 # Unauthorized access
121 $t->get_ok( "//$unauth_userid:$password@/api/v1/erm/documents/"
122 . $document->document_id
123 . "/file/content" )->status_is(403);
125 # Attempt to get non-existent document
126 my $document_to_delete =
127 $builder->build_object( { class => 'Koha::ERM::Documents' } );
128 my $non_existent_id = $document_to_delete->id;
129 $document_to_delete->delete;
132 "//$userid:$password@/api/v1/erm/documents/$non_existent_id/file/content"
133 )->status_is(404)->json_is( '/error' => 'Document not found' );
135 $schema->storage->txn_rollback;