Handle missing daemon keys better and fix subuser missing key errors
This commit is contained in:
parent
18e394eb14
commit
a4f03f5d02
6 changed files with 74 additions and 49 deletions
|
@ -4,7 +4,6 @@ namespace Tests\Unit\Http\Middleware\Server;
|
|||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
|
||||
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
|
@ -17,11 +16,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase
|
|||
*/
|
||||
private $keyProviderService;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Session\Session|\Mockery\Mock
|
||||
*/
|
||||
private $session;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
|
@ -30,7 +24,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase
|
|||
parent::setUp();
|
||||
|
||||
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
||||
$this->session = m::mock(Session::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,7 +36,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase
|
|||
$this->setRequestAttribute('server', $model);
|
||||
|
||||
$this->keyProviderService->shouldReceive('handle')->with($model, $user)->once()->andReturn('abc123');
|
||||
$this->session->shouldReceive('now')->with('server_data.token', 'abc123')->once()->andReturnNull();
|
||||
|
||||
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
$this->assertRequestHasAttribute('server_token');
|
||||
|
@ -74,6 +66,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase
|
|||
*/
|
||||
public function getMiddleware(): AuthenticateAsSubuser
|
||||
{
|
||||
return new AuthenticateAsSubuser($this->keyProviderService, $this->session);
|
||||
return new AuthenticateAsSubuser($this->keyProviderService);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,11 +14,13 @@ use Carbon\Carbon;
|
|||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Subuser;
|
||||
use Pterodactyl\Models\DaemonKey;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyProviderServiceTest extends TestCase
|
||||
|
@ -38,6 +40,11 @@ class DaemonKeyProviderServiceTest extends TestCase
|
|||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $subuserRepository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
|
@ -49,6 +56,7 @@ class DaemonKeyProviderServiceTest extends TestCase
|
|||
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
|
||||
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
$this->subuserRepository = m::mock(SubuserRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -154,6 +162,33 @@ class DaemonKeyProviderServiceTest extends TestCase
|
|||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a missing key is created for a subuser.
|
||||
*/
|
||||
public function testMissingKeyIsCreatedForSubuser()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$server = factory(Server::class)->make();
|
||||
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
||||
$subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->andReturn($subuser);
|
||||
|
||||
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
||||
|
||||
$response = $this->getService()->handle($server, $user, false);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the user should not get a key.
|
||||
*
|
||||
|
@ -169,6 +204,11 @@ class DaemonKeyProviderServiceTest extends TestCase
|
|||
['server_id', '=', $server->id],
|
||||
])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->getService()->handle($server, $user, false);
|
||||
}
|
||||
|
||||
|
@ -179,6 +219,11 @@ class DaemonKeyProviderServiceTest extends TestCase
|
|||
*/
|
||||
private function getService(): DaemonKeyProviderService
|
||||
{
|
||||
return new DaemonKeyProviderService($this->keyCreationService, $this->repository, $this->keyUpdateService);
|
||||
return new DaemonKeyProviderService(
|
||||
$this->keyCreationService,
|
||||
$this->repository,
|
||||
$this->keyUpdateService,
|
||||
$this->subuserRepository
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue