Email Tests

This commit is contained in:
David Bomba 2023-01-17 12:21:36 +11:00
parent 86a731ff47
commit 52a982d634
3 changed files with 96 additions and 8 deletions

View File

@ -16,7 +16,6 @@ use App\Models\Client;
use App\Utils\Ninja;
use App\Utils\Traits\AppSetup;
use App\Utils\Traits\ClientGroupSettingsSaver;
use Beganovich\Snappdf\Snappdf;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Storage;

View File

@ -89,24 +89,27 @@ class EmailService
if($this->company->is_disabled && !$this->override)
return true;
if(Ninja::isSelfHost())
return false;
/* To handle spam users we drop all emails from flagged accounts */
if(Ninja::isHosted() && $this->company->account && $this->company->account->is_flagged)
if($this->company->account && $this->company->account->is_flagged)
return true;
/* On the hosted platform we set default contacts a @example.com email address - we shouldn't send emails to these types of addresses */
if(Ninja::isHosted() && $this->hasValidEmails())
if($this->hasInValidEmails())
return true;
/* GMail users are uncapped */
if(Ninja::isHosted() && in_array($this->email_object->settings->email_sending_method, ['gmail', 'office365', 'client_postmark', 'client_mailgun']))
if(in_array($this->email_object->settings->email_sending_method, ['gmail', 'office365', 'client_postmark', 'client_mailgun']))
return false;
/* On the hosted platform, if the user is over the email quotas, we do not send the email. */
if(Ninja::isHosted() && $this->company->account && $this->company->account->emailQuotaExceeded())
if($this->company->account && $this->company->account->emailQuotaExceeded())
return true;
/* If the account is verified, we allow emails to flow */
if(Ninja::isHosted() && $this->company->account && $this->company->account->is_verified_account) {
if($this->company->account && $this->company->account->is_verified_account) {
//11-01-2022
@ -118,7 +121,7 @@ class EmailService
}
/* On the hosted platform if the user has not verified their account we fail here - but still check what they are trying to send! */
if(Ninja::isHosted() && $this->company->account && !$this->company->account->account_sms_verified){
if($this->company->account && !$this->company->account->account_sms_verified){
if(class_exists(\Modules\Admin\Jobs\Account\EmailFilter::class))
return (new \Modules\Admin\Jobs\Account\EmailFilter($this->email_object, $this->company))->run();
@ -133,7 +136,7 @@ class EmailService
return false;
}
private function hasValidEmails(): bool
private function hasInValidEmails(): bool
{
foreach($this->email_object->to as $address_object)
@ -145,6 +148,8 @@ class EmailService
if(!str_contains($address_object->address, "@"))
return true;
if($address_object->address == " ")
return true;
}

View File

@ -0,0 +1,84 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace Tests\Feature;
use App\Services\Email\EmailObject;
use App\Services\Email\EmailService;
use App\Utils\Traits\GeneratesCounter;
use App\Utils\Traits\MakesHash;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Tests\MockAccountData;
use Tests\TestCase;
use Illuminate\Mail\Mailables\Address;
/**
* @test
* @covers App\Services\Email\EmailService
*/
class EmailTest extends TestCase
{
use MakesHash;
use GeneratesCounter;
use MockAccountData;
public EmailService $email_service;
public EmailObject $email_object;
protected function setUp() :void
{
parent::setUp();
if(!class_exists(\Modules\Admin\Jobs\Account\EmailFilter::class))
$this->markTestSkipped('Skip test not needed in this environment');
$this->makeTestData();
$this->email_object = new EmailObject();
$this->email_object->to = [new Address("testing@gmail.com", "Cool Name")];
$this->email_object->attachments = [];
$this->email_object->settings = $this->client->getMergedSettings();
$this->email_object->company = $this->client->company;
$this->email_object->client = $this->client;
$this->email_object->email_template_subject = 'email_subject_statement';
$this->email_object->email_template_body = 'email_template_statement';
$this->email_object->variables = [
'$client' => $this->client->present()->name(),
'$start_date' => '2022-01-01',
'$end_date' => '2023-01-01',
];
$this->email_service = new EmailService($this->email_object, $this->company);
}
public function testPreFlightChecksHosted()
{
config(['ninja.environment' => 'hosted']);
$this->assertFalse($this->email_service->preFlightChecksFail());
}
public function testPreFlightChecksSelfHost()
{
config(['ninja.environment' => 'selfhost']);
$this->assertFalse($this->email_service->preFlightChecksFail());
}
}