|
6 | 6 | use Pterodactyl\Models\Server; |
7 | 7 | use Pterodactyl\Models\Subuser; |
8 | 8 | use Pterodactyl\Models\Permission; |
| 9 | +use Pterodactyl\Models\Allocation; |
9 | 10 |
|
10 | 11 | class ClientControllerTest extends ClientApiIntegrationTestCase |
11 | 12 | { |
@@ -38,6 +39,105 @@ public function testOnlyLoggedInUsersServersAreReturned() |
38 | 39 | $response->assertJsonPath('meta.pagination.per_page', 50); |
39 | 40 | } |
40 | 41 |
|
| 42 | + /** |
| 43 | + * Test that using ?filter[*]=name|uuid returns any server matching that name or UUID |
| 44 | + * with the search filters. |
| 45 | + */ |
| 46 | + public function testServersAreFilteredUsingNameAndUuidInformation() |
| 47 | + { |
| 48 | + /** @var \Pterodactyl\Models\User[] $users */ |
| 49 | + $users = factory(User::class)->times(2)->create(); |
| 50 | + $users[0]->update(['root_admin' => true]); |
| 51 | + |
| 52 | + /** @var \Pterodactyl\Models\Server[] $servers */ |
| 53 | + $servers = [ |
| 54 | + $this->createServerModel(['user_id' => $users[0]->id, 'name' => 'Julia']), |
| 55 | + $this->createServerModel(['user_id' => $users[1]->id, 'uuidShort' => '12121212', 'name' => 'Janice']), |
| 56 | + $this->createServerModel(['user_id' => $users[1]->id, 'uuid' => '88788878-12356789', 'external_id' => 'ext123', 'name' => 'Julia']), |
| 57 | + $this->createServerModel(['user_id' => $users[1]->id, 'uuid' => '88788878-abcdefgh', 'name' => 'Jennifer']), |
| 58 | + ]; |
| 59 | + |
| 60 | + $this->actingAs($users[1])->getJson('/api/client?filter[*]=Julia') |
| 61 | + ->assertOk() |
| 62 | + ->assertJsonCount(1, 'data') |
| 63 | + ->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort); |
| 64 | + |
| 65 | + $this->actingAs($users[1])->getJson('/api/client?filter[*]=ext123') |
| 66 | + ->assertOk() |
| 67 | + ->assertJsonCount(1, 'data') |
| 68 | + ->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort); |
| 69 | + |
| 70 | + $this->actingAs($users[1])->getJson('/api/client?filter[*]=ext123') |
| 71 | + ->assertOk() |
| 72 | + ->assertJsonCount(1, 'data') |
| 73 | + ->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort); |
| 74 | + |
| 75 | + $this->actingAs($users[1])->getJson('/api/client?filter[*]=12121212') |
| 76 | + ->assertOk() |
| 77 | + ->assertJsonCount(1, 'data') |
| 78 | + ->assertJsonPath('data.0.attributes.identifier', $servers[1]->uuidShort); |
| 79 | + |
| 80 | + $this->actingAs($users[1])->getJson('/api/client?filter[*]=88788878') |
| 81 | + ->assertOk() |
| 82 | + ->assertJsonCount(2, 'data') |
| 83 | + ->assertJsonPath('data.0.attributes.identifier', $servers[2]->uuidShort) |
| 84 | + ->assertJsonPath('data.1.attributes.identifier', $servers[3]->uuidShort); |
| 85 | + |
| 86 | + $this->actingAs($users[1])->getJson('/api/client?filter[*]=88788878-abcd') |
| 87 | + ->assertOk() |
| 88 | + ->assertJsonCount(1, 'data') |
| 89 | + ->assertJsonPath('data.0.attributes.identifier', $servers[3]->uuidShort); |
| 90 | + |
| 91 | + $this->actingAs($users[0])->getJson('/api/client?filter[*]=Julia&type=admin-all') |
| 92 | + ->assertOk() |
| 93 | + ->assertJsonCount(2, 'data') |
| 94 | + ->assertJsonPath('data.0.attributes.identifier', $servers[0]->uuidShort) |
| 95 | + ->assertJsonPath('data.1.attributes.identifier', $servers[2]->uuidShort); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test that using ?filter[*]=:25565 or ?filter[*]=192.168.1.1:25565 returns only those servers |
| 100 | + * with the same allocation for the given user. |
| 101 | + */ |
| 102 | + public function testServersAreFilteredUsingAllocationInformation() |
| 103 | + { |
| 104 | + /** @var \Pterodactyl\Models\User $user */ |
| 105 | + /** @var \Pterodactyl\Models\Server $server */ |
| 106 | + [$user, $server] = $this->generateTestAccount(); |
| 107 | + $server2 = $this->createServerModel(['user_id' => $user->id, 'node_id' => $server->node_id]); |
| 108 | + |
| 109 | + $allocation = factory(Allocation::class)->create(['node_id' => $server->node_id, 'server_id' => $server->id, 'ip' => '192.168.1.1', 'port' => 25565]); |
| 110 | + $allocation2 = factory(Allocation::class)->create(['node_id' => $server->node_id, 'server_id' => $server2->id, 'ip' => '192.168.1.1', 'port' => 25570]); |
| 111 | + |
| 112 | + $server->update(['allocation_id' => $allocation->id]); |
| 113 | + $server2->update(['allocation_id' => $allocation2->id]); |
| 114 | + |
| 115 | + $server->refresh(); |
| 116 | + $server2->refresh(); |
| 117 | + |
| 118 | + $this->actingAs($user)->getJson('/api/client?filter[*]=192.168.1.1') |
| 119 | + ->assertOk() |
| 120 | + ->assertJsonCount(2, 'data') |
| 121 | + ->assertJsonPath('data.0.attributes.identifier', $server->uuidShort) |
| 122 | + ->assertJsonPath('data.1.attributes.identifier', $server2->uuidShort); |
| 123 | + |
| 124 | + $this->actingAs($user)->getJson('/api/client?filter[*]=192.168.1.1:25565') |
| 125 | + ->assertOk() |
| 126 | + ->assertJsonCount(1, 'data') |
| 127 | + ->assertJsonPath('data.0.attributes.identifier', $server->uuidShort); |
| 128 | + |
| 129 | + $this->actingAs($user)->getJson('/api/client?filter[*]=:25570') |
| 130 | + ->assertOk() |
| 131 | + ->assertJsonCount(1, 'data') |
| 132 | + ->assertJsonPath('data.0.attributes.identifier', $server2->uuidShort); |
| 133 | + |
| 134 | + $this->actingAs($user)->getJson('/api/client?filter[*]=:255') |
| 135 | + ->assertOk() |
| 136 | + ->assertJsonCount(2, 'data') |
| 137 | + ->assertJsonPath('data.0.attributes.identifier', $server->uuidShort) |
| 138 | + ->assertJsonPath('data.1.attributes.identifier', $server2->uuidShort); |
| 139 | + } |
| 140 | + |
41 | 141 | /** |
42 | 142 | * Test that servers where the user is a subuser are returned by default in the API call. |
43 | 143 | */ |
|
0 commit comments