Install jetstream

This commit is contained in:
2025-03-10 15:07:11 +01:00
parent a8d5d57f6f
commit a9326a490b
98 changed files with 8383 additions and 58 deletions
+38
View File
@@ -0,0 +1,38 @@
<?php
use App\Models\User;
use Illuminate\Support\Str;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\Http\Livewire\ApiTokenManager;
use Livewire\Livewire;
test('api token permissions can be updated', function () {
if (Features::hasTeamFeatures()) {
$this->actingAs($user = User::factory()->withPersonalTeam()->create());
} else {
$this->actingAs($user = User::factory()->create());
}
$token = $user->tokens()->create([
'name' => 'Test Token',
'token' => Str::random(40),
'abilities' => ['create', 'read'],
]);
Livewire::test(ApiTokenManager::class)
->set(['managingPermissionsFor' => $token])
->set(['updateApiTokenForm' => [
'permissions' => [
'delete',
'missing-permission',
],
]])
->call('updateApiToken');
expect($user->fresh()->tokens->first())
->can('delete')->toBeTrue()
->can('read')->toBeFalse()
->can('missing-permission')->toBeFalse();
})->skip(function () {
return ! Features::hasApiFeatures();
}, 'API support is not enabled.');
+32
View File
@@ -0,0 +1,32 @@
<?php
use App\Models\User;
test('login screen can be rendered', function () {
$response = $this->get('/login');
$response->assertStatus(200);
});
test('users can authenticate using the login screen', function () {
$user = User::factory()->create();
$response = $this->post('/login', [
'email' => $user->email,
'password' => 'password',
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
});
test('users cannot authenticate with invalid password', function () {
$user = User::factory()->create();
$this->post('/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);
$this->assertGuest();
});
+14
View File
@@ -0,0 +1,14 @@
<?php
use App\Models\User;
use Laravel\Jetstream\Http\Livewire\LogoutOtherBrowserSessionsForm;
use Livewire\Livewire;
test('other browser sessions can be logged out', function () {
$this->actingAs(User::factory()->create());
Livewire::test(LogoutOtherBrowserSessionsForm::class)
->set('password', 'password')
->call('logoutOtherBrowserSessions')
->assertSuccessful();
});
+32
View File
@@ -0,0 +1,32 @@
<?php
use App\Models\User;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\Http\Livewire\ApiTokenManager;
use Livewire\Livewire;
test('api tokens can be created', function () {
if (Features::hasTeamFeatures()) {
$this->actingAs($user = User::factory()->withPersonalTeam()->create());
} else {
$this->actingAs($user = User::factory()->create());
}
Livewire::test(ApiTokenManager::class)
->set(['createApiTokenForm' => [
'name' => 'Test Token',
'permissions' => [
'read',
'update',
],
]])
->call('createApiToken');
expect($user->fresh()->tokens)->toHaveCount(1);
expect($user->fresh()->tokens->first())
->name->toEqual('Test Token')
->can('read')->toBeTrue()
->can('delete')->toBeFalse();
})->skip(function () {
return ! Features::hasApiFeatures();
}, 'API support is not enabled.');
+31
View File
@@ -0,0 +1,31 @@
<?php
use App\Models\User;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\Http\Livewire\DeleteUserForm;
use Livewire\Livewire;
test('user accounts can be deleted', function () {
$this->actingAs($user = User::factory()->create());
Livewire::test(DeleteUserForm::class)
->set('password', 'password')
->call('deleteUser');
expect($user->fresh())->toBeNull();
})->skip(function () {
return ! Features::hasAccountDeletionFeatures();
}, 'Account deletion is not enabled.');
test('correct password must be provided before account can be deleted', function () {
$this->actingAs($user = User::factory()->create());
Livewire::test(DeleteUserForm::class)
->set('password', 'wrong-password')
->call('deleteUser')
->assertHasErrors(['password']);
expect($user->fresh())->not->toBeNull();
})->skip(function () {
return ! Features::hasAccountDeletionFeatures();
}, 'Account deletion is not enabled.');
+29
View File
@@ -0,0 +1,29 @@
<?php
use App\Models\User;
use Illuminate\Support\Str;
use Laravel\Jetstream\Features;
use Laravel\Jetstream\Http\Livewire\ApiTokenManager;
use Livewire\Livewire;
test('api tokens can be deleted', function () {
if (Features::hasTeamFeatures()) {
$this->actingAs($user = User::factory()->withPersonalTeam()->create());
} else {
$this->actingAs($user = User::factory()->create());
}
$token = $user->tokens()->create([
'name' => 'Test Token',
'token' => Str::random(40),
'abilities' => ['create', 'read'],
]);
Livewire::test(ApiTokenManager::class)
->set(['apiTokenIdBeingDeleted' => $token->id])
->call('deleteApiToken');
expect($user->fresh()->tokens)->toHaveCount(0);
})->skip(function () {
return ! Features::hasApiFeatures();
}, 'API support is not enabled.');
+60
View File
@@ -0,0 +1,60 @@
<?php
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Laravel\Fortify\Features;
test('email verification screen can be rendered', function () {
$user = User::factory()->withPersonalTeam()->create([
'email_verified_at' => null,
]);
$response = $this->actingAs($user)->get('/email/verify');
$response->assertStatus(200);
})->skip(function () {
return ! Features::enabled(Features::emailVerification());
}, 'Email verification not enabled.');
test('email can be verified', function () {
Event::fake();
$user = User::factory()->create([
'email_verified_at' => null,
]);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);
$response = $this->actingAs($user)->get($verificationUrl);
Event::assertDispatched(Verified::class);
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
})->skip(function () {
return ! Features::enabled(Features::emailVerification());
}, 'Email verification not enabled.');
test('email can not verified with invalid hash', function () {
$user = User::factory()->create([
'email_verified_at' => null,
]);
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);
$this->actingAs($user)->get($verificationUrl);
expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
})->skip(function () {
return ! Features::enabled(Features::emailVerification());
}, 'Email verification not enabled.');
+4 -16
View File
@@ -1,19 +1,7 @@
<?php
namespace Tests\Feature;
it('returns a successful response', function () {
$response = $this->get('/');
// use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_the_application_returns_a_successful_response(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
$response->assertStatus(200);
});
@@ -0,0 +1,35 @@
<?php
use App\Models\User;
use Laravel\Jetstream\Features;
test('confirm password screen can be rendered', function () {
$user = Features::hasTeamFeatures()
? User::factory()->withPersonalTeam()->create()
: User::factory()->create();
$response = $this->actingAs($user)->get('/user/confirm-password');
$response->assertStatus(200);
});
test('password can be confirmed', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/user/confirm-password', [
'password' => 'password',
]);
$response->assertRedirect();
$response->assertSessionHasNoErrors();
});
test('password is not confirmed with invalid password', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post('/user/confirm-password', [
'password' => 'wrong-password',
]);
$response->assertSessionHasErrors();
});
+73
View File
@@ -0,0 +1,73 @@
<?php
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;
use Laravel\Fortify\Features;
test('reset password link screen can be rendered', function () {
$response = $this->get('/forgot-password');
$response->assertStatus(200);
})->skip(function () {
return ! Features::enabled(Features::resetPasswords());
}, 'Password updates are not enabled.');
test('reset password link can be requested', function () {
Notification::fake();
$user = User::factory()->create();
$response = $this->post('/forgot-password', [
'email' => $user->email,
]);
Notification::assertSentTo($user, ResetPassword::class);
})->skip(function () {
return ! Features::enabled(Features::resetPasswords());
}, 'Password updates are not enabled.');
test('reset password screen can be rendered', function () {
Notification::fake();
$user = User::factory()->create();
$response = $this->post('/forgot-password', [
'email' => $user->email,
]);
Notification::assertSentTo($user, ResetPassword::class, function (object $notification) {
$response = $this->get('/reset-password/'.$notification->token);
$response->assertStatus(200);
return true;
});
})->skip(function () {
return ! Features::enabled(Features::resetPasswords());
}, 'Password updates are not enabled.');
test('password can be reset with valid token', function () {
Notification::fake();
$user = User::factory()->create();
$response = $this->post('/forgot-password', [
'email' => $user->email,
]);
Notification::assertSentTo($user, ResetPassword::class, function (object $notification) use ($user) {
$response = $this->post('/reset-password', [
'token' => $notification->token,
'email' => $user->email,
'password' => 'password',
'password_confirmation' => 'password',
]);
$response->assertSessionHasNoErrors();
return true;
});
})->skip(function () {
return ! Features::enabled(Features::resetPasswords());
}, 'Password updates are not enabled.');
+26
View File
@@ -0,0 +1,26 @@
<?php
use App\Models\User;
use Laravel\Jetstream\Http\Livewire\UpdateProfileInformationForm;
use Livewire\Livewire;
test('current profile information is available', function () {
$this->actingAs($user = User::factory()->create());
$component = Livewire::test(UpdateProfileInformationForm::class);
expect($component->state['name'])->toEqual($user->name);
expect($component->state['email'])->toEqual($user->email);
});
test('profile information can be updated', function () {
$this->actingAs($user = User::factory()->create());
Livewire::test(UpdateProfileInformationForm::class)
->set('state', ['name' => 'Test Name', 'email' => 'test@example.com'])
->call('updateProfileInformation');
expect($user->fresh())
->name->toEqual('Test Name')
->email->toEqual('test@example.com');
});
+35
View File
@@ -0,0 +1,35 @@
<?php
use Laravel\Fortify\Features;
use Laravel\Jetstream\Jetstream;
test('registration screen can be rendered', function () {
$response = $this->get('/register');
$response->assertStatus(200);
})->skip(function () {
return ! Features::enabled(Features::registration());
}, 'Registration support is not enabled.');
test('registration screen cannot be rendered if support is disabled', function () {
$response = $this->get('/register');
$response->assertStatus(404);
})->skip(function () {
return Features::enabled(Features::registration());
}, 'Registration support is enabled.');
test('new users can register', function () {
$response = $this->post('/register', [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'password',
'password_confirmation' => 'password',
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature(),
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
})->skip(function () {
return ! Features::enabled(Features::registration());
}, 'Registration support is not enabled.');
@@ -0,0 +1,58 @@
<?php
use App\Models\User;
use Laravel\Fortify\Features;
use Laravel\Jetstream\Http\Livewire\TwoFactorAuthenticationForm;
use Livewire\Livewire;
test('two factor authentication can be enabled', function () {
$this->actingAs($user = User::factory()->create()->fresh());
$this->withSession(['auth.password_confirmed_at' => time()]);
Livewire::test(TwoFactorAuthenticationForm::class)
->call('enableTwoFactorAuthentication');
$user = $user->fresh();
expect($user->two_factor_secret)->not->toBeNull();
expect($user->recoveryCodes())->toHaveCount(8);
})->skip(function () {
return ! Features::canManageTwoFactorAuthentication();
}, 'Two factor authentication is not enabled.');
test('recovery codes can be regenerated', function () {
$this->actingAs($user = User::factory()->create()->fresh());
$this->withSession(['auth.password_confirmed_at' => time()]);
$component = Livewire::test(TwoFactorAuthenticationForm::class)
->call('enableTwoFactorAuthentication')
->call('regenerateRecoveryCodes');
$user = $user->fresh();
$component->call('regenerateRecoveryCodes');
expect($user->recoveryCodes())->toHaveCount(8);
expect(array_diff($user->recoveryCodes(), $user->fresh()->recoveryCodes()))->toHaveCount(8);
})->skip(function () {
return ! Features::canManageTwoFactorAuthentication();
}, 'Two factor authentication is not enabled.');
test('two factor authentication can be disabled', function () {
$this->actingAs($user = User::factory()->create()->fresh());
$this->withSession(['auth.password_confirmed_at' => time()]);
$component = Livewire::test(TwoFactorAuthenticationForm::class)
->call('enableTwoFactorAuthentication');
$this->assertNotNull($user->fresh()->two_factor_secret);
$component->call('disableTwoFactorAuthentication');
expect($user->fresh()->two_factor_secret)->toBeNull();
})->skip(function () {
return ! Features::canManageTwoFactorAuthentication();
}, 'Two factor authentication is not enabled.');
+50
View File
@@ -0,0 +1,50 @@
<?php
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Laravel\Jetstream\Http\Livewire\UpdatePasswordForm;
use Livewire\Livewire;
test('password can be updated', function () {
$this->actingAs($user = User::factory()->create());
Livewire::test(UpdatePasswordForm::class)
->set('state', [
'current_password' => 'password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])
->call('updatePassword');
expect(Hash::check('new-password', $user->fresh()->password))->toBeTrue();
});
test('current password must be correct', function () {
$this->actingAs($user = User::factory()->create());
Livewire::test(UpdatePasswordForm::class)
->set('state', [
'current_password' => 'wrong-password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
])
->call('updatePassword')
->assertHasErrors(['current_password']);
expect(Hash::check('password', $user->fresh()->password))->toBeTrue();
});
test('new passwords must match', function () {
$this->actingAs($user = User::factory()->create());
Livewire::test(UpdatePasswordForm::class)
->set('state', [
'current_password' => 'password',
'password' => 'new-password',
'password_confirmation' => 'wrong-password',
])
->call('updatePassword')
->assertHasErrors(['password']);
expect(Hash::check('password', $user->fresh()->password))->toBeTrue();
});
+47
View File
@@ -0,0 +1,47 @@
<?php
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "pest()" function to bind a different classes or traits.
|
*/
pest()->extend(Tests\TestCase::class)
->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
->in('Feature');
/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/
function something()
{
// ..
}
+3 -14
View File
@@ -1,16 +1,5 @@
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}
test('that true is true', function () {
expect(true)->toBeTrue();
});