Skip to content

Commit c82f273

Browse files
committed
Fix remaining broken tests
1 parent 6c20ea9 commit c82f273

File tree

7 files changed

+22
-61
lines changed

7 files changed

+22
-61
lines changed

app/Http/Middleware/Api/SetSessionDriver.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7-
use Illuminate\Contracts\Foundation\Application;
87
use Illuminate\Contracts\Config\Repository as ConfigRepository;
98

109
class SetSessionDriver
1110
{
12-
/**
13-
* @var \Illuminate\Contracts\Foundation\Application
14-
*/
15-
private $app;
16-
1711
/**
1812
* @var \Illuminate\Contracts\Config\Repository
1913
*/
@@ -22,12 +16,10 @@ class SetSessionDriver
2216
/**
2317
* SetSessionDriver constructor.
2418
*
25-
* @param \Illuminate\Contracts\Foundation\Application $app
26-
* @param \Illuminate\Contracts\Config\Repository $config
19+
* @param \Illuminate\Contracts\Config\Repository $config
2720
*/
28-
public function __construct(Application $app, ConfigRepository $config)
21+
public function __construct(ConfigRepository $config)
2922
{
30-
$this->app = $app;
3123
$this->config = $config;
3224
}
3325

app/Http/Middleware/RequireTwoFactorAuthentication.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Pterodactyl\Http\Middleware;
1111

1212
use Closure;
13+
use Illuminate\Support\Str;
1314
use Illuminate\Http\Request;
1415
use Prologue\Alerts\AlertsMessageBag;
1516

@@ -24,27 +25,12 @@ class RequireTwoFactorAuthentication
2425
*/
2526
private $alert;
2627

27-
/**
28-
* The names of routes that should be accessible without 2FA enabled.
29-
*
30-
* @var array
31-
*/
32-
protected $except = [
33-
'account.security',
34-
'account.security.revoke',
35-
'account.security.totp',
36-
'account.security.totp.set',
37-
'account.security.totp.disable',
38-
'auth.totp',
39-
'auth.logout',
40-
];
41-
4228
/**
4329
* The route to redirect a user to to enable 2FA.
4430
*
4531
* @var string
4632
*/
47-
protected $redirectRoute = 'account.security';
33+
protected $redirectRoute = 'account';
4834

4935
/**
5036
* RequireTwoFactorAuthentication constructor.
@@ -69,7 +55,8 @@ public function handle(Request $request, Closure $next)
6955
return $next($request);
7056
}
7157

72-
if (in_array($request->route()->getName(), $this->except)) {
58+
$current = $request->route()->getName();
59+
if (in_array($current, ['auth', 'account']) || Str::startsWith($current, ['auth.', 'account.'])) {
7360
return $next($request);
7461
}
7562

routes/base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
Route::get('/', 'IndexController@index')->name('index');
4+
Route::get('/account', 'IndexController@index')->name('account');
45

56
/*
67
|--------------------------------------------------------------------------

tests/Unit/Http/Controllers/Base/IndexControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testIndexController()
7878

7979
$response = $this->controller->index($this->request);
8080
$this->assertIsViewResponse($response);
81-
$this->assertViewNameEquals('base.index', $response);
81+
$this->assertViewNameEquals('templates.base.core', $response);
8282
$this->assertViewHasKey('servers', $response);
8383
$this->assertViewKeyEquals('servers', $paginator, $response);
8484
}

tests/Unit/Http/Middleware/API/SetSessionDriverTest.php

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@
33
namespace Tests\Unit\Http\Middleware\API;
44

55
use Mockery as m;
6-
use Barryvdh\Debugbar\LaravelDebugbar;
76
use Illuminate\Contracts\Config\Repository;
8-
use Illuminate\Contracts\Foundation\Application;
97
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
108
use Pterodactyl\Http\Middleware\Api\SetSessionDriver;
119

1210
class SetSessionDriverTest extends MiddlewareTestCase
1311
{
14-
/**
15-
* @var \Illuminate\Contracts\Foundation\Application|\Mockery\Mock
16-
*/
17-
private $appMock;
18-
1912
/**
2013
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2114
*/
@@ -28,29 +21,14 @@ public function setUp()
2821
{
2922
parent::setUp();
3023

31-
$this->appMock = m::mock(Application::class);
3224
$this->config = m::mock(Repository::class);
3325
}
3426

3527
/**
3628
* Test that a production environment does not try to disable debug bar.
3729
*/
38-
public function testProductionEnvironment()
39-
{
40-
$this->config->shouldReceive('get')->once()->with('app.debug')->andReturn(false);
41-
$this->config->shouldReceive('set')->once()->with('session.driver', 'array')->andReturnNull();
42-
43-
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
44-
}
45-
46-
/**
47-
* Test that a local environment does disable debug bar.
48-
*/
49-
public function testLocalEnvironment()
30+
public function testMiddleware()
5031
{
51-
$this->config->shouldReceive('get')->once()->with('app.debug')->andReturn(true);
52-
$this->appMock->shouldReceive('make')->once()->with(LaravelDebugbar::class)->andReturnSelf();
53-
$this->appMock->shouldReceive('disable')->once()->withNoArgs()->andReturnNull();
5432
$this->config->shouldReceive('set')->once()->with('session.driver', 'array')->andReturnNull();
5533

5634
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
@@ -63,6 +41,6 @@ public function testLocalEnvironment()
6341
*/
6442
private function getMiddleware(): SetSessionDriver
6543
{
66-
return new SetSessionDriver($this->appMock, $this->config);
44+
return new SetSessionDriver($this->config);
6745
}
6846
}

tests/Unit/Http/Middleware/RequireTwoFactorAuthenticationTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testTwoFactorEnabledForAdminsAsAdminUserWith2FADisabled()
8888

8989
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
9090
$this->assertInstanceOf(RedirectResponse::class, $response);
91-
$this->assertEquals(route('account.security'), $response->getTargetUrl());
91+
$this->assertEquals(route('account'), $response->getTargetUrl());
9292
}
9393

