Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysLees committed Sep 11, 2023
1 parent e689980 commit a87c5be
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 117 deletions.
16 changes: 0 additions & 16 deletions src/Commands/InstallCommand.php

This file was deleted.

62 changes: 62 additions & 0 deletions src/Commands/InstallTestsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace CodebarAg\LaravelAuth\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class InstallTestsCommand extends Command
{
public $signature = 'auth:install-tests';

public $description = 'Install the auth tests';

public function handle(): int
{
$this->comment('Publishing Auth Tests...');

File::copyDirectory(__DIR__ . '/../../stubs/tests/', base_path('tests'));

if(file_exists(base_path('phpunit.xml'))){
$this->replaceInFile('<testsuites>', '<testsuites>
<testsuite name="Auth">
<directory>tests/Auth</directory>
</testsuite>', base_path('phpunit.xml')
);
}

if(file_exists(base_path('phpunit.xml.dist'))){
$this->replaceInFile('<testsuites>', '<testsuites>
<testsuite name="Auth">
<directory>tests/Auth</directory>
</testsuite>', 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)));
}
}
3 changes: 3 additions & 0 deletions src/LaravelAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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`');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -23,32 +23,33 @@
]);

$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([
'email_verified_at' => null,
]);

$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');
33 changes: 33 additions & 0 deletions stubs/tests/Auth/Feature/LoginControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Feature\Controllers\Employees;

use App\Models\User;

it('unauthorized auth.login', function () {
$this->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');
25 changes: 25 additions & 0 deletions stubs/tests/Auth/Feature/LogoutControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\Models\User;
use function Pest\Laravel\assertAuthenticated;
use function Pest\Laravel\assertGuest;

test('unauthorized auth.logout', function () {
$this->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');
46 changes: 46 additions & 0 deletions stubs/tests/Auth/Feature/RequestPasswordControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use App\Models\User;
use CodebarAg\LaravelAuth\Notifications\ResetPasswordNotification;
use Illuminate\Support\Facades\Notification;

test('unauthorized auth.request-password.index', function () {
$response = $this->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');
45 changes: 45 additions & 0 deletions stubs/tests/Auth/Feature/ResetPasswordControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use App\Models\User;
use Illuminate\Routing\Middleware\ValidateSignature;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

test('unauthorized auth.reset-password.store with valid token', function () {
$user = User::factory()->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');
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace CodebarAg\LaravelAuth\Tests\Fixtures;
namespace CodebarAg\LaravelAuth\Tests\Auth\Fixtures;

use CodebarAg\LaravelAuth\Traits\HasAuthProviders;
use Illuminate\Contracts\Auth\MustVerifyEmail;
Expand Down
19 changes: 0 additions & 19 deletions tests/Feature/LogoutControllerTest.php

This file was deleted.

37 changes: 0 additions & 37 deletions tests/Feature/RequestPasswordControllerTest.php

This file was deleted.

Loading

0 comments on commit a87c5be

Please sign in to comment.