Fix some broken tests

This commit is contained in:
Dane Everitt 2018-02-25 15:34:01 -06:00
parent e28973bcae
commit 4c41bd9075
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 36 additions and 13 deletions

View File

@ -1,10 +1,10 @@
<?php <?php
namespace Tests\Unit\Http\Middleware\Api\Application; namespace Tests\Unit\Http\Middleware\API;
use Pterodactyl\Models\ApiKey; use Pterodactyl\Models\ApiKey;
use Tests\Unit\Http\Middleware\MiddlewareTestCase; use Tests\Unit\Http\Middleware\MiddlewareTestCase;
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateIPAccess; use Pterodactyl\Http\Middleware\Api\AuthenticateIPAccess;
class AuthenticateIPAccessTest extends MiddlewareTestCase class AuthenticateIPAccessTest extends MiddlewareTestCase
{ {
@ -65,7 +65,7 @@ class AuthenticateIPAccessTest extends MiddlewareTestCase
/** /**
* Return an instance of the middleware to be used when testing. * Return an instance of the middleware to be used when testing.
* *
* @return \Pterodactyl\Http\Middleware\Api\Application\AuthenticateIPAccess * @return \Pterodactyl\Http\Middleware\Api\AuthenticateIPAccess
*/ */
private function getMiddleware(): AuthenticateIPAccess private function getMiddleware(): AuthenticateIPAccess
{ {

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Tests\Unit\Http\Middleware\Api\Application; namespace Tests\Unit\Http\Middleware\API;
use Mockery as m; use Mockery as m;
use Cake\Chronos\Chronos; use Cake\Chronos\Chronos;
@ -8,10 +8,10 @@ use Pterodactyl\Models\ApiKey;
use Illuminate\Auth\AuthManager; use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Tests\Unit\Http\Middleware\MiddlewareTestCase; use Tests\Unit\Http\Middleware\MiddlewareTestCase;
use Pterodactyl\Http\Middleware\Api\AuthenticateKey;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateKey;
class AuthenticateKeyTest extends MiddlewareTestCase class AuthenticateKeyTest extends MiddlewareTestCase
{ {
@ -51,7 +51,7 @@ class AuthenticateKeyTest extends MiddlewareTestCase
$this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull(); $this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull();
try { try {
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
} catch (HttpException $exception) { } catch (HttpException $exception) {
$this->assertEquals(401, $exception->getStatusCode()); $this->assertEquals(401, $exception->getStatusCode());
$this->assertEquals(['WWW-Authenticate' => 'Bearer'], $exception->getHeaders()); $this->assertEquals(['WWW-Authenticate' => 'Bearer'], $exception->getHeaders());
@ -68,7 +68,7 @@ class AuthenticateKeyTest extends MiddlewareTestCase
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn('abcd1234'); $this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn('abcd1234');
$this->repository->shouldReceive('findFirstWhere')->andThrow(new RecordNotFoundException); $this->repository->shouldReceive('findFirstWhere')->andThrow(new RecordNotFoundException);
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
} }
/** /**
@ -90,7 +90,30 @@ class AuthenticateKeyTest extends MiddlewareTestCase
'last_used_at' => Chronos::now(), 'last_used_at' => Chronos::now(),
])->once()->andReturnNull(); ])->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
$this->assertEquals($model, $this->request->attributes->get('api_key'));
}
/**
* Test that a valid token can continue past the middleware when set as a user token.
*/
public function testValidTokenWithUserKey()
{
$model = factory(ApiKey::class)->make();
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'decrypted');
$this->repository->shouldReceive('findFirstWhere')->with([
['identifier', '=', $model->identifier],
['key_type', '=', ApiKey::TYPE_ACCOUNT],
])->once()->andReturn($model);
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
$this->auth->shouldReceive('guard->loginUsingId')->with($model->user_id)->once()->andReturnNull();
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
'last_used_at' => Chronos::now(),
])->once()->andReturnNull();
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_ACCOUNT);
$this->assertEquals($model, $this->request->attributes->get('api_key')); $this->assertEquals($model, $this->request->attributes->get('api_key'));
} }
@ -111,13 +134,13 @@ class AuthenticateKeyTest extends MiddlewareTestCase
])->once()->andReturn($model); ])->once()->andReturn($model);
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted'); $this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
} }
/** /**
* Return an instance of the middleware with mocked dependencies for testing. * Return an instance of the middleware with mocked dependencies for testing.
* *
* @return \Pterodactyl\Http\Middleware\Api\Application\AuthenticateKey * @return \Pterodactyl\Http\Middleware\Api\AuthenticateKey
*/ */
private function getMiddleware(): AuthenticateKey private function getMiddleware(): AuthenticateKey
{ {

View File

@ -1,13 +1,13 @@
<?php <?php
namespace Tests\Unit\Http\Middleware\Api\Application; namespace Tests\Unit\Http\Middleware\API;
use Mockery as m; use Mockery as m;
use Barryvdh\Debugbar\LaravelDebugbar; use Barryvdh\Debugbar\LaravelDebugbar;
use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Foundation\Application;
use Tests\Unit\Http\Middleware\MiddlewareTestCase; use Tests\Unit\Http\Middleware\MiddlewareTestCase;
use Pterodactyl\Http\Middleware\Api\Application\SetSessionDriver; use Pterodactyl\Http\Middleware\Api\SetSessionDriver;
class SetSessionDriverTest extends MiddlewareTestCase class SetSessionDriverTest extends MiddlewareTestCase
{ {
@ -60,7 +60,7 @@ class SetSessionDriverTest extends MiddlewareTestCase
/** /**
* Return an instance of the middleware with mocked dependencies for testing. * Return an instance of the middleware with mocked dependencies for testing.
* *
* @return \Pterodactyl\Http\Middleware\Api\Application\SetSessionDriver * @return \Pterodactyl\Http\Middleware\Api\SetSessionDriver
*/ */
private function getMiddleware(): SetSessionDriver private function getMiddleware(): SetSessionDriver
{ {