Skip to content

Commit

Permalink
Adding more dusk tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tnylea committed Jun 12, 2024
1 parent f857826 commit ce6cf6e
Show file tree
Hide file tree
Showing 19 changed files with 309 additions and 37 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</main>

@if(config('devdojo.auth.settings.enable_branding') && !app()->isLocal())
<a href="https://devdojo.com/platform/auth?utm_source=branding" target="_blank" class="flex fixed bottom-0 left-1/2 z-30 justify-center items-center px-2.5 py-1.5 w-auto text-xs font-medium rounded-t-lg border -translate-x-1/2 cursor-pointer bg-zinc-900 text-white/80 hover:text-white border-zinc-800">
<a href="https://devdojo.com/auth?utm_source=branding" target="_blank" class="flex fixed bottom-0 left-1/2 z-30 justify-center items-center px-2.5 py-1.5 w-auto text-xs font-medium rounded-t-lg border -translate-x-1/2 cursor-pointer bg-zinc-900 text-white/80 hover:text-white border-zinc-800">
<svg class="mr-1 -ml-1 w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 151 201" fill="none"><path fill="currentColor" fill-rule="evenodd" d="M75.847.132c-28.092 23.884-45.7 25-75 25v96.125c0 15.285 4.238 26.069 12.393 35.442l17.526-33.718.345-.661 5.06-9.74L76.496 35l40.323 77.58c20.95 2.616 30.894 8.93 30.894 8.93a219.818 219.818 0 0 0-24.117 1.321l-1.371.15c-1.345.158-2.69.326-4.017.502a227.52 227.52 0 0 0-41.712 9.705C50.36 141.907 30.44 153.7 18.4 161.993c9.303 8.615 22.183 16.475 38.353 26.344 5.927 3.616 12.296 7.503 19.093 11.795 6.796-4.292 13.165-8.179 19.091-11.795 16.494-10.066 29.564-18.042 38.907-26.861a205.398 205.398 0 0 0-35.223-19.64 225.71 225.71 0 0 1 30.106-6.358l10.533 20.272c7.627-9.153 11.586-19.721 11.586-34.493V25.132c-29.3 0-46.909-1.117-75-25Zm.649 112.615c-6.892.793-14.306 1.973-22.26 3.655l2.566-4.923 19.694-37.896 19.693 37.896c-6.582.089-13.155.513-19.693 1.268Z" clip-rule="evenodd"/></svg>
<p>Secured by DevDojo</p>
</a>
Expand Down
12 changes: 5 additions & 7 deletions resources/views/pages/auth/password/[token].blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Events\PasswordReset;
use function Laravel\Folio\name;
use function Laravel\Folio\{middleware, name};
use Livewire\Volt\Component;
use Livewire\Attributes\Validate;
Expand Down Expand Up @@ -78,10 +76,10 @@ function ($user, $password) {
/>

<form wire:submit="resetPassword" class="space-y-5">
<x-auth::elements.input label="Email address" type="email" id="email" name="email" wire:model="email" autofocus="true" />
<x-auth::elements.input label="Password" type="password" id="password" name="password" wire:model="password" />
<x-auth::elements.input label="Confirm Password" type="password" id="password_confirmation" name="password_confirmation" wire:model="passwordConfirmation" />
<x-auth::elements.button type="primary" rounded="md" submit="true">Reset password</x-auth::elements.button>
<x-auth::elements.input label="Email address" type="email" id="email" name="email" data-auth="email-input" wire:model="email" autofocus="true" />
<x-auth::elements.input label="Password" type="password" id="password" name="password" data-auth="password-input" wire:model="password" />
<x-auth::elements.input label="Confirm Password" type="password" id="password_confirmation" name="password_confirmation" data-auth="password-confirm-input" wire:model="passwordConfirmation" />
<x-auth::elements.button type="primary" data-auth="submit-button" rounded="md" submit="true">Reset password</x-auth::elements.button>
</form>
</x-auth::elements.container>
@endvolt
Expand Down
10 changes: 7 additions & 3 deletions resources/views/pages/auth/password/confirm.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php
use function Laravel\Folio\name;
use function Laravel\Folio\{middleware, name};
use Livewire\Volt\Component;
use Livewire\Attributes\Validate;
use Devdojo\Auth\Traits\HasConfigs;
if(!isset($_GET['preview']) || (isset($_GET['preview']) && $_GET['preview'] != true) || !app()->isLocal()){
middleware('auth');
}
name('password.confirm');
new class extends Component
Expand Down Expand Up @@ -40,8 +44,8 @@ public function confirm()
:show_subheadline="($language->passwordConfirm->show_subheadline ?? false)"
/>
<form wire:submit="confirm" class="space-y-5">
<x-auth::elements.input label="Password" type="password" id="password" name="password" autofocus="true" wire:model="password" />
<x-auth::elements.button type="primary" rounded="md" submit="true">Confirm password</x-auth::elements.button>
<x-auth::elements.input label="Password" type="password" id="password" name="password" data-auth="password-input" autofocus="true" wire:model="password" />
<x-auth::elements.button type="primary" rounded="md" data-auth="submit-button" submit="true">Confirm password</x-auth::elements.button>
</form>
</x-auth::elements.container>
@endvolt
Expand Down
8 changes: 4 additions & 4 deletions resources/views/pages/auth/password/reset.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use Devdojo\Auth\Traits\HasConfigs;
use Illuminate\Support\Facades\Password;
use function Laravel\Folio\name;
use function Laravel\Folio\{middleware, name};
use Livewire\Volt\Component;
use Livewire\Attributes\Validate;
Expand Down Expand Up @@ -68,13 +68,13 @@ public function sendResetPasswordLink()
</div>
@else
<form wire:submit="sendResetPasswordLink" class="space-y-5">
<x-auth::elements.input label="Email address" type="email" id="email" name="email" wire:model="email" autofocus="true" />
<x-auth::elements.button type="primary" rounded="md" submit="true">Send password reset link</x-auth::elements.button>
<x-auth::elements.input label="Email address" type="email" id="email" name="email" data-auth="email-input" wire:model="email" autofocus="true" />
<x-auth::elements.button type="primary" data-auth="submit-button" rounded="md" submit="true">Send password reset link</x-auth::elements.button>
</form>
@endif
<div class="mt-3 space-x-0.5 text-sm leading-5 text-center" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
<span class="opacity-[47%]">Or</span>
<x-auth::elements.text-link href="{{ route('auth.login') }}">return to login</x-auth::elements.text-link>
<x-auth::elements.text-link data-auth="login-link" href="{{ route('auth.login') }}">return to login</x-auth::elements.text-link>
</div>
</x-auth::elements.container>
@endvolt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function disable(){
<h2 class="text-lg font-semibold">Two factor authentication disabled.</h2>
<p class="-translate-y-1">When you enabled 2FA, you will be prompted for a secure code during authentication. This code can be retrieved from your phone's Google Authenticator application.</p>
<div class="relative w-auto">
<x-auth::elements.button type="primary" rounded="md" size="md" wire:click="enable" wire:target="enable">Enable</x-auth>
<x-auth::elements.button type="primary" data-auth="enable-button" rounded="md" size="md" wire:click="enable" wire:target="enable">Enable</x-auth>
</div>
</div>
@else
Expand Down
11 changes: 1 addition & 10 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@
// Add social routes
Route::get('auth/{driver}/redirect', [SocialController::class, 'redirect']);
Route::get('auth/{driver}/callback', [SocialController::class, 'callback']);
});

