1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/tests/Feature/RecurringExpenseApiTest.php

289 lines
8.6 KiB
PHP
Raw Normal View History

2021-08-24 03:23:53 +02: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
2021-08-24 03:23:53 +02:00
*/
2021-08-24 03:23:53 +02:00
namespace Tests\Feature;
2021-09-07 01:02:23 +02:00
use App\Models\RecurringInvoice;
2021-08-24 03:23:53 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
2021-09-07 01:02:23 +02:00
use Illuminate\Validation\ValidationException;
2021-08-24 03:23:53 +02:00
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
* @covers App\Http\Controllers\RecurringExpenseController
*/
class RecurringExpenseApiTest extends TestCase
{
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
protected function setUp() :void
2021-08-24 03:23:53 +02:00
{
parent::setUp();
$this->makeTestData();
Session::start();
$this->faker = \Faker\Factory::create();
Model::reguard();
}
2023-01-19 01:52:07 +01:00
public function testRecurringExpenseGetFiltered()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/recurring_expenses?filter=xx');
$response->assertStatus(200);
}
2021-08-24 03:23:53 +02:00
public function testRecurringExpenseGet()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/recurring_expenses/');
2021-08-24 03:23:53 +02:00
$response->assertStatus(200);
}
public function testRecurringExpenseGetSingleExpense()
2021-08-24 03:23:53 +02:00
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/recurring_expenses/'.$this->recurring_expense->hashed_id);
2021-08-24 03:23:53 +02:00
$response->assertStatus(200);
}
public function testRecurringExpensePost()
{
$data = [
'amount' => 10,
'client_id' => $this->client->hashed_id,
'number' => '123321',
2021-09-07 01:02:23 +02:00
'frequency_id' => 5,
2021-08-24 03:23:53 +02:00
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses', $data);
2021-08-24 03:23:53 +02:00
$response->assertStatus(200);
2021-08-24 03:29:56 +02:00
$arr = $response->json();
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/recurring_expenses/'.$arr['data']['id'], $data)->assertStatus(200);
2021-08-24 03:23:53 +02:00
try {
$response = $this->withHeaders([
2021-08-24 03:29:56 +02:00
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses', $data);
} catch (ValidationException $e) {
2021-08-24 03:29:56 +02:00
$response->assertStatus(302);
}
}
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
public function testRecurringExpensePut()
{
$data = [
'amount' => 20,
'public_notes' => 'Coolio',
];
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/recurring_expenses/'.$this->encodePrimaryKey($this->recurring_expense->id), $data);
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$response->assertStatus(200);
}
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
public function testRecurringExpenseNotArchived()
{
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->get('/api/v1/recurring_expenses/'.$this->encodePrimaryKey($this->recurring_expense->id));
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$arr = $response->json();
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$this->assertEquals(0, $arr['data']['archived_at']);
}
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
public function testRecurringExpenseArchived()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->recurring_expense->id)],
];
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses/bulk?action=archive', $data);
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$arr = $response->json();
2021-08-24 03:23:53 +02:00
2021-08-24 03:29:56 +02:00
$this->assertNotNull($arr['data'][0]['archived_at']);
}
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
public function testRecurringExpenseRestored()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->recurring_expense->id)],
];
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses/bulk?action=restore', $data);
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
$arr = $response->json();
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
$this->assertEquals(0, $arr['data'][0]['archived_at']);
}
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
public function testRecurringExpenseDeleted()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->recurring_expense->id)],
];
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses/bulk?action=delete', $data);
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
$arr = $response->json();
2021-08-24 03:23:53 +02:00
2021-08-24 03:32:33 +02:00
$this->assertTrue($arr['data'][0]['is_deleted']);
}
2021-09-07 01:02:23 +02:00
public function testRecurringExpenseStart()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->recurring_expense->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses/bulk?action=start', $data);
2021-09-07 01:02:23 +02:00
$arr = $response->json();
2021-09-07 01:02:23 +02:00
$this->assertEquals(RecurringInvoice::STATUS_ACTIVE, $arr['data'][0]['status_id']);
}
public function testRecurringExpensePaused()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->recurring_expense->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses/bulk?action=start', $data);
2021-09-07 01:02:23 +02:00
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses/bulk?action=stop', $data);
2021-09-07 01:02:23 +02:00
$arr = $response->json();
2021-09-07 01:02:23 +02:00
$this->assertEquals(RecurringInvoice::STATUS_PAUSED, $arr['data'][0]['status_id']);
}
public function testRecurringExpenseStartedWithTriggeredAction()
{
$data = [
'ids' => [$this->encodePrimaryKey($this->recurring_expense->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/recurring_expenses/'.$this->recurring_expense->hashed_id.'?start=true', []);
$arr = $response->json();
$this->assertEquals(RecurringInvoice::STATUS_ACTIVE, $arr['data']['status_id']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->put('/api/v1/recurring_expenses/'.$this->recurring_expense->hashed_id.'?stop=true', []);
$arr = $response->json();
$this->assertEquals(RecurringInvoice::STATUS_PAUSED, $arr['data']['status_id']);
}
public function testRecurringExpensePostWithStartAction()
{
$data = [
'amount' => 10,
'client_id' => $this->client->hashed_id,
'number' => '123321',
'frequency_id' => 5,
'remaining_cycles' =>5,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses?start=true', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals(RecurringInvoice::STATUS_ACTIVE, $arr['data']['status_id']);
}
public function testRecurringExpensePostWithStopAction()
{
$data = [
'amount' => 10,
'client_id' => $this->client->hashed_id,
'number' => '1233x21',
'frequency_id' => 5,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_expenses?stop=true', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals(RecurringInvoice::STATUS_PAUSED, $arr['data']['status_id']);
}
2021-08-24 03:23:53 +02:00
}