Skip to content

Commit 85e35f0

Browse files
committed
More tests
1 parent 6efecae commit 85e35f0

File tree

2 files changed

+260
-0
lines changed

2 files changed

+260
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Services\DaemonKeys;
26+
27+
use Mockery as m;
28+
use Carbon\Carbon;
29+
use Tests\TestCase;
30+
use phpmock\phpunit\PHPMock;
31+
use Illuminate\Contracts\Config\Repository;
32+
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
33+
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
34+
35+
class DaemonKeyCreationServiceTest extends TestCase
36+
{
37+
use PHPMock;
38+
39+
/**
40+
* @var \Carbon\Carbon|\Mockery\Mock
41+
*/
42+
protected $carbon;
43+
44+
/**
45+
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
46+
*/
47+
protected $config;
48+
49+
/**
50+
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
51+
*/
52+
protected $repository;
53+
54+
/**
55+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService
56+
*/
57+
protected $service;
58+
59+
/**
60+
* Setup tests.
61+
*/
62+
public function setUp()
63+
{
64+
parent::setUp();
65+
66+
$this->carbon = m::mock(Carbon::class);
67+
$this->config = m::Mock(Repository::class);
68+
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
69+
70+
$this->service = new DaemonKeyCreationService($this->carbon, $this->config, $this->repository);
71+
}
72+
73+
/**
74+
* Test that a daemon key is created.
75+
*/
76+
public function testDaemonKeyIsCreated()
77+
{
78+
$this->getFunctionMock('\\Pterodactyl\\Services\\DaemonKeys', 'str_random')
79+
->expects($this->once())->willReturn('random_string');
80+
81+
$this->config->shouldReceive('get')->with('pterodactyl.api.key_expire_time')->once()->andReturn(100);
82+
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
83+
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
84+
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
85+
86+
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
87+
->shouldReceive('create')->with([
88+
'user_id' => 1,
89+
'server_id' => 2,
90+
'secret' => DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . 'random_string',
91+
'expires_at' => '00:00:00',
92+
])->once()->andReturnNull();
93+
94+
$response = $this->service->handle(2, 1);
95+
$this->assertNotEmpty($response);
96+
$this->assertEquals('i_random_string', $response);
97+
}
98+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Tests\Unit\Services\DaemonKeys;
11+
12+
use Mockery as m;
13+
use Tests\TestCase;
14+
use Illuminate\Log\Writer;
15+
use Pterodactyl\Models\Server;
16+
use Pterodactyl\Models\DaemonKey;
17+
use GuzzleHttp\Exception\RequestException;
18+
use Illuminate\Database\ConnectionInterface;
19+
use Pterodactyl\Exceptions\DisplayException;
20+
use Pterodactyl\Exceptions\PterodactylException;
21+
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
22+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
23+
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
24+
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
25+
26+
class DaemonKeyDeletionServiceTest extends TestCase
27+
{
28+
/**
29+
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
30+
*/
31+
protected $connection;
32+
33+
/**
34+
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
35+
*/
36+
protected $daemonRepository;
37+
38+
/**
39+
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
40+
*/
41+
protected $exception;
42+
43+
/**
44+
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
45+
*/
46+
protected $repository;
47+
48+
/**
49+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
50+
*/
51+
protected $serverRepository;
52+
53+
/**
54+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
55+
*/
56+
protected $service;
57+
58+
/**
59+
* @var \Illuminate\Log\Writer|\Mockery\Mock
60+
*/
61+
protected $writer;
62+
63+
/**
64+
* Setup tests.
65+
*/
66+
public function setUp()
67+
{
68+
parent::setUp();
69+
70+
$this->connection = m::mock(ConnectionInterface::class);
71+
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
72+
$this->exception = m::mock(RequestException::class);
73+
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
74+
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
75+
$this->writer = m::mock(Writer::class);
76+
77+
$this->service = new DaemonKeyDeletionService(
78+
$this->connection,
79+
$this->repository,
80+
$this->daemonRepository,
81+
$this->serverRepository,
82+
$this->writer
83+
);
84+
}
85+
86+
/**
87+
* Test that a daemon key is deleted correctly.
88+
*/
89+
public function testKeyIsDeleted()
90+
{
91+
$server = factory(Server::class)->make();
92+
$key = factory(DaemonKey::class)->make();
93+
94+
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
95+
$this->repository->shouldReceive('findFirstWhere')->with([
96+
['user_id', '=', 100],
97+
['server_id', '=', $server->id],
98+
])->once()->andReturn($key);
99+
100+
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull();
101+
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
102+
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull();
103+
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
104+
105+
$this->service->handle($server, 100);
106+
$this->assertTrue(true);
107+
}
108+
109+
/**
110+
* Test that a daemon key can be deleted when only a server ID is passed.
111+
*/
112+
public function testKeyIsDeletedIfIdIsPassedInPlaceOfModel()
113+
{
114+
$server = factory(Server::class)->make();
115+
$key = factory(DaemonKey::class)->make();
116+
117+
$this->serverRepository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
118+
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
119+
$this->repository->shouldReceive('findFirstWhere')->with([
120+
['user_id', '=', 100],
121+
['server_id', '=', $server->id],
122+
])->once()->andReturn($key);
123+
124+
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull();
125+
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
126+
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull();
127+
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
128+
129+
$this->service->handle($server->id, 100);
130+
$this->assertTrue(true);
131+
}
132+
133+
/**
134+
* Test that an exception is properly handled if thrown by guzzle.
135+
*/
136+
public function testExceptionReturnedByGuzzleIsHandled()
137+
{
138+
$server = factory(Server::class)->make();
139+
$key = factory(DaemonKey::class)->make();
140+
141+
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
142+
$this->repository->shouldReceive('findFirstWhere')->with([
143+
['user_id', '=', 100],
144+
['server_id', '=', $server->id],
145+
])->once()->andReturn($key);
146+
147+
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturnNull();
148+
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->exception);
149+
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
150+
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
151+
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
152+
153+
try {
154+
$this->service->handle($server, 100);
155+
} catch (PterodactylException $exception) {
156+
$this->assertInstanceOf(DisplayException::class, $exception);
157+
$this->assertEquals(trans('admin/server.exceptions.daemon_exception', [
158+
'code' => 'E_CONN_REFUSED',
159+
]), $exception->getMessage());
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)