Route::get('hey', function () {
$rowsArray = [];
$socialProviders = config('devdojo.auth.providers', []);

foreach ($socialProviders as $key => $provider) {
$provider['slug'] = $key;
array_push($rowsArray, $provider);
}
dd($rowsArray);
});

10 changes: 10 additions & 0 deletions src/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function boot(): void
}

$this->handleStarterKitFunctionality();
$this->loadDynamicRoutesForTesting();
}

private function registerAuthFolioDirectory(): void
Expand Down Expand Up @@ -150,4 +151,13 @@ public function register()
$this->app->register(\Devdojo\Auth\Providers\DuskServiceProvider::class);
}
}

private function loadDynamicRoutesForTesting()
{
if (app()->environment('testing') || app()->environment('local')) {
Route::get('/auth/password_confirmation_test', function () {
return 'Test Confirmed';
})->middleware('web', 'auth', 'password.confirm');
}
}
}
25 changes: 25 additions & 0 deletions src/Providers/DuskServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public function boot(): void
return $this;
});

Browser::macro('loginAsJohnDoe', function(){
$this->loginAs(\Devdojo\Auth\Models\User::where('email', 'johndoe@gmail.com')->first());
return $this;
});

Browser::macro('enable2FAforJohnDoe', function(){
$johnDoe = \Devdojo\Auth\Models\User::where('email', 'johndoe@gmail.com')->first();
$johnDoe->two_factor_confirmed_at = now();
$johnDoe->save();
return $this;
});

