Debian Codegraph @

lemonldap-ng-common/t/42-Common-EmailTransport.t

raw
1 use warnings;
2 use strict;
3 use Test::More;
4
5 BEGIN { use_ok('Lemonldap::NG::Common::EmailTransport') }
6
7 my %base = (
8 SMTPServer => 'smtp.example.com',
9 SMTPPort => 587,
10 SMTPAuthUser => 'dwho',
11 SMTPAuthPass => 'dwho',
12 );
13
14 # Email::Sender provides sasl_authenticator since 1.300032 only, and
15 # Authen::SASL is an optional dependency
16 my $saslSupported = Email::Sender::Transport::SMTP->can('sasl_authenticator')
17 && eval { require Authen::SASL; 1 };
18
19 # No SMTP server -> no transport (local sendmail is used)
20 is( Lemonldap::NG::Common::EmailTransport->new( {} ),
21 undef, 'No transport without SMTPServer' );
22
23 # Default: credentials are given to Net::SMTP which chooses the mechanism
24 my $transport = Lemonldap::NG::Common::EmailTransport->new( {%base} );
25 is( $transport->sasl_username, 'dwho', 'sasl_username is set' );
26 is( $transport->sasl_password, 'dwho', 'sasl_password is set' );
27
28 # SMTPAuthMech is ignored when no user is set
29 $transport = Lemonldap::NG::Common::EmailTransport->new(
30 { %base, SMTPAuthUser => '', SMTPAuthMech => 'PLAIN' } );
31 ok( !$transport->sasl_username, 'No sasl_username without SMTPAuthUser' );
32
33 SKIP: {
34 skip 'Email::Sender or Authen::SASL is too old to select the mechanism', 7
35 unless $saslSupported;
36
37 $transport = Lemonldap::NG::Common::EmailTransport->new( {%base} );
38 ok( !$transport->sasl_authenticator, 'No SASL authenticator by default' );
39
40 # SMTPAuthMech: an Authen::SASL object restricted to the wanted
41 # mechanism(s) is used instead
42 $transport = Lemonldap::NG::Common::EmailTransport->new(
43 { %base, SMTPAuthMech => 'PLAIN LOGIN CRAM-MD5' } );
44 ok( !$transport->sasl_username, 'sasl_username is not set' );
45 my $sasl = $transport->sasl_authenticator;
46 ok( $sasl, 'SASL authenticator is set' );
47 is( $sasl->mechanism, 'PLAIN LOGIN CRAM-MD5', 'Mechanisms are restricted' );
48
49 # Only the wanted mechanism is used
50 $transport = Lemonldap::NG::Common::EmailTransport->new(
51 { %base, SMTPAuthMech => 'PLAIN' } );
52 my $client =
53 $transport->sasl_authenticator->client_new( 'smtp', 'smtp.example.com' );
54 is( $client->mechanism, 'PLAIN', 'Chosen mechanism' );
55
56 $transport = Lemonldap::NG::Common::EmailTransport->new(
57 { %base, SMTPAuthUser => '', SMTPAuthMech => 'PLAIN' } );
58 ok( !$transport->sasl_authenticator,
59 'No SASL object without SMTPAuthUser' );
60
61 my ( $res, $msg ) = Lemonldap::NG::Common::EmailTransport->configTest(
62 { %base, SMTPAuthMech => 'PLAIN' } );
63 ok( !$msg, 'No warning in configTest' ) or diag($msg);
64 }
65
66 done_testing();