|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/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 => 4; |
| 21 |
use Test::Exception; |
| 22 |
use Test::Warn; |
| 23 |
|
| 24 |
use Koha::File::Transports; |
| 25 |
|
| 26 |
use t::lib::TestBuilder; |
| 27 |
use t::lib::Mocks; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
subtest 'store() tests' => sub { |
| 33 |
plan tests => 6; |
| 34 |
$schema->storage->txn_begin; |
| 35 |
|
| 36 |
my $transport = $builder->build_object( |
| 37 |
{ |
| 38 |
class => 'Koha::File::Transports', |
| 39 |
value => { |
| 40 |
transport => 'ftp', |
| 41 |
password => undef, |
| 42 |
key_file => undef, |
| 43 |
download_directory => undef, |
| 44 |
upload_directory => undef |
| 45 |
} |
| 46 |
} |
| 47 |
); |
| 48 |
|
| 49 |
subtest 'Test store with empty directories' => sub { |
| 50 |
plan tests => 2; |
| 51 |
|
| 52 |
$transport->set( { download_directory => '', upload_directory => '' } )->store(); |
| 53 |
|
| 54 |
is( $transport->download_directory, '', 'Empty download directory remains empty' ); |
| 55 |
is( $transport->upload_directory, '', 'Empty upload directory remains empty' ); |
| 56 |
}; |
| 57 |
|
| 58 |
subtest 'Test store with directories missing trailing slash' => sub { |
| 59 |
plan tests => 2; |
| 60 |
|
| 61 |
$transport->set( { download_directory => '/tmp/download', upload_directory => '/tmp/upload' } )->store(); |
| 62 |
|
| 63 |
is( $transport->download_directory, '/tmp/download/', 'Added trailing slash to download directory' ); |
| 64 |
is( $transport->upload_directory, '/tmp/upload/', 'Added trailing slash to upload directory' ); |
| 65 |
}; |
| 66 |
|
| 67 |
subtest 'Test store with directories having trailing slash' => sub { |
| 68 |
plan tests => 2; |
| 69 |
|
| 70 |
$transport->set( { download_directory => '/tmp/download/', upload_directory => '/tmp/upload/' } )->store(); |
| 71 |
|
| 72 |
is( $transport->download_directory, '/tmp/download/', 'Kept existing trailing slash in download directory' ); |
| 73 |
is( $transport->upload_directory, '/tmp/upload/', 'Kept existing trailing slash in upload directory' ); |
| 74 |
}; |
| 75 |
|
| 76 |
subtest 'Test store with mixed trailing slashes' => sub { |
| 77 |
plan tests => 2; |
| 78 |
|
| 79 |
$transport->set( { download_directory => '/tmp/download', upload_directory => '/tmp/upload/' } )->store(); |
| 80 |
|
| 81 |
is( $transport->download_directory, '/tmp/download/', 'Added missing trailing slash to download directory' ); |
| 82 |
is( $transport->upload_directory, '/tmp/upload/', 'Kept existing trailing slash in upload directory' ); |
| 83 |
}; |
| 84 |
|
| 85 |
subtest 'Test store with undefined directories' => sub { |
| 86 |
plan tests => 2; |
| 87 |
|
| 88 |
$transport->set( { download_directory => undef, upload_directory => undef } )->store(); |
| 89 |
|
| 90 |
is( $transport->download_directory, undef, 'Undefined download directory remains undefined' ); |
| 91 |
is( $transport->upload_directory, undef, 'Undefined upload directory remains undefined' ); |
| 92 |
}; |
| 93 |
|
| 94 |
subtest 'Test encryption of sensitive data' => sub { |
| 95 |
plan tests => 2; |
| 96 |
|
| 97 |
$transport->set( { password => "test123", key_file => "test321" } )->store(); |
| 98 |
|
| 99 |
isnt( $transport->password, "test123", 'Password is encrypted on store' ); |
| 100 |
isnt( $transport->upload_directory, "test321", 'Key file is encrypted on store' ); |
| 101 |
|
| 102 |
}; |
| 103 |
|
| 104 |
$schema->storage->txn_rollback; |
| 105 |
}; |
| 106 |
|
| 107 |
subtest 'to_api() tests' => sub { |
| 108 |
|
| 109 |
plan tests => 2; |
| 110 |
|
| 111 |
$schema->storage->txn_begin; |
| 112 |
|
| 113 |
my $transport = $builder->build_object( { class => 'Koha::File::Transports', value => { status => undef } } ); |
| 114 |
|
| 115 |
ok( !exists $transport->to_api->{password}, 'Password is not part of the API representation' ); |
| 116 |
ok( !exists $transport->to_api->{key_file}, 'Key file is not part of the API representation' ); |
| 117 |
|
| 118 |
$schema->storage->txn_rollback; |
| 119 |
}; |
| 120 |
|
| 121 |
subtest 'plain_text_password() tests' => sub { |
| 122 |
|
| 123 |
plan tests => 2; |
| 124 |
|
| 125 |
$schema->storage->txn_begin; |
| 126 |
|
| 127 |
my $transport = $builder->build_object( |
| 128 |
{ |
| 129 |
class => 'Koha::File::Transports', |
| 130 |
value => { |
| 131 |
transport => 'ftp', |
| 132 |
password => undef, |
| 133 |
key_file => undef, |
| 134 |
} |
| 135 |
} |
| 136 |
); |
| 137 |
$transport->password('test123')->store(); |
| 138 |
|
| 139 |
my $transport_plain_text_password = $transport->plain_text_password; |
| 140 |
|
| 141 |
isnt( $transport_plain_text_password, $transport->password, 'Password and password hash shouldn\'t match' ); |
| 142 |
is( $transport_plain_text_password, 'test123', 'Password should be in plain text' ); |
| 143 |
|
| 144 |
$schema->storage->txn_rollback; |
| 145 |
}; |
| 146 |
|
| 147 |
subtest 'plain_text_key() tests' => sub { |
| 148 |
|
| 149 |
plan tests => 2; |
| 150 |
|
| 151 |
$schema->storage->txn_begin; |
| 152 |
|
| 153 |
my $transport = $builder->build_object( |
| 154 |
{ |
| 155 |
class => 'Koha::File::Transports', |
| 156 |
value => { |
| 157 |
transport => 'ftp', |
| 158 |
password => undef, |
| 159 |
key_file => undef |
| 160 |
} |
| 161 |
} |
| 162 |
); |
| 163 |
$transport->key_file("test321")->store(); |
| 164 |
|
| 165 |
my $transport_plain_text_key = $transport->plain_text_key; |
| 166 |
|
| 167 |
isnt( $transport_plain_text_key, $transport->key_file, 'Key file and key file hash shouldn\'t match' ); |
| 168 |
is( $transport_plain_text_key, "test321\n", 'Key file should be in plain text' ); |
| 169 |
|
| 170 |
$schema->storage->txn_rollback; |
| 171 |
}; |
| 172 |
|
| 173 |
1; |