1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
use strict;
use warnings;
use utf8;
use List::Util qw();
use vars (qw($Self));
use Kernel::System::Email::SMTP;
# The tests presented here try to ensure that the communication-log entries
# keep the correct status after some predefined situations.
# Some 'magic' techniques are used so we could fake an SMTP client/server connection environment:
# - localizing variables
# - local overwriting of package methods
# - definition of inline packages
# - autoload handler ( when the called method doesn't exist )
# This hash represents the fake environment for the SMTP, think about %ENV,
# it'll work more or less the same way.
my %FakeSMTPEnv = (
'message' => '',
'code' => 250,
'data' => 1,
'connect' => 1,
);
no strict 'refs'; ## no critic
# Overwrite the OTRS Email::SMTP check method to use our fake smtp client,
# but make this change local to the unit test scope, as you can see, it also
# makes use of the %FakeSMTPEnv.
local *{'Kernel::System::Email::SMTP::Check'} = sub {
my ( $Self, %Param ) = @_;
# Define an inline package/class that will work as our fake SMTP client.
# This package uses autoload to handle undefined methods, and when
# that happen, it'll check if the FakeSMTPEnv has an attribute with the same
# name and returns it, otherwise always returns True to ensure that the code
# that will use this object continues as everything is ok.
package FakeSMTP { ## no critic
our $AUTOLOAD;
sub new {
my $Class = shift;
return bless( {}, $Class, );
}
sub AUTOLOAD {
my $Self = shift;
my ($Method) = ( $AUTOLOAD =~ m/::([^:]+)$/i );
if ( !$Method || $Method eq 'DESTROY' ) {
return;
}
if ( exists $FakeSMTPEnv{$Method} ) {
my $Value = $FakeSMTPEnv{$Method};
my $ValueRef = ref $Value;
if ( $ValueRef && $ValueRef eq 'CODE' ) {
return $Value->();
}
return $Value;
}
return 1;
}
}
my $CommunicationLogObject = $Param{CommunicationLogObject};
$CommunicationLogObject->ObjectLogStart(
ObjectLogType => 'Connection',
);
if ( !$FakeSMTPEnv{'connect'} ) {
$CommunicationLogObject->ObjectLogStop(
ObjectLogType => 'Connection',
Status => 'Failed',
);
return (
Success => 0,
ErrorMessage => $FakeSMTPEnv{'message'},
);
}
$CommunicationLogObject->ObjectLogStop(
ObjectLogType => 'Connection',
Status => 'Successful',
);
return (
Success => 1,
SMTP => $Self->_GetSMTPSafeWrapper(
SMTP => FakeSMTP->new(),
),
);
};
use strict 'refs';
$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Helper' => {
RestoreDatabase => 1,
UseTmpArticleDir => 1,
},
);
my $HelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
$HelperObject->ConfigSettingChange(
Key => 'CheckEmailAddresses',
Value => 0,
);
$HelperObject->ConfigSettingChange(
Key => 'SendmailModule',
Value => 'Kernel::System::Email::SMTP',
);
my $UserObject = $Kernel::OM->Get('Kernel::System::User');
my $EmailObject = $Kernel::OM->Get('Kernel::System::Email');
my $MailQueueObject = $Kernel::OM->Get('Kernel::System::MailQueue');
my $MailQueueItemMaxAttempts = $Kernel::OM->Get('Kernel::Config')->Get('MailQueue')->{ItemMaxAttempts};
my ( $UserLogin, $UserID ) = $HelperObject->TestUserCreate(
Groups => [ 'admin', 'users' ],
);
my $SendEmail = sub {
my %Param = @_;
# Generate the mail and queue it.
$EmailObject->Send( %Param, );
# Get last item in the queue.
my $Items = $MailQueueObject->List();
$Items = [ sort { $b->{ID} <=> $a->{ID} } @{$Items} ];
my $LastItem = $Items->[0];
return ( $LastItem, $MailQueueObject->Send( %{$LastItem} ) );
};
my $MailQueueSend = sub {
# Send the queued emails.
my @Processed = ();
my $Items = $MailQueueObject->List();
for my $Item ( @{$Items} ) {
$MailQueueObject->Send(
%{$Item},
Force => 1,
);
push @Processed, $Item;
}
return \@Processed;
};
my $CheckForQueueItem = sub {
my %Param = @_;
my $TestBaseMessage = $Param{TestBaseMessage};
my $SMTPErrorType = $Param{SMTPErrorType};
my $MailQueueItemSent = $Param{MailQueueItemSent};
my $StillInQueue = $Param{StillInQueue};
# Check for the queue item.
my $MailQueueItems = $MailQueueObject->List(
ID => $MailQueueItemSent->{ID},
);
$Self->$StillInQueue(
scalar @{$MailQueueItems},
sprintf(
'%s, %s in queue',
$TestBaseMessage,
$StillInQueue eq 'True' ? 'still' : 'not',
),
);
return;
};
my $CheckForCommunicationLog = sub {
my %Param = @_;
my $TestBaseMessage = $Param{TestBaseMessage};
my $MailQueueItemSent = $Param{MailQueueItemSent};
my $CommunicationLogStatus = $Param{CommunicationLogStatus};
my $CommunicationLogDBObj = $Kernel::OM->Create(
'Kernel::System::CommunicationLog::DB',
);
my $ComLogLookupInfo = $CommunicationLogDBObj->ObjectLookupGet(
ObjectLogType => 'Message',
TargetObjectType => 'MailQueueItem',
TargetObjectID => $MailQueueItemSent->{ID},
);
# Get the communication info.
my $Communication = $CommunicationLogDBObj->CommunicationGet(
CommunicationID => $ComLogLookupInfo->{CommunicationID},
);
# Get all the communication objects.
my $CommunicationLogObjects = [
reverse @{
$CommunicationLogDBObj->ObjectLogList(
CommunicationID => $ComLogLookupInfo->{CommunicationID},
)
}
];
my $CommunicationLogConnection
= List::Util::first { $_->{ObjectLogType} eq 'Connection' } @{$CommunicationLogObjects};
$Self->True(
$CommunicationLogConnection->{ObjectLogStatus} eq $CommunicationLogStatus->{Connection},
$TestBaseMessage
. sprintf( ", communication log connection with status '%s'", $CommunicationLogStatus->{Connection} ),
);
my $CommunicationLogMessage = List::Util::first {
$_->{ObjectLogID} == $ComLogLookupInfo->{ObjectLogID}
}
@{$CommunicationLogObjects};
$Self->True(
$CommunicationLogMessage->{ObjectLogStatus} eq $CommunicationLogStatus->{Message},
$TestBaseMessage
. sprintf( ", communication log message with status '%s'", $CommunicationLogStatus->{Message} ),
);
$Self->True(
$Communication->{Status} eq $CommunicationLogStatus->{Communication},
$TestBaseMessage
. sprintf( ", communication log with status '%s'", $CommunicationLogStatus->{Communication} ),
);
return;
};
my $CheckForQueueNotifications = sub {
my %Param = @_;
my $ArticleID = $Param{ArticleID};
# Get all the queue items that aren't related to an article.
my $MailQueueItems = $MailQueueObject->List();
my @Notifications = grep { !$_->{ArticleID} } @$MailQueueItems;
$Self->True(
scalar @Notifications,
'Notifications were queued.',
);
return;
};
my @Tests = (
{
Name => 'Email with invalid recipient rejected (SMTP %s)',
FakeSMTPEnv => {
'data' => 0,
'code' => 513, # SMTP permanent error.
'message' => 'bad recipient',
},
Type => 'Simple',
ExpectedSendStatus => 'Failed',
CommunicationLogStatus => {
Message => 'Failed',
Communication => 'Failed',
Connection => 'Successful',
},
StillInQueue => 'False',
},
{
Name => 'Email with invalid recipient rejected (SMTP %s)',
FakeSMTPEnv => {
'data' => 0,
'code' => 451, # SMTP temporary error.
'message' => 'Requested action aborted: local error in processing',
},
Type => 'Simple',
ExpectedSendStatus => 'Failed',
CommunicationLogStatus => {
Message => 'Processing',
Communication => 'Processing',
Connection => 'Successful',
},
StillInQueue => 'True',
},
{
Name => 'Article with invalid recipient rejected (SMTP %s)',
FakeSMTPEnv => {
'data' => 0,
'code' => 513, # SMTP permanent error.
'message' => 'bad recipient',
},
Type => 'Article',
TransmissionError => 'True',
CommunicationLogStatus => {
Message => 'Failed',
Communication => 'Failed',
Connection => 'Successful',
},
StillInQueue => 'False',
},
{
Name => 'Article with invalid recipient rejected (SMTP %s)',
FakeSMTPEnv => {
'data' => 0,
'code' => 451, # SMTP temporary error.
'message' => 'Requested action aborted: local error in processing',
},
Type => 'Article',
TransmissionError => 'False',
CommunicationLogStatus => {
Message => 'Processing',
Communication => 'Processing',
Connection => 'Successful',
},
StillInQueue => 'True',
},
{
Name => 'Simple Email, Connection to smtp server failed (SMTP %s)',
FakeSMTPEnv => {
'connect' => 0,
'message' => 'couldnt connect to the smtp server',
},
Type => 'Simple',
ExpectedSendStatus => 'Failed',
CommunicationLogStatus => {
Message => 'Processing',
Communication => 'Processing',
Connection => 'Failed',
},
StillInQueue => 'True',
},
{
Name => 'Article, Connection to smtp server failed (SMTP %s)',
FakeSMTPEnv => {
'connect' => 0,
'message' => 'couldnt connect to the smtp server',
},
Type => 'Article',
TransmissionError => 'False',
CommunicationLogStatus => {
Message => 'Processing',
Communication => 'Processing',
Connection => 'Failed',
},
StillInQueue => 'True',
},
{
Name => 'Article, Connection to smtp server failed (SMTP %s)',
FakeSMTPEnv => {
'connect' => 0,
'message' => 'couldnt connect to the smtp server',
},
Type => 'Article',
TransmissionError => 'False',
TransmissionErrorAttempt => {
$MailQueueItemMaxAttempts => 'True',
},
CommunicationLogStatus => {
Message => 'Processing',
Communication => 'Processing',
Connection => 'Failed',
$MailQueueItemMaxAttempts => {
Message => 'Failed',
Communication => 'Failed',
Connection => 'Failed',
},
},
Attempts => $MailQueueItemMaxAttempts,
StillInQueue => 'True',
StillInQueueAttempt => {
$MailQueueItemMaxAttempts => 'False',
},
},
{
Name => 'Email send throw exception',
FakeSMTPEnv => {
'data' => sub { die 'dummy exception'; },
},
Type => 'Simple',
ExpectedSendStatus => 'Failed',
CommunicationLogStatus => {
Message => 'Processing',
Communication => 'Processing',
Connection => 'Successful',
},
StillInQueue => 'True',
},
{
Name => 'Email successfully sent',
Type => 'Simple',
ExpectedSendStatus => 'Success',
CommunicationLogStatus => {
Message => 'Successful',
Communication => 'Successful',
Connection => 'Successful',
},
StillInQueue => 'False',
},
{
Name => 'Article successfully sent',
Type => 'Article',
ExpectedSendStatus => 'Success',
CommunicationLogStatus => {
Message => 'Successful',
Communication => 'Successful',
Connection => 'Successful',
},
StillInQueue => 'False',
TransmissionError => 'False',
},
);
TEST:
for my $Test (@Tests) {
# Ensure mail queue is empty.
$MailQueueObject->Delete();
# Set fake smtp environment.
my %TestFakeSMTPEnv = (
%FakeSMTPEnv,
%{ $Test->{FakeSMTPEnv} || {} },
);
# Change the client environment according to the test,
# these changes are local to the current scope (the for).
local $FakeSMTPEnv{'data'} = $TestFakeSMTPEnv{'data'};
local $FakeSMTPEnv{'code'} = $TestFakeSMTPEnv{'code'};
local $FakeSMTPEnv{'message'} = $TestFakeSMTPEnv{'message'};
local $FakeSMTPEnv{'connect'} = $TestFakeSMTPEnv{'connect'};
# SMTP permanent / temporary label.
my $FakeSMTPCode = $Test->{FakeSMTPEnv}->{'code'};
my $SMTPErrorType = $FakeSMTPCode && $FakeSMTPCode =~ m/^5/i ? 'permanent' : 'temporary';
# Build the full test base name.
my $TestBaseMessage = sprintf $Test->{Name}, $SMTPErrorType;
if ( $Test->{Type} eq 'Simple' ) {
# Just try to send a simple mail.
my ( $ItemSent, $SendResult ) = $SendEmail->(
From => 'john.smith@example.com',
To => 'john.smith2@example.com',
Subject => 'some subject',
Body => 'Some Body',
MimeType => 'text/html',
Charset => 'utf8',
);
$Self->True(
$SendResult->{Status} eq $Test->{ExpectedSendStatus},
$TestBaseMessage,
);
$CheckForQueueItem->(
TestBaseMessage => $TestBaseMessage,
SMTPErrorType => $SMTPErrorType,
StillInQueue => $Test->{StillInQueue},
MailQueueItemSent => $ItemSent,
);
# Test the communication log.
$CheckForCommunicationLog->(
TestBaseMessage => $TestBaseMessage,
MailQueueItemSent => $ItemSent,
CommunicationLogStatus => $Test->{CommunicationLogStatus},
);
next TEST;
}
# Try to send an article.
my $ArticleData = $Test->{Article};
my $TicketData = delete $ArticleData->{Ticket};
my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
my $ArticleBackendObject = $Kernel::OM->Get('Kernel::System::Ticket::Article::Backend::Email');
# Create the ticket.
my $TicketID = $TicketObject->TicketCreate(
Title => 'Some Ticket_Title',
Queue => 'Raw',
Lock => 'unlock',
Priority => '3 normal',
State => 'closed successful',
CustomerNo => '123465',
CustomerUser => 'customer@example.com',
OwnerID => $UserID,
UserID => 1,
);
# Create the article.
my $ArticleID = $ArticleBackendObject->ArticleSend(
Body => 'Simple string',
MimeType => 'text/plain',
From => 'unittest@example.org',
To => 'unittest@example.org',
SenderType => 'customer',
IsVisibleForCustomer => 1,
HistoryType => 'AddNote',
HistoryComment => 'note',
Subject => 'Unittest data',
Charset => 'utf-8',
UserID => 1,
TicketID => $TicketID,
);
my $Attempts = $Test->{Attempts} || 1;
if ( $Attempts > 1 ) {
$TestBaseMessage = "[Attempt %s] " . $TestBaseMessage;
}
for my $Attempt ( 1 .. $Attempts ) {
my $AttemptBaseMessage = sprintf $TestBaseMessage, $Attempt;
my $AttemptCommunicationLogStatus = $Test->{CommunicationLogStatus};
if ( $AttemptCommunicationLogStatus->{$Attempt} ) {
$AttemptCommunicationLogStatus = $AttemptCommunicationLogStatus->{$Attempt};
}
my $AttemptTransmissionError = $Test->{TransmissionError};
if ( $Test->{TransmissionErrorAttempt} && $Test->{TransmissionErrorAttempt}->{$Attempt} ) {
$AttemptTransmissionError = $Test->{TransmissionErrorAttempt}->{$Attempt};
}
my $AttemptStillInQueue = $Test->{StillInQueue};
if ( $Test->{StillInQueueAttempt} && $Test->{StillInQueueAttempt}->{$Attempt} ) {
$AttemptStillInQueue = $Test->{StillInQueueAttempt}->{$Attempt};
}
# Send the queued emails.
my $MailQueueItemsSent = $MailQueueSend->();
# Test transmission error for the article.
my $ArticleTransmissionError = $ArticleBackendObject->ArticleGetTransmissionError(
ArticleID => $ArticleID,
);
$Self->$AttemptTransmissionError(
$ArticleTransmissionError,
sprintf(
'%s, transmission error%sfound',
$AttemptBaseMessage,
$AttemptTransmissionError eq 'True' ? ' ' : ' not ',
),
);
# Test the queue item.
$CheckForQueueItem->(
TestBaseMessage => $AttemptBaseMessage,
SMTPErrorType => $SMTPErrorType,
MailQueueItemSent => $MailQueueItemsSent->[0],
StillInQueue => $AttemptStillInQueue,
);
# Test the communication log.
$CheckForCommunicationLog->(
TestBaseMessage => $AttemptBaseMessage,
MailQueueItemSent => $MailQueueItemsSent->[0],
CommunicationLogStatus => $AttemptCommunicationLogStatus,
);
# Article Email Notifications Events.
if ( $ArticleTransmissionError->{Status} eq 'Failed' ) {
$CheckForQueueNotifications->(
ArticleID => $ArticleID,
);
}
}
}
# restore to the previous state is done by RestoreDatabase
1;
|