|
Lines 17-27
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 8; |
20 |
use Test::More tests => 9; |
| 21 |
use Test::Exception; |
21 |
use Test::Exception; |
| 22 |
use Test::NoWarnings; |
22 |
use Test::NoWarnings; |
| 23 |
use Test::Warn; |
23 |
use Test::Warn; |
|
|
24 |
use Test::MockModule; |
| 24 |
|
25 |
|
|
|
26 |
use Koha::Encryption; |
| 25 |
use Koha::File::Transports; |
27 |
use Koha::File::Transports; |
| 26 |
|
28 |
|
| 27 |
use t::lib::TestBuilder; |
29 |
use t::lib::TestBuilder; |
|
Lines 30-35
use t::lib::Mocks;
Link Here
|
| 30 |
my $schema = Koha::Database->new->schema; |
32 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
33 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
34 |
|
|
|
35 |
subtest 'scalar context and mutually exclusive authentication tests' => sub { |
| 36 |
plan tests => 10; |
| 37 |
|
| 38 |
$schema->storage->txn_begin; |
| 39 |
|
| 40 |
# Setup mock upload path for key file operations |
| 41 |
my $path = '/tmp/kohadev_test'; |
| 42 |
t::lib::Mocks::mock_config( 'upload_path', $path ); |
| 43 |
mkdir $path if !-d $path; |
| 44 |
|
| 45 |
# Mock Net::SFTP::Foreign to capture constructor parameters |
| 46 |
my %sftp_new_params; |
| 47 |
my $mock_sftp = Test::MockModule->new('Net::SFTP::Foreign'); |
| 48 |
$mock_sftp->mock( |
| 49 |
'new', |
| 50 |
sub { |
| 51 |
my $class = shift; |
| 52 |
%sftp_new_params = @_; |
| 53 |
my $obj = bless { status => 0, error => undef }, $class; |
| 54 |
return $obj; |
| 55 |
} |
| 56 |
); |
| 57 |
my $undef; |
| 58 |
$mock_sftp->mock( 'error', sub { return $undef; } ); |
| 59 |
$mock_sftp->mock( 'status', sub { return 0; } ); |
| 60 |
$mock_sftp->mock( 'cwd', sub { return '/'; } ); |
| 61 |
|
| 62 |
# Test 1: Transport with no password and no key - plain_text_password returns undef |
| 63 |
my $transport_no_auth = $builder->build_object( |
| 64 |
{ |
| 65 |
class => 'Koha::File::Transports', |
| 66 |
value => { transport => 'sftp', password => undef, key_file => undef } |
| 67 |
} |
| 68 |
); |
| 69 |
|
| 70 |
is( |
| 71 |
scalar $transport_no_auth->plain_text_password, undef, |
| 72 |
'plain_text_password returns undef in scalar context when no password' |
| 73 |
); |
| 74 |
|
| 75 |
# Test 2: Connection with no auth should pass password => undef (not empty list) |
| 76 |
$transport_no_auth->connect; |
| 77 |
ok( exists $sftp_new_params{password}, 'password key exists in connection params' ); |
| 78 |
is( $sftp_new_params{password}, undef, 'password parameter is undef when no password set' ); |
| 79 |
ok( !exists $sftp_new_params{key_path}, 'key_path is not set when no key file exists' ); |
| 80 |
|
| 81 |
# Test 3: Transport with key only - should use key_path, not password |
| 82 |
# Must encrypt key_file since plain_text_key() will decrypt it |
| 83 |
my $encrypted_key = Koha::Encryption->new->encrypt_hex('test_key_content'); |
| 84 |
my $transport_key_only = $builder->build_object( |
| 85 |
{ |
| 86 |
class => 'Koha::File::Transports', |
| 87 |
value => { transport => 'sftp', password => undef, key_file => $encrypted_key } |
| 88 |
} |
| 89 |
); |
| 90 |
$transport_key_only->store; # Trigger key file write |
| 91 |
$transport_key_only->discard_changes; |
| 92 |
|
| 93 |
%sftp_new_params = (); # Clear params |
| 94 |
$transport_key_only->connect; |
| 95 |
ok( exists $sftp_new_params{key_path}, 'key_path exists in connection params when key is set' ); |
| 96 |
ok( !exists $sftp_new_params{password}, 'password is not set when using key-based auth' ); |
| 97 |
|
| 98 |
# Test 4: Transport with password only - should use password, not key_path |
| 99 |
# Must encrypt password since plain_text_password() will decrypt it |
| 100 |
my $encrypted_pass = Koha::Encryption->new->encrypt_hex('testpass'); |
| 101 |
my $transport_pass_only = $builder->build_object( |
| 102 |
{ |
| 103 |
class => 'Koha::File::Transports', |
| 104 |
value => { transport => 'sftp', password => $encrypted_pass, key_file => undef } |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
%sftp_new_params = (); # Clear params |
| 109 |
$transport_pass_only->connect; |
| 110 |
ok( exists $sftp_new_params{password}, 'password exists in connection params when password is set' ); |
| 111 |
is( $sftp_new_params{password}, 'testpass', 'password is correctly decrypted' ); |
| 112 |
ok( !exists $sftp_new_params{key_path}, 'key_path is not set when using password auth' ); |
| 113 |
|
| 114 |
# Test 5: Transport with both key and password - should prefer key |
| 115 |
my $encrypted_both_pass = Koha::Encryption->new->encrypt_hex('bothpass'); |
| 116 |
my $encrypted_both_key = Koha::Encryption->new->encrypt_hex('both_test_key'); |
| 117 |
my $transport_both = $builder->build_object( |
| 118 |
{ |
| 119 |
class => 'Koha::File::Transports', |
| 120 |
value => { transport => 'sftp', password => $encrypted_both_pass, key_file => $encrypted_both_key } |
| 121 |
} |
| 122 |
); |
| 123 |
$transport_both->store; # Trigger key file write |
| 124 |
$transport_both->discard_changes; |
| 125 |
|
| 126 |
%sftp_new_params = (); # Clear params |
| 127 |
$transport_both->connect; |
| 128 |
ok( |
| 129 |
exists $sftp_new_params{key_path} && !exists $sftp_new_params{password}, |
| 130 |
'When both key and password exist, key_path is used and password is excluded' |
| 131 |
); |
| 132 |
|
| 133 |
# Cleanup |
| 134 |
unlink $transport_key_only->_locate_key_file if $transport_key_only->_locate_key_file; |
| 135 |
unlink $transport_both->_locate_key_file if $transport_both->_locate_key_file; |
| 136 |
|
| 137 |
$schema->storage->txn_rollback; |
| 138 |
}; |
| 139 |
|
| 33 |
subtest 'store() and _post_store_trigger() tests' => sub { |
140 |
subtest 'store() and _post_store_trigger() tests' => sub { |
| 34 |
plan tests => 2; |
141 |
plan tests => 2; |
| 35 |
|
142 |
|
| 36 |
- |
|
|