@@ -38,33 +38,6 @@ public function testOnlyLoggedInUsersServersAreReturned()
3838 $ response ->assertJsonPath ('meta.pagination.per_page ' , 50 );
3939 }
4040
41- /**
42- * Tests that all of the servers on the system are returned when making the request as an
43- * administrator and including the ?filter=all parameter in the URL.
44- */
45- public function testFilterIncludeAllServersWhenAdministrator ()
46- {
47- /** @var \Pterodactyl\Models\User[] $users */
48- $ users = factory (User::class)->times (3 )->create ();
49- $ users [0 ]->root_admin = true ;
50-
51- $ servers = [
52- $ this ->createServerModel (['user_id ' => $ users [0 ]->id ]),
53- $ this ->createServerModel (['user_id ' => $ users [1 ]->id ]),
54- $ this ->createServerModel (['user_id ' => $ users [2 ]->id ]),
55- ];
56-
57- $ response = $ this ->actingAs ($ users [0 ])->getJson ('/api/client?type=all ' );
58-
59- $ response ->assertOk ();
60- $ response ->assertJsonCount (3 , 'data ' );
61-
62- for ($ i = 0 ; $ i < 3 ; $ i ++) {
63- $ response ->assertJsonPath ("data. {$ i }.attributes.server_owner " , $ i === 0 );
64- $ response ->assertJsonPath ("data. {$ i }.attributes.identifier " , $ servers [$ i ]->uuidShort );
65- }
66- }
67-
6841 /**
6942 * Test that servers where the user is a subuser are returned by default in the API call.
7043 */
@@ -143,4 +116,59 @@ public function testPermissionsAreReturned()
143116 ],
144117 ]);
145118 }
119+
120+ /**
121+ * Test that only servers a user can access because they are an administrator are returned. This
122+ * will always exclude any servers they can see because they're the owner or a subuser of the server.
123+ */
124+ public function testOnlyAdminLevelServersAreReturned ()
125+ {
126+ /** @var \Pterodactyl\Models\User[] $users */
127+ $ users = factory (User::class)->times (4 )->create ();
128+ $ users [0 ]->update (['root_admin ' => true ]);
129+
130+ $ servers = [
131+ $ this ->createServerModel (['user_id ' => $ users [0 ]->id ]),
132+ $ this ->createServerModel (['user_id ' => $ users [1 ]->id ]),
133+ $ this ->createServerModel (['user_id ' => $ users [2 ]->id ]),
134+ $ this ->createServerModel (['user_id ' => $ users [3 ]->id ]),
135+ ];
136+
137+ Subuser::query ()->create ([
138+ 'user_id ' => $ users [0 ]->id ,
139+ 'server_id ' => $ servers [1 ]->id ,
140+ 'permissions ' => [Permission::ACTION_WEBSOCKET_CONNECT ],
141+ ]);
142+
143+ // Only servers 2 & 3 (0 indexed) should be returned by the API at this point. The user making
144+ // the request is the owner of server 0, and a subuser of server 1 so they should be exluded.
145+ $ response = $ this ->actingAs ($ users [0 ])->getJson ('/api/client?type=admin ' );
146+
147+ $ response ->assertOk ();
148+ $ response ->assertJsonCount (2 , 'data ' );
149+
150+ $ response ->assertJsonPath ('data.0.attributes.server_owner ' , false );
151+ $ response ->assertJsonPath ('data.0.attributes.identifier ' , $ servers [2 ]->uuidShort );
152+ $ response ->assertJsonPath ('data.1.attributes.server_owner ' , false );
153+ $ response ->assertJsonPath ('data.1.attributes.identifier ' , $ servers [3 ]->uuidShort );
154+ }
155+
156+ /**
157+ * Test that no servers get returned if the user requests all admin level servers by using
158+ * ?type=admin in the request.
159+ */
160+ public function testNoServersAreReturnedIfAdminFilterIsPassedByRegularUser ()
161+ {
162+ /** @var \Pterodactyl\Models\User[] $users */
163+ $ users = factory (User::class)->times (3 )->create ();
164+
165+ $ this ->createServerModel (['user_id ' => $ users [0 ]->id ]);
166+ $ this ->createServerModel (['user_id ' => $ users [1 ]->id ]);
167+ $ this ->createServerModel (['user_id ' => $ users [2 ]->id ]);
168+
169+ $ response = $ this ->actingAs ($ users [0 ])->getJson ('/api/client?type=admin ' );
170+
171+ $ response ->assertOk ();
172+ $ response ->assertJsonCount (0 , 'data ' );
173+ }
146174}
0 commit comments