diff --git a/tests/Unit/Http/Middleware/Api/Application/AuthenticateIPAccessTest.php b/tests/Unit/Http/Middleware/API/AuthenticateIPAccessTest.php similarity index 91% rename from tests/Unit/Http/Middleware/Api/Application/AuthenticateIPAccessTest.php rename to tests/Unit/Http/Middleware/API/AuthenticateIPAccessTest.php index cf23d0292..59b9137d9 100644 --- a/tests/Unit/Http/Middleware/Api/Application/AuthenticateIPAccessTest.php +++ b/tests/Unit/Http/Middleware/API/AuthenticateIPAccessTest.php @@ -1,10 +1,10 @@ request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull(); try { - $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); + $this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION); } catch (HttpException $exception) { $this->assertEquals(401, $exception->getStatusCode()); $this->assertEquals(['WWW-Authenticate' => 'Bearer'], $exception->getHeaders()); @@ -68,7 +68,7 @@ class AuthenticateKeyTest extends MiddlewareTestCase $this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn('abcd1234'); $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(), ])->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')); } @@ -111,13 +134,13 @@ class AuthenticateKeyTest extends MiddlewareTestCase ])->once()->andReturn($model); $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 \Pterodactyl\Http\Middleware\Api\Application\AuthenticateKey + * @return \Pterodactyl\Http\Middleware\Api\AuthenticateKey */ private function getMiddleware(): AuthenticateKey { diff --git a/tests/Unit/Http/Middleware/Api/Application/SetSessionDriverTest.php b/tests/Unit/Http/Middleware/API/SetSessionDriverTest.php similarity index 90% rename from tests/Unit/Http/Middleware/Api/Application/SetSessionDriverTest.php rename to tests/Unit/Http/Middleware/API/SetSessionDriverTest.php index 7804f8209..0f33f6735 100644 --- a/tests/Unit/Http/Middleware/Api/Application/SetSessionDriverTest.php +++ b/tests/Unit/Http/Middleware/API/SetSessionDriverTest.php @@ -1,13 +1,13 @@