Skip to content

Commit e0fb7fa

Browse files
committed
Fix failing ApiKeyCreationService test
1 parent e3df073 commit e0fb7fa

File tree

2 files changed

+35
-50
lines changed

2 files changed

+35
-50
lines changed

app/Services/Api/KeyCreationService.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
namespace Pterodactyl\Services\Api;
44

55
use Pterodactyl\Models\APIKey;
6-
use Illuminate\Database\ConnectionInterface;
76
use Illuminate\Contracts\Encryption\Encrypter;
87
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
98

109
class KeyCreationService
1110
{
12-
/**
13-
* @var \Illuminate\Database\ConnectionInterface
14-
*/
15-
private $connection;
16-
1711
/**
1812
* @var \Illuminate\Contracts\Encryption\Encrypter
1913
*/
@@ -28,17 +22,12 @@ class KeyCreationService
2822
* ApiKeyService constructor.
2923
*
3024
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
31-
* @param \Illuminate\Database\ConnectionInterface $connection
3225
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
3326
*/
34-
public function __construct(
35-
ApiKeyRepositoryInterface $repository,
36-
ConnectionInterface $connection,
37-
Encrypter $encrypter
38-
) {
39-
$this->repository = $repository;
40-
$this->connection = $connection;
27+
public function __construct(ApiKeyRepositoryInterface $repository, Encrypter $encrypter)
28+
{
4129
$this->encrypter = $encrypter;
30+
$this->repository = $repository;
4231
}
4332

4433
/**
@@ -54,7 +43,7 @@ public function __construct(
5443
public function handle(array $data): APIKey
5544
{
5645
$data = array_merge($data, [
57-
'identifer' => str_random(APIKey::IDENTIFIER_LENGTH),
46+
'identifier' => str_random(APIKey::IDENTIFIER_LENGTH),
5847
'token' => $this->encrypter->encrypt(str_random(APIKey::KEY_LENGTH)),
5948
]);
6049

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
<?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-
*/
92

103
namespace Tests\Unit\Services\Api;
114

125
use Mockery as m;
136
use Tests\TestCase;
147
use phpmock\phpunit\PHPMock;
158
use Pterodactyl\Models\APIKey;
16-
use Illuminate\Database\ConnectionInterface;
17-
use Pterodactyl\Services\Api\PermissionService;
9+
use Illuminate\Contracts\Encryption\Encrypter;
1810
use Pterodactyl\Services\Api\KeyCreationService;
1911
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
2012

@@ -23,14 +15,9 @@ class KeyCreationServiceTest extends TestCase
2315
use PHPMock;
2416

2517
/**
26-
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
18+
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
2719
*/
28-
private $connection;
29-
30-
/**
31-
* @var \Pterodactyl\Services\Api\PermissionService|\Mockery\Mock
32-
*/
33-
private $permissionService;
20+
private $encrypter;
3421

3522
/**
3623
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface|\Mockery\Mock
@@ -44,8 +31,7 @@ public function setUp()
4431
{
4532
parent::setUp();
4633

47-
$this->connection = m::mock(ConnectionInterface::class);
48-
$this->permissionService = m::mock(PermissionService::class);
34+
$this->encrypter = m::mock(Encrypter::class);
4935
$this->repository = m::mock(ApiKeyRepositoryInterface::class);
5036
}
5137

@@ -57,32 +43,42 @@ public function testKeyIsCreated()
5743
$model = factory(APIKey::class)->make();
5844

5945
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
60-
->expects($this->exactly(1))->with(APIKey::KEY_LENGTH)->willReturn($model->token);
46+
->expects($this->exactly(2))->willReturnCallback(function ($length) {
47+
return 'str_' . $length;
48+
});
6149

62-
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
50+
$this->encrypter->shouldReceive('encrypt')->with('str_' . APIKey::KEY_LENGTH)->once()->andReturn($model->token);
6351

6452
$this->repository->shouldReceive('create')->with([
6553
'test-data' => 'test',
54+
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH,
6655
'token' => $model->token,
6756
], true, true)->once()->andReturn($model);
6857

69-
$this->permissionService->shouldReceive('getPermissions')->withNoArgs()->once()->andReturn([
70-
'_user' => ['server' => ['list', 'multiple-dash-test']],
71-
'server' => ['create', 'admin-dash-test'],
72-
]);
58+
$response = $this->getService()->handle(['test-data' => 'test']);
59+
60+
$this->assertNotEmpty($response);
61+
$this->assertInstanceOf(APIKey::class, $response);
62+
$this->assertSame($model, $response);
63+
}
64+
65+
public function testIdentifierAndTokenAreOnlySetByFunction()
66+
{
67+
$model = factory(APIKey::class)->make();
68+
69+
$this->getFunctionMock('\\Pterodactyl\\Services\\Api', 'str_random')
70+
->expects($this->exactly(2))->willReturnCallback(function ($length) {
71+
return 'str_' . $length;
72+
});
7373

74-
$this->permissionService->shouldReceive('create')->with($model->id, 'user.server-list')->once()->andReturnNull();
75-
$this->permissionService->shouldReceive('create')->with($model->id, 'user.server-multiple-dash-test')->once()->andReturnNull();
76-
$this->permissionService->shouldReceive('create')->with($model->id, 'server-create')->once()->andReturnNull();
77-
$this->permissionService->shouldReceive('create')->with($model->id, 'server-admin-dash-test')->once()->andReturnNull();
74+
$this->encrypter->shouldReceive('encrypt')->with('str_' . APIKey::KEY_LENGTH)->once()->andReturn($model->token);
7875

79-
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
76+
$this->repository->shouldReceive('create')->with([
77+
'identifier' => 'str_' . APIKey::IDENTIFIER_LENGTH,
78+
'token' => $model->token,
79+
], true, true)->once()->andReturn($model);
8080

81-
$response = $this->getService()->handle(
82-
['test-data' => 'test'],
83-
['invalid-node', 'server-list', 'server-multiple-dash-test'],
84-
['invalid-node', 'server-create', 'server-admin-dash-test']
85-
);
81+
$response = $this->getService()->handle(['identifier' => 'customIdentifier', 'token' => 'customToken']);
8682

8783
$this->assertNotEmpty($response);
8884
$this->assertInstanceOf(APIKey::class, $response);
@@ -96,6 +92,6 @@ public function testKeyIsCreated()
9692
*/
9793
private function getService(): KeyCreationService
9894
{
99-
return new KeyCreationService($this->repository, $this->connection, $this->permissionService);
95+
return new KeyCreationService($this->repository, $this->encrypter);
10096
}
10197
}

0 commit comments

Comments
 (0)