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