Skip to content

Commit

Permalink
add stubs (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
inmanturbo committed Feb 24, 2024
1 parent 224f95b commit b8ed39d
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 0 deletions.
39 changes: 39 additions & 0 deletions stubs/app/Providers/PassportServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Jetstream\Jetstream;
use Laravel\Passport\Passport;

class PassportServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
//
}

/**
* Bootstrap services.
*/
public function boot(): void
{
Passport::tokensCan([
'create' => 'Create resources',
'read' => 'Read Resources',
'update' => 'Update Resources',
'delete' => 'Delete Resources',
]);

// default scope for passport tokens
Passport::setDefaultScope([
// 'create',
'read',
// 'update',
// 'delete',
]);
}
}
10 changes: 10 additions & 0 deletions stubs/bootstrap/providers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

// This file is automatically generated by Laravel...

return [
App\Providers\AppServiceProvider::class,
App\Providers\FortifyServiceProvider::class,
App\Providers\JetstreamServiceProvider::class,
App\Providers\PassportServiceProvider::class,
];
121 changes: 121 additions & 0 deletions stubs/config/auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option defines the default authentication "guard" and password
| reset "broker" for your application. You may change these values
| as required, but they're a perfect start for most applications.
|
*/

'defaults' => [
'guard' => env('AUTH_GUARD', 'web'),
'passwords' => env('AUTH_PASSWORD_BROKER', 'users'),
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| which utilizes session storage plus the Eloquent user provider.
|
| All authentication guards have a user provider, which defines how the
| users are actually retrieved out of your database or other storage
| system used by the application. Typically, Eloquent is utilized.
|
| Supported: "session"
|
*/

'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication guards have a user provider, which defines how the
| users are actually retrieved out of your database or other storage
| system used by the application. Typically, Eloquent is utilized.
|
| If you have multiple user tables or models you may configure multiple
| providers to represent the model / table. These providers may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => env('AUTH_MODEL', App\Models\User::class),
],

// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],

/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| These configuration options specify the behavior of Laravel's password
| reset functionality, including the table utilized for token storage
| and the user provider that is invoked to actually retrieve users.
|
| The expiry time is the number of minutes that each reset token will be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
| The throttle setting is the number of seconds a user must wait before
| generating more password reset tokens. This prevents the user from
| quickly generating a very large amount of password reset tokens.
|
*/

'passwords' => [
'users' => [
'provider' => 'users',
'table' => env('AUTH_PASSWORD_RESET_TOKEN_TABLE', 'password_reset_tokens'),
'expire' => 60,
'throttle' => 60,
],
],

/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| window expires and users are asked to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/

'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),

];
21 changes: 21 additions & 0 deletions stubs/resources/views/api/index.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<x-app-layout>
<x-slot name="header">
<h2 class="text-xl font-semibold leading-tight text-gray-800">
{{ __('Api Tokens') }}
</h2>
</x-slot>

<div>
<div class="py-10 mx-auto max-w-7xl sm:px-6 lg:px-8">
@livewire('jetstream-passport.oauth-client-manager')
</div>
</div>

<x-section-border />

<div>
<div class="py-10 mx-auto max-w-7xl sm:px-6 lg:px-8">
@livewire('jetstream-passport.api-token-manager')
</div>
</div>
</x-app-layout>
36 changes: 36 additions & 0 deletions stubs/tests/Feature/ApiTokenPermissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use App\Models\User;
use HeaderX\JetstreamPassport\Http\Livewire\ApiTokenManager;
use Illuminate\Support\Facades\Artisan;
use Laravel\Jetstream\Features;
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());
}

Artisan::call('passport:client', ['--personal' => true, '--name' => 'Laravel Personal Access Client']);

$token = $user->createToken('Test Token', ['create', 'read'])->token;

Livewire::test(ApiTokenManager::class)
->set(['managingPermissionsForId' => $token->id])
->set(['updateApiTokenForm' => [
'scopes' => [
'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.');
35 changes: 35 additions & 0 deletions stubs/tests/Feature/CreateApiTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use App\Models\User;
use HeaderX\JetstreamPassport\Http\Livewire\ApiTokenManager;
use Illuminate\Support\Facades\Artisan;
use Laravel\Jetstream\Features;
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());
}

Artisan::call('passport:client', ['--personal' => true, '--name' => 'Laravel Personal Access Client']);

Livewire::test(ApiTokenManager::class)
->set(['createApiTokenForm' => [
'name' => 'Test Token',
'scopes' => [
'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.');
27 changes: 27 additions & 0 deletions stubs/tests/Feature/DeleteApiTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use App\Models\User;
use HeaderX\JetstreamPassport\Http\Livewire\ApiTokenManager;
use Illuminate\Support\Facades\Artisan;
use Laravel\Jetstream\Features;
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());
}

Artisan::call('passport:client', ['--personal' => true, '--name' => 'Laravel Personal Access Client']);

$token = $user->createToken('Test Token', ['create', 'read'])->token;

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.');

0 comments on commit b8ed39d

Please sign in to comment.