Bug 33146: Unit tests
[koha-ffzg.git] / t / db_dependent / api / v1 / erm_documents.t
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 Koha::ERM::Documents;
27 use Koha::Database;
28
29 use MIME::Base64 qw( decode_base64 );
30 use Koha::ERM::Licenses;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 my $t = Test::Mojo->new('Koha::REST::V1');
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37
38 subtest 'get() tests' => sub {
39
40     plan tests => 13;
41
42     $schema->storage->txn_begin;
43
44     my $document = $builder->build_object(
45         {
46             class => 'Koha::ERM::Documents',
47             value => {
48                 file_content => '123',
49                 file_name    => 'name'
50             }
51         }
52     );
53
54     my $librarian = $builder->build_object(
55         {
56             class => 'Koha::Patrons',
57             value => { flags => 2**28 }
58         }
59     );
60     my $password = 'thePassword123';
61     $librarian->set_password( { password => $password, skip_validation => 1 } );
62     my $userid = $librarian->userid;
63
64     my $patron = $builder->build_object(
65         {
66             class => 'Koha::Patrons',
67             value => { flags => 0 }
68         }
69     );
70
71     $patron->set_password( { password => $password, skip_validation => 1 } );
72     my $unauth_userid = $patron->userid;
73
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');
78
79     # Create a document through a license, gets returned
80     my $license = $builder->build_object( { class => 'Koha::ERM::Licenses' } );
81
82     $license->documents(
83         [
84             {
85                 file_content => '321',
86                 file_name    => '321.jpeg'
87             },
88             {
89                 file_content => '456',
90                 file_name    => '456.jpeg'
91             }
92         ]
93     );
94     my @documents           = $license->documents->as_list;
95     my $license_document_id = $documents[0]->document_id;
96
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') );
101
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;
105
106     $license->documents(
107         [
108             {
109                 document_id  => $remaining_document_id,
110                 file_content => '456',
111                 file_name    => '456.jpeg'
112             }
113         ]
114     );
115
116     $t->get_ok( "//$userid:$password@/api/v1/erm/documents/"
117           . $deleted_document_id
118           . "/file/content" )->status_is(404);
119
120     # Unauthorized access
121     $t->get_ok( "//$unauth_userid:$password@/api/v1/erm/documents/"
122           . $document->document_id
123           . "/file/content" )->status_is(403);
124
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;
130
131     $t->get_ok(
132 "//$userid:$password@/api/v1/erm/documents/$non_existent_id/file/content"
133     )->status_is(404)->json_is( '/error' => 'Document not found' );
134
135     $schema->storage->txn_rollback;
136 };