Bugzilla – Attachment 109007 Details for
Bug 22343
Add configuration options for SMTP servers
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 22343: Add classes for handling SMTP servers
Bug-22343-Add-classes-for-handling-SMTP-servers.patch (text/plain), 10.17 KB, created by
Tomás Cohen Arazi (tcohen)
on 2020-08-24 15:31:34 UTC
(
hide
)
Description:
Bug 22343: Add classes for handling SMTP servers
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2020-08-24 15:31:34 UTC
Size:
10.17 KB
patch
obsolete
>From 31bd7d605ff6cebf3e5f3cf1c27ea31c525dcbf3 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Tue, 28 Jul 2020 12:35:26 -0300 >Subject: [PATCH] Bug 22343: Add classes for handling SMTP servers > >This patch introduces classes to handle SMTP servers. It is done in a >way that a default server can be set, and then per-library ones can be >set. All should be done with Koha::SMTP::Servers methods, that take care >of overwriting library-specific configs, and handle the defaults >retrieval and setting correctly. > >To test: >1. Apply this patches >2. Run: > $ kshell > $ perl installer/data/mysql/updatedatabase.pl >=> SUCCESS: Atomic update is ok, smtp_servers table created >3. Run: > k$ prove t/db_dependent/Koha/SMTP/ >=> SUCCESS: Tests pass! >4. Sign off :-D >--- > Koha/SMTP/Server.pm | 126 +++++++++++++++++++++++++++++ > Koha/SMTP/Servers.pm | 95 ++++++++++++++++++++++ > t/db_dependent/Koha/SMTP/Server.t | 72 +++++++++++++++++ > t/db_dependent/Koha/SMTP/Servers.t | 52 ++++++++++++ > 4 files changed, 345 insertions(+) > create mode 100644 Koha/SMTP/Server.pm > create mode 100644 Koha/SMTP/Servers.pm > create mode 100644 t/db_dependent/Koha/SMTP/Server.t > create mode 100644 t/db_dependent/Koha/SMTP/Servers.t > >diff --git a/Koha/SMTP/Server.pm b/Koha/SMTP/Server.pm >new file mode 100644 >index 0000000000..dd6cc9c934 >--- /dev/null >+++ b/Koha/SMTP/Server.pm >@@ -0,0 +1,126 @@ >+package Koha::SMTP::Server; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Koha::Database; >+use Koha::Exceptions::Object; >+use Koha::SMTP::Servers; >+ >+use Email::Sender::Transport::SMTP; >+ >+use base qw(Koha::Object); >+ >+=head1 NAME >+ >+Koha::SMTP::Server - Koha SMTP Server Object class >+ >+=head1 API >+ >+=head2 Class methods >+ >+=head3 transport >+ >+ my $transport = $smtp_server->transport; >+ sendmail( $message, { transport => $transport } ); >+ >+Returns an I<Email::Sender::Transport::SMTP> object that can be used directly >+with Email::Sender. >+ >+=cut >+ >+sub transport { >+ my ($self) = @_; >+ >+ my $params = { >+ host => $self->host, >+ port => $self->port, >+ }; >+ >+ $params->{ssl} = $self->ssl_mode >+ unless $self->ssl_mode eq 'disabled'; >+ >+ $params->{timeout} = $self->timeout >+ if $self->timeout; >+ >+ $params->{sasl_username} = $self->user_name >+ if $self->user_name; >+ >+ $params->{sasl_password} = $self->password >+ if $self->password; >+ >+ >+ my $transport = Email::Sender::Transport::SMTP->new( $params ); >+ >+ return $transport; >+} >+ >+=head3 libraries >+ >+ my $libraries = $smtp_server->libraries >+ >+Accessor to get the list of libraries that are linked to this SMTP server >+ >+=cut >+ >+sub libraries { >+ my ($self) = @_; >+ >+ my @library_ids = $self->_result->library_smtp_servers->get_column('library_id')->all; >+ return Koha::Libraries->search( { branchcode => { -in => \@library_ids } } ); >+} >+ >+=head3 is_system_default >+ >+ if ( $smtp_server->is_system_default ) { ... } >+ >+Method that tells if a Koha::SMTP::Server is the hardcoded one. >+ >+=cut >+ >+sub is_system_default { >+ my ($self) = @_; >+ >+ return $self->{_is_system_default}; >+} >+ >+=head3 to_api_mapping >+ >+This method returns the mapping for representing a Koha::SMTP::Server object >+on the API. >+ >+=cut >+ >+sub to_api_mapping { >+ return { >+ id => 'smtp_server_id' >+ }; >+} >+ >+=head2 Internal methods >+ >+=head3 _type >+ >+Return type of Object relating to Schema ResultSet >+ >+=cut >+ >+sub _type { >+ return 'SmtpServer'; >+} >+ >+1; >diff --git a/Koha/SMTP/Servers.pm b/Koha/SMTP/Servers.pm >new file mode 100644 >index 0000000000..a4354c3775 >--- /dev/null >+++ b/Koha/SMTP/Servers.pm >@@ -0,0 +1,95 @@ >+package Koha::SMTP::Servers; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Koha::Database; >+use Koha::Exceptions; >+ >+use Koha::SMTP::Server; >+ >+use base qw(Koha::Objects); >+ >+=head1 NAME >+ >+Koha::SMTP::Servers - Koha SMTP Server Object set class >+ >+=head1 API >+ >+=head2 Class methods >+ >+=head3 get_default >+ >+ my $server = Koha::SMTP::Servers->new->get_default; >+ >+Returns the default I<Koha::SMTP::Server> object. >+ >+=cut >+ >+sub get_default { >+ my ($self) = @_; >+ >+ my $default = Koha::SMTP::Server->new( $self->default_setting ); >+ $default->{_is_system_default} = 1; >+ return $default; >+} >+ >+=head2 Internal methods >+ >+=head3 _type >+ >+Return type of object, relating to Schema ResultSet >+ >+=cut >+ >+sub _type { >+ return 'SmtpServer'; >+} >+ >+=head3 default_setting >+ >+ my $hash = Koha::SMTP::Servers::default_setting; >+ >+Returns the default setting that is to be used when no user-defined default >+SMTP server is provided >+ >+=cut >+ >+sub default_setting { >+ return { >+ name => 'localhost', >+ host => 'localhost', >+ port => 25, >+ timeout => 120, >+ ssl_mode => 'disabled', >+ user_name => undef, >+ password => undef, >+ debug => 0 >+ }; >+} >+ >+=head3 object_class >+ >+Return object class >+ >+=cut >+ >+sub object_class { >+ return 'Koha::SMTP::Server'; >+} >+ >+1; >diff --git a/t/db_dependent/Koha/SMTP/Server.t b/t/db_dependent/Koha/SMTP/Server.t >new file mode 100644 >index 0000000000..1e86fe4f8d >--- /dev/null >+++ b/t/db_dependent/Koha/SMTP/Server.t >@@ -0,0 +1,72 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Test::More tests => 2; >+use Test::Exception; >+use Test::Warn; >+ >+use Koha::SMTP::Servers; >+ >+use t::lib::TestBuilder; >+use t::lib::Mocks; >+ >+my $schema = Koha::Database->new->schema; >+my $builder = t::lib::TestBuilder->new; >+ >+subtest 'transport() tests' => sub { >+ >+ plan tests => 4; >+ >+ $schema->storage->txn_begin; >+ >+ my $server = $builder->build_object( >+ { >+ class => 'Koha::SMTP::Servers', >+ value => { ssl_mode => 'disabled' } >+ } >+ ); >+ >+ my $transport = $server->transport; >+ >+ is( ref($transport), 'Email::Sender::Transport::SMTP', 'Type is correct' ); >+ is( $transport->ssl, 0, 'SSL is not set' ); >+ >+ $server->set({ ssl_mode => 'ssl' })->store; >+ $transport = $server->transport; >+ >+ is( ref($transport), 'Email::Sender::Transport::SMTP', 'Type is correct' ); >+ is( $transport->ssl, 'ssl', 'SSL is set' ); >+ >+ $schema->storage->txn_rollback; >+}; >+ >+subtest 'is_system_default() tests' => sub { >+ >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $smtp_server = $builder->build_object({ class => 'Koha::SMTP::Servers' }); >+ ok( !$smtp_server->is_system_default, 'A generated server is not the system default' ); >+ >+ my $system_default_server = Koha::SMTP::Servers->get_default; >+ ok( $system_default_server->is_system_default, 'The server returned by get_default is the system default' ); >+ >+ $schema->storage->txn_rollback; >+}; >diff --git a/t/db_dependent/Koha/SMTP/Servers.t b/t/db_dependent/Koha/SMTP/Servers.t >new file mode 100644 >index 0000000000..adefdaed12 >--- /dev/null >+++ b/t/db_dependent/Koha/SMTP/Servers.t >@@ -0,0 +1,52 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use Test::More tests => 1; >+ >+use Koha::SMTP::Servers; >+ >+use t::lib::TestBuilder; >+use t::lib::Mocks; >+ >+my $schema = Koha::Database->new->schema; >+my $builder = t::lib::TestBuilder->new; >+ >+subtest 'get_default() tests' => sub { >+ >+ plan tests => 3; >+ >+ $schema->storage->txn_begin; >+ >+ my $server = Koha::SMTP::Servers->get_default; >+ is( ref($server), 'Koha::SMTP::Server', >+ 'An object of the right type is returned' ); >+ >+ ok( !$server->in_storage, >+ 'The default server is correctly retrieved' ); >+ >+ my $unblessed_server = $server->unblessed; >+ delete $unblessed_server->{id}; >+ is_deeply( >+ $unblessed_server, >+ Koha::SMTP::Servers::default_setting, >+ 'The default setting is returned if no user-defined default' >+ ); >+ >+ $schema->storage->txn_rollback; >+}; >-- >2.28.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 22343
:
107598
|
107599
|
107600
|
107601
|
107602
|
107603
|
107604
|
107605
|
107987
|
107988
|
107989
|
107990
|
107991
|
107992
|
107993
|
107994
|
107995
|
107996
|
107997
|
107998
|
107999
|
108009
|
108107
|
108108
|
108109
|
108110
|
108111
|
108112
|
108113
|
108114
|
108115
|
108116
|
108117
|
108118
|
108119
|
108131
|
108132
|
108133
|
108134
|
108135
|
108136
|
108137
|
108138
|
108139
|
108140
|
108141
|
108142
|
108143
|
108529
|
108537
|
108538
|
108539
|
108540
|
108541
|
108542
|
108543
|
108544
|
108545
|
108546
|
108547
|
108548
|
108549
|
108609
|
108638
|
108642
|
108643
|
108644
|
108645
|
108646
|
108647
|
108648
|
108649
|
108650
|
108651
|
108652
|
108653
|
108654
|
108655
|
108656
|
108715
|
108716
|
109003
|
109004
|
109005
|
109006
|
109007
|
109008
|
109009
|
109010
|
109011
|
109012
|
109013
|
109014
|
109015
|
109016
|
109017
|
109018
|
109155
|
109156
|
109157
|
109158
|
109159
|
109160
|
109161
|
109162
|
109163
|
109164
|
109165
|
109166
|
109167
|
109168
|
109169
|
109170
|
109601
|
109602
|
109603
|
109604
|
109605
|
109606
|
109607
|
109608
|
109609
|
109610
|
109611
|
109612
|
109613
|
109614
|
109615
|
109616
|
109723
|
109724
|
109725
|
109726
|
109727
|
109728
|
109729
|
109730
|
109731
|
109732
|
109733
|
109734
|
109735
|
109736
|
109737
|
109738
|
109739
|
109740
|
109787
|
109788
|
111048
|
111087
|
111088
|
111089
|
111090
|
111154
|
111165
|
111166
|
111175
|
111176
|
111213
|
111215
|
111216
|
111442
|
111482
|
113240
|
113827
|
113847