mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
improve validation
This commit is contained in:
parent
3f0f5663a9
commit
ff695cdad0
@ -78,6 +78,13 @@ class UpdateCompanyRequest extends Request
|
||||
}
|
||||
|
||||
$rules['expense_mailbox'] = ['sometimes','email', 'nullable', new ValidExpenseMailbox(), Rule::unique('companies')->ignore($this->company->id)];
|
||||
$rules['expense_mailbox_active'] = ['sometimes','boolean'];
|
||||
$rules['inbound_mailbox_allow_company_users'] = ['sometimes','boolean'];
|
||||
$rules['inbound_mailbox_allow_vendors'] = ['sometimes','boolean'];
|
||||
$rules['inbound_mailbox_allow_clients'] = ['sometimes','boolean'];
|
||||
$rules['inbound_mailbox_allow_unknown'] = ['sometimes','boolean'];
|
||||
$rules['inbound_mailbox_whitelist'] = ['sometimes', 'string', 'nullable', 'regex:/^[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4}(,[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4})*$/'];
|
||||
$rules['inbound_mailbox_blacklist'] = ['sometimes', 'string', 'nullable', 'regex:/^[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4}(,[\w\-\.\+]+@([\w-]+\.)+[\w-]{2,4})*$/'];
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
@ -22,8 +22,7 @@ use Symfony\Component\Validator\Constraints\EmailValidator;
|
||||
class ValidExpenseMailbox implements Rule
|
||||
{
|
||||
|
||||
private $validated_schema = false;
|
||||
private array $endings;
|
||||
private array $endings = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@ -35,10 +34,7 @@ class ValidExpenseMailbox implements Rule
|
||||
if (empty($value) || !config('ninja.inbound_mailbox.expense_mailbox_endings')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Validate Schema
|
||||
$validated = false;
|
||||
|
||||
foreach ($this->endings as $ending) {
|
||||
if (str_ends_with($value, $ending)) {
|
||||
return true;
|
||||
@ -54,9 +50,6 @@ class ValidExpenseMailbox implements Rule
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
if (!$this->validated_schema)
|
||||
return ctrans('texts.expense_mailbox_invalid');
|
||||
|
||||
return ctrans('texts.expense_mailbox_taken');
|
||||
return ctrans('texts.expense_mailbox_invalid');
|
||||
}
|
||||
}
|
||||
|
@ -232,7 +232,8 @@ return [
|
||||
'webhook_id' => env('PAYPAL_WEBHOOK_ID', null),
|
||||
],
|
||||
'inbound_mailbox' => [
|
||||
'expense_mailbox_endings' => env('EXPENSE_MAILBOX_ENDINGS', '@expense.invoicing.co'),
|
||||
'expense_mailbox_endings' => env('EXPENSE_MAILBOX_ENDINGS', false),
|
||||
// 'expense_mailbox_endings' => env('EXPENSE_MAILBOX_ENDINGS', '@expense.invoicing.co'),
|
||||
'inbound_webhook_token' => env('INBOUND_WEBHOOK_TOKEN', null),
|
||||
'global_inbound_blacklist' => env('GLOBAL_INBOUND_BLACKLIST', ''),
|
||||
'global_inbound_whitelist' => env('GLOBAL_INBOUND_WHITELIST', ''),
|
||||
|
@ -50,6 +50,49 @@ class CompanyTest extends TestCase
|
||||
$this->makeTestData();
|
||||
}
|
||||
|
||||
|
||||
public function testCompanyExpenseMailbox()
|
||||
{
|
||||
// Test valid email address
|
||||
$company_update = [
|
||||
'expense_mailbox' => 'valid@example.com',
|
||||
];
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $company_update);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$this->assertEquals('valid@example.com', $response->json('data.expense_mailbox'));
|
||||
|
||||
// Test invalid email address
|
||||
$company_update = [
|
||||
'expense_mailbox' => 'invalid-email',
|
||||
];
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $company_update);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors(['expense_mailbox']);
|
||||
|
||||
// Test empty email address
|
||||
$company_update = [
|
||||
'expense_mailbox' => '',
|
||||
];
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'X-API-SECRET' => config('ninja.api_secret'),
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->putJson('/api/v1/companies/'.$this->encodePrimaryKey($this->company->id), $company_update);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$this->assertEmpty($response->json('data.expense_mailbox'));
|
||||
}
|
||||
|
||||
public function testEnsureStrReplace()
|
||||
{
|
||||
$x = '**********';
|
||||
@ -216,4 +259,6 @@ class CompanyTest extends TestCase
|
||||
])->delete('/api/v1/companies/'.$this->encodePrimaryKey($company->id))
|
||||
->assertStatus(200);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user