Skip to content

Commit 774c968

Browse files
committed
More test suite coverage
1 parent 8908a75 commit 774c968

File tree

14 files changed

+373
-46
lines changed

14 files changed

+373
-46
lines changed

app/Jobs/Schedule/RunTaskJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RunTaskJob extends Job implements ShouldQueue
4040
/**
4141
* @var int
4242
*/
43-
protected $schedule;
43+
public $schedule;
4444

4545
/**
4646
* @var int

app/Services/DaemonKeys/DaemonKeyProviderService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function handle($server, $user, $updateIfExpired = true)
8383
['server_id', '=', $server],
8484
]);
8585

86-
if (! $updateIfExpired || max($this->carbon->now()->diffInSeconds($key->expires_at, false), 0) > 0) {
86+
if (! $updateIfExpired || $this->carbon->now()->diffInSeconds($key->expires_at, false) > 0) {
8787
return $key->secret;
8888
}
8989

app/Services/Helpers/TemporaryPasswordService.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
namespace Pterodactyl\Services\Helpers;
1111

1212
use Illuminate\Contracts\Hashing\Hasher;
13-
use Illuminate\Database\DatabaseManager;
14-
use Illuminate\Config\Repository as ConfigRepository;
13+
use Illuminate\Database\ConnectionInterface;
14+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
1515

1616
class TemporaryPasswordService
1717
{
1818
const HMAC_ALGO = 'sha256';
1919

2020
/**
21-
* @var \Illuminate\Config\Repository
21+
* @var \Illuminate\Contracts\Config\Repository
2222
*/
2323
protected $config;
2424

2525
/**
26-
* @var \Illuminate\Database\DatabaseManager
26+
* @var \Illuminate\Database\ConnectionInterface
2727
*/
28-
protected $database;
28+
protected $connection;
2929

3030
/**
3131
* @var \Illuminate\Contracts\Hashing\Hasher
@@ -35,17 +35,17 @@ class TemporaryPasswordService
3535
/**
3636
* TemporaryPasswordService constructor.
3737
*
38-
* @param \Illuminate\Config\Repository $config
39-
* @param \Illuminate\Database\DatabaseManager $database
40-
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
38+
* @param \Illuminate\Contracts\Config\Repository $config
39+
* @param \Illuminate\Database\ConnectionInterface $connection
40+
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
4141
*/
4242
public function __construct(
4343
ConfigRepository $config,
44-
DatabaseManager $database,
44+
ConnectionInterface $connection,
4545
Hasher $hasher
4646
) {
4747
$this->config = $config;
48-
$this->database = $database;
48+
$this->connection = $connection;
4949
$this->hasher = $hasher;
5050
}
5151

