Skip to content

Commit

Permalink
Add deliberate warning when Mail exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Dec 11, 2023
1 parent 36f573c commit 836a3a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/src/Controller/RegisterAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace UserFrosting\Sprinkle\Account\Controller;

use Illuminate\Database\Connection;
use PHPMailer\PHPMailer\Exception as PHPMailerException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand Down Expand Up @@ -171,7 +172,14 @@ protected function handle(Request $request): ?array

// Send activation email
if ($this->requireEmailVerification() === true) {
$this->verificationEmail->send($user, 'mail/verify-account.html.twig');
try {
$this->verificationEmail->send($user, 'mail/verify-account.html.twig');
} catch (PHPMailerException $e) {
// Use abstract message for security reasons - We don't want to show email is not working
$this->alert->addMessageTranslated('danger', 'REGISTRATION.UNKNOWN');

throw $e;
}
}

return $user;
Expand Down
44 changes: 44 additions & 0 deletions app/tests/Controller/RegisterActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use UserFrosting\Sprinkle\Core\Testing\RefreshDatabase;
use UserFrosting\Sprinkle\Core\Throttle\Throttler;
use UserFrosting\Sprinkle\Core\Util\Captcha;
use PHPMailer\PHPMailer\Exception as PHPMailerException;

/**
* Tests RegisterAction
Expand Down Expand Up @@ -266,6 +267,49 @@ public function testRegisterWithEmailVerification(): void
], $response);
}

public function testRegisterWithFailedEmailVerification(): void
{
/** @var Mailer */
$mailer = Mockery::mock(Mailer::class)
->makePartial()
->shouldReceive('send')->once()
->andThrow(PHPMailerException::class)
->getMock();
$this->ci->set(Mailer::class, $mailer);

$this->setMasterUser();
$captcha = $this->getCaptcha();
$this->forceLocaleConfig();
$this->setRequireEmailVerification(true);

// Set POST data
$data = [
'spiderbro' => 'http://',
'captcha' => $captcha->getCaptcha(),
'user_name' => 'RegisteredUser',
'first_name' => 'Testing',
'last_name' => 'Register',
'email' => '[email protected]',
'password' => 'FooBarFooBar123',
'passwordc' => 'FooBarFooBar123',
'locale' => '',
];

// Create request with method and url and fetch response
$request = $this->createJsonRequest('POST', '/account/register', $data);
$response = $this->handleRequest($request);

// Assert response status & body
$this->assertResponseStatus(500, $response);
$this->assertJsonResponse('Fatal error attempting mail, contact your server administrator. If you are the admin, please check the UserFrosting log.', $response, 'title');

// Assert alert
/** @var AlertStream */
$ms = $this->ci->get(AlertStream::class);
$messages = $ms->getAndClearMessages();
$this->assertSame('danger', array_reverse($messages)[0]['type']);
}

public function testRegisterWithFailedThrottle(): void
{
// Create fake throttler
Expand Down

0 comments on commit 836a3a2

Please sign in to comment.