misc_pterodactyl-panel/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php

61 lines
1.6 KiB
PHP
Raw Normal View History

2018-03-04 22:30:16 +00:00
<?php
namespace Pterodactyl\Tests\Integration\Api\Application;
use Pterodactyl\Models\User;
use Pterodactyl\Models\PersonalAccessToken;
2018-03-04 22:30:16 +00:00
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
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
/**
* @var \Pterodactyl\Models\User
2018-03-04 22:30:16 +00:00
*/
private $user;
2018-03-04 22:30:16 +00:00
/**
* @var string[]
2018-03-04 22:30:16 +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.
*/
public function setUp(): void
2018-03-04 22:30:16 +00:00
{
parent::setUp();
$this->user = User::factory()->create(['root_admin' => true]);
2018-03-04 22:30:16 +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.
*/
protected function createNewAccessToken(array $abilities = ['*']): PersonalAccessToken
2018-03-04 22:30:16 +00:00
{
$token = $this->user->createToken('test', $abilities);
2018-03-04 22:30:16 +00:00
$this->withHeader('Authorization', 'Bearer ' . $token->plainTextToken);
2018-03-04 22:30:16 +00:00
return $token->accessToken;
2018-03-04 22:30:16 +00:00
}
}