|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Api\Application; |
| 4 | + |
| 5 | +use Pterodactyl\Models\User; |
| 6 | +use PHPUnit\Framework\Assert; |
| 7 | +use Pterodactyl\Models\ApiKey; |
| 8 | +use Pterodactyl\Services\Acl\Api\AdminAcl; |
| 9 | +use Tests\Traits\Integration\CreatesTestModels; |
| 10 | +use Tests\Traits\IntegrationJsonRequestAssertions; |
| 11 | +use Pterodactyl\Tests\Integration\IntegrationTestCase; |
| 12 | +use Illuminate\Foundation\Testing\DatabaseTransactions; |
| 13 | +use Pterodactyl\Transformers\Api\Application\BaseTransformer; |
| 14 | +use Pterodactyl\Transformers\Api\Client\BaseClientTransformer; |
| 15 | + |
| 16 | +abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase |
| 17 | +{ |
| 18 | + use CreatesTestModels, DatabaseTransactions, IntegrationJsonRequestAssertions; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Pterodactyl\Models\ApiKey |
| 22 | + */ |
| 23 | + private $key; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Pterodactyl\Models\User |
| 27 | + */ |
| 28 | + private $user; |
| 29 | + |
| 30 | + /** |
| 31 | + * Bootstrap application API tests. Creates a default admin user and associated API key |
| 32 | + * and also sets some default headers required for accessing the API. |
| 33 | + */ |
| 34 | + public function setUp() |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->user = $this->createApiUser(); |
| 39 | + $this->key = $this->createApiKey($this->user); |
| 40 | + |
| 41 | + $this->withHeader('Accept', 'application/vnd.pterodactyl.v1+json'); |
| 42 | + $this->withHeader('Authorization', 'Bearer ' . $this->getApiKey()->identifier . decrypt($this->getApiKey()->token)); |
| 43 | + |
| 44 | + $this->withMiddleware('api..key:' . ApiKey::TYPE_APPLICATION); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return \Pterodactyl\Models\User |
| 49 | + */ |
| 50 | + public function getApiUser(): User |
| 51 | + { |
| 52 | + return $this->user; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return \Pterodactyl\Models\ApiKey |
| 57 | + */ |
| 58 | + public function getApiKey(): ApiKey |
| 59 | + { |
| 60 | + return $this->key; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a new default API key and refreshes the headers using it. |
| 65 | + * |
| 66 | + * @param \Pterodactyl\Models\User $user |
| 67 | + * @param array $permissions |
| 68 | + * @return \Pterodactyl\Models\ApiKey |
| 69 | + */ |
| 70 | + protected function createNewDefaultApiKey(User $user, array $permissions = []): ApiKey |
| 71 | + { |
| 72 | + $this->key = $this->createApiKey($user, $permissions); |
| 73 | + $this->refreshHeaders($this->key); |
| 74 | + |
| 75 | + return $this->key; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Refresh the authorization header for a request to use a different API key. |
| 80 | + * |
| 81 | + * @param \Pterodactyl\Models\ApiKey $key |
| 82 | + */ |
| 83 | + protected function refreshHeaders(ApiKey $key) |
| 84 | + { |
| 85 | + $this->withHeader('Authorization', 'Bearer ' . $key->identifier . decrypt($key->token)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Create an administrative user. |
| 90 | + * |
| 91 | + * @return \Pterodactyl\Models\User |
| 92 | + */ |
| 93 | + protected function createApiUser(): User |
| 94 | + { |
| 95 | + return factory(User::class)->create([ |
| 96 | + 'root_admin' => true, |
| 97 | + ]); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Create a new application API key for a given user model. |
| 102 | + * |
| 103 | + * @param \Pterodactyl\Models\User $user |
| 104 | + * @param array $permissions |
| 105 | + * @return \Pterodactyl\Models\ApiKey |
| 106 | + */ |
| 107 | + protected function createApiKey(User $user, array $permissions = []): ApiKey |
| 108 | + { |
| 109 | + return factory(ApiKey::class)->create(array_merge([ |
| 110 | + 'user_id' => $user->id, |
| 111 | + 'key_type' => ApiKey::TYPE_APPLICATION, |
| 112 | + 'r_servers' => AdminAcl::READ | AdminAcl::WRITE, |
| 113 | + 'r_nodes' => AdminAcl::READ | AdminAcl::WRITE, |
| 114 | + 'r_allocations' => AdminAcl::READ | AdminAcl::WRITE, |
| 115 | + 'r_users' => AdminAcl::READ | AdminAcl::WRITE, |
| 116 | + 'r_locations' => AdminAcl::READ | AdminAcl::WRITE, |
| 117 | + 'r_nests' => AdminAcl::READ | AdminAcl::WRITE, |
| 118 | + 'r_eggs' => AdminAcl::READ | AdminAcl::WRITE, |
| 119 | + 'r_database_hosts' => AdminAcl::READ | AdminAcl::WRITE, |
| 120 | + 'r_server_databases' => AdminAcl::READ | AdminAcl::WRITE, |
| 121 | + 'r_packs' => AdminAcl::READ | AdminAcl::WRITE, |
| 122 | + ], $permissions)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Return a transformer that can be used for testing purposes. |
| 127 | + * |
| 128 | + * @param string $abstract |
| 129 | + * @return \Pterodactyl\Transformers\Api\Application\BaseTransformer |
| 130 | + */ |
| 131 | + protected function getTransformer(string $abstract): BaseTransformer |
| 132 | + { |
| 133 | + /** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */ |
| 134 | + $transformer = $this->app->make($abstract); |
| 135 | + $transformer->setKey($this->getApiKey()); |
| 136 | + |
| 137 | + Assert::assertInstanceOf(BaseTransformer::class, $transformer); |
| 138 | + Assert::assertNotInstanceOf(BaseClientTransformer::class, $transformer); |
| 139 | + |
| 140 | + return $transformer; |
| 141 | + } |
| 142 | +} |
0 commit comments