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 \Subusers ;
114
125use Mockery as m ;
136use Tests \TestCase ;
14- use phpmock \phpunit \PHPMock ;
157use Pterodactyl \Models \User ;
168use Pterodactyl \Models \Server ;
179use Pterodactyl \Models \Subuser ;
3022
3123class SubuserCreationServiceTest extends TestCase
3224{
33- use PHPMock;
34-
3525 /**
3626 * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
3727 */
@@ -79,25 +69,13 @@ public function setUp()
7969 {
8070 parent ::setUp ();
8171
82- $ this ->getFunctionMock ('\\Pterodactyl \\Services \\Subusers ' , 'str_random ' )->expects ($ this ->any ())->willReturn ('random_string ' );
83-
8472 $ this ->connection = m::mock (ConnectionInterface::class);
8573 $ this ->keyCreationService = m::mock (DaemonKeyCreationService::class);
8674 $ this ->permissionService = m::mock (PermissionCreationService::class);
8775 $ this ->subuserRepository = m::mock (SubuserRepositoryInterface::class);
8876 $ this ->serverRepository = m::mock (ServerRepositoryInterface::class);
8977 $ this ->userCreationService = m::mock (UserCreationService::class);
9078 $ this ->userRepository = m::mock (UserRepositoryInterface::class);
91-
92- $ this ->service = new SubuserCreationService (
93- $ this ->connection ,
94- $ this ->keyCreationService ,
95- $ this ->permissionService ,
96- $ this ->serverRepository ,
97- $ this ->subuserRepository ,
98- $ this ->userCreationService ,
99- $ this ->userRepository
100- );
10179 }
10280
10381 /**
@@ -107,26 +85,33 @@ public function testAccountIsCreatedForNewUser()
10785 {
10886 $ permissions = ['test-1 ' => 'test:1 ' , 'test-2 ' => null ];
10987 $ server = factory (Server::class)->make ();
110- $ user = factory (User::class)->make ();
88+ $ user = factory (User::class)->make ([
89+ 'email ' => 'known.1+test@example.com ' ,
90+ ]);
11191 $ subuser = factory (Subuser::class)->make (['user_id ' => $ user ->id , 'server_id ' => $ server ->id ]);
11292
11393 $ this ->connection ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
11494 $ this ->userRepository ->shouldReceive ('findFirstWhere ' )->with ([['email ' , '= ' , $ user ->email ]])->once ()->andThrow (new RecordNotFoundException );
115- $ this ->userCreationService ->shouldReceive ('handle ' )->with ([
116- 'email ' => $ user ->email ,
117- 'username ' => substr (strtok ($ user ->email , '@ ' ), 0 , 8 ) . '_ ' . 'random_string ' ,
118- 'name_first ' => 'Server ' ,
119- 'name_last ' => 'Subuser ' ,
120- 'root_admin ' => false ,
121- ])->once ()->andReturn ($ user );
95+ $ this ->userCreationService ->shouldReceive ('handle ' )->with (m::on (function ($ data ) use ($ user ) {
96+ $ subset = m::subset ([
97+ 'email ' => $ user ->email ,
98+ 'name_first ' => 'Server ' ,
99+ 'name_last ' => 'Subuser ' ,
100+ 'root_admin ' => false ,
101+ ])->match ($ data );
102+
103+ $ username = substr (array_get ($ data , 'username ' , '' ), 0 , -3 ) === 'known.1test ' ;
104+
105+ return $ subset && $ username ;
106+ }))->once ()->andReturn ($ user );
122107
123108 $ this ->subuserRepository ->shouldReceive ('create ' )->with (['user_id ' => $ user ->id , 'server_id ' => $ server ->id ])
124109 ->once ()->andReturn ($ subuser );
125110 $ this ->keyCreationService ->shouldReceive ('handle ' )->with ($ server ->id , $ user ->id )->once ()->andReturnNull ();
126111 $ this ->permissionService ->shouldReceive ('handle ' )->with ($ subuser ->id , array_keys ($ permissions ))->once ()->andReturnNull ();
127112 $ this ->connection ->shouldReceive ('commit ' )->withNoArgs ()->once ()->andReturnNull ();
128113
129- $ response = $ this ->service ->handle ($ server , $ user ->email , array_keys ($ permissions ));
114+ $ response = $ this ->getService () ->handle ($ server , $ user ->email , array_keys ($ permissions ));
130115 $ this ->assertInstanceOf (Subuser::class, $ response );
131116 $ this ->assertSame ($ subuser , $ response );
132117 }
@@ -155,7 +140,7 @@ public function testExistingUserCanBeAddedAsASubuser()
155140 $ this ->permissionService ->shouldReceive ('handle ' )->with ($ subuser ->id , $ permissions )->once ()->andReturnNull ();
156141 $ this ->connection ->shouldReceive ('commit ' )->withNoArgs ()->once ()->andReturnNull ();
157142
158- $ response = $ this ->service ->handle ($ server ->id , $ user ->email , $ permissions );
143+ $ response = $ this ->getService () ->handle ($ server ->id , $ user ->email , $ permissions );
159144 $ this ->assertInstanceOf (Subuser::class, $ response );
160145 $ this ->assertSame ($ subuser , $ response );
161146 }
@@ -172,7 +157,7 @@ public function testExceptionIsThrownIfUserIsServerOwner()
172157 $ this ->userRepository ->shouldReceive ('findFirstWhere ' )->with ([['email ' , '= ' , $ user ->email ]])->once ()->andReturn ($ user );
173158
174159 try {
175- $ this ->service ->handle ($ server , $ user ->email , []);
160+ $ this ->getService () ->handle ($ server , $ user ->email , []);
176161 } catch (DisplayException $ exception ) {
177162 $ this ->assertInstanceOf (UserIsServerOwnerException::class, $ exception );
178163 $ this ->assertEquals (trans ('exceptions.subusers.user_is_owner ' ), $ exception ->getMessage ());
@@ -195,10 +180,28 @@ public function testExceptionIsThrownIfUserIsAlreadyASubuser()
195180 ])->once ()->andReturn (1 );
196181
197182 try {
198- $ this ->service ->handle ($ server , $ user ->email , []);
183+ $ this ->getService () ->handle ($ server , $ user ->email , []);
199184 } catch (DisplayException $ exception ) {
200185 $ this ->assertInstanceOf (ServerSubuserExistsException::class, $ exception );
201186 $ this ->assertEquals (trans ('exceptions.subusers.subuser_exists ' ), $ exception ->getMessage ());
202187 }
203188 }
189+
190+ /**
191+ * Return an instance of the service with mocked dependencies.
192+ *
193+ * @return \Pterodactyl\Services\Subusers\SubuserCreationService
194+ */
195+ private function getService (): SubuserCreationService
196+ {
197+ return new SubuserCreationService (
198+ $ this ->connection ,
199+ $ this ->keyCreationService ,
200+ $ this ->permissionService ,
201+ $ this ->serverRepository ,
202+ $ this ->subuserRepository ,
203+ $ this ->userCreationService ,
204+ $ this ->userRepository
205+ );
206+ }
204207}
0 commit comments