You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CaptchaConfigTest.php

136 lines
4.6 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\CaptchaConfig;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
function expectCaptchaConfig($condition, $message)
{
if (!$condition) {
fwrite(STDERR, $message . PHP_EOL);
exit(1);
}
}
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$capsule->getConnection('default')->getSchemaBuilder()->create('configurations', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('value')->nullable();
});
$defaults = [
'captch_reg' => 'false',
'captch_login' => 'false',
'captch_respass' => 'true',
'enable_email_verify' => 'true',
'email_verify_ttl' => '900',
'email_verify_iplimit' => '5',
];
// The legacy PHP configuration must no longer override database-backed values.
$System_Config = [
'captch_reg' => 'true',
'captch_login' => 'true',
'captch_respass' => 'false',
'enable_email_verify' => 'false',
'email_verify_ttl' => '1',
'email_verify_iplimit' => '1',
];
foreach ($defaults as $key => $default) {
expectCaptchaConfig(CaptchaConfig::get($key) === $default, 'Missing ' . $key . ' did not use its safe default.');
}
$overrides = [
'captch_reg' => 'true',
'captch_login' => 'true',
'captch_respass' => 'false',
'enable_email_verify' => 'false',
'email_verify_ttl' => '1800',
'email_verify_iplimit' => '12',
];
foreach ($overrides as $key => $value) {
Capsule::table('configurations')->insert(['name' => $key, 'value' => $value]);
}
CaptchaConfig::clearCache();
foreach ($overrides as $key => $value) {
expectCaptchaConfig(CaptchaConfig::get($key) === $value, 'Database override for ' . $key . ' was ignored.');
}
Capsule::table('configurations')->where('name', 'email_verify_ttl')->update(['value' => '2700']);
expectCaptchaConfig(CaptchaConfig::get('email_verify_ttl') === '1800', 'Per-request configuration cache unexpectedly changed.');
CaptchaConfig::clearCache();
expectCaptchaConfig(CaptchaConfig::get('email_verify_ttl') === '2700', 'Clearing the request cache did not refresh the TTL.');
$invalidOverrides = [
'captch_reg' => 'yes',
'captch_login' => '1',
'captch_respass' => '',
'enable_email_verify' => 'TRUE',
'email_verify_ttl' => '0',
'email_verify_iplimit' => '10001',
];
foreach ($invalidOverrides as $key => $value) {
Capsule::table('configurations')->where('name', $key)->update(['value' => $value]);
}
CaptchaConfig::clearCache();
foreach ($defaults as $key => $default) {
$expected = in_array($key, ['email_verify_ttl', 'email_verify_iplimit'], true) ? $default : 'true';
expectCaptchaConfig(CaptchaConfig::get($key) === $expected, 'Invalid database value for ' . $key . ' did not fail closed.');
}
foreach (['true', 'false'] as $value) {
expectCaptchaConfig(CaptchaConfig::validateBoolean($value) === $value, 'Valid Boolean setting was rejected.');
}
foreach (['', '1', 'TRUE', 'yes', null] as $invalid) {
try {
CaptchaConfig::validateBoolean($invalid);
expectCaptchaConfig(false, 'Invalid Boolean setting was accepted: ' . var_export($invalid, true));
} catch (InvalidArgumentException $exception) {
// Expected.
}
}
foreach ([
['email_verify_ttl', '1'],
['email_verify_ttl', '86400'],
['email_verify_iplimit', '1'],
['email_verify_iplimit', '10000'],
] as [$key, $value]) {
expectCaptchaConfig(CaptchaConfig::validatePositiveInt($value, $key) === $value, 'Valid ' . $key . ' setting was rejected.');
}
foreach ([
['email_verify_ttl', '0'],
['email_verify_ttl', '86401'],
['email_verify_iplimit', '0'],
['email_verify_iplimit', '10001'],
['email_verify_iplimit', '-1'],
['email_verify_iplimit', '1.5'],
['email_verify_iplimit', 'abc'],
['email_verify_iplimit', true],
] as [$key, $value]) {
try {
CaptchaConfig::validatePositiveInt($value, $key);
expectCaptchaConfig(false, 'Invalid ' . $key . ' setting was accepted: ' . $value);
} catch (InvalidArgumentException $exception) {
// Expected.
}
}
$capsule->getConnection('default')->getSchemaBuilder()->drop('configurations');
CaptchaConfig::clearCache();
try {
CaptchaConfig::get('captch_login');
expectCaptchaConfig(false, 'A missing configuration table silently disabled the login captcha.');
} catch (RuntimeException $exception) {
// A storage error must stop verification settings from being used.
}
echo 'Captcha configuration checks passed.' . PHP_EOL;