Skip to content

Commit cd35727

Browse files
committed
Add test coverage to ensure filters don't unexpectedly get broken
1 parent 40d4459 commit cd35727

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

app/Models/Filters/MultiFieldServerFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public function __invoke(Builder $query, $value, string $property)
4848
function (Builder $builder) use ($parts) {
4949
$builder->orWhere('allocations.ip', $parts[0]);
5050
if (!is_null($parts[1] ?? null)) {
51-
$builder->where('allocations.port', 'LIKE', "%{$parts[1]}");
51+
$builder->where('allocations.port', 'LIKE', "{$parts[1]}%");
5252
}
5353
},
5454
// Otherwise, just try to search for that specific port in the allocations.
5555
function (Builder $builder) use ($value) {
56-
$builder->orWhere('allocations.port', substr($value, 1));
56+
$builder->orWhere('allocations.port', 'LIKE', substr($value, 1) . '%');
5757
}
5858
);
5959
})

tests/Integration/Api/Client/ClientControllerTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Pterodactyl\Models\Server;
77
use Pterodactyl\Models\Subuser;
88
use Pterodactyl\Models\Permission;
9+
use Pterodactyl\Models\Allocation;
910

1011
class ClientControllerTest extends ClientApiIntegrationTestCase
1112
{
@@ -38,6 +39,105 @@ public function testOnlyLoggedInUsersServersAreReturned()
3839
$response->assertJsonPath('meta.pagination.per_page', 50);
3940
}
4041

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+
41141
/**
42142
* Test that servers where the user is a subuser are returned by default in the API call.
43143
*/

0 commit comments

Comments
 (0)