9494
/**
@@ -132,7 +132,7 @@ public function testTwoFactorEnabledForAllUsersAsUserWith2FADisabled()
132132

133133
$response = $this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
134134
$this->assertInstanceOf(RedirectResponse::class, $response);
135-
$this->assertEquals(route('account.security'), $response->getTargetUrl());
135+
$this->assertEquals(route('account'), $response->getTargetUrl());
136136
}
137137

138138
/**
@@ -156,7 +156,8 @@ public function testTwoFactorEnabledForAllUsersAsUserWith2FAEnabled()
156156
public function ignoredRoutesDataProvider()
157157
{
158158
return [
159-
['account.security'],
159+
['auth'],
160+
['account'],
160161
['account.security.revoke'],
161162
['account.security.totp'],
162163
['account.security.totp.set'],

tests/Unit/Services/Users/TwoFactorSetupServiceTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Tests\TestCase;
77
use Pterodactyl\Models\User;
88
use PragmaRX\Google2FA\Google2FA;
9-
use Illuminate\Contracts\Config\Repository;
9+
use Illuminate\Support\Collection;
1010
use Illuminate\Contracts\Encryption\Encrypter;
1111
use Pterodactyl\Services\Users\TwoFactorSetupService;
1212
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
@@ -40,7 +40,6 @@ public function setUp()
4040
{
4141
parent::setUp();
4242

43-
$this->config = m::mock(Repository::class);
4443
$this->encrypter = m::mock(Encrypter::class);
4544
$this->google2FA = m::mock(Google2FA::class);
4645
$this->repository = m::mock(UserRepositoryInterface::class);
@@ -53,16 +52,19 @@ public function testSecretAndImageAreReturned()
5352
{
5453
$model = factory(User::class)->make();
5554

56-
$this->config->shouldReceive('get')->with('pterodactyl.auth.2fa.bytes')->once()->andReturn(32);
55+
config()->set('pterodactyl.auth.2fa.bytes', 32);
56+
config()->set('app.name', 'CompanyName');
57+
5758
$this->google2FA->shouldReceive('generateSecretKey')->with(32)->once()->andReturn('secretKey');
58-
$this->config->shouldReceive('get')->with('app.name')->once()->andReturn('CompanyName');
5959
$this->google2FA->shouldReceive('getQRCodeGoogleUrl')->with('CompanyName', $model->email, 'secretKey')->once()->andReturn('http://url.com');
6060
$this->encrypter->shouldReceive('encrypt')->with('secretKey')->once()->andReturn('encryptedSecret');
6161
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, ['totp_secret' => 'encryptedSecret'])->once()->andReturnNull();
6262

6363
$response = $this->getService()->handle($model);
6464
$this->assertNotEmpty($response);
65-
$this->assertSame('http://url.com', $response);
65+
$this->assertInstanceOf(Collection::class, $response);
66+
$this->assertSame('http://url.com', $response->get('image'));
67+
$this->assertSame('secretKey', $response->get('secret'));
6668
}
6769

6870
/**
@@ -72,6 +74,6 @@ public function testSecretAndImageAreReturned()
7274
*/
7375
private function getService(): TwoFactorSetupService
7476
{
75-
return new TwoFactorSetupService($this->config, $this->encrypter, $this->google2FA, $this->repository);
77+
return new TwoFactorSetupService($this->encrypter, $this->google2FA, $this->repository);
7678
}
7779
}

0 commit comments

Comments
 (0)