Skip to content

Commit 4c41bd9

Browse files
committed
Fix some broken tests
1 parent e28973b commit 4c41bd9

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

tests/Unit/Http/Middleware/Api/Application/AuthenticateIPAccessTest.php renamed to tests/Unit/Http/Middleware/API/AuthenticateIPAccessTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Tests\Unit\Http\Middleware\Api\Application;
3+
namespace Tests\Unit\Http\Middleware\API;
44

55
use Pterodactyl\Models\ApiKey;
66
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
7-
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateIPAccess;
7+
use Pterodactyl\Http\Middleware\Api\AuthenticateIPAccess;
88

99
class AuthenticateIPAccessTest extends MiddlewareTestCase
1010
{
@@ -65,7 +65,7 @@ public function testWithInvalidIP()
6565
/**
6666
* Return an instance of the middleware to be used when testing.
6767
*
68-
* @return \Pterodactyl\Http\Middleware\Api\Application\AuthenticateIPAccess
68+
* @return \Pterodactyl\Http\Middleware\Api\AuthenticateIPAccess
6969
*/
7070
private function getMiddleware(): AuthenticateIPAccess
7171
{

tests/Unit/Http/Middleware/Api/Application/AuthenticateKeyTest.php renamed to tests/Unit/Http/Middleware/API/AuthenticateKeyTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3-
namespace Tests\Unit\Http\Middleware\Api\Application;
3+
namespace Tests\Unit\Http\Middleware\API;
44

55
use Mockery as m;
66
use Cake\Chronos\Chronos;
77
use Pterodactyl\Models\ApiKey;
88
use Illuminate\Auth\AuthManager;
99
use Illuminate\Contracts\Encryption\Encrypter;
1010
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
11+
use Pterodactyl\Http\Middleware\Api\AuthenticateKey;
1112
use Symfony\Component\HttpKernel\Exception\HttpException;
1213
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
1314
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
14-
use Pterodactyl\Http\Middleware\Api\Application\AuthenticateKey;
1515

1616
class AuthenticateKeyTest extends MiddlewareTestCase
1717
{
@@ -51,7 +51,7 @@ public function testMissingBearerTokenThrowsException()
5151
$this->request->shouldReceive('bearerToken')->withNoArgs()->once()->andReturnNull();
5252

5353
try {
54-
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
54+
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
5555
} catch (HttpException $exception) {
5656
$this->assertEquals(401, $exception->getStatusCode());
5757
$this->assertEquals(['WWW-Authenticate' => 'Bearer'], $exception->getHeaders());
@@ -68,7 +68,7 @@ public function testInvalidIdentifier()
6868
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn('abcd1234');
6969
$this->repository->shouldReceive('findFirstWhere')->andThrow(new RecordNotFoundException);
7070

71-
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
71+
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
7272
}
7373

7474
/**
@@ -90,7 +90,30 @@ public function testValidToken()
9090
'last_used_at' => Chronos::now(),
9191
])->once()->andReturnNull();
9292

93-
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
93+
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
94+
$this->assertEquals($model, $this->request->attributes->get('api_key'));
95+
}
96+
97+
/**
98+
* Test that a valid token can continue past the middleware when set as a user token.
99+
*/
100+
public function testValidTokenWithUserKey()
101+
{
102+
$model = factory(ApiKey::class)->make();
103+
104+
$this->request->shouldReceive('bearerToken')->withNoArgs()->twice()->andReturn($model->identifier . 'decrypted');
105+
$this->repository->shouldReceive('findFirstWhere')->with([
106+
['identifier', '=', $model->identifier],
107+
['key_type', '=', ApiKey::TYPE_ACCOUNT],
108+
])->once()->andReturn($model);
109+
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
110+
$this->auth->shouldReceive('guard->loginUsingId')->with($model->user_id)->once()->andReturnNull();
111+
112+
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
113+
'last_used_at' => Chronos::now(),
114+
])->once()->andReturnNull();
115+
116+
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_ACCOUNT);
94117
$this->assertEquals($model, $this->request->attributes->get('api_key'));
95118
}
96119

@@ -111,13 +134,13 @@ public function testInvalidTokenForIdentifier()
111134
])->once()->andReturn($model);
112135
$this->encrypter->shouldReceive('decrypt')->with($model->token)->once()->andReturn('decrypted');
113136

114-
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
137+
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions(), ApiKey::TYPE_APPLICATION);
115138
}
116139

117140
/**
118141
* Return an instance of the middleware with mocked dependencies for testing.
119142
*
120-
* @return \Pterodactyl\Http\Middleware\Api\Application\AuthenticateKey
143+
* @return \Pterodactyl\Http\Middleware\Api\AuthenticateKey
121144
*/
122145
private function getMiddleware(): AuthenticateKey
123146
{

tests/Unit/Http/Middleware/Api/Application/SetSessionDriverTest.php renamed to tests/Unit/Http/Middleware/API/SetSessionDriverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
namespace Tests\Unit\Http\Middleware\Api\Application;
3+
namespace Tests\Unit\Http\Middleware\API;
44

55
use Mockery as m;
66
use Barryvdh\Debugbar\LaravelDebugbar;
77
use Illuminate\Contracts\Config\Repository;
88
use Illuminate\Contracts\Foundation\Application;
99
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
10-
use Pterodactyl\Http\Middleware\Api\Application\SetSessionDriver;
10+
use Pterodactyl\Http\Middleware\Api\SetSessionDriver;
1111

1212
class SetSessionDriverTest extends MiddlewareTestCase
1313
{
@@ -60,7 +60,7 @@ public function testLocalEnvironment()
6060
/**
6161
* Return an instance of the middleware with mocked dependencies for testing.
6262
*
63-
* @return \Pterodactyl\Http\Middleware\Api\Application\SetSessionDriver
63+
* @return \Pterodactyl\Http\Middleware\Api\SetSessionDriver
6464
*/
6565
private function getMiddleware(): SetSessionDriver
6666
{

0 commit comments

Comments
 (0)