From 9e373a9b0d9a09fddf7a7288bd60a2751c45d29f Mon Sep 17 00:00:00 2001 From: TasneemTantawy Date: Sun, 22 Dec 2024 16:29:56 +0300 Subject: [PATCH] object group controller test --- .../Autocomplete/CurrencyControllerTest.php | 18 +- .../ObjectGroupControllerTest.php | 157 ++++++++++++++++++ 2 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 tests/integration/Api/Autocomplete/ObjectGroupControllerTest.php diff --git a/tests/integration/Api/Autocomplete/CurrencyControllerTest.php b/tests/integration/Api/Autocomplete/CurrencyControllerTest.php index 0ee08f220a..cc1f64a56a 100644 --- a/tests/integration/Api/Autocomplete/CurrencyControllerTest.php +++ b/tests/integration/Api/Autocomplete/CurrencyControllerTest.php @@ -100,7 +100,6 @@ final class CurrencyControllerTest extends TestCase { // create test data $this->createTestCurrencies(10, true); - $this->createTestCurrencies(5, false); // test API $response = $this->get(route('api.v1.autocomplete.currencies'), ['Accept' => 'application/json']); @@ -121,6 +120,22 @@ final class CurrencyControllerTest extends TestCase { $response->assertJsonCount(10); } + public function testGivenAuthenticatedRequestWhenCallingTheCurrenciesEndpointDoesNotReturnDisabledCurrencies(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + // create test data + $this->createTestCurrencies(10, false); + + // test API + $response = $this->get(route('api.v1.autocomplete.currencies'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(0); + } + public function testGivenAuthenticatedRequestWhenCallingTheCurrenciesEndpointWithQueryThenReturnsCurrenciesWithLimit(): void { // act as a user @@ -168,5 +183,4 @@ final class CurrencyControllerTest extends TestCase { } - } \ No newline at end of file diff --git a/tests/integration/Api/Autocomplete/ObjectGroupControllerTest.php b/tests/integration/Api/Autocomplete/ObjectGroupControllerTest.php new file mode 100644 index 0000000000..b68912450e --- /dev/null +++ b/tests/integration/Api/Autocomplete/ObjectGroupControllerTest.php @@ -0,0 +1,157 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\integration\Api\Autocomplete; + +use FireflyIII\Models\ObjectGroup; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\integration\TestCase; +use FireflyIII\User; +use FireflyIII\Models\UserGroup; + +/** + * Class ObjectGroupControllerTest + * + * @internal + * + * @coversNothing + */ +final class ObjectGroupControllerTest extends TestCase { + /** + * @covers \FireflyIII\Api\V1\Controllers\Autocomplete\ObjectGroupController + */ + use RefreshDatabase; + protected function createAuthenticatedUser(): User + { + $userGroup = UserGroup::create(['title' => 'Test Group']); + + + $user= User::create([ + 'email' => 'test@email.com', + 'password' => 'password', + ]); + $user->user_group_id = $userGroup->id; + $user->save(); + return $user; + } + + + private function createTestObjectGroups(int $count, User $user): void + { + for ($i = 1; $i <= $count; ++$i) { + $objectGroup = ObjectGroup::create([ + 'title' => 'Object Group '.$i, + 'order' => $i, + 'user_group_id' => $user->user_group_id, + 'user_id' => $user->id, + ]); + } + } + + public function testGivenAnUnauthenticatedRequestWhenCallingTheObjectGroupEndpointThenReturn401HttpCode(): void + { + $response = $this->get(route('api.v1.autocomplete.object-groups'), ['Accept' => 'application/json']); + $response->assertStatus(401); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertContent('{"message":"Unauthenticated","exception":"AuthenticationException"}'); + } + + public function testGivenAuthenticatedRequestWhenCallingTheObjectGroupsEndpointThenReturns200HttpCode(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + // test API + $response = $this->get(route('api.v1.autocomplete.object-groups'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + } + + + public function testGivenAuthenticatedRequestWhenCallingTheObjectGroupsEndpointThenReturnsObjectGroups(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestObjectGroups(5, $user); + $response = $this->get(route('api.v1.autocomplete.object-groups'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(5); + $response->assertJsonFragment(['title' => 'Object Group 1']); + $response->assertJsonStructure([ + '*' => [ + 'id', + 'name', + 'title' + ], + ]); + } + + public function testGivenAuthenticatedRequestWhenCallingTheObjectGroupsEndpointWithQueryThenReturnsObjectGroupsWithLimit(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestObjectGroups(5, $user); + $response = $this->get(route('api.v1.autocomplete.object-groups', [ + 'query' => 'Object Group', + 'limit' => 3, + ]), ['Accept' => 'application/json']); + + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(3); + $response->assertJsonFragment(['name' => 'Object Group 1']); + $response->assertJsonStructure([ + '*' => [ + 'id', + 'name', + 'title', + ], + ]); + + } + + public function testGivenAuthenticatedRequestWhenCallingTheObjectGroupsEndpointWithQueryThenReturnsObjectGroupsThatMatchQuery(): void + { + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); + + $this->createTestObjectGroups(20, $user); + $response = $this->get(route('api.v1.autocomplete.object-groups', [ + 'query' => 'Object Group 1', + 'limit' => 20, + ]), ['Accept' => 'application/json']); + + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + // Object Group 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 (11) + $response->assertJsonCount(11); + $response->assertJsonMissing(['name' => 'Object Group 2']); + } + + +} \ No newline at end of file