diff --git a/app/Http/Requests/Company/UpdateCompanyRequest.php b/app/Http/Requests/Company/UpdateCompanyRequest.php index 44ef906d4476..1d26fb044b0d 100644 --- a/app/Http/Requests/Company/UpdateCompanyRequest.php +++ b/app/Http/Requests/Company/UpdateCompanyRequest.php @@ -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; } diff --git a/app/Http/ValidationRules/Company/ValidExpenseMailbox.php b/app/Http/ValidationRules/Company/ValidExpenseMailbox.php index eb7be7c3e447..6d3a26a70a60 100644 --- a/app/Http/ValidationRules/Company/ValidExpenseMailbox.php +++ b/app/Http/ValidationRules/Company/ValidExpenseMailbox.php @@ -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'); } } diff --git a/config/ninja.php b/config/ninja.php index a0444ce2ab8e..db302f6dbf0e 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -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', ''), diff --git a/tests/Feature/CompanyTest.php b/tests/Feature/CompanyTest.php index b6ca9508ec2f..55b15b45ef67 100644 --- a/tests/Feature/CompanyTest.php +++ b/tests/Feature/CompanyTest.php @@ -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); } + + }