forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexControllerTest.php
More file actions
144 lines (119 loc) · 5.16 KB
/
IndexControllerTest.php
File metadata and controls
144 lines (119 loc) · 5.16 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
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Http\Controllers\Base;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use Tests\Assertions\ControllerAssertionsTrait;
use Pterodactyl\Http\Controllers\Base\IndexController;
use Pterodactyl\Services\Servers\ServerAccessHelperService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class IndexControllerTest extends TestCase
{
use ControllerAssertionsTrait;
/**
* @var \Pterodactyl\Services\Servers\ServerAccessHelperService
*/
protected $access;
/**
* @var \Pterodactyl\Http\Controllers\Base\IndexController
*/
protected $controller;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
protected $daemonRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->access = m::mock(ServerAccessHelperService::class);
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->request = m::mock(Request::class);
$this->controller = new IndexController($this->daemonRepository, $this->access, $this->repository);
}
/**
* Test the index controller.
*/
public function testIndexController()
{
$model = factory(User::class)->make();
$this->request->shouldReceive('user')->withNoArgs()->andReturn($model);
$this->request->shouldReceive('input')->with('query')->once()->andReturn('searchTerm');
$this->repository->shouldReceive('search')->with('searchTerm')->once()->andReturnSelf()
->shouldReceive('filterUserAccessServers')->with(
$model->id, $model->root_admin, 'all', ['user']
)->once()->andReturn(['test']);
$response = $this->controller->getIndex($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('base.index', $response);
$this->assertViewHasKey('servers', $response);
$this->assertViewKeyEquals('servers', ['test'], $response);
}
/**
* Test the status controller.
*/
public function testStatusController()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]');
$response = $this->controller->status($this->request, $server->uuid);
$this->assertIsJsonResponse($response);
$this->assertResponseJsonEquals(['test'], $response);
}
/**
* Test the status controller if a server is not installed.
*/
public function testStatusControllerWhenServerNotInstalled()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 0]);
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
$response = $this->controller->status($this->request, $server->uuid);
$this->assertIsJsonResponse($response);
$this->assertResponseCodeEquals(200, $response);
$this->assertResponseJsonEquals(['status' => 20], $response);
}
/**
* Test the status controller when a server is suspended.
*/
public function testStatusControllerWhenServerIsSuspended()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make(['suspended' => 1, 'installed' => 1]);
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
$response = $this->controller->status($this->request, $server->uuid);
$this->assertIsJsonResponse($response);
$this->assertResponseCodeEquals(200, $response);
$this->assertResponseJsonEquals(['status' => 30], $response);
}
}