forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityControllerTest.php
More file actions
155 lines (132 loc) · 4.88 KB
/
SecurityControllerTest.php
File metadata and controls
155 lines (132 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Tests\Unit\Http\Controllers\Base;
use Mockery as m;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Prologue\Alerts\AlertsMessageBag;
use Illuminate\Contracts\Config\Repository;
use Tests\Unit\Http\Controllers\ControllerTestCase;
use Pterodactyl\Services\Users\TwoFactorSetupService;
use Pterodactyl\Services\Users\ToggleTwoFactorService;
use Pterodactyl\Http\Controllers\Base\SecurityController;
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
class SecurityControllerTest extends ControllerTestCase
{
/**
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
*/
protected $alert;
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
protected $config;
/**
* @var \Pterodactyl\Contracts\Repository\SessionRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService|\Mockery\Mock
*/
protected $toggleTwoFactorService;
/**
* @var \Pterodactyl\Services\Users\TwoFactorSetupService|\Mockery\Mock
*/
protected $twoFactorSetupService;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->alert = m::mock(AlertsMessageBag::class);
$this->config = m::mock(Repository::class);
$this->repository = m::mock(SessionRepositoryInterface::class);
$this->toggleTwoFactorService = m::mock(ToggleTwoFactorService::class);
$this->twoFactorSetupService = m::mock(TwoFactorSetupService::class);
}
/**
* Test TOTP generation controller.
*/
public function testIndexWithout2FactorEnabled()
{
$model = $this->generateRequestUserModel(['use_totp' => 0]);
$this->twoFactorSetupService->shouldReceive('handle')->with($model)->once()->andReturn(new Collection([
'image' => 'test-image',
'secret' => 'secret-code',
]));
$response = $this->getController()->index($this->request);
$this->assertIsJsonResponse($response);
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
$this->assertResponseJsonEquals(['enabled' => false, 'qr_image' => 'test-image', 'secret' => 'secret-code'], $response);
}
/**
* Test TOTP setting controller when no exception is thrown by the service.
*/
public function testIndexWith2FactorEnabled()
{
$this->generateRequestUserModel(['use_totp' => 1]);
$response = $this->getController()->index($this->request);
$this->assertIsJsonResponse($response);
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
$this->assertResponseJsonEquals(['enabled' => true], $response);
}
/**
* Test that a 2FA token can be stored or deleted.
*
* @param string $func
* @dataProvider functionCallDataProvider
*/
public function testStore(string $func)
{
$model = $this->generateRequestUserModel();
$this->mockRequestInput('token', 'some-token');
if ($func === 'delete') {
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'some-token', false);
} else {
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'some-token');
}
$response = $this->getController()->{$func}($this->request);
$this->assertIsJsonResponse($response);
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
$this->assertResponseJsonEquals(['success' => true], $response);
}
/**
* Test an invalid token exception is handled.
*
* @param string $func
* @dataProvider functionCallDataProvider
*/
public function testStoreWithInvalidTokenException(string $func)
{
$this->generateRequestUserModel();
$this->mockRequestInput('token');
$this->toggleTwoFactorService->shouldReceive('handle')->andThrow(new TwoFactorAuthenticationTokenInvalid);
$response = $this->getController()->{$func}($this->request);
$this->assertIsJsonResponse($response);
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
$this->assertResponseJsonEquals(['success' => false], $response);
}
/**
* @return array
*/
public function functionCallDataProvider()
{
return [['store'], ['delete']];
}
/**
* Return an instance of the controller for testing with mocked dependencies.
*
* @return \Pterodactyl\Http\Controllers\Base\SecurityController
*/
private function getController(): SecurityController
{
return new SecurityController(
$this->alert,
$this->config,
$this->repository,
$this->toggleTwoFactorService,
$this->twoFactorSetupService
);
}
}