Bug 14868: Use x-koha-authorization in current routes
[koha.git] / t / db_dependent / Upload.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use File::Temp qw/ tempdir /;
5 use Test::More tests => 8;
6
7 use Test::MockModule;
8 use t::lib::Mocks;
9 use t::lib::TestBuilder;
10
11 use C4::Context;
12 use Koha::Database;
13 use Koha::Upload;
14
15 my $schema  = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $dbh = C4::Context->dbh;
18
19 our $current_upload = 0;
20 our $uploads = [
21     [
22         { name => 'file1', cat => 'A', size => 6000 },
23         { name => 'file2', cat => 'A', size => 8000 },
24     ],
25     [
26         { name => 'file3', cat => 'B', size => 1000 },
27     ],
28     [
29         { name => 'file4', cat => undef, size => 5000 }, # temporary
30     ],
31     [
32         { name => 'file2', cat => 'A', size => 8000 },
33         # uploading a duplicate in cat A should fail
34     ],
35     [
36         { name => 'file4', cat => undef, size => 5000 }, # temp duplicate
37     ],
38 ];
39
40 # Redirect upload dir structure and mock File::Spec and CGI
41 my $tempdir = tempdir( CLEANUP => 1 );
42 t::lib::Mocks::mock_config('upload_path', $tempdir);
43 my $specmod = Test::MockModule->new( 'File::Spec' );
44 $specmod->mock( 'tmpdir' => sub { return $tempdir; } );
45 my $cgimod = Test::MockModule->new( 'CGI' );
46 $cgimod->mock( 'new' => \&newCGI );
47
48 # Start testing
49 subtest 'Test01' => sub {
50     plan tests => 7;
51     test01();
52 };
53 subtest 'Test02' => sub {
54     plan tests => 4;
55     test02();
56 };
57 subtest 'Test03' => sub {
58     plan tests => 2;
59     test03();
60 };
61 subtest 'Test04' => sub {
62     plan tests => 3;
63     test04();
64 };
65 subtest 'Test05' => sub {
66     plan tests => 5;
67     test05();
68 };
69 subtest 'Test06' => sub {
70     plan tests => 2;
71     test06();
72 };
73 subtest 'Test07' => sub {
74     plan tests => 2;
75     test07();
76 };
77 subtest 'Test08: allows_add_by' => sub {
78     plan tests => 4;
79     test08();
80 };
81 $schema->storage->txn_rollback;
82
83 sub test01 {
84     # Delete existing records (for later tests)
85     $dbh->do( "DELETE FROM uploaded_files" );
86
87     my $upl = Koha::Upload->new({
88         category => $uploads->[$current_upload]->[0]->{cat},
89     });
90     my $cgi= $upl->cgi;
91     my $res= $upl->result;
92     is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' );
93     is( $upl->count, 2, 'Count returns 2 also' );
94     foreach my $r ( $upl->get({ id => $res }) ) {
95         if( $r->{name} eq 'file1' ) {
96             is( $r->{uploadcategorycode}, 'A', 'Check category A' );
97             is( $r->{filesize}, 6000, 'Check size of file1' );
98         } elsif( $r->{name} eq 'file2' ) {
99             is( $r->{filesize}, 8000, 'Check size of file2' );
100             is( $r->{public}, undef, 'Check public undefined' );
101         }
102     }
103     is( $upl->err, undef, 'No errors reported' );
104 }
105
106 sub test02 {
107     my $upl = Koha::Upload->new({
108         category => $uploads->[$current_upload]->[0]->{cat},
109         public => 1,
110     });
111     my $cgi= $upl->cgi;
112     is( $upl->count, 1, 'Upload 2 includes one file' );
113     my $res= $upl->result;
114     my $r = $upl->get({ id => $res, filehandle => 1 });
115     is( $r->{uploadcategorycode}, 'B', 'Check category B' );
116     is( $r->{public}, 1, 'Check public == 1' );
117     is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' );
118 }
119
120 sub test03 {
121     my $upl = Koha::Upload->new({ tmp => 1 }); #temporary
122     my $cgi= $upl->cgi;
123     is( $upl->count, 1, 'Upload 3 includes one temporary file' );
124     my $r = $upl->get({ id => $upl->result });
125     is( $r->{uploadcategorycode} =~ /_upload$/, 1, 'Check category temp file' );
126 }
127
128 sub test04 { # Fail on a file already there
129     my $upl = Koha::Upload->new({
130         category => $uploads->[$current_upload]->[0]->{cat},
131     });
132     my $cgi= $upl->cgi;
133     is( $upl->count, 0, 'Upload 4 failed as expected' );
134     is( $upl->result, undef, 'Result is undefined' );
135     my $e = $upl->err;
136     is( $e->{file2}, 1, "Errcode 1 [already exists] reported" );
137 }
138
139 sub test05 { # add temporary file with same name and contents, delete it
140     my $upl = Koha::Upload->new({ tmp => 1 });
141     my $cgi= $upl->cgi;
142     is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' );
143     my $id = $upl->result;
144     my $r = $upl->get({ id => $id });
145     my @d = $upl->delete({ id => $id });
146     is( $d[0], $r->{name}, 'Delete successful' );
147     is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' );
148     is( scalar $upl->get({ id => $id }), undef, 'Record also gone' );
149     is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' );
150 }
151
152 sub test06 { #some extra tests for get
153     my $upl = Koha::Upload->new({ public => 1 });
154     my @rec = $upl->get({ term => 'file' });
155     is( @rec, 1, 'Get returns only one public result (file3)' );
156     $upl = Koha::Upload->new; # public == 0
157     @rec = $upl->get({ term => 'file' });
158     is( @rec, 4, 'Get returns now four results' );
159 }
160
161 sub test07 { #simple test for httpheaders and getCategories
162     my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet');
163     is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders');
164     $dbh->do("INSERT INTO authorised_values (category, authorised_value, lib) VALUES (?,?,?) ", undef, ( 'UPLOAD', 'HAVE_AT_LEAST_ONE', 'Hi there' ));
165     my $cat = Koha::Upload->getCategories;
166     is( @$cat >= 1, 1, 'getCategories returned at least one category' );
167 }
168
169 sub test08 { # allows_add_by
170     my $builder = t::lib::TestBuilder->new;
171     my $patron = $builder->build({
172         source => 'Borrower',
173         value  => { flags => 0 }, #no permissions
174     });
175     my $patronid = $patron->{borrowernumber};
176     is( Koha::Upload->allows_add_by( $patron->{userid} ),
177         undef, 'Patron is not allowed to do anything' );
178
179     # add some permissions: edit_catalogue
180     my $fl = 2**9; # edit_catalogue
181     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
182     is( Koha::Upload->allows_add_by( $patron->{userid} ),
183         undef, 'Patron is still not allowed to add uploaded files' );
184
185     # replace flags by all tools
186     $fl = 2**13; # tools
187     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
188     is( Koha::Upload->allows_add_by( $patron->{userid} ),
189         1, 'Patron should be allowed now to add uploaded files' );
190
191     # remove all tools and add upload_general_files only
192     $fl = 0; # no modules
193     $schema->resultset('Borrower')->find( $patronid )->update({ flags => $fl });
194     $builder->build({
195         source => 'UserPermission',
196         value  => {
197             borrowernumber => $patronid,
198             module_bit     => { module_bit => { flag => 'tools' } },
199             code           => 'upload_general_files',
200         },
201     });
202     is( Koha::Upload->allows_add_by( $patron->{userid} ),
203         1, 'Patron is still allowed to add uploaded files' );
204 }
205
206 sub newCGI {
207     my ( $class, $hook ) = @_;
208     my $read = 0;
209     foreach my $uh ( @{$uploads->[ $current_upload ]} ) {
210         for( my $i=0; $i< $uh->{size}; $i+=1000 ) {
211             $read+= 1000;
212             &$hook( $uh->{name}, 'a'x1000, $read );
213         }
214     }
215     $current_upload++;
216     return $class;
217 }