From de3603fcfa4b806a07b0db463100308b3f823369 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Wed, 19 Feb 2025 16:24:40 +0100 Subject: [PATCH] Update tests --- .../v1/Controllers/GroupControllerTest.php | 54 +++++++++++++++++++ .../v1/Controllers/GroupControllerTest.php | 6 ++- tests/Unit/Events/GroupDeletingTest.php | 25 --------- tests/Unit/GroupModelTest.php | 2 - .../DissociateTwofaccountFromGroupTest.php | 6 +-- 5 files changed, 61 insertions(+), 32 deletions(-) delete mode 100644 tests/Unit/Events/GroupDeletingTest.php diff --git a/tests/Api/v1/Controllers/GroupControllerTest.php b/tests/Api/v1/Controllers/GroupControllerTest.php index 969b5e43..4ed75e14 100644 --- a/tests/Api/v1/Controllers/GroupControllerTest.php +++ b/tests/Api/v1/Controllers/GroupControllerTest.php @@ -145,6 +145,26 @@ class GroupControllerTest extends FeatureTestCase ]); } + #[Test] + public function test_store_with_existing_group_name_returns_validation_error() + { + $this->actingAs($this->user, 'api-guard') + ->json('POST', '/api/v1/groups', [ + 'name' => $this->userGroupA->name, + ]) + ->assertStatus(422); + } + + #[Test] + public function test_store_with_all_group_name_returns_validation_error() + { + $this->actingAs($this->user, 'api-guard') + ->json('POST', '/api/v1/groups', [ + 'name' => __('commons.all'), + ]) + ->assertStatus(422); + } + #[Test] public function test_store_invalid_data_returns_validation_error() { @@ -193,6 +213,20 @@ class GroupControllerTest extends FeatureTestCase ]); } + #[Test] + public function test_show_missing_group_with_id_0_returns_the_virtual_all_group_resource() + { + $userTwofaccounts = $this->user->twofaccounts; + + $response = $this->actingAs($this->user, 'api-guard') + ->json('GET', '/api/v1/groups/0') + ->assertOk() + ->assertJsonFragment([ + 'name' => __('commons.all'), + 'twofaccounts_count' => $userTwofaccounts->count(), + ]); + } + #[Test] public function test_update_returns_updated_group_resource() { @@ -392,6 +426,15 @@ class GroupControllerTest extends FeatureTestCase ]); } + #[Test] + public function test_accounts_of_the_all_group_returns_user_twofaccounts_collection() + { + $response = $this->actingAs($this->user, 'api-guard') + ->json('GET', '/api/v1/groups/0/twofaccounts') + ->assertOk() + ->assertJsonCount(2); + } + /** * test Group deletion via API */ @@ -430,6 +473,17 @@ class GroupControllerTest extends FeatureTestCase ]); } + #[Test] + public function test_destroy_the_all_group_is_forbidden() + { + $response = $this->actingAs($this->anotherUser, 'api-guard') + ->json('DELETE', '/api/v1/groups/0') + ->assertForbidden() + ->assertJsonStructure([ + 'message', + ]); + } + #[Test] public function test_destroy_group_resets_user_preferences() { diff --git a/tests/Unit/Api/v1/Controllers/GroupControllerTest.php b/tests/Unit/Api/v1/Controllers/GroupControllerTest.php index d6c3c3bf..a2988897 100644 --- a/tests/Unit/Api/v1/Controllers/GroupControllerTest.php +++ b/tests/Unit/Api/v1/Controllers/GroupControllerTest.php @@ -88,10 +88,11 @@ class GroupControllerTest extends TestCase #[Test] public function test_show_returns_api_resource() { + $request = Mockery::mock(GroupStoreRequest::class); $controller = Mockery::mock(GroupController::class)->makePartial(); $group = Group::factory()->make(); - $response = $controller->show($group); + $response = $controller->show($request, $group); $this->assertInstanceOf(GroupResource::class, $response); } @@ -138,10 +139,11 @@ class GroupControllerTest extends TestCase #[Test] public function test_accounts_returns_api_resources() { + $request = Mockery::mock(GroupStoreRequest::class); $controller = Mockery::mock(GroupController::class)->makePartial(); $group = Group::factory()->make(); - $response = $controller->accounts($group); + $response = $controller->accounts($request, $group); $this->assertContainsOnlyInstancesOf(TwoFAccountReadResource::class, $response->collection); } diff --git a/tests/Unit/Events/GroupDeletingTest.php b/tests/Unit/Events/GroupDeletingTest.php deleted file mode 100644 index fe907de2..00000000 --- a/tests/Unit/Events/GroupDeletingTest.php +++ /dev/null @@ -1,25 +0,0 @@ -make(); - $event = new GroupDeleting($group); - - $this->assertSame($group, $event->group); - } -} diff --git a/tests/Unit/GroupModelTest.php b/tests/Unit/GroupModelTest.php index 6c651dc8..05716e9a 100644 --- a/tests/Unit/GroupModelTest.php +++ b/tests/Unit/GroupModelTest.php @@ -3,7 +3,6 @@ namespace Tests\Unit; use App\Events\GroupDeleted; -use App\Events\GroupDeleting; use App\Models\Group; use App\Models\TwoFAccount; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -32,7 +31,6 @@ class GroupModelTest extends ModelTestCase 'user_id' => 'integer', ], [ - 'deleting' => GroupDeleting::class, 'deleted' => GroupDeleted::class, ] ); diff --git a/tests/Unit/Listeners/DissociateTwofaccountFromGroupTest.php b/tests/Unit/Listeners/DissociateTwofaccountFromGroupTest.php index 829494fa..177140fb 100644 --- a/tests/Unit/Listeners/DissociateTwofaccountFromGroupTest.php +++ b/tests/Unit/Listeners/DissociateTwofaccountFromGroupTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Listeners; -use App\Events\GroupDeleting; +use App\Events\GroupDeleted; use App\Listeners\DissociateTwofaccountFromGroup; use Illuminate\Support\Facades\Event; use PHPUnit\Framework\Attributes\CoversClass; @@ -16,12 +16,12 @@ use Tests\TestCase; class DissociateTwofaccountFromGroupTest extends TestCase { #[Test] - public function test_DissociateTwofaccountFromGroup_listen_to_groupDeleting_event() + public function test_DissociateTwofaccountFromGroup_listen_to_groupDeleted_event() { Event::fake(); Event::assertListening( - GroupDeleting::class, + GroupDeleted::class, DissociateTwofaccountFromGroup::class ); }