Browser::macro('assertRedirectAfterAuthUrlIsCorrect', function () {
$redirectExpectedToBe = '/';
if (class_exists(\Devdojo\Genesis\Genesis::class)) {
Expand All @@ -67,5 +79,18 @@ public function boot(): void

return $this;
});

Browser::macro('typeAndSubmit', function (?string $selector, string $value) {
$this->type($selector, $value)
->click('@submit-button');

return $this;
});

Browser::macro('visitPasswordConfirmTestPage', function () {
$this->visit('/auth/password_confirmation_test');
return $this;
});

}
}
55 changes: 55 additions & 0 deletions tests/Browser/ConfirmPasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This is for the /auth/password/confirm page
*/

use Devdojo\Auth\Tests\Browser\Pages\ConfirmPassword;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;

uses(DatabaseMigrations::class);

test('Guest redirect to login page', function(){
$browser = $this->browse(function (Browser $browser) {
$browser
->visit(new ConfirmPassword)
->assertPathIs('/auth/login');
});
});

test('Password Confirm Protected Page Redirects', function () {
$browser = $this->browse(function (Browser $browser) {
$browser
->visitPasswordConfirmTestPage()
->assertPathIs('/auth/login')
->createJohnDoe()
->loginAsJohnDoe()
->visitPasswordConfirmTestPage()
->assertPathIs('/auth/password/confirm');
});
});

test('Password Confirm Works', function(){
$browser = $this->browse(function (Browser $browser) {
$browser
->createJohnDoe()
->loginAsJohnDoe()
->visit(new ConfirmPassword)
->type('@password-input', 'password')
->clickAndWaitForReload('@submit-button')
->assertRedirectAfterAuthUrlIsCorrect();
});
});

test('Password Confirm Works and redirect to password protected page', function(){
$browser = $this->browse(function (Browser $browser) {
$browser
->createJohnDoe()
->loginAsJohnDoe()
->visitPasswordConfirmTestPage()
->type('@password-input', 'password')
->clickAndWaitForReload('@submit-button')
->assertSee('Test Confirmed');
});
});
73 changes: 73 additions & 0 deletions tests/Browser/ForgotPasswordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/**
* This is for the /auth/password/reset and /auth/password/hAsHeDsTrInG page
*/

use Devdojo\Auth\Tests\Browser\Pages\PasswordResetRequest;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;

uses(DatabaseMigrations::class);

test('Empty Email Validation', function () {
$browser = $this->browse(function (Browser $browser) {
$browser
->visit(new PasswordResetRequest)
->authAttributeRemove('#email', 'required')
->testValidationErrorOnSubmit('The email field is required');
});
});

