From e7d9dc57d88064145caa9474a50de238fae9009a Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 16 Aug 2025 14:52:29 +0200 Subject: [PATCH] Add some tests. --- .../Controllers/Chart/AccountController.php | 3 +- .../Controllers/Chart/BalanceController.php | 1 + .../V1/Controllers/Chart/BudgetController.php | 4 +- .../Controllers/Chart/CategoryController.php | 4 +- .../V1/Controllers/Data/PurgeController.php | 14 +++++++ phpunit.xml | 3 ++ routes/api.php | 4 +- .../Api/Chart/AccountControllerTest.php | 42 +++++++++++++++++++ .../Api/Chart/BalanceControllerTest.php | 42 +++++++++++++++++++ .../Api/Chart/BudgetControllerTest.php | 42 +++++++++++++++++++ .../Api/Chart/CategoryControllerTest.php | 42 +++++++++++++++++++ tests/integration/TestCase.php | 22 +++++----- 12 files changed, 207 insertions(+), 16 deletions(-) create mode 100644 tests/integration/Api/Chart/AccountControllerTest.php create mode 100644 tests/integration/Api/Chart/BalanceControllerTest.php create mode 100644 tests/integration/Api/Chart/BudgetControllerTest.php create mode 100644 tests/integration/Api/Chart/CategoryControllerTest.php diff --git a/app/Api/V1/Controllers/Chart/AccountController.php b/app/Api/V1/Controllers/Chart/AccountController.php index d617b3fd44..e60ac1f877 100644 --- a/app/Api/V1/Controllers/Chart/AccountController.php +++ b/app/Api/V1/Controllers/Chart/AccountController.php @@ -49,7 +49,7 @@ class AccountController extends Controller protected array $acceptedRoles = [UserRoleEnum::READ_ONLY]; - private array $chartData; + private array $chartData = []; private AccountRepositoryInterface $repository; /** @@ -61,6 +61,7 @@ class AccountController extends Controller $this->middleware( function ($request, $next) { $this->repository = app(AccountRepositoryInterface::class); + $this->validateUserGroup($request); $this->repository->setUserGroup($this->userGroup); $this->repository->setUser($this->user); diff --git a/app/Api/V1/Controllers/Chart/BalanceController.php b/app/Api/V1/Controllers/Chart/BalanceController.php index 1b5d1d610e..d589be1661 100644 --- a/app/Api/V1/Controllers/Chart/BalanceController.php +++ b/app/Api/V1/Controllers/Chart/BalanceController.php @@ -38,6 +38,7 @@ class BalanceController extends Controller parent::__construct(); $this->middleware( function ($request, $next) { + $this->validateUserGroup($request); $this->repository = app(AccountRepositoryInterface::class); $this->collector = app(GroupCollectorInterface::class); $this->repository->setUserGroup($this->userGroup); diff --git a/app/Api/V1/Controllers/Chart/BudgetController.php b/app/Api/V1/Controllers/Chart/BudgetController.php index 4a91fc86d1..f3533231cc 100644 --- a/app/Api/V1/Controllers/Chart/BudgetController.php +++ b/app/Api/V1/Controllers/Chart/BudgetController.php @@ -27,6 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\Chart; use Carbon\Carbon; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\Data\DateRequest; +use FireflyIII\Api\V1\Requests\Data\SameDateRequest; use FireflyIII\Enums\UserRoleEnum; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Budget; @@ -67,7 +68,6 @@ class BudgetController extends Controller $this->repository = app(BudgetRepositoryInterface::class); $this->blRepository = app(BudgetLimitRepositoryInterface::class); $this->opsRepository = app(OperationsRepositoryInterface::class); - $this->validateUserGroup($request); $this->repository->setUserGroup($this->userGroup); $this->opsRepository->setUserGroup($this->userGroup); $this->blRepository->setUserGroup($this->userGroup); @@ -85,7 +85,7 @@ class BudgetController extends Controller * * @throws FireflyException */ - public function overview(DateRequest $request): JsonResponse + public function overview(SameDateRequest $request): JsonResponse { $params = $request->getAll(); diff --git a/app/Api/V1/Controllers/Chart/CategoryController.php b/app/Api/V1/Controllers/Chart/CategoryController.php index f54321ef0e..28ca50f883 100644 --- a/app/Api/V1/Controllers/Chart/CategoryController.php +++ b/app/Api/V1/Controllers/Chart/CategoryController.php @@ -27,6 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\Chart; use Carbon\Carbon; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Api\V1\Requests\Data\DateRequest; +use FireflyIII\Api\V1\Requests\Data\SameDateRequest; use FireflyIII\Enums\AccountTypeEnum; use FireflyIII\Enums\TransactionTypeEnum; use FireflyIII\Enums\UserRoleEnum; @@ -59,6 +60,7 @@ class CategoryController extends Controller parent::__construct(); $this->middleware( function ($request, $next) { + $this->validateUserGroup($request); $this->accountRepos = app(AccountRepositoryInterface::class); $this->currencyRepos = app(CurrencyRepositoryInterface::class); $this->accountRepos->setUserGroup($this->userGroup); @@ -79,7 +81,7 @@ class CategoryController extends Controller * * @SuppressWarnings("PHPMD.UnusedFormalParameter") */ - public function overview(DateRequest $request): JsonResponse + public function overview(SameDateRequest $request): JsonResponse { /** @var Carbon $start */ $start = $this->parameters->get('start'); diff --git a/app/Api/V1/Controllers/Data/PurgeController.php b/app/Api/V1/Controllers/Data/PurgeController.php index f1c69e8ec6..0ba815042e 100644 --- a/app/Api/V1/Controllers/Data/PurgeController.php +++ b/app/Api/V1/Controllers/Data/PurgeController.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers\Data; use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Enums\UserRoleEnum; use FireflyIII\Models\Account; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; @@ -44,6 +45,19 @@ use Illuminate\Http\JsonResponse; */ class PurgeController extends Controller { + protected array $acceptedRoles = [UserRoleEnum::FULL]; + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + $this->validateUserGroup($request); + + return $next($request); + } + ); + } + /** * TODO cleanup and use repositories. */ diff --git a/phpunit.xml b/phpunit.xml index d6b3e3e27d..b4ce7ee365 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -38,6 +38,9 @@ + + + diff --git a/routes/api.php b/routes/api.php index 1291758d67..be21f9248c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -102,10 +102,10 @@ Route::group( [ 'namespace' => 'FireflyIII\Api\V1\Controllers\Chart', 'prefix' => 'v1/chart/balance', - 'as' => 'api.v1.chart.balance', + 'as' => 'api.v1.chart.balance.', ], static function (): void { - Route::get('balance', ['uses' => 'BalanceController@balance', 'as' => 'balance.balance']); + Route::get('balance', ['uses' => 'BalanceController@balance', 'as' => 'balance']); } ); diff --git a/tests/integration/Api/Chart/AccountControllerTest.php b/tests/integration/Api/Chart/AccountControllerTest.php new file mode 100644 index 0000000000..5049ad5414 --- /dev/null +++ b/tests/integration/Api/Chart/AccountControllerTest.php @@ -0,0 +1,42 @@ +user)) { + $this->user = $this->createAuthenticatedUser(); + } + $this->actingAs($this->user); + } + + public function testGetOverviewChartFails(): void + { + $this->actingAs($this->user); + $response = $this->getJson(route('api.v1.chart.account.overview')); + $response->assertStatus(422); + + } + public function testGetOverviewChart(): void + { + $this->actingAs($this->user); + $params = [ + 'start' => '2024-01-01', + 'end' => '2024-01-31', + ]; + $response = $this->getJson(route('api.v1.chart.account.overview') . '?' . http_build_query($params)); + $response->assertStatus(200); + + } +} diff --git a/tests/integration/Api/Chart/BalanceControllerTest.php b/tests/integration/Api/Chart/BalanceControllerTest.php new file mode 100644 index 0000000000..ab43528f91 --- /dev/null +++ b/tests/integration/Api/Chart/BalanceControllerTest.php @@ -0,0 +1,42 @@ +user)) { + $this->user = $this->createAuthenticatedUser(); + } + $this->actingAs($this->user); + } + + public function testGetOverviewChartFails(): void + { + $this->actingAs($this->user); + $response = $this->getJson(route('api.v1.chart.balance.balance')); + $response->assertStatus(422); + + } + public function testGetOverviewChart(): void + { + $this->actingAs($this->user); + $params = [ + 'start' => '2024-01-01', + 'end' => '2024-01-31', + ]; + $response = $this->getJson(route('api.v1.chart.balance.balance') . '?' . http_build_query($params)); + $response->assertStatus(200); + + } +} diff --git a/tests/integration/Api/Chart/BudgetControllerTest.php b/tests/integration/Api/Chart/BudgetControllerTest.php new file mode 100644 index 0000000000..07d5f31666 --- /dev/null +++ b/tests/integration/Api/Chart/BudgetControllerTest.php @@ -0,0 +1,42 @@ +user)) { + $this->user = $this->createAuthenticatedUser(); + } + $this->actingAs($this->user); + } + + public function testGetOverviewChartFails(): void + { + $this->actingAs($this->user); + $response = $this->getJson(route('api.v1.chart.budget.overview')); + $response->assertStatus(422); + + } + public function testGetOverviewChart(): void + { + $this->actingAs($this->user); + $params = [ + 'start' => '2024-01-01', + 'end' => '2024-01-31', + ]; + $response = $this->getJson(route('api.v1.chart.budget.overview') . '?' . http_build_query($params)); + $response->assertStatus(200); + + } +} diff --git a/tests/integration/Api/Chart/CategoryControllerTest.php b/tests/integration/Api/Chart/CategoryControllerTest.php new file mode 100644 index 0000000000..aee7f2daf0 --- /dev/null +++ b/tests/integration/Api/Chart/CategoryControllerTest.php @@ -0,0 +1,42 @@ +user)) { + $this->user = $this->createAuthenticatedUser(); + } + $this->actingAs($this->user); + } + + public function testGetOverviewChartFails(): void + { + $this->actingAs($this->user); + $response = $this->getJson(route('api.v1.chart.category.overview')); + $response->assertStatus(422); + + } + public function testGetOverviewChart(): void + { + $this->actingAs($this->user); + $params = [ + 'start' => '2024-01-01', + 'end' => '2024-01-31', + ]; + $response = $this->getJson(route('api.v1.chart.category.overview') . '?' . http_build_query($params)); + $response->assertStatus(200); + + } +} diff --git a/tests/integration/TestCase.php b/tests/integration/TestCase.php index 8f88b2d372..ec86a0dbce 100644 --- a/tests/integration/TestCase.php +++ b/tests/integration/TestCase.php @@ -29,7 +29,6 @@ use FireflyIII\Models\UserRole; use FireflyIII\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; -use Illuminate\Support\Facades\DB; use Tests\integration\Traits\CollectsValues; /** @@ -42,7 +41,7 @@ abstract class TestCase extends BaseTestCase use RefreshDatabase; protected const MAX_ITERATIONS = 2; - protected $seed = true; + protected $seed = true; public function dateRangeProvider(): array { @@ -57,15 +56,20 @@ abstract class TestCase extends BaseTestCase ]; } + protected function getAuthenticatedUser(): User + { + return User::where('email', 'james@firefly')->first(); + } + protected function createAuthenticatedUser(): User { $group = UserGroup::create(['title' => 'test@email.com']); - $role = UserRole::where('title', 'owner')->first(); - $user = User::create([ - 'email' => 'test@email.com', - 'password' => 'password', - 'user_group_id' => $group->id, - ]); + $role = UserRole::where('title', 'owner')->first(); + $user = User::create([ + 'email' => 'test@email.com', + 'password' => 'password', + 'user_group_id' => $group->id, + ]); GroupMembership::create( [ @@ -76,8 +80,6 @@ abstract class TestCase extends BaseTestCase ); - - return $user; }