View | Details | Raw Unified | Return to bug 39190
Collapse All | Expand All

(-)a/Koha/File/Transport.pm (+268 lines)
Line 0 Link Here
1
package Koha::File::Transport;
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
use constant {
20
    DEFAULT_TIMEOUT      => 10,
21
    TEST_FILE_NAME       => '.koha_test_file',
22
    TEST_FILE_CONTENT    => "Hello, world!\n",
23
    KEY_FILE_PERMISSIONS => '0600',
24
};
25
26
use Koha::Database;
27
use Koha::Exceptions::Object;
28
use Koha::Encryption;
29
30
use base qw(Koha::Object);
31
32
=head1 NAME
33
34
Koha::File::Transport - Base class for file transport handling
35
36
=head1 DESCRIPTION
37
38
Base class providing common functionality for FTP/SFTP file transport.
39
40
=cut
41
42
=head1 API
43
44
=head2 Class methods
45
46
=head3 store
47
48
    $server->store;
49
50
Overloaded store method that ensures directory paths end with a forward slash.
51
52
=cut
53
54
sub store {
55
    my ($self) = @_;
56
57
    # Encrypt sensitive data if changed
58
    $self->_encrypt_sensitive_data();
59
60
    # Normalize directory paths
61
    for my $dir_field (qw(download_directory upload_directory)) {
62
        my $dir = $self->$dir_field;
63
        next                            unless $dir && $dir ne '';
64
        $self->$dir_field( $dir . '/' ) unless substr( $dir, -1 ) eq '/';
65
    }
66
67
    # Store
68
    $self->SUPER::store;
69
70
    # Return the updated object including the encrypt_sensitive_data
71
    return $self->discard_changes;
72
}
73
74
=head3 plain_text_password
75
76
    my $password = $server->plain_text_password;
77
78
Returns the decrypted plaintext password.
79
80
=cut
81
82
sub plain_text_password {
83
    my ($self) = @_;
84
    return unless $self->password;
85
    return Koha::Encryption->new->decrypt_hex( $self->password );
86
}
87
88
=head3 plain_text_key
89
90
    my $key = $server->plain_text_key;
91
92
Returns the decrypted plaintext key file.
93
94
=cut
95
96
sub plain_text_key {
97
    my ($self) = @_;
98
    return unless $self->key_file;
99
    return Koha::Encryption->new->decrypt_hex( $self->key_file ) . "\n";
100
}
101
102
=head3 to_api
103
104
    my $json = $transport->to_api;
105
106
Returns a JSON representation of the object suitable for API output,
107
excluding sensitive data.
108
109
=cut
110
111
sub to_api {
112
    my ( $self, $params ) = @_;
113
114
    my $json = $self->SUPER::to_api($params) or return;
115
    delete @{$json}{qw(password key_file)};    # Remove sensitive data
116
117
    return $json;
118
}
119
120
=head3 to_api_mapping
121
122
This method returns the mapping for representing a Koha::File::Transport object
123
on the API.
124
125
=cut
126
127
sub to_api_mapping {
128
    return { id => 'sftp_server_id' };
129
}
130
131
=head3 test_connection
132
133
    $transport->test_connection
134
135
Method to test the connection for the configuration of the current file server
136
137
=cut
138
139
sub test_connection {
140
    my ($self) = @_;
141
142
    $self->connect or return;
143
144
    for my $dir_type (qw(download upload)) {
145
        my $dir = $self->{"${dir_type}_directory"};
146
        next if $dir eq '';
147
148
        $self->change_directory($dir) or return;
149
        $self->list_files()           or return;
150
    }
151
152
    return 1;
153
}
154
155
=head2 Subclass methods
156
157
Interface methods that must be implemented by subclasses
158
159
=head3 connect
160
161
    $transport->connect();
162
163
Method for connecting the current transport to the file server
164
165
=cut
166
167
sub connect {
168
    my ($self) = @_;
169
    die "Subclass must implement connect";
170
}
171
172
=head3 upload_file
173
174
    $transport->upload_file($file);
175
176
Method for uploading a file to the current file server
177
178
=cut
179
180
sub upload_file {
181
    my ( $self, $local_file, $remote_file ) = @_;
182
    die "Subclass must implement upload_file";
183
}
184
185
=head3 download_file
186
187
    $transport->download_file($file);
188
189
Method for downloading a file from the current file server
190
191
=cut
192
193
sub download_file {
194
    my ( $self, $remote_file, $local_file ) = @_;
195
    die "Subclass must implement download_file";
196
}
197
198
=head3 change_directory
199
200
    my $files = $transport->change_directory($path);
201
202
Method for changing the current directory on the connected file server
203
204
=cut
205
206
sub change_directory {
207
    my ( $self, $path ) = @_;
208
    die "Subclass must implement change_directory";
209
}
210
211
=head3 list_files
212
213
    my $files = $transport->list_files($path);
