Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Unit/Http/Middleware/AdminAuthenticateTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Unit\Http\Middleware;
4
5
use Pterodactyl\Models\User;
6
use Pterodactyl\Http\Middleware\AdminAuthenticate;
7
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
8
9
class AdminAuthenticateTest extends MiddlewareTestCase
10
{
11
/**
12
* Test that an admin is authenticated.
13
*/
14
public function testAdminsAreAuthenticated()
15
{
16
$user = User::factory()->make(['root_admin' => 1]);
17
18
$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);
19
20
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
21
}
22
23
/**
24
* Test that a missing user in the request triggers an error.
25
*/
26
public function testExceptionIsThrownIfUserDoesNotExist()
27
{
28
$this->expectException(AccessDeniedHttpException::class);
29
30
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturnNull();
31
32
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
33
}
34
35
/**
36
* Test that an exception is thrown if the user is not an admin.
37
*/
38
public function testExceptionIsThrownIfUserIsNotAnAdmin()
39
{
40
$this->expectException(AccessDeniedHttpException::class);
41
42
$user = User::factory()->make(['root_admin' => 0]);
43
44
$this->request->shouldReceive('user')->withNoArgs()->twice()->andReturn($user);
45
46
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
47
}
48
49
/**
50
* Return an instance of the middleware using mocked dependencies.
51
*/
52
private function getMiddleware(): AdminAuthenticate
53
{
54
return new AdminAuthenticate();
55
}
56
}
57
58