|
| 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 | +} |
0 commit comments