2017-09-02 05:21:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Base;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2018-01-14 18:06:15 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2017-09-02 05:21:15 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
use Pterodactyl\Services\Api\KeyCreationService;
|
2017-12-03 20:29:14 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2018-01-14 19:30:55 +00:00
|
|
|
use Pterodactyl\Http\Requests\Base\StoreAccountKeyRequest;
|
|
|
|
use Pterodactyl\Http\Controllers\Base\AccountKeyController;
|
2017-09-02 05:21:15 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
class AccountKeyControllerTest extends ControllerTestCase
|
2017-09-02 05:21:15 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-12-03 20:29:14 +00:00
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
2017-09-02 05:21:15 +00:00
|
|
|
*/
|
|
|
|
protected $alert;
|
|
|
|
|
|
|
|
/**
|
2017-12-03 20:29:14 +00:00
|
|
|
* @var \Pterodactyl\Services\Api\KeyCreationService|\Mockery\Mock
|
2017-09-02 05:21:15 +00:00
|
|
|
*/
|
|
|
|
protected $keyService;
|
|
|
|
|
|
|
|
/**
|
2017-12-03 20:29:14 +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()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->repository->shouldReceive('getAccountKeys')->with($model)->once()->andReturn(collect(['testkeys']));
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2017-12-03 20:29:14 +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);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertViewKeyEquals('keys', collect(['testkeys']), $response);
|
2017-09-02 05:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the create API view controller.
|
|
|
|
*/
|
2018-01-14 19:30:55 +00:00
|
|
|
public function testCreateController()
|
2017-09-02 05:21:15 +00:00
|
|
|
{
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->generateRequestUserModel();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2017-12-03 20:29:14 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-14 19:30:55 +00:00
|
|
|
* Test the store functionality for a user.
|
2017-09-02 05:21:15 +00:00
|
|
|
*/
|
2018-01-14 19:30:55 +00:00
|
|
|
public function testStoreController()
|
2017-09-02 05:21:15 +00:00
|
|
|
{
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->setRequestMockClass(StoreAccountKeyRequest::class);
|
|
|
|
$model = $this->generateRequestUserModel();
|
2018-01-14 18:06:15 +00:00
|
|
|
$keyModel = factory(ApiKey::class)->make();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('user')->withNoArgs()->andReturn($model);
|
|
|
|
$this->request->shouldReceive('input')->with('allowed_ips')->once()->andReturnNull();
|
|
|
|
$this->request->shouldReceive('input')->with('memo')->once()->andReturnNull();
|
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->keyService->shouldReceive('setKeyType')->with(ApiKey::TYPE_ACCOUNT)->once()->andReturnSelf();
|
2017-09-02 05:21:15 +00:00
|
|
|
$this->keyService->shouldReceive('handle')->with([
|
|
|
|
'user_id' => $model->id,
|
|
|
|
'allowed_ips' => null,
|
|
|
|
'memo' => null,
|
2018-01-14 19:30:55 +00:00
|
|
|
])->once()->andReturn($keyModel);
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2017-12-03 20:29:14 +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-12-03 20:29:14 +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()
|
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
$this->repository->shouldReceive('deleteAccountKey')->with($model, 'testIdentifier')->once()->andReturn(1);
|
2017-09-02 05:21:15 +00:00
|
|
|
|
2018-01-14 19:30:55 +00:00
|
|
|
$response = $this->getController()->revoke($this->request, 'testIdentifier');
|
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
|
|
|
}
|
|
|
|
|
2017-12-03 20:29:14 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the controller with mocked dependencies for testing.
|
|
|
|
*
|
2018-01-14 19:30:55 +00:00
|
|
|
* @return \Pterodactyl\Http\Controllers\Base\AccountKeyController
|
2017-12-03 20:29:14 +00:00
|
|
|
*/
|
2018-01-14 19:30:55 +00:00
|
|
|
private function getController(): AccountKeyController
|
2017-12-03 20:29:14 +00:00
|
|
|
{
|
2018-01-14 19:30:55 +00:00
|
|
|
return new AccountKeyController($this->alert, $this->repository, $this->keyService);
|
2017-12-03 20:29:14 +00:00
|
|
|
}
|
2017-09-02 05:21:15 +00:00
|
|
|
}
|