1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/tests/Feature/Payments/StorePaymentValidationTest.php

209 lines
5.5 KiB
PHP
Raw Normal View History

2022-01-12 04:40:05 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
2022-01-12 04:40:05 +01:00
*/
2022-01-12 04:40:05 +01:00
namespace Tests\Feature\Payments;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutEvents;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class StorePaymentValidationTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
use WithoutEvents;
protected function setUp() :void
2022-01-12 04:40:05 +01:00
{
parent::setUp();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
$this->makeTestData();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
2022-06-10 00:20:40 +02:00
public function testNumericParse()
{
$this->assertFalse(is_numeric('2760.0,139.14'));
2022-06-10 00:20:40 +02:00
}
public function testNoAmountGiven()
{
$data = [
2022-06-10 00:20:40 +02:00
// 'amount' => 0,
'client_id' => $this->client->hashed_id,
'invoices' => [
[
'invoice_id' => $this->invoice->hashed_id,
'amount' => 10,
],
],
'credits' => [
[
'credit_id' => $this->credit->hashed_id,
'amount' => 5,
],
2022-06-10 00:20:40 +02:00
],
'date' => '2019/12/12',
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/', $data);
2022-06-10 00:20:40 +02:00
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($e->validator->getMessageBag());
}
$response->assertStatus(200);
}
public function testInValidPaymentAmount()
{
$data = [
'amount' => '10,33',
2022-06-10 00:20:40 +02:00
'client_id' => $this->client->hashed_id,
'invoices' => [
],
'date' => '2019/12/12',
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/', $data);
2022-06-10 00:20:40 +02:00
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($e->validator->getMessageBag());
}
$response->assertStatus(302);
}
2022-01-12 04:40:05 +01:00
public function testValidPayment()
{
$data = [
2022-01-12 04:40:05 +01:00
'amount' => 0,
'client_id' => $this->client->hashed_id,
'invoices' => [
],
'date' => '2019/12/12',
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/', $data);
2022-01-12 04:40:05 +01:00
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($e->validator->getMessageBag());
}
$response->assertStatus(200);
}
public function testValidPaymentWithAmount()
{
$data = [
2022-01-12 04:40:05 +01:00
'amount' => 0,
'client_id' => $this->client->hashed_id,
'invoices' => [
[
'invoice_id' => $this->invoice->hashed_id,
'amount' => 10,
],
],
'credits' => [
[
'credit_id' => $this->credit->hashed_id,
'amount' => 5,
],
2022-01-12 04:40:05 +01:00
],
'date' => '2019/12/12',
];
$response = false;
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/', $data);
2022-01-12 04:40:05 +01:00
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($e->validator->getMessageBag());
}
$response->assertStatus(200);
}
public function testValidPaymentWithInvalidData()
{
$data = [
2022-01-12 04:40:05 +01:00
'amount' => 0,
'client_id' => $this->client->hashed_id,
'invoices' => [
[
'invoice_id' => $this->invoice->hashed_id,
],
],
'credits' => [
[
'credit_id' => $this->credit->hashed_id,
'amount' => 5,
],
2022-01-12 04:40:05 +01:00
],
'date' => '2019/12/12',
];
$response = false;
try {
2022-01-12 04:40:05 +01:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/payments/', $data);
} catch (ValidationException $e) {
2022-01-12 04:40:05 +01:00
$response->assertStatus(302);
}
2022-09-18 02:15:51 +02:00
$response->assertStatus(302);
2022-01-12 04:40:05 +01:00
}
}