214
215
Method for listing files in the current directory of the connected file server
216
217
=cut
218
219
sub list_files {
220
    my ( $self, $path ) = @_;
221
    die "Subclass must implement list_files";
222
}
223
224
=head2 Internal methods
225
226
=head3 _encrypt_sensitive_data
227
228
Handle encryption of sensitive data
229
230
=cut
231
232
sub _encrypt_sensitive_data {
233
    my ($self) = @_;
234
    my $encryption = Koha::Encryption->new;
235
236
    # Only encrypt if the value has changed (is_changed from Koha::Object)
237
    if ( ( !$self->in_storage || $self->is_changed('password') ) && $self->password ) {
238
        $self->password( $encryption->encrypt_hex( $self->password ) );
239
    }
240
241
    if ( ( !$self->in_storage || $self->is_changed('key_file') ) && $self->key_file ) {
242
        $self->key_file( $encryption->encrypt_hex( _dos2unix( $self->key_file ) ) );
243
    }
244
}
245
246
=head3 _dos2unix
247
248
Return a CR-free string from an input
249
250
=cut
251
252
sub _dos2unix {
253
    my $dosStr = shift;
254
255
    return $dosStr =~ s/\015\012/\012/gr;
256
}
257
258
=head3 _type
259
260
Return type of Object relating to Schema Result
261
262
=cut
263
264
sub _type {
265
    return 'SftpServer';
266
}
267
268
1;
(-)a/Koha/File/Transport/FTP.pm (+183 lines)
Line 0 Link Here
1
package Koha::File::Transport::FTP;
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
use Net::FTP;
20
use Try::Tiny;
21
22
use base qw(Koha::File::Transport);
23
24
=head1 NAME
25
26
Koha::File::Transport::FTP - FTP implementation of file transport
27
28
=head2 Class methods
29
30
=head3 connect
31
32
    my $success = $self->connect;
33
34
Start the FTP transport connect, returns true on success or undefined on failure.
35
36
=cut
37
38
sub connect {
39
    my ($self) = @_;
40
41
    $self->{connection} = Net::FTP->new(
42
        $self->host,
43
        Port    => $self->port,
44
        Timeout => $self->DEFAULT_TIMEOUT,
45
        Passive => $self->passive ? 1 : 0,
46
    ) or return $self->_abort_operation();
47
48
    $self->{connection}->login( $self->user_name, $self->plain_text_password )
49
        or return $self->_abort_operation();
50
51
    $self->add_message(
52
        {
53
            message => "Connect succeeded",
54
            type    => 'success',
55
            payload => { detail => '' }
56
        }
57
    );
58
59
    return 1;
60
}
61
62
=head3 upload_file
63
64
    my $success =  $transport->upload_file($fh);
65
66
Passed a filehandle, this will upload the file to the current directory of the server connection.
67
68
Returns true on success or undefined on failure.
69
70
=cut
71
72
sub upload_file {
73
    my ( $self, $local_file, $remote_file ) = @_;
74
75
    $self->{connection}->put( $local_file, $remote_file )
76
        or return $self->_abort_operation();
77
78
    $self->add_message(
79
        {
80
            message => "Upload succeeded",
81
            type    => 'success',
82
            payload => { detail => '' }
83
        }
84
    );
85
86
    return 1;
87
}
88
89
=head3 download_file
90
91
    my $success =  $transport->download_file($filename);
92
93
Passed a filename, this will download the file from the current directory of the server connection.
94
95
Returns true on success or undefined on failure.
96
97
=cut
98
99
sub download_file {
100
    my ( $self, $remote_file, $local_file ) = @_;
101
102
    $self->{connection}->get( $remote_file, $local_file )
103
        or return $self->_abort_operation();
104
105
    $self->add_message(
106
        {
107
            message => "Download succeeded",
108
            type    => 'success',
109
            payload => { detail => '' }
110
        }
111
    );
112
113
    return 1;
114
}
115
116
=head3 change_directory
117
118
    my $success = $server->change_directory($directory);
119
120
Passed a directory name, this will change the current directory of the server connection.
121
122
Returns true on success or undefined on failure.
123
124
=cut
125
126
sub change_directory {
127
    my ( $self, $remote_directory ) = @_;
128
129
    $self->{connection}->cwd($remote_directory) or $self->_abort_operation();
130
131
    $self->add_message(
132
        {
133
            message => "Changed directory succeeded",
134
            type    => 'success',
135
            payload => { detail => '' }
136
        }
137
    );
138
139
    return 1;
140
}
141
142
=head3 list_files
143
144
    my @files = $server->list_files;
