From a87c5be4aa3f227d08d55b606569ae44b5be1129 Mon Sep 17 00:00:00 2001 From: Rhys Lees <43909932+RhysLees@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:27:18 +0100 Subject: [PATCH] WIP --- src/Commands/InstallCommand.php | 16 ----- src/Commands/InstallTestsCommand.php | 62 +++++++++++++++++++ src/LaravelAuthServiceProvider.php | 3 + .../Auth}/Feature/EmailVerificationTest.php | 17 ++--- .../Auth/Feature/LoginControllerTest.php | 33 ++++++++++ .../Auth/Feature/LogoutControllerTest.php | 25 ++++++++ .../Feature/RequestPasswordControllerTest.php | 46 ++++++++++++++ .../Feature/ResetPasswordControllerTest.php | 45 ++++++++++++++ .../tests/Auth}/Fixtures/FixtureUser.php | 2 +- tests/Feature/LogoutControllerTest.php | 19 ------ .../Feature/RequestPasswordControllerTest.php | 37 ----------- tests/Feature/ResetPasswordControllerTest.php | 36 ----------- 12 files changed, 224 insertions(+), 117 deletions(-) delete mode 100644 src/Commands/InstallCommand.php create mode 100644 src/Commands/InstallTestsCommand.php rename {tests => stubs/tests/Auth}/Feature/EmailVerificationTest.php (77%) create mode 100644 stubs/tests/Auth/Feature/LoginControllerTest.php create mode 100644 stubs/tests/Auth/Feature/LogoutControllerTest.php create mode 100644 stubs/tests/Auth/Feature/RequestPasswordControllerTest.php create mode 100644 stubs/tests/Auth/Feature/ResetPasswordControllerTest.php rename {tests => stubs/tests/Auth}/Fixtures/FixtureUser.php (85%) delete mode 100644 tests/Feature/LogoutControllerTest.php delete mode 100644 tests/Feature/RequestPasswordControllerTest.php delete mode 100644 tests/Feature/ResetPasswordControllerTest.php diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php deleted file mode 100644 index 055d669..0000000 --- a/src/Commands/InstallCommand.php +++ /dev/null @@ -1,16 +0,0 @@ -signature = $package->name.':install'; - } -} diff --git a/src/Commands/InstallTestsCommand.php b/src/Commands/InstallTestsCommand.php new file mode 100644 index 0000000..5a691db --- /dev/null +++ b/src/Commands/InstallTestsCommand.php @@ -0,0 +1,62 @@ +comment('Publishing Auth Tests...'); + + File::copyDirectory(__DIR__ . '/../../stubs/tests/', base_path('tests')); + + if(file_exists(base_path('phpunit.xml'))){ + $this->replaceInFile('', ' + + tests/Auth + ', base_path('phpunit.xml') + ); + } + + if(file_exists(base_path('phpunit.xml.dist'))){ + $this->replaceInFile('', ' + + tests/Auth + ', base_path('phpunit.xml.dist') + ); + } + + if(file_exists(base_path('tests/Pest.php'))){ + $this->replaceInFile(')->in(\'Feature\');', + ')->in(\'Feature\', \'Auth\');', + base_path('tests/Pest.php') + ); + } + + + + $this->comment('All done'); + + return self::SUCCESS; + } + + /** + * Replace a given string within a given file. + * + * @param string $search + * @param string $replace + * @param string $path + * @return void + */ + protected function replaceInFile($search, $replace, $path) + { + file_put_contents($path, str_replace($search, $replace, file_get_contents($path))); + } +} diff --git a/src/LaravelAuthServiceProvider.php b/src/LaravelAuthServiceProvider.php index f4ec1f9..3cde50c 100644 --- a/src/LaravelAuthServiceProvider.php +++ b/src/LaravelAuthServiceProvider.php @@ -2,6 +2,7 @@ namespace CodebarAg\LaravelAuth; +use CodebarAg\LaravelAuth\Commands\InstallTestsCommand; use CodebarAg\LaravelAuth\Models\AuthProvider; use CodebarAg\LaravelAuth\Observers\AuthProviderObserver; use Illuminate\Auth\Notifications\VerifyEmail; @@ -37,12 +38,14 @@ public function configurePackage(Package $package): void ->hasAssets() ->hasTranslations() ->hasMigration('create_auth_provider_table') + ->hasCommand(InstallTestsCommand::class) ->hasInstallCommand(function (InstallCommand $command) { $command ->publishConfigFile() ->publishAssets() ->publishMigrations() ->askToRunMigrations(); +// ->info('If you want to install the tests, run `php artisan auth:install-tests`'); }); } diff --git a/tests/Feature/EmailVerificationTest.php b/stubs/tests/Auth/Feature/EmailVerificationTest.php similarity index 77% rename from tests/Feature/EmailVerificationTest.php rename to stubs/tests/Auth/Feature/EmailVerificationTest.php index 2026ccf..db40d5b 100644 --- a/tests/Feature/EmailVerificationTest.php +++ b/stubs/tests/Auth/Feature/EmailVerificationTest.php @@ -11,9 +11,9 @@ ]); $this->actingAs($user) - ->get('/email/verify') + ->get(route('auth.verification.notice')) ->assertOk(); -})->group('auth', 'verify-email')->skip(); +})->group('auth', 'verify-email'); test('email can be verified', function () { Event::fake(); @@ -23,19 +23,19 @@ ]); $verificationUrl = URL::temporarySignedRoute( - 'verification.verify', + 'auth.verification.verify', now()->addMinutes(60), ['id' => $user->id, 'hash' => sha1($user->email)] ); $this->actingAs($user) ->get($verificationUrl) - ->assertRedirect(route('frontend.start.index')); + ->assertRedirect(); Event::assertDispatched(Verified::class); expect($user->fresh()->hasVerifiedEmail())->toBeTrue(); -})->group('auth', 'verify-email')->skip(); +})->group('auth', 'verify-email'); test('email can not verified with invalid hash', function () { $user = User::factory()->create([ @@ -43,12 +43,13 @@ ]); $verificationUrl = URL::temporarySignedRoute( - 'verification.verify', + 'auth.verification.verify', now()->addMinutes(60), ['id' => $user->id, 'hash' => sha1('wrong-email')] ); - $this->actingAs($user)->get($verificationUrl); + $this->actingAs($user) + ->get($verificationUrl); expect($user->fresh()->hasVerifiedEmail())->toBeFalse(); -})->group('auth', 'verify-email')->skip(); +})->group('auth', 'verify-email'); diff --git a/stubs/tests/Auth/Feature/LoginControllerTest.php b/stubs/tests/Auth/Feature/LoginControllerTest.php new file mode 100644 index 0000000..d0d7e04 --- /dev/null +++ b/stubs/tests/Auth/Feature/LoginControllerTest.php @@ -0,0 +1,33 @@ +get(route('auth.login')) + ->assertOk(); +})->group('auth', 'login'); + +it('authorized auth.login', function () { + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $this->actingAs($user) + ->get(route('auth.login')) + ->assertRedirect(); +})->group('auth', 'login'); + +test('authorized customer.auth.login.store', function () { + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $this->actingAs($user) + ->post(route('auth.login.store'), [ + 'email' => $user->email, + 'password' => 'password', + ]) + ->assertRedirect(); +})->group('auth', 'login'); diff --git a/stubs/tests/Auth/Feature/LogoutControllerTest.php b/stubs/tests/Auth/Feature/LogoutControllerTest.php new file mode 100644 index 0000000..557f727 --- /dev/null +++ b/stubs/tests/Auth/Feature/LogoutControllerTest.php @@ -0,0 +1,25 @@ +post(route('auth.logout')) + ->assertRedirect(); +})->group('auth', 'logout'); + +test('authorized auth.logout', function () { + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + Auth::login($user); + + assertAuthenticated(null, $user); + + $this->post(route('auth.logout')) + ->assertRedirect(); + + assertGuest(null, $user); +})->group('auth', 'logout'); diff --git a/stubs/tests/Auth/Feature/RequestPasswordControllerTest.php b/stubs/tests/Auth/Feature/RequestPasswordControllerTest.php new file mode 100644 index 0000000..10aa477 --- /dev/null +++ b/stubs/tests/Auth/Feature/RequestPasswordControllerTest.php @@ -0,0 +1,46 @@ +get(route('auth.request-password')); + $response->assertOk(); +})->group('auth', 'request-password'); + +test('authorized auth.request-password.index', function () { + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $this->actingAs($user) + ->get(route('auth.request-password')) + ->assertRedirect(); +})->group('auth', 'request-password'); + +test('unauthorized auth.request-password.store', function () { + Notification::fake(); + + $user = User::factory()->create(); + + $this->post(route('auth.request-password.store'), [ + 'email' => $user->email, + ]) + ->assertSessionDoesntHaveErrors() + ->assertRedirect(); + + Notification::assertSentTo($user, ResetPasswordNotification::class); +})->group('auth', 'request-password'); + +test('authorized auth.request-password.store', function () { + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $this->actingAs($user) + ->post(route('auth.request-password.store'), [ + 'email' => $user->email, + ]) + ->assertRedirect(); +})->group('auth', 'request-password'); diff --git a/stubs/tests/Auth/Feature/ResetPasswordControllerTest.php b/stubs/tests/Auth/Feature/ResetPasswordControllerTest.php new file mode 100644 index 0000000..40d7bcc --- /dev/null +++ b/stubs/tests/Auth/Feature/ResetPasswordControllerTest.php @@ -0,0 +1,45 @@ +create([ + 'email_verified_at' => null, + ]); + + $this->actingAs($user) + ->post(route('auth.request-password.store'), [ + 'email' => $user->email, + ]); + + $entry = DB::table(config('laravel-auth.password_reset_table'))->get()->first(); + + $this->withoutMiddleware([ValidateSignature::class]); + + $password = Str::random(); + + $response = $this->post(route('auth.reset-password.store'), [ + 'token' => $entry->token, + 'email' => $user->email, + 'password' => $password, + 'password_confirmation' => $password, + ]); + + $response->assertSessionDoesntHaveErrors(); + $response->assertRedirect(); +})->group('auth', 'reset-password'); + +test('authorized reset-password.store', function () { + $token = Str::random(); + + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $this->actingAs($user) + ->post(route('auth.reset-password.store', $token)) + ->assertRedirect(); +})->group('auth', 'reset-password'); diff --git a/tests/Fixtures/FixtureUser.php b/stubs/tests/Auth/Fixtures/FixtureUser.php similarity index 85% rename from tests/Fixtures/FixtureUser.php rename to stubs/tests/Auth/Fixtures/FixtureUser.php index 6c4597e..f08fe0b 100644 --- a/tests/Fixtures/FixtureUser.php +++ b/stubs/tests/Auth/Fixtures/FixtureUser.php @@ -1,6 +1,6 @@ post(route('logout')); - $response->assertRedirect(route('login')); -})->group('auth', 'logout')->skip(); - -test('authorized auth.logout', function () { - $user = authenticatedUser(); - assertAuthenticated(null, $user); - - $response = $this->post(route('logout')); - - assertGuest(null, $user); - $response->assertRedirect(route('frontend.start.index')); -})->group('auth', 'logout')->skip(); diff --git a/tests/Feature/RequestPasswordControllerTest.php b/tests/Feature/RequestPasswordControllerTest.php deleted file mode 100644 index 182fd85..0000000 --- a/tests/Feature/RequestPasswordControllerTest.php +++ /dev/null @@ -1,37 +0,0 @@ -get(route('request-password')); - $response->assertOk(); -})->group('auth', 'request-password')->skip(); - -test('authorized auth.request-password.index', function () { - - authenticatedUser(); - $response = $this->get(route('request-password')); - $response->assertRedirect(route('frontend.start.index')); -})->group('auth', 'request-password')->skip(); - -test('unauthorized auth.request-password.store', function () { - Notification::fake(); - - $user = user(); - - $response = $this->post(route('request-password.store'), [ - 'email' => $user->email, - ]); - - $response->assertSessionDoesntHaveErrors(); - $response->assertRedirect(route('request-password')); - - Notification::assertSentTo($user, ResetPasswordNotification::class); -})->group('auth', 'request-password')->skip(); - -test('authorized auth.request-password.store', function () { - authenticatedUser(); - $response = $this->post(route('request-password.store')); - $response->assertRedirect(route('frontend.start.index')); -})->group('auth', 'request-password')->skip(); diff --git a/tests/Feature/ResetPasswordControllerTest.php b/tests/Feature/ResetPasswordControllerTest.php deleted file mode 100644 index 4cf0429..0000000 --- a/tests/Feature/ResetPasswordControllerTest.php +++ /dev/null @@ -1,36 +0,0 @@ -post(route('request-password.store'), [ - 'email' => $user->email, - ]); - - $entry = DB::table('password_resets')->get()->first(); - - $this->withoutMiddleware([ValidateSignature::class]); - - $password = Str::random(); - - $response = $this->post(route('reset-password.store'), [ - 'token' => $entry->token, - 'email' => $user->email, - 'password' => $password, - 'password_confirmation' => $password, - ]); - - $response->assertSessionDoesntHaveErrors(); - $response->assertRedirect(route('frontend.start.index')); -})->group('auth', 'reset-password')->skip(); - -test('authorized reset-password.store', function () { - $token = Str::random(); - authenticatedUser(); - $response = $this->post(route('reset-password.store', $token)); - $response->assertRedirect(route('frontend.start.index')); -})->group('auth', 'reset-password')->skip();