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
103namespace Tests \Unit \Services \Api ;
114
125use Mockery as m ;
136use Tests \TestCase ;
147use phpmock \phpunit \PHPMock ;
158use Pterodactyl \Models \APIKey ;
16- use Illuminate \Database \ConnectionInterface ;
17- use Pterodactyl \Services \Api \PermissionService ;
9+ use Illuminate \Contracts \Encryption \Encrypter ;
1810use Pterodactyl \Services \Api \KeyCreationService ;
1911use 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