From 70248be9adfe029a7faeee9afafeb48abb9064f1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 20 Feb 2023 15:09:20 +1100 Subject: [PATCH] Validate custom date ranges --- .../TaskScheduler/StoreSchedulerRequest.php | 2 + tests/Feature/Scheduler/SchedulerTest.php | 102 ++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php b/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php index fcc52ae8c302..e98b94283114 100644 --- a/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php +++ b/app/Http/Requests/TaskScheduler/StoreSchedulerRequest.php @@ -40,6 +40,8 @@ class StoreSchedulerRequest extends Request 'template' => 'bail|required|string', 'parameters' => 'bail|array', 'parameters.clients' => ['bail','sometimes', 'array', new ValidClientIds()], + 'parameters.start_date' => ['bail', 'sometimes', 'date'], + 'parameters.end_date' => ['bail', 'sometimes', 'date', 'after_or_equal:parameters.start_date'], ]; return $rules; diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index a080ced33659..ba6440cce136 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -54,6 +54,108 @@ class SchedulerTest extends TestCase ); } + public function testCustomDateRanges() + { + $data = [ + 'name' => 'A test statement scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'client_statement', + 'parameters' => [ + 'date_range' => EmailStatement::CUSTOM_RANGE, + 'show_payments_table' => true, + 'show_aging_table' => true, + 'status' => 'paid', + 'clients' => [], + 'start_date' => now()->format('Y-m-d'), + 'end_date' => now()->addDays(4)->format('Y-m-d') + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(200); + } + + public function testCustomDateRangesFails() + { + $data = [ + 'name' => 'A test statement scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'client_statement', + 'parameters' => [ + 'date_range' => EmailStatement::CUSTOM_RANGE, + 'show_payments_table' => true, + 'show_aging_table' => true, + 'status' => 'paid', + 'clients' => [], + 'start_date' => now()->format('Y-m-d'), + 'end_date' => now()->subDays(4)->format('Y-m-d') + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(422); + + + $data = [ + 'name' => 'A test statement scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'client_statement', + 'parameters' => [ + 'date_range' => EmailStatement::CUSTOM_RANGE, + 'show_payments_table' => true, + 'show_aging_table' => true, + 'status' => 'paid', + 'clients' => [], + 'start_date' => now()->format('Y-m-d'), + 'end_date' => null + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(422); + + + + $data = [ + 'name' => 'A test statement scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'client_statement', + 'parameters' => [ + 'date_range' => EmailStatement::CUSTOM_RANGE, + 'show_payments_table' => true, + 'show_aging_table' => true, + 'status' => 'paid', + 'clients' => [], + 'start_date' => null, + 'end_date' => now()->format('Y-m-d') + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(422); + + } + public function testClientCountResolution() { $c = Client::factory()->create([