forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDaemonKeyProviderServiceTest.php
More file actions
114 lines (94 loc) · 3.3 KB
/
DaemonKeyProviderServiceTest.php
File metadata and controls
114 lines (94 loc) · 3.3 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
<?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\Services\DaemonKeys;
use Mockery as m;
use Carbon\Carbon;
use Tests\TestCase;
use Pterodactyl\Models\DaemonKey;
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
class DaemonKeyProviderServiceTest extends TestCase
{
/**
* @var \Carbon\Carbon|\Mockery\Mock
*/
protected $carbon;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService|\Mockery\Mock
*/
protected $keyUpdateService;
/**
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->carbon = new Carbon();
$this->carbon->setTestNow();
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
$this->service = new DaemonKeyProviderService($this->carbon, $this->keyUpdateService, $this->repository);
}
/**
* Test that a key is returned.
*/
public function testKeyIsReturned()
{
$key = factory(DaemonKey::class)->make();
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $key->user_id],
['server_id', '=', $key->server_id],
])->once()->andReturn($key);
$response = $this->service->handle($key->server_id, $key->user_id);
$this->assertNotEmpty($response);
$this->assertEquals($key->secret, $response);
}
/**
* Test that an expired key is updated and then returned.
*/
public function testExpiredKeyIsUpdated()
{
$key = factory(DaemonKey::class)->make([
'expires_at' => $this->carbon->subHour(),
]);
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $key->user_id],
['server_id', '=', $key->server_id],
])->once()->andReturn($key);
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn(true);
$response = $this->service->handle($key->server_id, $key->user_id);
$this->assertNotEmpty($response);
$this->assertTrue($response);
}
/**
* Test that an expired key is not updated and the expired key is returned.
*/
public function testExpiredKeyIsNotUpdated()
{
$key = factory(DaemonKey::class)->make([
'expires_at' => $this->carbon->subHour(),
]);
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', $key->user_id],
['server_id', '=', $key->server_id],
])->once()->andReturn($key);
$response = $this->service->handle($key->server_id, $key->user_id, false);
$this->assertNotEmpty($response);
$this->assertEquals($key->secret, $response);
}
}