test('Invalid Email Validation', function () {
$browser = $this->browse(function (Browser $browser) {
$browser
->visit(new PasswordResetRequest)
->authAttributeChange('#email', 'type', 'text')
->type('@email-input', 'johndoe')
->testValidationErrorOnSubmit('The email field must be a valid email address');
});
});

test('Email Does Not Exist', function () {
$browser = $this->browse(function (Browser $browser) {
$browser
->visit(new PasswordResetRequest)
->type('@email-input', 'jimmycrackcorn@gmail.com')
->testValidationErrorOnSubmit('We can\'t find a user with that email address');
});
});

test('Email reset functionality', function () {
$browser = $this->browse(function (Browser $browser) {

$browser
->visit(new PasswordResetRequest)
->createJohnDoe()
->clearLogFile()
->typeAndSubmit('@email-input', 'johndoe@gmail.com')
->waitForText('We have emailed your password reset link')
->getLogFile(function ($content) use ($browser) {

$foundLine = $this->findLineContainingSubstring($content, 'Reset Password:');
$url = str_replace('Reset Password: ', '', $foundLine);
$browser
->visit($url)
->assertSeeIn('#auth-heading-title', 'Reset Password')
->type('@password-input', 'password123')
->type('@password-confirm-input', 'password123')
->clickAndWaitForReload('@submit-button')
->assertRedirectAfterAuthUrlIsCorrect();
});
});
});

test('Link to Login Page', function () {
$browser = $this->browse(function (Browser $browser) {
$browser
->visit(new PasswordResetRequest)
->click('@login-link')
->waitFor('@auth-login')
->assertPathIs('/auth/login');
});
});
3 changes: 2 additions & 1 deletion tests/Browser/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
$this->browse(function (Browser $browser) {
$browser->visit(new Login)
->createJohnDoe()
->loginAsJohnDoe();
->formLoginAsJohnDoe()
->assertRedirectAfterAuthUrlIsCorrect();
});
});

Expand Down
Empty file added tests/Browser/LogoutTest.php
Empty file.
16 changes: 16 additions & 0 deletions tests/Browser/Pages/ConfirmPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Devdojo\Auth\Tests\Browser\Pages;

use Laravel\Dusk\Page;

class ConfirmPassword extends Page
{
/**
* Get the URL for the page.
*/
public function url(): string
{
return '/auth/password/confirm';
}
}
12 changes: 2 additions & 10 deletions tests/Browser/Pages/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,17 @@ public function url(): string
return '/auth/login';
}

public function loginAsJohnDoe(Browser $browser)
public function formLoginAsJohnDoe(Browser $browser)
{
$browser
->visit('/auth/login')
->type('@email-input', 'johndoe@gmail.com')
->click('@submit-button')
->waitFor('@password-input')
->type('@password-input', 'password')
->clickAndWaitForReload('@submit-button')
->assertRedirectAfterAuthUrlIsCorrect();
->clickAndWaitForReload('@submit-button');

return $this;
}

public function typeAndSubmit(Browser $browser, $selector, $value)
{
$browser->type($selector, $value)
->click('@submit-button');

return $this;
}
}
16 changes: 16 additions & 0 deletions tests/Browser/Pages/PasswordResetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Devdojo\Auth\Tests\Browser\Pages;

use Laravel\Dusk\Page;

class PasswordResetRequest extends Page
{
/**
* Get the URL for the page.
*/
public function url(): string
{
return '/auth/password/reset';
}
}
16 changes: 16 additions & 0 deletions tests/Browser/Pages/TwoFactorAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Devdojo\Auth\Tests\Browser\Pages;

use Laravel\Dusk\Page;

class TwoFactorAuth extends Page
{
/**
* Get the URL for the page.
*/
public function url(): string
{
return '/user/two-factor-authentication';
}
}
Loading

0 comments on commit ce6cf6e

Please sign in to comment.