1111
1212use Mockery as m ;
1313use Tests \TestCase ;
14- use Illuminate \Log \Writer ;
1514use Pterodactyl \Models \Server ;
1615use Pterodactyl \Models \Subuser ;
16+ use Tests \Traits \MocksRequestException ;
1717use GuzzleHttp \Exception \RequestException ;
1818use Illuminate \Database \ConnectionInterface ;
19- use Pterodactyl \Exceptions \DisplayException ;
2019use Pterodactyl \Exceptions \PterodactylException ;
2120use Pterodactyl \Services \Subusers \SubuserUpdateService ;
2221use Pterodactyl \Services \Subusers \PermissionCreationService ;
2322use Pterodactyl \Services \DaemonKeys \DaemonKeyProviderService ;
2423use Pterodactyl \Contracts \Repository \SubuserRepositoryInterface ;
2524use Pterodactyl \Contracts \Repository \PermissionRepositoryInterface ;
25+ use Pterodactyl \Exceptions \Http \Connection \DaemonConnectionException ;
2626use Pterodactyl \Contracts \Repository \Daemon \ServerRepositoryInterface as DaemonServerRepositoryInterface ;
2727
2828class SubuserUpdateServiceTest extends TestCase
2929{
30+ use MocksRequestException;
31+
3032 /**
3133 * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
3234 */
33- protected $ connection ;
35+ private $ connection ;
3436
3537 /**
3638 * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
3739 */
38- protected $ daemonRepository ;
39-
40- /**
41- * @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
42- */
43- protected $ exception ;
40+ private $ daemonRepository ;
4441
4542 /**
4643 * @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
@@ -50,27 +47,17 @@ class SubuserUpdateServiceTest extends TestCase
5047 /**
5148 * @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock
5249 */
53- protected $ permissionRepository ;
50+ private $ permissionRepository ;
5451
5552 /**
5653 * @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
5754 */
58- protected $ permissionService ;
55+ private $ permissionService ;
5956
6057 /**
6158 * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
6259 */
63- protected $ repository ;
64-
65- /**
66- * @var \Pterodactyl\Services\Subusers\SubuserUpdateService
67- */
68- protected $ service ;
69-
70- /**
71- * @var \Illuminate\Log\Writer|\Mockery\Mock
72- */
73- protected $ writer ;
60+ private $ repository ;
7461
7562 /**
7663 * Setup tests.
@@ -81,22 +68,10 @@ public function setUp()
8168
8269 $ this ->connection = m::mock (ConnectionInterface::class);
8370 $ this ->daemonRepository = m::mock (DaemonServerRepositoryInterface::class);
84- $ this ->exception = m::mock (RequestException::class);
8571 $ this ->keyProviderService = m::mock (DaemonKeyProviderService::class);
8672 $ this ->permissionRepository = m::mock (PermissionRepositoryInterface::class);
8773 $ this ->permissionService = m::mock (PermissionCreationService::class);
8874 $ this ->repository = m::mock (SubuserRepositoryInterface::class);
89- $ this ->writer = m::mock (Writer::class);
90-
91- $ this ->service = new SubuserUpdateService (
92- $ this ->connection ,
93- $ this ->keyProviderService ,
94- $ this ->daemonRepository ,
95- $ this ->permissionService ,
96- $ this ->permissionRepository ,
97- $ this ->repository ,
98- $ this ->writer
99- );
10075 }
10176
10277 /**
@@ -105,22 +80,20 @@ public function setUp()
10580 public function testPermissionsAreUpdated ()
10681 {
10782 $ subuser = factory (Subuser::class)->make ();
108- $ subuser ->server = factory (Server::class)->make ();
83+ $ subuser ->setRelation ( ' server ' , factory (Server::class)->make () );
10984
110- $ this ->repository ->shouldReceive ('getWithServer ' )->with ($ subuser-> id )->once ()->andReturn ($ subuser );
85+ $ this ->repository ->shouldReceive ('getWithServer ' )->with ($ subuser )->once ()->andReturn ($ subuser );
11186 $ this ->connection ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
112- $ this ->permissionRepository ->shouldReceive ('deleteWhere ' )->with ([['subuser_id ' , '= ' , $ subuser ->id ]])
113- ->once ()->andReturnNull ();
87+ $ this ->permissionRepository ->shouldReceive ('deleteWhere ' )->with ([['subuser_id ' , '= ' , $ subuser ->id ]])->once ()->andReturnNull ();
11488 $ this ->permissionService ->shouldReceive ('handle ' )->with ($ subuser ->id , ['some-permission ' ])->once ()->andReturnNull ();
11589
116- $ this ->keyProviderService ->shouldReceive ('handle ' )->with ($ subuser ->server_id , $ subuser ->user_id , false )
117- ->once ()->andReturn ('test123 ' );
118- $ this ->daemonRepository ->shouldReceive ('setNode ' )->with ($ subuser ->server ->node_id )->once ()->andReturnSelf ()
119- ->shouldReceive ('revokeAccessKey ' )->with ('test123 ' )->once ()->andReturnNull ();
90+ $ this ->keyProviderService ->shouldReceive ('handle ' )->with ($ subuser ->server_id , $ subuser ->user_id , false )->once ()->andReturn ('test123 ' );
91+ $ this ->daemonRepository ->shouldReceive ('setNode ' )->with ($ subuser ->server ->node_id )->once ()->andReturnSelf ();
92+ $ this ->daemonRepository ->shouldReceive ('revokeAccessKey ' )->with ('test123 ' )->once ()->andReturnNull ();
12093
12194 $ this ->connection ->shouldReceive ('commit ' )->withNoArgs ()->once ()->andReturnNull ();
12295
123- $ this ->service ->handle ($ subuser-> id , ['some-permission ' ]);
96+ $ this ->getService () ->handle ($ subuser , ['some-permission ' ]);
12497 $ this ->assertTrue (true );
12598 }
12699
@@ -129,29 +102,42 @@ public function testPermissionsAreUpdated()
129102 */
130103 public function testExceptionIsThrownIfDaemonConnectionFails ()
131104 {
105+ $ this ->configureExceptionMock ();
106+
132107 $ subuser = factory (Subuser::class)->make ();
133- $ subuser ->server = factory (Server::class)->make ();
108+ $ subuser ->setRelation ( ' server ' , factory (Server::class)->make () );
134109
135- $ this ->repository ->shouldReceive ('getWithServer ' )->with ($ subuser-> id )->once ()->andReturn ($ subuser );
110+ $ this ->repository ->shouldReceive ('getWithServer ' )->with ($ subuser )->once ()->andReturn ($ subuser );
136111 $ this ->connection ->shouldReceive ('beginTransaction ' )->withNoArgs ()->once ()->andReturnNull ();
137- $ this ->permissionRepository ->shouldReceive ('deleteWhere ' )->with ([['subuser_id ' , '= ' , $ subuser ->id ]])
138- ->once ()->andReturnNull ();
112+ $ this ->permissionRepository ->shouldReceive ('deleteWhere ' )->with ([['subuser_id ' , '= ' , $ subuser ->id ]])->once ()->andReturnNull ();
139113 $ this ->permissionService ->shouldReceive ('handle ' )->with ($ subuser ->id , [])->once ()->andReturnNull ();
140114
141- $ this ->keyProviderService ->shouldReceive ('handle ' )->with ($ subuser ->server_id , $ subuser ->user_id , false )
142- ->once ()->andReturn ('test123 ' );
143- $ this ->daemonRepository ->shouldReceive ('setNode ' )->once ()->andThrow ($ this ->exception );
115+ $ this ->keyProviderService ->shouldReceive ('handle ' )->with ($ subuser ->server_id , $ subuser ->user_id , false )->once ()->andReturn ('test123 ' );
116+ $ this ->daemonRepository ->shouldReceive ('setNode ' )->with ($ subuser ->server ->node_id )->once ()->andThrow ($ this ->getExceptionMock ());
144117 $ this ->connection ->shouldReceive ('rollBack ' )->withNoArgs ()->once ()->andReturnNull ();
145- $ this ->writer ->shouldReceive ('warning ' )->with ($ this ->exception )->once ()->andReturnNull ();
146- $ this ->exception ->shouldReceive ('getResponse ' )->withNoArgs ()->once ()->andReturnNull ();
147118
148119 try {
149- $ this ->service ->handle ($ subuser-> id , []);
120+ $ this ->getService () ->handle ($ subuser , []);
150121 } catch (PterodactylException $ exception ) {
151- $ this ->assertInstanceOf (DisplayException::class, $ exception );
152- $ this ->assertEquals (trans ('exceptions.daemon_connection_failed ' , [
153- 'code ' => 'E_CONN_REFUSED ' ,
154- ]), $ exception ->getMessage ());
122+ $ this ->assertInstanceOf (DaemonConnectionException::class, $ exception );
123+ $ this ->assertInstanceOf (RequestException::class, $ exception ->getPrevious ());
155124 }
156125 }
126+
127+ /**
128+ * Return an instance of the service with mocked dependencies for testing.
129+ *
130+ * @return \Pterodactyl\Services\Subusers\SubuserUpdateService
131+ */
132+ private function getService (): SubuserUpdateService
133+ {
134+ return new SubuserUpdateService (
135+ $ this ->connection ,
136+ $ this ->keyProviderService ,
137+ $ this ->daemonRepository ,
138+ $ this ->permissionService ,
139+ $ this ->permissionRepository ,
140+ $ this ->repository
141+ );
142+ }
157143}
0 commit comments