Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Traits/UserConfigTest.php
- This file was added.
<?php | |||||
namespace Tests\Feature\Traits; | |||||
use App\Backends\Amavis\Policy as AmavisPolicy; | |||||
use App\Backends\Amavis\User as AmavisUser; | |||||
use App\Backends\Spamassassin\Userpref as SpamPref; | |||||
use App\User; | |||||
use Illuminate\Support\Facades\Queue; | |||||
use Tests\TestCase; | |||||
class UserConfigTest extends TestCase | |||||
{ | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | |||||
public function setUp(): void | |||||
{ | |||||
parent::setUp(); | |||||
$this->deleteTestUser('user-test@' . \config('app.domain')); | |||||
} | |||||
/** | |||||
* {@inheritDoc} | |||||
*/ | |||||
public function tearDown(): void | |||||
{ | |||||
$this->deleteTestUser('user-test@' . \config('app.domain')); | |||||
parent::tearDown(); | |||||
} | |||||
/** | |||||
* Test User::getConfig() and setConfig() methods for UserSettings | |||||
*/ | |||||
public function testUserSettings(): void | |||||
{ | |||||
$user = $this->getTestUser('user-test@' . \config('app.domain')); | |||||
$user->setSetting('greylist_enabled', null); | |||||
$user->setSetting('guam_enabled', null); | |||||
$user->setSetting('password_policy', null); | |||||
$user->setSetting('max_password_age', null); | |||||
$user->setSetting('limit_geo', null); | |||||
// greylist_enabled | |||||
$this->assertSame(true, $user->getConfig()['greylist_enabled']); | |||||
$result = $user->setConfig(['greylist_enabled' => false, 'unknown' => false]); | |||||
$this->assertSame(['unknown' => "The requested configuration parameter is not supported."], $result); | |||||
$this->assertSame(false, $user->getConfig()['greylist_enabled']); | |||||
$this->assertSame('false', $user->getSetting('greylist_enabled')); | |||||
$result = $user->setConfig(['greylist_enabled' => true]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame(true, $user->getConfig()['greylist_enabled']); | |||||
$this->assertSame('true', $user->getSetting('greylist_enabled')); | |||||
// guam_enabled | |||||
$this->assertSame(false, $user->getConfig()['guam_enabled']); | |||||
$result = $user->setConfig(['guam_enabled' => false]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame(false, $user->getConfig()['guam_enabled']); | |||||
$this->assertSame(null, $user->getSetting('guam_enabled')); | |||||
$result = $user->setConfig(['guam_enabled' => true]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame(true, $user->getConfig()['guam_enabled']); | |||||
$this->assertSame('true', $user->getSetting('guam_enabled')); | |||||
// max_apssword_age | |||||
$this->assertSame(null, $user->getConfig()['max_password_age']); | |||||
$result = $user->setConfig(['max_password_age' => -1]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame(null, $user->getConfig()['max_password_age']); | |||||
$this->assertSame(null, $user->getSetting('max_password_age')); | |||||
$result = $user->setConfig(['max_password_age' => 12]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame('12', $user->getConfig()['max_password_age']); | |||||
$this->assertSame('12', $user->getSetting('max_password_age')); | |||||
// password_policy | |||||
$result = $user->setConfig(['password_policy' => true]); | |||||
$this->assertSame(['password_policy' => "Specified password policy is invalid."], $result); | |||||
$this->assertSame(null, $user->getConfig()['password_policy']); | |||||
$this->assertSame(null, $user->getSetting('password_policy')); | |||||
$result = $user->setConfig(['password_policy' => 'min:-1']); | |||||
$this->assertSame(['password_policy' => "Specified password policy is invalid."], $result); | |||||
$result = $user->setConfig(['password_policy' => 'min:-1']); | |||||
$this->assertSame(['password_policy' => "Specified password policy is invalid."], $result); | |||||
$result = $user->setConfig(['password_policy' => 'min:10,unknown']); | |||||
$this->assertSame(['password_policy' => "Specified password policy is invalid."], $result); | |||||
\config(['app.password_policy' => 'min:5,max:100']); | |||||
$result = $user->setConfig(['password_policy' => 'min:4,max:255']); | |||||
$this->assertSame(['password_policy' => "Minimum password length cannot be less than 5."], $result); | |||||
\config(['app.password_policy' => 'min:5,max:100']); | |||||
$result = $user->setConfig(['password_policy' => 'min:10,max:255']); | |||||
$this->assertSame(['password_policy' => "Maximum password length cannot be more than 100."], $result); | |||||
\config(['app.password_policy' => 'min:5,max:255']); | |||||
$result = $user->setConfig(['password_policy' => 'min:10,max:255']); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame('min:10,max:255', $user->getConfig()['password_policy']); | |||||
$this->assertSame('min:10,max:255', $user->getSetting('password_policy')); | |||||
// limit_geo | |||||
$this->assertSame([], $user->getConfig()['limit_geo']); | |||||
$result = $user->setConfig(['limit_geo' => '']); | |||||
$err = "Specified configuration is invalid. Expected a list of two-letter country codes."; | |||||
$this->assertSame(['limit_geo' => $err], $result); | |||||
$this->assertSame(null, $user->getSetting('limit_geo')); | |||||
$result = $user->setConfig(['limit_geo' => ['usa']]); | |||||
$this->assertSame(['limit_geo' => $err], $result); | |||||
$this->assertSame(null, $user->getSetting('limit_geo')); | |||||
$result = $user->setConfig(['limit_geo' => []]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame(null, $user->getSetting('limit_geo')); | |||||
$result = $user->setConfig(['limit_geo' => ['US', 'ru']]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame(['US', 'RU'], $user->getConfig()['limit_geo']); | |||||
$this->assertSame('["US","RU"]', $user->getSetting('limit_geo')); | |||||
} | |||||
/** | |||||
* Test User::getConfig()/setConfig() methods with Amavis settings | |||||
*/ | |||||
public function testAmavisOptions(): void | |||||
{ | |||||
$this->markTestIncomplete(); | |||||
} | |||||
/** | |||||
* Test User::getConfig()/setConfig() methods with Spamassassin settings | |||||
*/ | |||||
public function testSpamassassinOptions(): void | |||||
{ | |||||
$user = $this->getTestUser('user-test@' . \config('app.domain')); | |||||
SpamPref::where('username', $user->email)->delete(); | |||||
// whitelist_from | |||||
$this->assertSame([], $user->getConfig()['sa_whitelist_from']); | |||||
$result = $user->setConfig(['sa_whitelist_from' => []]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame([], $user->getConfig()['sa_whitelist_from']); | |||||
$whitelist = ['test@test.com', '*@test.pl', '*.domain.net']; | |||||
$result = $user->setConfig(['sa_whitelist_from' => $whitelist]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame($whitelist, $user->getConfig()['sa_whitelist_from']); | |||||
$whitelist[] = 'test test'; | |||||
$result = $user->setConfig(['sa_whitelist_from' => $whitelist]); | |||||
$this->assertSame(['sa_whitelist_from' => [3 => "Invalid option value."]], $result); | |||||
unset($whitelist[3]); | |||||
$this->assertSame($whitelist, $user->getConfig()['sa_whitelist_from']); | |||||
$result = $user->setConfig(['sa_whitelist_from' => []]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame([], $user->getConfig()['sa_whitelist_from']); | |||||
// blacklist_from | |||||
$this->assertSame([], $user->getConfig()['sa_blacklist_from']); | |||||
$result = $user->setConfig(['sa_blacklist_from' => []]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame([], $user->getConfig()['sa_blacklist_from']); | |||||
$blacklist = ['test@test.com', '*@test.pl', '*.domain.net']; | |||||
$result = $user->setConfig(['sa_blacklist_from' => $whitelist]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame($blacklist, $user->getConfig()['sa_blacklist_from']); | |||||
$blacklist[] = 'test test'; | |||||
$result = $user->setConfig(['sa_blacklist_from' => $blacklist]); | |||||
$this->assertSame(['sa_blacklist_from' => [3 => "Invalid option value."]], $result); | |||||
unset($blacklist[3]); | |||||
$this->assertSame($blacklist, $user->getConfig()['sa_blacklist_from']); | |||||
$result = $user->setConfig(['sa_blacklist_from' => []]); | |||||
$this->assertSame([], $result); | |||||
$this->assertSame([], $user->getConfig()['sa_blacklist_from']); | |||||
} | |||||
/** | |||||
* Test UserConfigTrait actions on user deletion | |||||
*/ | |||||
public function testUserDelete(): void | |||||
{ | |||||
Queue::fake(); | |||||
$user = $this->getTestUser('user-test@' . \config('app.domain')); | |||||
AmavisUser::query()->delete(); | |||||
AmavisPolicy::query()->delete(); | |||||
SpamPref::saveFor($user->email, ['whitelist_from' => ['test']]); | |||||
AmavisPolicy::saveFor($user->email, ['spam_tag_level' => 5]); | |||||
$this->assertSame(1, SpamPref::where('username', $user->email)->count()); | |||||
$this->assertSame(1, AmavisUser::where('email', $user->email)->count()); | |||||
$this->assertSame(1, AmavisPolicy::count()); | |||||
$user->delete(); | |||||
$this->assertTrue($user->fresh()->trashed()); | |||||
$this->assertSame(0, SpamPref::where('username', $user->email)->count()); | |||||
$this->assertSame(0, AmavisUser::where('email', $user->email)->count()); | |||||
$this->assertSame(0, AmavisPolicy::count()); | |||||
} | |||||
} |