From 16bf0e4e6d26f99b0e1b4f050bedebc0cf42ae67 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Fri, 24 Mar 2023 17:18:28 +0100 Subject: [PATCH] Fix first user not being set as admin --- .../Http/Auth/RegisterControllerTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Feature/Http/Auth/RegisterControllerTest.php b/tests/Feature/Http/Auth/RegisterControllerTest.php index 6718590b..f0b67b54 100644 --- a/tests/Feature/Http/Auth/RegisterControllerTest.php +++ b/tests/Feature/Http/Auth/RegisterControllerTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Http\Auth; +use App\Models\User; use Illuminate\Support\Facades\DB; use Tests\FeatureTestCase; @@ -94,4 +95,35 @@ public function test_register_with_invalid_data_returns_validation_error() ]) ->assertStatus(422); } + + /** + * @test + */ + public function test_register_first_user_only_as_admin() + { + $this->assertDatabaseCount('users', 0); + + $response = $this->json('POST', '/user', [ + 'name' => self::USERNAME, + 'email' => self::EMAIL, + 'password' => self::PASSWORD, + 'password_confirmation' => self::PASSWORD, + ]); + + $this->assertDatabaseCount('users', 1); + $this->assertDatabaseHas('users', [ + 'name' => self::USERNAME, + 'email' => self::EMAIL, + 'is_admin' => true, + ]); + + $response = $this->json('POST', '/user', [ + 'name' => 'jane', + 'email' => 'jane@example.org', + 'password' => self::PASSWORD, + 'password_confirmation' => self::PASSWORD, + ]); + + $this->assertEquals(1, User::admins()->count()); + } }