From 58fe54846969093de81f9d03ae4058f61cdc653f Mon Sep 17 00:00:00 2001 From: Bobby Iliev Date: Sat, 11 May 2024 21:34:09 +0300 Subject: [PATCH] Add basic auth tests --- resources/views/pages/auth/register.blade.php | 90 ++++++++++--------- tests/Feature/AuthenticationTest.php | 85 ++++++++++++++++++ tests/Feature/AuthenticationWIP.php | 40 --------- tests/Pest.php | 2 +- 4 files changed, 132 insertions(+), 85 deletions(-) create mode 100644 tests/Feature/AuthenticationTest.php delete mode 100644 tests/Feature/AuthenticationWIP.php diff --git a/resources/views/pages/auth/register.blade.php b/resources/views/pages/auth/register.blade.php index 58d925b..2c0b562 100644 --- a/resources/views/pages/auth/register.blade.php +++ b/resources/views/pages/auth/register.blade.php @@ -30,7 +30,7 @@ public $social_providers = []; public function rules() - { + { $nameValidationRules = []; if(config('devdojo.auth.settings.registration_include_name_field')){ $nameValidationRules = ['name' => 'required']; @@ -94,52 +94,54 @@ public function register() ?> - +
+ - @volt('auth.register') - + @volt('auth.register') + - - -
- - @if($showNameField) - - @endif - - @if($showEmailField) - @php - $autofocusEmail = ($showNameField) ? false : true; - @endphp - - @endif - - @if($showPasswordField) - - @endif - - Continue - - -
- Already have an account? - Sign in -
- - @if(count($this->social_providers)) - or -
- @foreach($this->social_providers as $slug => $provider) - - @endforeach + + +
+ + @if($showNameField) + + @endif + + @if($showEmailField) + @php + $autofocusEmail = ($showNameField) ? false : true; + @endphp + + @endif + + @if($showPasswordField) + + @endif + + Continue + + +
+ Already have an account? + Sign in
- @endif + + @if(count($this->social_providers)) + or +
+ @foreach($this->social_providers as $slug => $provider) + + @endforeach +
+ @endif - - @endvolt + + @endvolt - \ No newline at end of file + +
diff --git a/tests/Feature/AuthenticationTest.php b/tests/Feature/AuthenticationTest.php new file mode 100644 index 0000000..4dd8228 --- /dev/null +++ b/tests/Feature/AuthenticationTest.php @@ -0,0 +1,85 @@ +delete(); +}); + +it('validates email and password fields', function () { + Livewire::test('auth.register') + ->set('email', 'invalid-email') + ->set('password', '123') + ->call('register') + ->assertHasErrors(['email' => 'email', 'password' => 'min']); +}); + +it('registers a new user and logs in', function () { + $this->withoutExceptionHandling(); + $this->mock(Registered::class); + config()->set('devdojo.auth.settings.registration_include_name_field', true); + + Livewire::test('auth.register') + ->set('email', 'user@example.com') + ->set('password', 'secret1234') + ->set('name', 'John Doe') + ->call('register') + ->assertHasNoErrors() + ->assertRedirect('/'); + + $this->assertTrue(Auth::check()); + $this->assertEquals('user@example.com', Auth::user()->email); + $this->assertEquals('John Doe', Auth::user()->name); +}); + +it('conditionally displays name and password fields based on configuration', function () { + config()->set('devdojo.auth.settings.registration_include_name_field', true); + config()->set('devdojo.auth.settings.registration_show_password_same_screen', true); + + Livewire::test('auth.register') + ->assertSet('showNameField', true) + ->assertSet('showPasswordField', true) + ->assertSeeHtml('wire:model="name"') + ->assertSeeHtml('wire:model="password"') + ->assertDontSeeHtml('wire:model="password_confirmation"'); +}); + +it('checks for required fields and validation errors', function () { + Livewire::test('auth.register') + ->set('email', 'not-an-email') + ->call('register') + ->assertHasErrors(['email' => 'email']); + + Livewire::test('auth.register') + ->set('password', 'short') + ->call('register') + ->assertHasErrors(['password' => 'min']); +}); + +it('renders social login buttons if providers are available', function () { + config()->set('devdojo.auth.providers', [ + 'google' => [ + 'name' => 'Google', + 'active' => true, + 'label' => 'Google' + ], + 'facebook' => [ + 'name' => 'Facebook', + 'active' => true, + 'label' => 'Facebook' + ], + 'twitter' => [ + 'name' => 'Twitter', + 'active' => false, + 'label' => 'Twitter' + ] + ]); + + Livewire::test('auth.register') + ->assertSee('Google') + ->assertSee('Facebook') + ->assertDontSee('Twitter'); +}); diff --git a/tests/Feature/AuthenticationWIP.php b/tests/Feature/AuthenticationWIP.php deleted file mode 100644 index 8a71666..0000000 --- a/tests/Feature/AuthenticationWIP.php +++ /dev/null @@ -1,40 +0,0 @@ -delete(); -}); - -test('user can register, login, and logout', function () { - $email = 'test@example.com'; - $password = 'password'; - - // Test user registration - $response = $this->post('/auth/register', [ - 'email' => $email, - 'password' => Hash::make($password), - ]); - - $response->assertRedirect('/'); // Assuming '/' is the homepage - - // Check if user is created - $user = User::where('email', $email)->first(); - expect($user)->not->toBeNull(); - - // Test logout - Auth::login($user); - $response = $this->post('/logout'); - $response->assertRedirect('/'); // Assuming '/' is the homepage after logout - - // Test login - $response = $this->post('/auth/login', [ - 'email' => $email, - 'password' => $password, - ]); - - $response->assertRedirect('/'); // Assuming '/' is the homepage -}); \ No newline at end of file diff --git a/tests/Pest.php b/tests/Pest.php index 2df40e4..50ab1e4 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -12,7 +12,7 @@ */ uses( - Tests\TestCase::class + Tests\TestCase::class, // Illuminate\Foundation\Testing\RefreshDatabase::class, )->in('Feature');