@@ -55,11 +55,11 @@ public function __construct(
5555
* @param string $email
5656
* @return string
5757
*/
58-
public function generateReset($email)
58+
public function handle($email)
5959
{
6060
$token = hash_hmac(self::HMAC_ALGO, str_random(40), $this->config->get('app.key'));
6161

62-
$this->database->table('password_resets')->insert([
62+
$this->connection->table('password_resets')->insert([
6363
'email' => $email,
6464
'token' => $this->hasher->make($token),
6565
]);

app/Services/Schedules/ProcessScheduleService.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
1717

1818
class ProcessScheduleService
1919
{
20+
/**
21+
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
22+
*/
2023
protected $repository;
2124

25+
/**
26+
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService
27+
*/
2228
protected $runnerService;
2329

24-
public function __construct(
25-
RunTaskService $runnerService,
26-
ScheduleRepositoryInterface $repository
27-
) {
30+
/**
31+
* ProcessScheduleService constructor.
32+
*
33+
* @param \Pterodactyl\Services\Schedules\Tasks\RunTaskService $runnerService
34+
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
35+
*/
36+
public function __construct(RunTaskService $runnerService, ScheduleRepositoryInterface $repository)
37+
{
2838
$this->repository = $repository;
2939
$this->runnerService = $runnerService;
3040
}

app/Services/Schedules/Tasks/RunTaskService.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
namespace Pterodactyl\Services\Schedules\Tasks;
1111

1212
use Pterodactyl\Models\Task;
13-
use Illuminate\Contracts\Bus\Dispatcher;
1413
use Pterodactyl\Jobs\Schedule\RunTaskJob;
14+
use Illuminate\Foundation\Bus\DispatchesJobs;
1515
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
1616

1717
class RunTaskService
1818
{
19-
/**
20-
* @var \Illuminate\Contracts\Bus\Dispatcher
21-
*/
22-
protected $dispatcher;
19+
use DispatchesJobs;
2320

2421
/**
2522
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
@@ -29,14 +26,10 @@ class RunTaskService
2926
/**
3027
* RunTaskService constructor.
3128
*
32-
* @param \Illuminate\Contracts\Bus\Dispatcher $dispatcher
3329
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
3430
*/
35-
public function __construct(
36-
Dispatcher $dispatcher,
37-
TaskRepositoryInterface $repository
38-
) {
39-
$this->dispatcher = $dispatcher;
31+
public function __construct(TaskRepositoryInterface $repository)
32+
{
4033
$this->repository = $repository;
4134
}
4235

@@ -55,6 +48,6 @@ public function handle($task)
5548
}
5649

5750
$this->repository->update($task->id, ['is_queued' => true]);
58-
$this->dispatcher->dispatch((new RunTaskJob($task->id, $task->schedule_id))->delay($task->time_offset));
51+
$this->dispatch((new RunTaskJob($task->id, $task->schedule_id))->delay($task->time_offset));
5952
}
6053
}

app/Services/Users/UserCreationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function handle(array $data)
9393
$this->connection->beginTransaction();
9494
if (! isset($data['password']) || empty($data['password'])) {
9595
$data['password'] = $this->hasher->make(str_random(30));
96-
$token = $this->passwordService->generateReset($data['email']);
96+
$token = $this->passwordService->handle($data['email']);
9797
}
9898

9999
$user = $this->repository->create($data);

database/factories/ModelFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,6 @@
202202
'server_id' => $faker->randomNumber(),
203203
'user_id' => $faker->randomNumber(),
204204
'secret' => 'i_' . str_random(40),
205+
'expires_at' => \Carbon\Carbon::now()->addMinutes(10)->toDateTimeString(),
205206
];
206207
});

tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public function setUp()
4646
{
4747
parent::setUp();
4848

49-
$this->carbon = m::mock(Carbon::class);
49+
$this->carbon = new Carbon();
50+
$this->carbon->setTestNow();
51+
5052
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
5153
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
5254

@@ -65,9 +67,6 @@ public function testKeyIsReturned()
6567
['server_id', '=', $key->server_id],
6668
])->once()->andReturn($key);
6769

68-
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf();
69-
$this->carbon->shouldReceive('diffInSeconds')->with($key->expires_at, false)->once()->andReturn(100);
70-
7170
$response = $this->service->handle($key->server_id, $key->user_id);
7271
$this->assertNotEmpty($response);
7372
$this->assertEquals($key->secret, $response);
@@ -78,16 +77,15 @@ public function testKeyIsReturned()
7877
*/
7978
public function testExpiredKeyIsUpdated()
8079
{
81-
$key = factory(DaemonKey::class)->make();
80+
$key = factory(DaemonKey::class)->make([
81+
'expires_at' => $this->carbon->subHour(),
82+
]);
8283

8384
$this->repository->shouldReceive('findFirstWhere')->with([
8485
['user_id', '=', $key->user_id],
8586
['server_id', '=', $key->server_id],
8687
])->once()->andReturn($key);
8788

88-
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf();
89-
$this->carbon->shouldReceive('diffInSeconds')->with($key->expires_at, false)->once()->andReturn(-100);
90-
9189
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn(true);
9290

9391
$response = $this->service->handle($key->server_id, $key->user_id);
@@ -100,7 +98,9 @@ public function testExpiredKeyIsUpdated()
10098
*/
10199
public function testExpiredKeyIsNotUpdated()
102100
{
103-
$key = factory(DaemonKey::class)->make();
101+
$key = factory(DaemonKey::class)->make([
102+
'expires_at' => $this->carbon->subHour(),
103+
]);
104104

105105
$this->repository->shouldReceive('findFirstWhere')->with([
106106
['user_id', '=', $key->user_id],
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Helpers;
11+
12+
use Mockery as m;
13+
use Tests\TestCase;
14+
use phpmock\phpunit\PHPMock;
15+
use Illuminate\Contracts\Hashing\Hasher;
16+
use Illuminate\Contracts\Config\Repository;
17+
use Illuminate\Database\ConnectionInterface;
18+
use Pterodactyl\Services\Helpers\TemporaryPasswordService;
19+
20+
class TemporaryPasswordServiceTest extends TestCase
21+
{
22+
use PHPMock;
23+
24+
/**
25+
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
26+
*/
27+
protected $config;
28+
29+
/**
30+
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
31+
*/
32+
protected $connection;
33+
34+
/**
35+
* @var \Illuminate\Contracts\Hashing\Hasher|\Mockery\Mock
36+
*/
37+
protected $hasher;
38+
39+
/**
40+
* @var \Pterodactyl\Services\Helpers\TemporaryPasswordService
41+
*/
42+
protected $service;
43+
44+
/**
45+
* Setup tests.
46+
*/
47+
public function setUp()
48+
{
49+
parent::setUp();
50+
51+
$this->config = m::mock(Repository::class);
52+
$this->connection = m::mock(ConnectionInterface::class);
53+
$this->hasher = m::mock(Hasher::class);
54+
55+
$this->service = new TemporaryPasswordService($this->config, $this->connection, $this->hasher);
56+
}
57+
58+
/**
59+
* Test that a temporary password is stored and the token is returned.
60+
*/
61+
public function testTemporaryPasswordIsStored()
62+
{
63+
$this->getFunctionMock('\\Pterodactyl\\Services\\Helpers', 'str_random')
64+
->expects($this->once())->with(40)->willReturn('random_string');
65+
66+
$this->config->shouldReceive('get')->with('app.key')->once()->andReturn('123456');
67+
$token = hash_hmac(TemporaryPasswordService::HMAC_ALGO, 'random_string', '123456');
68+
69+
$this->hasher->shouldReceive('make')->with($token)->once()->andReturn('hashed_token');
70+
$this->connection->shouldReceive('table')->with('password_resets')->once()->andReturnSelf();
71+
$this->connection->shouldReceive('insert')->with([
72+
'email' => 'test@example.com',
73+
'token' => 'hashed_token',
74+
])->once()->andReturnNull();
75+
76+
$response = $this->service->handle('test@example.com');
77+
$this->assertNotEmpty($response);
78+
$this->assertEquals($token, $response);
79+
}
80+
}

0 commit comments

Comments
 (0)