145
146
Returns an array of filenames found in the current directory of the server connection.
147
148
=cut
149
150
sub list_files {
151
    my ($self) = @_;
152
    my $file_list = $self->{connection}->ls or return $self->_abort_operation();
153
154
    $self->add_message(
155
        {
156
            message => "Listing files succeeded",
157
            type    => 'success',
158
            payload => { detail => '' }
159
        }
160
    );
161
162
    return $file_list;
163
}
164
165
sub _abort_operation {
166
    my ( $self, $message ) = @_;
167
168
    $self->add_message(
169
        {
170
            message => $self->{connection} ? $self->{connection}->message : $@,
171
            type    => 'error',
172
            payload => { detail => $self->{connection} ? $self->{connection}->status : '' }
173
        }
174
    );
175
176
    if ( $self->{connection} ) {
177
        $self->{connection}->abort;
178
    }
179
180
    return;
181
}
182
183
1;
(-)a/Koha/File/Transport/SFTP.pm (+142 lines)
Line 0 Link Here
1
package Koha::File::Transport::SFTP;
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 Koha::Logger;
21
22
use Net::SFTP::Foreign;
23
use Try::Tiny;
24
25
use base qw(Koha::File::Transport);
26
27
=head1 NAME
28
29
Koha::File::Transport::SFTP - SFTP implementation of file transport
30
31
=cut
32
33
sub connect {
34
    my ($self) = @_;
35
36
    $self->{connection} = Net::SFTP::Foreign->new(
37
        host     => $self->host,
38
        user     => $self->user_name,
39
        password => $self->plain_text_password,
40
        timeout  => $self->DEFAULT_TIMEOUT,
41
        more     => [qw(-o StrictHostKeyChecking=no)],
42
    );
43
    $self->{connection}->die_on_error("SFTP failure for remote host");
44
45
    return $self->_abort_operation() if ( $self->{connection}->error );
46
47
    $self->add_message(
48
        {
49
            message => $self->{connection}->status,
50
            type    => 'success',
51
            payload => { detail => '' }
52
        }
53
    );
54
55
    return 1;
56
}
57
58
sub upload_file {
59
    my ( $self, $local_file, $remote_file ) = @_;
60
61
    my $logger = Koha::Logger->get_logger();
62
63
    $self->{connection}->put( $local_file, $remote_file ) or return $self->_abort_operation();
64
65
    $self->add_message(
66
        {
67
            message => $self->{connection}->status,
68
            type    => 'success',
69
            payload => { detail => '' }
70
        }
71
    );
72
73
    return 1;
74
}
75
76
sub download_file {
77
    my ( $self, $remote_file, $local_file ) = @_;
78
79
    $self->{connection}->get( $remote_file, $local_file ) or return $self->_abort_operation();
80
81
    $self->add_message(
82
        {
83
            message => $self->{connection}->status,
84
            type    => 'success',
85
            payload => { detail => '' }
86
        }
87
    );
88
89
    return 1;
90
}
91
92
sub change_directory {
93
    my ( $self, $remote_directory ) = @_;
94
95
    $self->{connection}->setcwd($remote_directory) or return $self->_abort_operation();
96
97
    $self->add_message(
98
        {
99
            message => $self->{connection}->status,
100
            type    => 'success',
101
            payload => { detail => '' }
102
        }
103
    );
104
105
    return 1;
106
}
107
108
sub list_files {
109
    my ($self) = @_;
110
111
    my $file_list = $self->{connection}->ls or return $self->_abort_operation();
112
113
    $self->add_message(
114
        {
115
            message => $self->{connection}->status,
116
            type    => 'success',
117
            payload => { detail => '' }
118
        }
119
    );
120
121
    return $file_list;
122
}
123
124
sub _abort_operation {
125
    my ($self) = @_;
126
127
    $self->add_message(
128
        {
129
            message => $self->{connection}->error,
130
            type    => 'error',
131
            payload => { detail => $self->{connection} ? $self->{connection}->status : '' }
132
        }
133
    );
134
135
    if ( $self->{connection} ) {
136
        $self->{connection}->abort;
137
    }
138
139
    return;
140
}
141
142
1;
(-)a/Koha/File/Transports.pm (+86 lines)
Line 0 Link Here
1
package Koha::File::Transports;
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 Koha::Database;
21
use Koha::Exceptions;
22
23
use Koha::File::Transport::SFTP;
24
use Koha::File::Transport::FTP;
25
26
use base qw(Koha::Objects);
27
28
=head1 NAME
29
30
Koha::File::Transports - Koha File Transports Object set class
31
32
=head1 API
33
34
=head2 Internal methods
35
36
=head3 _type
37
38
Return type of object, relating to Schema ResultSet
39
40
=cut
41
42
sub _type {
43
    return 'SftpServer';
44
}
45
46
=head3 _polymorphic_field
47
48
Return the field in the table that defines the polymorphic class to be built
49
50
=cut
51
52
sub _polymorphic_field {
53
    return 'transport';    # This field defines which subclass to use
54
}
55
56
=head3 _polymorphic_map
57
58
Return the mapping for field value to class name for the polymorphic class
59
60
=cut
61
62
sub _polymorphic_map {
63
    return {
64
        sftp => 'Koha::File::Transport::SFTP',
65
        ftp  => 'Koha::File::Transport::FTP',
66
    };
67
}
68
69
=head3 object_class
70
71
Return object class dynamically based on transport
72
73
=cut
74
75
sub object_class {
76
    my ( $self, $object ) = @_;
77
78
    return 'Koha::File::Transport' unless $object;
79
80
    my $field = $self->_polymorphic_field;
81
    my $map   = $self->_polymorphic_map;
82
83
    return $map->{ lc( $object->$field ) } || 'Koha::File::Transport';
84
}
85
86
1;
(-)a/t/db_dependent/Koha/File/Transport.t (+173 lines)
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;
(-)a/t/db_dependent/Koha/File/Transport/FTP.t (+93 lines)
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::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 'connect() tests' => sub {
33
    plan tests => 1;
34
35
    my $transport = $builder->build_object(
36
        {
37
            class => 'Koha::File::Transports',
38
            value => { transport => 'ftp', password => 'testpass' }
39
        }
40
    );
41
42
    can_ok( $transport, 'connect' );
43
};
44
45
subtest 'upload_file() tests' => sub {
46
    plan tests => 1;
47
    my $transport = $builder->build_object(
48
        {
49
            class => 'Koha::File::Transports',
50
            value => { transport => 'ftp', password => 'testpass' }
51
        }
52
    );
53
54
    can_ok( $transport, 'upload_file' );
55
};
56
57
subtest 'download_file() tests' => sub {
58
    plan tests => 1;
59
    my $transport = $builder->build_object(
60
        {
61
            class => 'Koha::File::Transports',
62
            value => { transport => 'ftp', password => 'testpass' }
63
        }
64
    );
65
66
    can_ok( $transport, 'download_file' );
67
};
68
69
subtest 'change_directory() tests' => sub {
70
    plan tests => 1;
71
    my $transport = $builder->build_object(
72
        {
73
            class => 'Koha::File::Transports',
74
            value => { transport => 'ftp', password => 'testpass' }
75
        }
76
    );
77
78
    can_ok( $transport, 'change_directory' );
79
};
80
81
subtest 'list_files() tests' => sub {
82
    plan tests => 1;
83
    my $transport = $builder->build_object(
84
        {
85
            class => 'Koha::File::Transports',
86
            value => { transport => 'ftp', password => 'testpass' }
87
        }
88
    );
89
90
    can_ok( $transport, 'list_files' );
91
};
92
93
1;
(-)a/t/db_dependent/Koha/File/Transport/SFTP.t (-1 / +94 lines)
Line 0 Link Here
0
- 
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::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 'connect() tests' => sub {
33
    plan tests => 2;
34
35
    my $transport = $builder->build_object(
36
        {
37
            class => 'Koha::File::Transports',
38
            value => { transport => 'sftp', password => 'testpass' }
39
        }
40
    );
41
42
    can_ok( $transport, 'connect' );
43
    dies_ok { $transport->connect } 'connect() should die without proper setup';
44
};
45
46
subtest 'upload_file() tests' => sub {
47
    plan tests => 1;
48
    my $transport = $builder->build_object(
49
        {
50
            class => 'Koha::File::Transports',
51
            value => { transport => 'sftp', password => 'testpass' }
52
        }
53
    );
54
55
    can_ok( $transport, 'upload_file' );
56
};
57
58
subtest 'download_file() tests' => sub {
59
    plan tests => 1;
60
    my $transport = $builder->build_object(
61
        {
62
            class => 'Koha::File::Transports',
63
            value => { transport => 'sftp', password => 'testpass' }
64
        }
65
    );
66
67
    can_ok( $transport, 'download_file' );
68
};
69
70
subtest 'change_directory() tests' => sub {
71
    plan tests => 1;
72
    my $transport = $builder->build_object(
73
        {
74
            class => 'Koha::File::Transports',
75
            value => { transport => 'sftp', password => 'testpass' }
76
        }
77
    );
78
79
    can_ok( $transport, 'change_directory' );
80
};
81
82
subtest 'list_files() tests' => sub {
83
    plan tests => 1;
84
    my $transport = $builder->build_object(
85
        {
86
            class => 'Koha::File::Transports',
87
            value => { transport => 'sftp', password => 'testpass' }
88
        }
89
    );
90
91
    can_ok( $transport, 'list_files' );
92
};
93
94
1;

Return to bug 39190