Skip to content

Commit 8908a75

Browse files
committed
More tests for daemon keys
1 parent 85e35f0 commit 8908a75

File tree

3 files changed

+197
-1
lines changed

3 files changed

+197
-1
lines changed

database/factories/ModelFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,5 @@
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(),
206205
];
207206
});
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 Carbon\Carbon;
14+
use Tests\TestCase;
15+
use Pterodactyl\Models\DaemonKey;
16+
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
17+
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
18+
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
19+
20+
class DaemonKeyProviderServiceTest extends TestCase
21+
{
22+
/**
23+
* @var \Carbon\Carbon|\Mockery\Mock
24+
*/
25+
protected $carbon;
26+
27+
/**
28+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService|\Mockery\Mock
29+
*/
30+
protected $keyUpdateService;
31+
32+
/**
33+
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
34+
*/
35+
protected $repository;
36+
37+
/**
38+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
39+
*/
40+
protected $service;
41+
42+
/**
43+
* Setup tests.
44+
*/
45+
public function setUp()
46+
{
47+
parent::setUp();
48+
49+
$this->carbon = m::mock(Carbon::class);
50+
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
51+
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
52+
53+
$this->service = new DaemonKeyProviderService($this->carbon, $this->keyUpdateService, $this->repository);
54+
}
55+
56+
/**
57+
* Test that a key is returned.
58+
*/
59+
public function testKeyIsReturned()
60+
{
61+
$key = factory(DaemonKey::class)->make();
62+
63+
$this->repository->shouldReceive('findFirstWhere')->with([
64+
['user_id', '=', $key->user_id],
65+
['server_id', '=', $key->server_id],
66+
])->once()->andReturn($key);
67+
68+
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf();
69+
$this->carbon->shouldReceive('diffInSeconds')->with($key->expires_at, false)->once()->andReturn(100);
70+
71+
$response = $this->service->handle($key->server_id, $key->user_id);
72+
$this->assertNotEmpty($response);
73+
$this->assertEquals($key->secret, $response);
74+
}
75+
76+
/**
77+
* Test that an expired key is updated and then returned.
78+
*/
79+
public function testExpiredKeyIsUpdated()
80+
{
81+
$key = factory(DaemonKey::class)->make();
82+
83+
$this->repository->shouldReceive('findFirstWhere')->with([
84+
['user_id', '=', $key->user_id],
85+
['server_id', '=', $key->server_id],
86+
])->once()->andReturn($key);
87+
88+
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf();
89+
$this->carbon->shouldReceive('diffInSeconds')->with($key->expires_at, false)->once()->andReturn(-100);
90+
91+
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn(true);
92+
93+
$response = $this->service->handle($key->server_id, $key->user_id);
94+
$this->assertNotEmpty($response);
95+
$this->assertTrue($response);
96+
}
97+
98+
/**
99+
* Test that an expired key is not updated and the expired key is returned.
100+
*/
101+
public function testExpiredKeyIsNotUpdated()
102+
{
103+
$key = factory(DaemonKey::class)->make();
104+
105+
$this->repository->shouldReceive('findFirstWhere')->with([
106+
['user_id', '=', $key->user_id],
107+
['server_id', '=', $key->server_id],
108+
])->once()->andReturn($key);
109+
110+
$response = $this->service->handle($key->server_id, $key->user_id, false);
111+
$this->assertNotEmpty($response);
112+
$this->assertEquals($key->secret, $response);
113+
}
114+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Carbon\Carbon;
14+
use Tests\TestCase;
15+
use phpmock\phpunit\PHPMock;
16+
use Illuminate\Contracts\Config\Repository;
17+
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
18+
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
19+
20+
class DaemonKeyUpdateServiceTest extends TestCase
21+
{
22+
use PHPMock;
23+
24+
/**
25+
* @var \Carbon\Carbon|\Mockery\Mock
26+
*/
27+
protected $carbon;
28+
29+
/**
30+
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
31+
*/
32+
protected $config;
33+
34+
/**
35+
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
36+
*/
37+
protected $repository;
38+
39+
/**
40+
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService
41+
*/
42+
protected $service;
43+
44+
/**
45+
* Setup tests.
46+
*/
47+
public function setUp()
48+
{
49+
parent::setUp();
50+
51+
$this->carbon = m::Mock(Carbon::class);
52+
$this->config = m::mock(Repository::class);
53+
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
54+
55+
$this->service = new DaemonKeyUpdateService($this->carbon, $this->config, $this->repository);
56+
}
57+
58+
/**
59+
* Test that a key is updated.
60+
*/
61+
public function testKeyIsUpdated()
62+
{
63+
$secret = DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . 'random_string';
64+
65+
$this->getFunctionMock('\\Pterodactyl\\Services\\DaemonKeys', 'str_random')
66+
->expects($this->once())->with(40)->willReturn('random_string');
67+
68+
$this->config->shouldReceive('get')->with('pterodactyl.api.key_expire_time')->once()->andReturn(100);
69+
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
70+
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
71+
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
72+
73+
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf();
74+
$this->repository->shouldReceive('update')->with(123, [
75+
'secret' => $secret,
76+
'expires_at' => '00:00:00',
77+
])->once()->andReturnNull();
78+
79+
$response = $this->service->handle(123);
80+
$this->assertNotEmpty($response);
81+
$this->assertEquals($secret, $response);
82+
}
83+
}

0 commit comments

Comments
 (0)