From 2e5931f3044457be01b1ac4a3b4225073d393652 Mon Sep 17 00:00:00 2001 From: tasnim Date: Tue, 20 Aug 2024 15:00:52 +0300 Subject: [PATCH 1/3] add test cases for api/v1/autocomplete/CategoryController --- .../Autocomplete/CategoryControllerTest.php | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 tests/integration/Api/Autocomplete/CategoryControllerTest.php diff --git a/tests/integration/Api/Autocomplete/CategoryControllerTest.php b/tests/integration/Api/Autocomplete/CategoryControllerTest.php new file mode 100644 index 0000000000..18b60ce0e8 --- /dev/null +++ b/tests/integration/Api/Autocomplete/CategoryControllerTest.php @@ -0,0 +1,143 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\integration\Api\Autocomplete; + +use FireflyIII\Models\Category; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Tests\integration\TestCase; +use FireflyIII\User; + +/** + * Class CategoryControllerTest + * + * @internal + * + * @coversNothing + */ +final class CategoryControllerTest extends TestCase { + /** + * @covers \FireflyIII\Api\V1\Controllers\Autocomplete\CategoryController + */ + use RefreshDatabase; + + private function createAuthenticatedUser(): User { + return User::create([ + 'email' => 'test@email.com', + 'password' => 'password', + ]); + } + + private function createTestCategories(int $count, User $user): void { + for ($i = 1; $i <= $count; $i++) { + $category = Category::create([ + 'user_id' => $user->id, + 'name' => 'Category ' . $i, + 'user_group_id' => $user->user_group_id, + ]); + } + } + + + public function testGivenAnUnauthenticatedRequestWhenCallingTheCategoriesEndpointThenReturns401HttpCode(): void { + // test API + $response = $this->get(route('api.v1.autocomplete.categories'), ['Accept' => 'application/json']); + $response->assertStatus(401); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertContent('{"message":"Unauthenticated","exception":"AuthenticationException"}'); + } + + public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointThenReturns200HttpCode(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + + // Act as the created user + $this->actingAs($user, 'api'); + $response = $this->get(route('api.v1.autocomplete.categories'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + + } + + public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointThenReturnsCategories(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + + // Act as the created user + $this->actingAs($user, 'api'); + $this->createTestCategories(5, $user); + $response = $this->get(route('api.v1.autocomplete.categories'), ['Accept' => 'application/json']); + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(5); + $response->assertJsonFragment(['name' => 'Category 1']); + $response->assertJsonStructure([ + '*' => [ + 'id', + 'name', + ], + ]); + } + + public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointWithQueryThenReturnsCategoriesWithLimit(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + + // Act as the created user + $this->actingAs($user, 'api'); + $this->createTestCategories(5, $user); + $response = $this->get(route('api.v1.autocomplete.categories', [ + 'query' => 'Category', + 'limit' => 3 + ]), ['Accept' => 'application/json']); + + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + $response->assertJsonCount(3); + } + + public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointWithQueryThenReturnsCategoriesThatMatchQuery(): void + { + // act as a user + $user = $this->createAuthenticatedUser(); + + // Act as the created user + $this->actingAs($user, 'api'); + $this->createTestCategories(20, $user); + $response = $this->get(route('api.v1.autocomplete.categories', [ + 'query' => 'Category 1', + 'limit' => 20, + ]), ['Accept' => 'application/json']); + + $response->assertStatus(200); + $response->assertHeader('Content-Type', 'application/json'); + // Category 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 (11) + $response->assertJsonCount(11); + $response->assertJsonMissing(['name' => 'Category 2']); + } + + +} \ No newline at end of file From 23045ebd59ff82e752883f164ec2160b88ff8f30 Mon Sep 17 00:00:00 2001 From: tasnim Date: Tue, 20 Aug 2024 16:02:43 +0300 Subject: [PATCH 2/3] remove useless comments --- .../Autocomplete/CategoryControllerTest.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/integration/Api/Autocomplete/CategoryControllerTest.php b/tests/integration/Api/Autocomplete/CategoryControllerTest.php index 18b60ce0e8..a900a69e28 100644 --- a/tests/integration/Api/Autocomplete/CategoryControllerTest.php +++ b/tests/integration/Api/Autocomplete/CategoryControllerTest.php @@ -70,10 +70,9 @@ final class CategoryControllerTest extends TestCase { public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointThenReturns200HttpCode(): void { // act as a user - $user = $this->createAuthenticatedUser(); + $user = $this->createAuthenticatedUser(); + $this->actingAs($user); - // Act as the created user - $this->actingAs($user, 'api'); $response = $this->get(route('api.v1.autocomplete.categories'), ['Accept' => 'application/json']); $response->assertStatus(200); $response->assertHeader('Content-Type', 'application/json'); @@ -82,11 +81,9 @@ final class CategoryControllerTest extends TestCase { public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointThenReturnsCategories(): void { - // act as a user $user = $this->createAuthenticatedUser(); + $this->actingAs($user); - // Act as the created user - $this->actingAs($user, 'api'); $this->createTestCategories(5, $user); $response = $this->get(route('api.v1.autocomplete.categories'), ['Accept' => 'application/json']); $response->assertStatus(200); @@ -103,11 +100,9 @@ final class CategoryControllerTest extends TestCase { public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointWithQueryThenReturnsCategoriesWithLimit(): void { - // act as a user $user = $this->createAuthenticatedUser(); + $this->actingAs($user); - // Act as the created user - $this->actingAs($user, 'api'); $this->createTestCategories(5, $user); $response = $this->get(route('api.v1.autocomplete.categories', [ 'query' => 'Category', @@ -121,11 +116,9 @@ final class CategoryControllerTest extends TestCase { public function testGivenAuthenticatedRequestWhenCallingTheCategoriesEndpointWithQueryThenReturnsCategoriesThatMatchQuery(): void { - // act as a user $user = $this->createAuthenticatedUser(); + $this->actingAs($user); - // Act as the created user - $this->actingAs($user, 'api'); $this->createTestCategories(20, $user); $response = $this->get(route('api.v1.autocomplete.categories', [ 'query' => 'Category 1', @@ -139,5 +132,4 @@ final class CategoryControllerTest extends TestCase { $response->assertJsonMissing(['name' => 'Category 2']); } - } \ No newline at end of file From bd1cfffb61184ab3bd7f6f7bff1d57d3ac21fbc9 Mon Sep 17 00:00:00 2001 From: tasnim Date: Sun, 25 Aug 2024 09:07:27 +0300 Subject: [PATCH 3/3] add my name --- tests/integration/Api/Autocomplete/CategoryControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/Api/Autocomplete/CategoryControllerTest.php b/tests/integration/Api/Autocomplete/CategoryControllerTest.php index a900a69e28..4abcbb84d0 100644 --- a/tests/integration/Api/Autocomplete/CategoryControllerTest.php +++ b/tests/integration/Api/Autocomplete/CategoryControllerTest.php @@ -1,7 +1,7 @@