2018-03-04 22:30:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Application;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\User;
|
2021-08-07 22:31:52 +00:00
|
|
|
use Pterodactyl\Models\PersonalAccessToken;
|
2018-03-04 22:30:16 +00:00
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Tests\Traits\Integration\CreatesTestModels;
|
|
|
|
use Pterodactyl\Tests\Traits\Http\IntegrationJsonRequestAssertions;
|
2018-03-04 22:30:16 +00:00
|
|
|
|
|
|
|
abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
use CreatesTestModels;
|
|
|
|
use DatabaseTransactions;
|
|
|
|
use IntegrationJsonRequestAssertions;
|
2018-03-04 22:30:16 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-07 22:31:52 +00:00
|
|
|
* @var \Pterodactyl\Models\User
|
2018-03-04 22:30:16 +00:00
|
|
|
*/
|
2021-08-07 22:31:52 +00:00
|
|
|
private $user;
|
2018-03-04 22:30:16 +00:00
|
|
|
|
|
|
|
/**
|
2021-08-07 22:31:52 +00:00
|
|
|
* @var string[]
|
2018-03-04 22:30:16 +00:00
|
|
|
*/
|
2021-08-07 22:31:52 +00:00
|
|
|
protected $defaultHeaders = [
|
|
|
|
'Accept' => 'application/vnd.pterodactyl.v1+json',
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
];
|
2018-03-04 22:30:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap application API tests. Creates a default admin user and associated API key
|
|
|
|
* and also sets some default headers required for accessing the API.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2018-03-04 22:30:16 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-08-07 22:20:43 +00:00
|
|
|
$this->user = User::factory()->create(['root_admin' => true]);
|
2018-03-04 22:30:16 +00:00
|
|
|
|
2021-08-07 22:31:52 +00:00
|
|
|
$this->createNewAccessToken();
|
2018-03-04 22:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getApiUser(): User
|
|
|
|
{
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new default API key and refreshes the headers using it.
|
|
|
|
*/
|
2021-08-07 22:31:52 +00:00
|
|
|
protected function createNewAccessToken(array $abilities = ['*']): PersonalAccessToken
|
2018-03-04 22:30:16 +00:00
|
|
|
{
|
2021-08-07 22:31:52 +00:00
|
|
|
$token = $this->user->createToken('test', $abilities);
|
2018-03-04 22:30:16 +00:00
|
|
|
|
2021-08-07 22:31:52 +00:00
|
|
|
$this->withHeader('Authorization', 'Bearer ' . $token->plainTextToken);
|
2018-03-04 22:30:16 +00:00
|
|
|
|
2021-08-07 22:31:52 +00:00
|
|
|
return $token->accessToken;
|
2018-03-04 22:30:16 +00:00
|
|
|
}
|
|
|
|
}
|