Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Stories/GreylistTest.php
<?php | <?php | ||||
namespace Tests\Feature\Stories; | namespace Tests\Feature\Stories; | ||||
use App\Policy\Greylist; | use App\Policy\Greylist; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
/** | /** | ||||
* @group slow | * @group slow | ||||
* @group data | * @group data | ||||
* @group greylist | * @group greylist | ||||
*/ | */ | ||||
class GreylistTest extends TestCase | class GreylistTest extends TestCase | ||||
{ | { | ||||
private $clientAddress; | private $clientAddress; | ||||
private $instance; | |||||
private $requests = []; | private $requests = []; | ||||
private $net; | private $net; | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->setUpTest(); | $this->setUpTest(); | ||||
$this->useServicesUrl(); | $this->useServicesUrl(); | ||||
$this->instance = $this->generateInstanceId(); | |||||
$this->clientAddress = '212.103.80.148'; | $this->clientAddress = '212.103.80.148'; | ||||
$this->net = \App\IP4Net::getNet($this->clientAddress); | $this->net = \App\IP4Net::getNet($this->clientAddress); | ||||
DB::delete("DELETE FROM greylist_connect WHERE sender_domain = 'sender.domain';"); | DB::delete("DELETE FROM greylist_connect WHERE sender_domain = 'sender.domain';"); | ||||
DB::delete("DELETE FROM greylist_settings;"); | |||||
DB::delete("DELETE FROM greylist_whitelist WHERE sender_domain = 'sender.domain';"); | DB::delete("DELETE FROM greylist_whitelist WHERE sender_domain = 'sender.domain';"); | ||||
} | } | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
DB::delete("DELETE FROM greylist_connect WHERE sender_domain = 'sender.domain';"); | DB::delete("DELETE FROM greylist_connect WHERE sender_domain = 'sender.domain';"); | ||||
DB::delete("DELETE FROM greylist_settings;"); | |||||
DB::delete("DELETE FROM greylist_whitelist WHERE sender_domain = 'sender.domain';"); | DB::delete("DELETE FROM greylist_whitelist WHERE sender_domain = 'sender.domain';"); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
public function testWithTimestamp() | public function testWithTimestamp() | ||||
{ | { | ||||
$request = new Greylist\Request( | $request = new Greylist\Request( | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | public function testRetry() | ||||
'recipient' => $this->domainOwner->email, | 'recipient' => $this->domainOwner->email, | ||||
'client_address' => $this->clientAddress | 'client_address' => $this->clientAddress | ||||
] | ] | ||||
); | ); | ||||
$this->assertFalse($request->shouldDefer()); | $this->assertFalse($request->shouldDefer()); | ||||
} | } | ||||
public function testDomainDisabled() | |||||
{ | |||||
$setting = Greylist\Setting::create( | |||||
[ | |||||
'object_id' => $this->domainHosted->id, | |||||
'object_type' => \App\Domain::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'false' | |||||
] | |||||
); | |||||
$request = new Greylist\Request( | |||||
[ | |||||
'sender' => 'someone@sender.domain', | |||||
'recipient' => $this->domainOwner->email, | |||||
'client_address' => $this->clientAddress | |||||
] | |||||
); | |||||
$this->assertFalse($request->shouldDefer()); | |||||
} | |||||
public function testDomainEnabled() | |||||
{ | |||||
$connect = Greylist\Connect::create( | |||||
[ | |||||
'sender_local' => 'someone', | |||||
'sender_domain' => 'sender.domain', | |||||
'recipient_hash' => hash('sha256', $this->domainOwner->email), | |||||
'recipient_id' => $this->domainOwner->id, | |||||
'recipient_type' => \App\User::class, | |||||
'connect_count' => 1, | |||||
'net_id' => \App\IP4Net::getNet('212.103.80.148')->id, | |||||
'net_type' => \App\IP4Net::class | |||||
] | |||||
); | |||||
$setting = Greylist\Setting::create( | |||||
[ | |||||
'object_id' => $this->domainHosted->id, | |||||
'object_type' => \App\Domain::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'true' | |||||
] | |||||
); | |||||
$request = new Greylist\Request( | |||||
[ | |||||
'sender' => 'someone@sender.domain', | |||||
'recipient' => $this->domainOwner->email, | |||||
'client_address' => $this->clientAddress | |||||
] | |||||
); | |||||
$this->assertTrue($request->shouldDefer()); | |||||
$connect->created_at = \Carbon\Carbon::now()->subMinutes(6); | |||||
$connect->save(); | |||||
$this->assertFalse($request->shouldDefer()); | |||||
} | |||||
public function testDomainDisabledUserDisabled() | |||||
{ | |||||
$connect = Greylist\Connect::create( | |||||
[ | |||||
'sender_local' => 'someone', | |||||
'sender_domain' => 'sender.domain', | |||||
'recipient_hash' => hash('sha256', $this->domainOwner->email), | |||||
'recipient_id' => $this->domainOwner->id, | |||||
'recipient_type' => \App\User::class, | |||||
'connect_count' => 1, | |||||
'net_id' => $this->net->id, | |||||
'net_type' => \App\IP4Net::class | |||||
] | |||||
); | |||||
$settingDomain = Greylist\Setting::create( | |||||
[ | |||||
'object_id' => $this->domainHosted->id, | |||||
'object_type' => \App\Domain::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'false' | |||||
] | |||||
); | |||||
$settingUser = Greylist\Setting::create( | |||||
[ | |||||
'object_id' => $this->domainOwner->id, | |||||
'object_type' => \App\User::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'false' | |||||
] | |||||
); | |||||
$request = new Greylist\Request( | |||||
[ | |||||
'sender' => 'someone@sender.domain', | |||||
'recipient' => $this->domainOwner->email, | |||||
'client_address' => $this->clientAddress | |||||
] | |||||
); | |||||
$this->assertFalse($request->shouldDefer()); | |||||
} | |||||
public function testDomainDisabledUserEnabled() | |||||
{ | |||||
$connect = Greylist\Connect::create( | |||||
[ | |||||
'sender_local' => 'someone', | |||||
'sender_domain' => 'sender.domain', | |||||
'recipient_hash' => hash('sha256', $this->domainOwner->email), | |||||
'recipient_id' => $this->domainOwner->id, | |||||
'recipient_type' => \App\User::class, | |||||
'connect_count' => 1, | |||||
'net_id' => $this->net->id, | |||||
'net_type' => \App\IP4Net::class | |||||
] | |||||
); | |||||
$settingDomain = Greylist\Setting::create( | |||||
[ | |||||
'object_id' => $this->domainHosted->id, | |||||
'object_type' => \App\Domain::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'false' | |||||
] | |||||
); | |||||
$settingUser = Greylist\Setting::create( | |||||
[ | |||||
'object_id' => $this->domainOwner->id, | |||||
'object_type' => \App\User::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'true' | |||||
] | |||||
); | |||||
$request = new Greylist\Request( | |||||
[ | |||||
'sender' => 'someone@sender.domain', | |||||
'recipient' => $this->domainOwner->email, | |||||
'client_address' => $this->clientAddress | |||||
] | |||||
); | |||||
$this->assertTrue($request->shouldDefer()); | |||||
$connect->created_at = \Carbon\Carbon::now()->subMinutes(6); | |||||
$connect->save(); | |||||
$this->assertFalse($request->shouldDefer()); | |||||
} | |||||
public function testInvalidDomain() | public function testInvalidDomain() | ||||
{ | { | ||||
$connect = Greylist\Connect::create( | $connect = Greylist\Connect::create( | ||||
[ | [ | ||||
'sender_local' => 'someone', | 'sender_local' => 'someone', | ||||
'sender_domain' => 'sender.domain', | 'sender_domain' => 'sender.domain', | ||||
'recipient_hash' => hash('sha256', $this->domainOwner->email), | 'recipient_hash' => hash('sha256', $this->domainOwner->email), | ||||
'recipient_id' => 1234, | 'recipient_id' => 1234, | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | public function testUserDisabled() | ||||
'recipient_id' => $this->domainOwner->id, | 'recipient_id' => $this->domainOwner->id, | ||||
'recipient_type' => \App\User::class, | 'recipient_type' => \App\User::class, | ||||
'connect_count' => 1, | 'connect_count' => 1, | ||||
'net_id' => $this->net->id, | 'net_id' => $this->net->id, | ||||
'net_type' => \App\IP4Net::class | 'net_type' => \App\IP4Net::class | ||||
] | ] | ||||
); | ); | ||||
$setting = Greylist\Setting::create( | $this->domainOwner->setSetting('greylist_enabled', 'false'); | ||||
[ | |||||
'object_id' => $this->domainOwner->id, | |||||
'object_type' => \App\User::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'false' | |||||
] | |||||
); | |||||
$request = new Greylist\Request( | $request = new Greylist\Request( | ||||
[ | [ | ||||
'sender' => 'someone@sender.domain', | 'sender' => 'someone@sender.domain', | ||||
'recipient' => $this->domainOwner->email, | 'recipient' => $this->domainOwner->email, | ||||
'client_address' => $this->clientAddress | 'client_address' => $this->clientAddress | ||||
] | ] | ||||
); | ); | ||||
$this->assertFalse($request->shouldDefer()); | $this->assertFalse($request->shouldDefer()); | ||||
// Ensure we also find the setting by alias | // Ensure we also find the setting by alias | ||||
$aliases = $this->domainOwner->aliases()->orderBy('alias')->get(); | $aliases = $this->domainOwner->aliases()->orderBy('alias')->get(); | ||||
$request = new Greylist\Request( | $request = new Greylist\Request( | ||||
[ | [ | ||||
'sender' => 'someone@sender.domain', | 'sender' => 'someone@sender.domain', | ||||
'recipient' => $aliases[0]->alias, | 'recipient' => $aliases[0]->alias, | ||||
'client_address' => $this->clientAddress | 'client_address' => $this->clientAddress | ||||
] | ] | ||||
Show All 12 Lines | public function testUserEnabled() | ||||
'recipient_id' => $this->domainOwner->id, | 'recipient_id' => $this->domainOwner->id, | ||||
'recipient_type' => \App\User::class, | 'recipient_type' => \App\User::class, | ||||
'connect_count' => 1, | 'connect_count' => 1, | ||||
'net_id' => $this->net->id, | 'net_id' => $this->net->id, | ||||
'net_type' => \App\IP4Net::class | 'net_type' => \App\IP4Net::class | ||||
] | ] | ||||
); | ); | ||||
$setting = Greylist\Setting::create( | $this->domainOwner->setSetting('greylist_enabled', 'true'); | ||||
[ | |||||
'object_id' => $this->domainOwner->id, | |||||
'object_type' => \App\User::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'true' | |||||
] | |||||
); | |||||
$request = new Greylist\Request( | $request = new Greylist\Request( | ||||
[ | [ | ||||
'sender' => 'someone@sender.domain', | 'sender' => 'someone@sender.domain', | ||||
'recipient' => $this->domainOwner->email, | 'recipient' => $this->domainOwner->email, | ||||
'client_address' => $this->clientAddress | 'client_address' => $this->clientAddress | ||||
] | ] | ||||
); | ); | ||||
Show All 25 Lines | public function testMultipleUsersAllDisabled() | ||||
'recipient_id' => $user->id, | 'recipient_id' => $user->id, | ||||
'recipient_type' => \App\User::class, | 'recipient_type' => \App\User::class, | ||||
'connect_count' => 1, | 'connect_count' => 1, | ||||
'net_id' => $this->net->id, | 'net_id' => $this->net->id, | ||||
'net_type' => \App\IP4Net::class | 'net_type' => \App\IP4Net::class | ||||
] | ] | ||||
); | ); | ||||
Greylist\Setting::create( | $user->setSetting('greylist_enabled', 'false'); | ||||
[ | |||||
'object_id' => $user->id, | |||||
'object_type' => \App\User::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => 'false' | |||||
] | |||||
); | |||||
if ($user->email == $this->domainOwner->email) { | if ($user->email == $this->domainOwner->email) { | ||||
continue; | continue; | ||||
} | } | ||||
$request = new Greylist\Request( | $request = new Greylist\Request( | ||||
[ | [ | ||||
'sender' => 'someone@sender.domain', | 'sender' => 'someone@sender.domain', | ||||
Show All 25 Lines | public function testMultipleUsersAnyEnabled() | ||||
'recipient_id' => $user->id, | 'recipient_id' => $user->id, | ||||
'recipient_type' => \App\User::class, | 'recipient_type' => \App\User::class, | ||||
'connect_count' => 1, | 'connect_count' => 1, | ||||
'net_id' => $this->net->id, | 'net_id' => $this->net->id, | ||||
'net_type' => \App\IP4Net::class | 'net_type' => \App\IP4Net::class | ||||
] | ] | ||||
); | ); | ||||
Greylist\Setting::create( | $user->setSetting('greylist_enabled', ($user->id == $this->jack->id) ? 'true' : 'false'); | ||||
[ | |||||
'object_id' => $user->id, | |||||
'object_type' => \App\User::class, | |||||
'key' => 'greylist_enabled', | |||||
'value' => ($user->id == $this->jack->id) ? 'true' : 'false' | |||||
] | |||||
); | |||||
if ($user->email == $this->domainOwner->email) { | if ($user->email == $this->domainOwner->email) { | ||||
continue; | continue; | ||||
} | } | ||||
$request = new Greylist\Request( | $request = new Greylist\Request( | ||||
[ | [ | ||||
'sender' => 'someone@sender.domain', | 'sender' => 'someone@sender.domain', | ||||
'recipient' => $user->email, | 'recipient' => $user->email, | ||||
'client_address' => $this->clientAddress | 'client_address' => $this->clientAddress | ||||
] | ] | ||||
); | ); | ||||
if ($user->id == $this->jack->id) { | if ($user->id == $this->jack->id) { | ||||
$this->assertTrue($request->shouldDefer()); | $this->assertTrue($request->shouldDefer()); | ||||
} else { | } else { | ||||
$this->assertFalse($request->shouldDefer()); | $this->assertFalse($request->shouldDefer()); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private function generateInstanceId() | |||||
{ | |||||
$instance = []; | |||||
for ($x = 0; $x < 3; $x++) { | |||||
for ($y = 0; $y < 3; $y++) { | |||||
$instance[] = substr('01234567889', rand(0, 9), 1); | |||||
} | |||||
} | |||||
return implode('.', $instance); | |||||
} | |||||
} | } |