From 21862adbae2c434f375760e91697f51288fde14b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 17 Sep 2023 19:11:04 +1000 Subject: [PATCH] Fixes for tess --- .../PaymentAppliedValidAmount.php | 6 + tests/Feature/PaymentTest.php | 387 +++++++++--------- 2 files changed, 195 insertions(+), 198 deletions(-) diff --git a/app/Http/ValidationRules/PaymentAppliedValidAmount.php b/app/Http/ValidationRules/PaymentAppliedValidAmount.php index 1d674e07d8c5..2958f04229cf 100644 --- a/app/Http/ValidationRules/PaymentAppliedValidAmount.php +++ b/app/Http/ValidationRules/PaymentAppliedValidAmount.php @@ -90,6 +90,12 @@ class PaymentAppliedValidAmount implements Rule return false; } } + + if(count($this->input['invoices']) >=1 && $payment->status_id == Payment::STATUS_PENDING){ + $this->message = 'Cannot apply a payment until the status is completed.'; + return false; + } + } if (round($payment_amounts, 3) >= round($invoice_amounts, 3)) { diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 25cd22f894b2..e62ea4502b05 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -42,6 +42,8 @@ class PaymentTest extends TestCase use DatabaseTransactions; use MockAccountData; + public $faker; + protected function setUp() :void { parent::setUp(); @@ -53,13 +55,49 @@ class PaymentTest extends TestCase Model::reguard(); $this->makeTestData(); - $this->withoutExceptionHandling(); + // $this->withoutExceptionHandling(); $this->withoutMiddleware( ThrottleRequests::class ); } + public function testPendingPaymentLogic() + { + $payment = Payment::factory()->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $this->client->id, + 'status_id' => Payment::STATUS_PENDING, + 'amount' => 100 + ]); + + $data = [ + 'amount' => $this->invoice->amount, + 'client_id' => $this->client->hashed_id, + 'invoices' => [ + [ + 'invoice_id' => $this->invoice->hashed_id, + 'amount' => $this->invoice->amount, + ], + ], + 'date' => '2020/12/11', + 'idempotency_key' => 'dsjafhajklsfhlaksjdhlkajsdjdfjdfljasdfhkjlsafhljfkfhsjlfhiuwayerfiuwaskjgbzmvnjzxnjcbgfkjhdgfoiwwrasdfasdfkashjdfkaskfjdasfda' + + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->putJson('/api/v1/payments/'.$payment->hashed_id, $data); + + $response->assertStatus(422); + + } + + + + public function testPaymentGetBetweenQuery1() { $response = $this->withHeaders([ @@ -185,16 +223,15 @@ class PaymentTest extends TestCase ]; $response = false; - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/', $data); - } catch (ValidationException $e) { - // $message = json_decode($e->validator->getMessageBag(), 1); - } + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments/', $data); + + $response->assertStatus(422); - $this->assertFalse($response); + // $this->assertFalse($response); } @@ -245,7 +282,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/payments/'.$this->encodePrimaryKey($Payment->id), $Payment->toArray()); + ])->putJson('/api/v1/payments/'.$this->encodePrimaryKey($Payment->id), $Payment->toArray()); $response->assertStatus(200); @@ -287,16 +324,17 @@ class PaymentTest extends TestCase ]; - try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); + ])->postJson('/api/v1/payments/', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertTrue(array_key_exists('client_id', $message)); - } + $response->assertStatus(422); + + // $this->assertTrue(array_key_exists('client_id', $message)); + // } } public function testStorePaymentWithClientId() @@ -339,15 +377,17 @@ class PaymentTest extends TestCase $response = null; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices,paymentables', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertNotNull($message); - } + ])->postJson('/api/v1/payments?include=invoices,paymentables', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); + // $this->assertNotNull($message); + // } + + if ($response) { $arr = $response->json(); @@ -399,19 +439,19 @@ class PaymentTest extends TestCase $response = false; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertNotNull($message); - } + ])->postJson('/api/v1/payments?include=invoices', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); + // $this->assertNotNull($message); + // } - if ($response) { + // if ($response) { $response->assertStatus(200); - } + // } } public function testPartialPaymentAmount() @@ -455,37 +495,29 @@ class PaymentTest extends TestCase 'date' => '2019/12/12', ]; - $response = false; + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments?include=invoices', $data); + + $response->assertStatus(200); - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertNotNull($message); - } + $arr = $response->json(); - if ($response) { - $response->assertStatus(200); + $payment_id = $arr['data']['id']; - $arr = $response->json(); + $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); - $payment_id = $arr['data']['id']; + $this->assertNotNull($payment); + $this->assertNotNull($payment->invoices()); + $this->assertEquals(1, $payment->invoices()->count()); - $payment = Payment::whereId($this->decodePrimaryKey($payment_id))->first(); - - $this->assertNotNull($payment); - $this->assertNotNull($payment->invoices()); - $this->assertEquals(1, $payment->invoices()->count()); - - $pivot_invoice = $payment->invoices()->first(); - $this->assertEquals($pivot_invoice->pivot->amount, 2); - $this->assertEquals($pivot_invoice->partial, 0); - $this->assertEquals($pivot_invoice->amount, 10.0000); - $this->assertEquals($pivot_invoice->balance, 8.0000); - } + $pivot_invoice = $payment->invoices()->first(); + $this->assertEquals($pivot_invoice->pivot->amount, 2); + $this->assertEquals($pivot_invoice->partial, 0); + $this->assertEquals($pivot_invoice->amount, 10.0000); + $this->assertEquals($pivot_invoice->balance, 8.0000); + } public function testPaymentGreaterThanPartial() @@ -539,14 +571,14 @@ class PaymentTest extends TestCase $response = false; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - } + ])->postJson('/api/v1/payments?include=invoices', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); + // } $arr = $response->json(); $response->assertStatus(200); @@ -618,7 +650,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); + ])->postJson('/api/v1/payments?include=invoices', $data); $arr = $response->json(); $response->assertStatus(200); @@ -691,16 +723,17 @@ class PaymentTest extends TestCase 'date' => '2019/12/12', ]; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); + ])->postJson('/api/v1/payments?include=invoices', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); + $response->assertStatus(422); - $this->assertTrue(array_key_exists('amount', $message)); - } + // $this->assertTrue(array_key_exists('amount', $message)); + // } } public function testPaymentChangesBalancesCorrectly() @@ -745,18 +778,18 @@ class PaymentTest extends TestCase $response = false; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); + ])->postJson('/api/v1/payments?include=invoices', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertTrue(array_key_exists('amount', $message)); - } + // $this->assertTrue(array_key_exists('amount', $message)); + // } - if ($response) { + // if ($response) { $response->assertStatus(200); $invoice = Invoice::find($this->decodePrimaryKey($invoice->hashed_id)); @@ -766,7 +799,7 @@ class PaymentTest extends TestCase $payment = $invoice->payments()->first(); $this->assertEquals($payment->applied, 2); - } + // } } public function testUpdatePaymentValidationWorks() @@ -812,20 +845,20 @@ class PaymentTest extends TestCase $response = false; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); + ])->putJson('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertTrue(array_key_exists('invoices', $message)); - } + // $this->assertTrue(array_key_exists('invoices', $message)); + // }/ - if ($response) { + // if ($response) { $response->assertStatus(200); - } + // } } public function testUpdatePaymentValidationPasses() @@ -876,21 +909,21 @@ class PaymentTest extends TestCase $response = false; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - \Log::error(print_r($e->validator->getMessageBag(), 1)); + ])->putJson('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); + // \Log::error(print_r($e->validator->getMessageBag(), 1)); - $this->assertTrue(array_key_exists('invoices', $message)); - } + // $this->assertTrue(array_key_exists('invoices', $message)); + // } - if ($response) { - $response->assertStatus(200); - } + // if ($response) { + $response->assertStatus(422); + // } } public function testDoublePaymentTestWithInvalidAmounts() @@ -935,15 +968,15 @@ class PaymentTest extends TestCase $response = false; - try { + // try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - \Log::error(print_r($e->validator->getMessageBag(), 1)); - } + ])->postJson('/api/v1/payments/', $data); + // } catch (ValidationException $e) { + // $message = json_decode($e->validator->getMessageBag(), 1); + // \Log::error(print_r($e->validator->getMessageBag(), 1)); + // } $response->assertStatus(200); @@ -990,7 +1023,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->put('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); + ])->putJson('/api/v1/payments/'.$this->encodePrimaryKey($payment->id), $data); } catch (ValidationException $e) { $message = json_decode($e->validator->getMessageBag(), 1); @@ -1040,7 +1073,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/', $data); + ])->postJson('/api/v1/payments/', $data); $response->assertStatus(200); @@ -1099,7 +1132,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); + ])->postJson('/api/v1/payments?include=invoices', $data); } catch (ValidationException $e) { $message = json_decode($e->validator->getMessageBag(), 1); $this->assertNotNull($message); @@ -1163,7 +1196,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); + ])->postJson('/api/v1/payments?include=invoices', $data); } catch (ValidationException $e) { $message = json_decode($e->validator->getMessageBag(), 1); $this->assertNotNull($message); @@ -1250,7 +1283,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); + ])->postJson('/api/v1/payments?include=invoices', $data); } catch (ValidationException $e) { $message = json_decode($e->validator->getMessageBag(), 1); $this->assertNotNull($message); @@ -1299,19 +1332,15 @@ class PaymentTest extends TestCase ]; - $response = null; - - try { - $response = $this->withHeaders([ + + $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertNotNull($message); - } + ])->postJson('/api/v1/payments?include=invoices', $data); + + $response->assertStatus(422); - $this->assertNull($response); + } public function testStorePaymentWithCredits() @@ -1351,7 +1380,7 @@ class PaymentTest extends TestCase $credit_calc = new InvoiceSum($credit); $credit_calc->build(); - $credit = $this->credit_calc->getCredit(); + $credit = $credit_calc->getCredit(); $credit->save(); //$10 credit $data = [ @@ -1365,7 +1394,7 @@ class PaymentTest extends TestCase ], 'credits' => [ [ - 'credit_id' => $credit->id, + 'credit_id' => $credit->hashed_id, 'amount' => 5, ], ], @@ -1373,30 +1402,22 @@ class PaymentTest extends TestCase ]; - $response = null; + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments?include=invoices', $data); - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertNotNull($message); - } + $arr = $response->json(); + $response->assertStatus(200); - if ($response) { - $arr = $response->json(); - $response->assertStatus(200); + $payment_id = $arr['data']['id']; - $payment_id = $arr['data']['id']; + $payment = Payment::find($this->decodePrimaryKey($payment_id)); + + $this->assertNotNull($payment); + $this->assertNotNull($payment->invoices()); + $this->assertEquals(1, $payment->invoices()->count()); - $payment = Payment::find($this->decodePrimaryKey($payment_id))->first(); - - $this->assertNotNull($payment); - $this->assertNotNull($payment->invoices()); - $this->assertEquals(1, $payment->invoices()->count()); - } } public function testStorePaymentExchangeRate() @@ -1443,30 +1464,22 @@ class PaymentTest extends TestCase ]; - $response = null; + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments?include=invoices', $data); - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments?include=invoices', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - $this->assertNotNull($message); - } + $arr = $response->json(); + $response->assertStatus(200); - if ($response) { - $arr = $response->json(); - $response->assertStatus(200); + $payment_id = $arr['data']['id']; - $payment_id = $arr['data']['id']; + $payment = Payment::find($this->decodePrimaryKey($payment_id)); - $payment = Payment::find($this->decodePrimaryKey($payment_id)); + $this->assertNotNull($payment); + $this->assertNotNull($payment->invoices()); + $this->assertEquals(1, $payment->invoices()->count()); - $this->assertNotNull($payment); - $this->assertNotNull($payment->invoices()); - $this->assertEquals(1, $payment->invoices()->count()); - } } public function testPaymentActionArchive() @@ -1512,7 +1525,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/', $data); + ])->postJson('/api/v1/payments/', $data); $response->assertStatus(200); @@ -1529,7 +1542,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/bulk?action=archive', $data); + ])->postJson('/api/v1/payments/bulk?action=archive', $data); $arr = $response->json(); @@ -1538,7 +1551,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/bulk?action=restore', $data); + ])->postJson('/api/v1/payments/bulk?action=restore', $data); $arr = $response->json(); @@ -1547,7 +1560,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/bulk?action=delete', $data); + ])->postJson('/api/v1/payments/bulk?action=delete', $data); $arr = $response->json(); @@ -1618,17 +1631,11 @@ class PaymentTest extends TestCase ], ]; - $response = false; - - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/refund', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - nlog($message); - } + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments/refund', $data); + $arr = $response->json(); @@ -1644,7 +1651,7 @@ class PaymentTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments/bulk?action=delete', $data); + ])->postJson('/api/v1/payments/bulk?action=delete', $data); $this->assertEquals(10, $invoice->fresh()->balance); $this->assertEquals(10, $invoice->fresh()->balance); @@ -1659,34 +1666,18 @@ class PaymentTest extends TestCase 'number' => 'duplicate', ]; - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - nlog($message); - } - - $arr = $response->json(); + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments', $data); $response->assertStatus(200); - $response = false; + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/payments', $data); - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/payments', $data); - } catch (ValidationException $e) { - $message = json_decode($e->validator->getMessageBag(), 1); - nlog($message); - } - - if ($response) { - $response->assertStatus(302); - } + $response->assertStatus(422); } }