From db81209fe149c86775210655316c39b9d39cd57e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 20 Mar 2025 16:01:28 -0700 Subject: [PATCH 1/6] Organize existing tests --- .../Users/Ui/EmailAssignedToUserTest.php | 41 ++++++++++++++ tests/Feature/Users/Ui/PrintUserTest.php | 33 ++++++++++++ tests/Feature/Users/Ui/ViewUserTest.php | 54 ------------------- 3 files changed, 74 insertions(+), 54 deletions(-) create mode 100644 tests/Feature/Users/Ui/EmailAssignedToUserTest.php create mode 100644 tests/Feature/Users/Ui/PrintUserTest.php diff --git a/tests/Feature/Users/Ui/EmailAssignedToUserTest.php b/tests/Feature/Users/Ui/EmailAssignedToUserTest.php new file mode 100644 index 000000000..4f2250d84 --- /dev/null +++ b/tests/Feature/Users/Ui/EmailAssignedToUserTest.php @@ -0,0 +1,41 @@ +settings->enableMultipleFullCompanySupport(); + + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $superuser = User::factory()->superuser()->create(); + $user = User::factory()->for($companyB)->create(); + + $this->actingAs(User::factory()->viewUsers()->for($companyA)->create()) + ->post(route('users.email', ['userId' => $user->id])) + ->assertStatus(403); + + $this->actingAs(User::factory()->viewUsers()->for($companyB)->create()) + ->post(route('users.email', ['userId' => $user->id])) + ->assertStatus(302); + + $this->actingAs($superuser) + ->post(route('users.email', ['userId' => $user->id])) + ->assertStatus(302); + + Notification::assertSentTo( + [$user], CurrentInventory::class + ); + } +} diff --git a/tests/Feature/Users/Ui/PrintUserTest.php b/tests/Feature/Users/Ui/PrintUserTest.php new file mode 100644 index 000000000..3e389232f --- /dev/null +++ b/tests/Feature/Users/Ui/PrintUserTest.php @@ -0,0 +1,33 @@ +settings->enableMultipleFullCompanySupport(); + + [$companyA, $companyB] = Company::factory()->count(2)->create(); + + $superuser = User::factory()->superuser()->create(); + $user = User::factory()->for($companyB)->create(); + + $this->actingAs(User::factory()->viewUsers()->for($companyA)->create()) + ->get(route('users.print', ['userId' => $user->id])) + ->assertStatus(302); + + $this->actingAs(User::factory()->viewUsers()->for($companyB)->create()) + ->get(route('users.print', ['userId' => $user->id])) + ->assertStatus(200); + + $this->actingAs($superuser) + ->get(route('users.print', ['userId' => $user->id])) + ->assertOk() + ->assertStatus(200); + } +} diff --git a/tests/Feature/Users/Ui/ViewUserTest.php b/tests/Feature/Users/Ui/ViewUserTest.php index cb21a1e9e..dd452d06e 100644 --- a/tests/Feature/Users/Ui/ViewUserTest.php +++ b/tests/Feature/Users/Ui/ViewUserTest.php @@ -4,8 +4,6 @@ namespace Tests\Feature\Users\Ui; use App\Models\Company; use App\Models\User; -use App\Notifications\CurrentInventory; -use Illuminate\Support\Facades\Notification; use Tests\TestCase; class ViewUserTest extends TestCase @@ -28,56 +26,4 @@ class ViewUserTest extends TestCase ->assertOk() ->assertStatus(200); } - - public function testPermissionsForPrintAllInventoryPage() - { - $this->settings->enableMultipleFullCompanySupport(); - - [$companyA, $companyB] = Company::factory()->count(2)->create(); - - $superuser = User::factory()->superuser()->create(); - $user = User::factory()->for($companyB)->create(); - - $this->actingAs(User::factory()->viewUsers()->for($companyA)->create()) - ->get(route('users.print', ['userId' => $user->id])) - ->assertStatus(302); - - $this->actingAs(User::factory()->viewUsers()->for($companyB)->create()) - ->get(route('users.print', ['userId' => $user->id])) - ->assertStatus(200); - - $this->actingAs($superuser) - ->get(route('users.print', ['userId' => $user->id])) - ->assertOk() - ->assertStatus(200); - } - - public function testUserWithoutCompanyPermissionsCannotSendInventory() - { - - Notification::fake(); - - $this->settings->enableMultipleFullCompanySupport(); - - [$companyA, $companyB] = Company::factory()->count(2)->create(); - - $superuser = User::factory()->superuser()->create(); - $user = User::factory()->for($companyB)->create(); - - $this->actingAs(User::factory()->viewUsers()->for($companyA)->create()) - ->post(route('users.email', ['userId' => $user->id])) - ->assertStatus(403); - - $this->actingAs(User::factory()->viewUsers()->for($companyB)->create()) - ->post(route('users.email', ['userId' => $user->id])) - ->assertStatus(302); - - $this->actingAs($superuser) - ->post(route('users.email', ['userId' => $user->id])) - ->assertStatus(302); - - Notification::assertSentTo( - [$user], CurrentInventory::class - ); - } } From 0ceda098ffc02ff900417891358df3b0dcda50d6 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 20 Mar 2025 16:10:01 -0700 Subject: [PATCH 2/6] Spilt test scenarios --- tests/Feature/Users/Ui/ViewUserTest.php | 29 ++++++++++++++++++------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/Feature/Users/Ui/ViewUserTest.php b/tests/Feature/Users/Ui/ViewUserTest.php index dd452d06e..d2cd8a96b 100644 --- a/tests/Feature/Users/Ui/ViewUserTest.php +++ b/tests/Feature/Users/Ui/ViewUserTest.php @@ -8,22 +8,35 @@ use Tests\TestCase; class ViewUserTest extends TestCase { - public function testPermissionsForUserDetailPage() + + public function testRequiresPermissionToViewUser() + { + $this->actingAs(User::factory()->create()) + ->get(route('users.show', User::factory()->create())) + ->assertStatus(403); + } + + public function testCanViewUser() + { + $actor = User::factory()->viewUsers()->create(); + + $this->actingAs($actor) + ->get(route('users.show', User::factory()->create())) + ->assertOk() + ->assertStatus(200); + } + + public function testCannotViewUserFromAnotherCompany() { $this->settings->enableMultipleFullCompanySupport(); [$companyA, $companyB] = Company::factory()->count(2)->create(); - $superuser = User::factory()->superuser()->create(); + $actor = User::factory()->for($companyA)->viewUsers()->create(); $user = User::factory()->for($companyB)->create(); - $this->actingAs(User::factory()->editUsers()->for($companyA)->create()) + $this->actingAs($actor) ->get(route('users.show', $user)) ->assertStatus(302); - - $this->actingAs($superuser) - ->get(route('users.show', $user)) - ->assertOk() - ->assertStatus(200); } } From 2cfff8e07c130ea269667da2048e849091aebe81 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 20 Mar 2025 16:12:31 -0700 Subject: [PATCH 3/6] Formatting --- tests/Feature/Users/Ui/ViewUserTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Feature/Users/Ui/ViewUserTest.php b/tests/Feature/Users/Ui/ViewUserTest.php index d2cd8a96b..ca45601ff 100644 --- a/tests/Feature/Users/Ui/ViewUserTest.php +++ b/tests/Feature/Users/Ui/ViewUserTest.php @@ -8,7 +8,6 @@ use Tests\TestCase; class ViewUserTest extends TestCase { - public function testRequiresPermissionToViewUser() { $this->actingAs(User::factory()->create()) From ce585539aa7f55cc47fa9271dfd9c2926cb561bc Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 20 Mar 2025 16:13:13 -0700 Subject: [PATCH 4/6] Improve test name --- .../Users/Ui/{PrintUserTest.php => PrintUserInventoryTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/Feature/Users/Ui/{PrintUserTest.php => PrintUserInventoryTest.php} (95%) diff --git a/tests/Feature/Users/Ui/PrintUserTest.php b/tests/Feature/Users/Ui/PrintUserInventoryTest.php similarity index 95% rename from tests/Feature/Users/Ui/PrintUserTest.php rename to tests/Feature/Users/Ui/PrintUserInventoryTest.php index 3e389232f..7d76a30bf 100644 --- a/tests/Feature/Users/Ui/PrintUserTest.php +++ b/tests/Feature/Users/Ui/PrintUserInventoryTest.php @@ -6,7 +6,7 @@ use App\Models\Company; use App\Models\User; use Tests\TestCase; -class PrintUserTest extends TestCase +class PrintUserInventoryTest extends TestCase { public function testPermissionsForPrintAllInventoryPage() { From ac56640d40820dbca09a25e9021ee3be5af1c99b Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 20 Mar 2025 16:21:42 -0700 Subject: [PATCH 5/6] Spilt test scenarios --- .../Users/Ui/PrintUserInventoryTest.php | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/Feature/Users/Ui/PrintUserInventoryTest.php b/tests/Feature/Users/Ui/PrintUserInventoryTest.php index 7d76a30bf..4de7c7cdd 100644 --- a/tests/Feature/Users/Ui/PrintUserInventoryTest.php +++ b/tests/Feature/Users/Ui/PrintUserInventoryTest.php @@ -8,26 +8,34 @@ use Tests\TestCase; class PrintUserInventoryTest extends TestCase { - public function testPermissionsForPrintAllInventoryPage() + public function testPermissionRequiredToPrintUserInventory() + { + $this->actingAs(User::factory()->create()) + ->get(route('users.print', User::factory()->create())) + ->assertStatus(403); + } + + public function testCanPrintUserInventory() + { + $actor = User::factory()->viewUsers()->create(); + + $this->actingAs($actor) + ->get(route('users.print', User::factory()->create())) + ->assertOk() + ->assertStatus(200); + } + + public function testCannotPrintUserInventoryFromAnotherCompany() { $this->settings->enableMultipleFullCompanySupport(); [$companyA, $companyB] = Company::factory()->count(2)->create(); - $superuser = User::factory()->superuser()->create(); + $actor = User::factory()->for($companyA)->viewUsers()->create(); $user = User::factory()->for($companyB)->create(); - $this->actingAs(User::factory()->viewUsers()->for($companyA)->create()) - ->get(route('users.print', ['userId' => $user->id])) + $this->actingAs($actor) + ->get(route('users.print', $user)) ->assertStatus(302); - - $this->actingAs(User::factory()->viewUsers()->for($companyB)->create()) - ->get(route('users.print', ['userId' => $user->id])) - ->assertStatus(200); - - $this->actingAs($superuser) - ->get(route('users.print', ['userId' => $user->id])) - ->assertOk() - ->assertStatus(200); } } From ac597b517b56a79c407afd120e1678fece10f198 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Mon, 24 Mar 2025 12:39:17 -0700 Subject: [PATCH 6/6] Remove blank line --- tests/Feature/Users/Ui/EmailAssignedToUserTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Feature/Users/Ui/EmailAssignedToUserTest.php b/tests/Feature/Users/Ui/EmailAssignedToUserTest.php index 4f2250d84..a6403679c 100644 --- a/tests/Feature/Users/Ui/EmailAssignedToUserTest.php +++ b/tests/Feature/Users/Ui/EmailAssignedToUserTest.php @@ -12,7 +12,6 @@ class EmailAssignedToUserTest extends TestCase { public function testUserWithoutCompanyPermissionsCannotSendInventory() { - Notification::fake(); $this->settings->enableMultipleFullCompanySupport();