2017-09-02 05:21:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Base;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\User;
|
2017-11-19 19:32:17 +00:00
|
|
|
use Pterodactyl\Models\APIKey;
|
2017-09-02 05:21:15 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
use Pterodactyl\Services\Api\KeyCreationService;
|
2017-11-19 19:32:17 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-09-02 05:21:15 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Base\APIController;
|
|
|
|
use Pterodactyl\Http\Requests\Base\ApiKeyFormRequest;
|
|
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
class APIControllerTest extends ControllerTestCase
|
2017-09-02 05:21:15 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-11-19 19:32:17 +00:00
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
2017-09-02 05:21:15 +00:00
|
|
|
*/
|
|
|
|
protected $alert;
|
|
|
|
|
|
|
|
/**
|
2017-11-19 19:32:17 +00:00
|
|
|
* @var \Pterodactyl\Services\Api\KeyCreationService|\Mockery\Mock
|
2017-09-02 05:21:15 +00:00
|
|
|
*/
|
|
|
|
protected $keyService;
|
|
|
|
|
|
|
|
/**
|
2017-11-19 19:32:17 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
|
2017-09-02 05:21:15 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->alert = m::mock(AlertsMessageBag::class);
|
|
|
|
$this->keyService = m::mock(KeyCreationService::class);
|
|
|
|
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index controller.
|
|
|
|
*/
|
|
|
|
public function testIndexController()
|
|
|
|
{
|
2017-11-19 19:32:17 +00:00
|
|
|
$model = $this->setRequestUser();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('findWhere')->with([['user_id', '=', $model->id]])->once()->andReturn(['testkeys']);
|
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
$response = $this->getController()->index($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
2017-09-02 05:21:15 +00:00
|
|
|
$this->assertViewNameEquals('base.api.index', $response);
|
|
|
|
$this->assertViewHasKey('keys', $response);
|
|
|
|
$this->assertViewKeyEquals('keys', ['testkeys'], $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the create API view controller.
|
|
|
|
*
|
|
|
|
* @dataProvider rootAdminDataProvider
|
|
|
|
*/
|
|
|
|
public function testCreateController($admin)
|
|
|
|
{
|
2017-11-19 19:32:17 +00:00
|
|
|
$this->setRequestUser(factory(User::class)->make(['root_admin' => $admin]));
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
$response = $this->getController()->create($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
2017-09-02 05:21:15 +00:00
|
|
|
$this->assertViewNameEquals('base.api.new', $response);
|
|
|
|
$this->assertViewHasKey('permissions.user', $response);
|
|
|
|
$this->assertViewHasKey('permissions.admin', $response);
|
|
|
|
|
|
|
|
if ($admin) {
|
|
|
|
$this->assertViewKeyNotEquals('permissions.admin', null, $response);
|
|
|
|
} else {
|
|
|
|
$this->assertViewKeyEquals('permissions.admin', null, $response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the store functionality for a non-admin user.
|
|
|
|
*
|
|
|
|
* @dataProvider rootAdminDataProvider
|
|
|
|
*/
|
|
|
|
public function testStoreController($admin)
|
|
|
|
{
|
2017-11-19 19:32:17 +00:00
|
|
|
$this->setRequestMockClass(ApiKeyFormRequest::class);
|
|
|
|
$model = $this->setRequestUser(factory(User::class)->make(['root_admin' => $admin]));
|
|
|
|
$keyModel = factory(APIKey::class)->make();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
|
|
|
if ($admin) {
|
|
|
|
$this->request->shouldReceive('input')->with('admin_permissions', [])->once()->andReturn(['admin.permission']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->andReturn($model);
|
|
|
|
$this->request->shouldReceive('input')->with('allowed_ips')->once()->andReturnNull();
|
|
|
|
$this->request->shouldReceive('input')->with('memo')->once()->andReturnNull();
|
|
|
|
$this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['test.permission']);
|
|
|
|
|
|
|
|
$this->keyService->shouldReceive('handle')->with([
|
|
|
|
'user_id' => $model->id,
|
|
|
|
'allowed_ips' => null,
|
|
|
|
'memo' => null,
|
2017-11-19 19:32:17 +00:00
|
|
|
], ['test.permission'], ($admin) ? ['admin.permission'] : [])->once()->andReturn($keyModel);
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
$this->alert->shouldReceive('success')->with(trans('base.api.index.keypair_created'))->once()->andReturnSelf();
|
|
|
|
$this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
$response = $this->getController()->store($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account.api', $response);
|
2017-09-02 05:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the API key revocation controller.
|
|
|
|
*/
|
|
|
|
public function testRevokeController()
|
|
|
|
{
|
2017-11-19 19:32:17 +00:00
|
|
|
$model = $this->setRequestUser();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('deleteWhere')->with([
|
|
|
|
['user_id', '=', $model->id],
|
2017-11-19 19:32:17 +00:00
|
|
|
['token', '=', 'testKey123'],
|
2017-09-02 05:21:15 +00:00
|
|
|
])->once()->andReturnNull();
|
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
$response = $this->getController()->revoke($this->request, 'testKey123');
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsResponse($response);
|
2017-09-02 05:21:15 +00:00
|
|
|
$this->assertEmpty($response->getContent());
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertResponseCodeEquals(204, $response);
|
2017-09-02 05:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data provider to determine if a user is a root admin.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rootAdminDataProvider()
|
|
|
|
{
|
|
|
|
return [[0], [1]];
|
|
|
|
}
|
2017-11-19 19:32:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the controller with mocked dependencies for testing.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Http\Controllers\Base\APIController
|
|
|
|
*/
|
|
|
|
private function getController(): APIController
|
|
|
|
{
|
|
|
|
return new APIController($this->alert, $this->repository, $this->keyService);
|
|
|
|
}
|
2017-09-02 05:21:15 +00:00
|
|
|
}
|