Add application API keys
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Enums\Permission;
|
||||
use App\Models\Project;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use App\Models\UserApplication;
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
use Laravel\Fortify\Features;
|
||||
|
||||
@@ -55,6 +56,26 @@ abstract class TestCase extends BaseTestCase
|
||||
);
|
||||
}
|
||||
|
||||
protected function withApplicationKey(User $user, ?string $name = null): static
|
||||
{
|
||||
$plainTextKey = $this->createPlainTextApplicationKey($user, $name);
|
||||
|
||||
return $this->withHeader('Authorization', "Bearer {$plainTextKey}");
|
||||
}
|
||||
|
||||
protected function createPlainTextApplicationKey(User $user, ?string $name = null): string
|
||||
{
|
||||
$plainTextKey = UserApplication::generatePlainTextKey();
|
||||
|
||||
UserApplication::query()->create([
|
||||
'key_hash' => UserApplication::hashKey($plainTextKey),
|
||||
'name' => $name ?? 'Test application',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
return $plainTextKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, Permission|string> $permissions
|
||||
* @return array<int, string>
|
||||
|
||||
Reference in New Issue
Block a user