Skip to content

Commit

Permalink
updated all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
naterfute committed Oct 27, 2024
1 parent 304bcf0 commit 98f228c
Show file tree
Hide file tree
Showing 48 changed files with 159 additions and 288 deletions.
2 changes: 1 addition & 1 deletion tests/Assertions/AssertsActivityLogged.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
trait AssertsActivityLogged
{
/**
* @param \Illuminate\Database\Eloquent\Model|array $subjects
* @param Model|array $subjects
*/
public function assertActivityFor(string $event, ?Model $actor, ...$subjects): void
{
Expand Down
21 changes: 0 additions & 21 deletions tests/CreatesApplication.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ public function testDeleteUser()
/**
* Test that an API key without write permissions cannot create, update, or
* delete a user model.
*
* @dataProvider userWriteEndpointsDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('userWriteEndpointsDataProvider')]
public function testApiKeyWithoutWritePermissions(string $method, string $url)
{
$this->createNewDefaultApiKey($this->getApiUser(), ['r_users' => AdminAcl::READ]);
Expand Down
14 changes: 7 additions & 7 deletions tests/Integration/Api/Client/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AccountControllerTest extends ClientApiIntegrationTestCase
*/
public function testAccountDetailsAreReturned()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$response = $this->actingAs($user)->get('/api/client/account');
Expand All @@ -38,7 +38,7 @@ public function testAccountDetailsAreReturned()
*/
public function testEmailIsUpdated()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$response = $this->actingAs($user)->putJson('/api/client/account/email', [
Expand All @@ -57,7 +57,7 @@ public function testEmailIsUpdated()
*/
public function testEmailIsNotUpdatedWhenPasswordIsInvalid()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$response = $this->actingAs($user)->putJson('/api/client/account/email', [
Expand All @@ -76,7 +76,7 @@ public function testEmailIsNotUpdatedWhenPasswordIsInvalid()
*/
public function testEmailIsNotUpdatedWhenNotValid()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$response = $this->actingAs($user)->putJson('/api/client/account/email', [
Expand All @@ -103,7 +103,7 @@ public function testEmailIsNotUpdatedWhenNotValid()
*/
public function testPasswordIsUpdated()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$initialHash = $user->password;
Expand All @@ -129,7 +129,7 @@ public function testPasswordIsUpdated()
*/
public function testPasswordIsNotUpdatedIfCurrentPasswordIsInvalid()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$response = $this->actingAs($user)->putJson('/api/client/account/password', [
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testErrorIsReturnedForInvalidRequestData()
*/
public function testErrorIsReturnedIfPasswordIsNotConfirmed()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$response = $this->actingAs($user)->putJson('/api/client/account/password', [
Expand Down
37 changes: 18 additions & 19 deletions tests/Integration/Api/Client/ApiKeyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ protected function tearDown(): void
*/
public function testApiKeysAreReturned()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();
/** @var \Pterodactyl\Models\ApiKey $key */
/** @var ApiKey $key */
$key = ApiKey::factory()->for($user)->create([
'key_type' => ApiKey::TYPE_ACCOUNT,
]);
Expand All @@ -44,12 +44,11 @@ public function testApiKeysAreReturned()
* Test that an API key can be created for the client account. This also checks that the
* API key secret is returned as metadata in the response since it will not be returned
* after that point.
*
* @dataProvider validIPAddressDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('validIPAddressDataProvider')]
public function testApiKeyCanBeCreatedForAccount(array $data)
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

// Small subtest to ensure we're always comparing the number of keys to the
Expand All @@ -67,7 +66,7 @@ public function testApiKeyCanBeCreatedForAccount(array $data)
->assertOk()
->assertJsonPath('object', ApiKey::RESOURCE_NAME);

/** @var \Pterodactyl\Models\ApiKey $key */
/** @var ApiKey $key */
$key = ApiKey::query()->where('identifier', $response->json('attributes.identifier'))->firstOrFail();

$this->assertJsonTransformedWith($response->json('attributes'), $key);
Expand Down Expand Up @@ -104,7 +103,7 @@ public function testApiKeyCannotSpecifyMoreThanFiftyIps()
*/
public function testApiKeyLimitIsApplied()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();
ApiKey::factory()->times(25)->for($user)->create([
'key_type' => ApiKey::TYPE_ACCOUNT,
Expand Down Expand Up @@ -145,9 +144,9 @@ public function testValidationErrorIsReturnedForBadRequests()
->assertJsonPath('errors.0.detail', 'The description may not be greater than 500 characters.');

$this->postJson('/api/client/account/api-keys', [
'description' => 'Foobar',
'allowed_ips' => ['hodor', '127.0.0.1', 'hodor/24'],
])
'description' => 'Foobar',
'allowed_ips' => ['hodor', '127.0.0.1', 'hodor/24'],
])
->assertUnprocessable()
->assertJsonPath('errors.0.detail', '"hodor" is not a valid IP address or CIDR range.')
->assertJsonPath('errors.0.meta.source_field', 'allowed_ips.0')
Expand All @@ -160,9 +159,9 @@ public function testValidationErrorIsReturnedForBadRequests()
*/
public function testApiKeyCanBeDeleted()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();
/** @var \Pterodactyl\Models\ApiKey $key */
/** @var ApiKey $key */
$key = ApiKey::factory()->for($user)->create([
'key_type' => ApiKey::TYPE_ACCOUNT,
]);
Expand All @@ -179,9 +178,9 @@ public function testApiKeyCanBeDeleted()
*/
public function testNonExistentApiKeyDeletionReturns404Error()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();
/** @var \Pterodactyl\Models\ApiKey $key */
/** @var ApiKey $key */
$key = ApiKey::factory()->create([
'user_id' => $user->id,
'key_type' => ApiKey::TYPE_ACCOUNT,
Expand All @@ -200,11 +199,11 @@ public function testNonExistentApiKeyDeletionReturns404Error()
*/
public function testApiKeyBelongingToAnotherUserCannotBeDeleted()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();
/** @var \Pterodactyl\Models\User $user2 */
/** @var User $user2 */
$user2 = User::factory()->create();
/** @var \Pterodactyl\Models\ApiKey $key */
/** @var ApiKey $key */
$key = ApiKey::factory()->for($user2)->create([
'key_type' => ApiKey::TYPE_ACCOUNT,
]);
Expand All @@ -223,9 +222,9 @@ public function testApiKeyBelongingToAnotherUserCannotBeDeleted()
*/
public function testApplicationApiKeyCannotBeDeleted()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();
/** @var \Pterodactyl\Models\ApiKey $key */
/** @var ApiKey $key */
$key = ApiKey::factory()->for($user)->create([
'key_type' => ApiKey::TYPE_APPLICATION,
]);
Expand Down
7 changes: 4 additions & 3 deletions tests/Integration/Api/Client/ClientApiIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ protected function tearDown(): void
* to keep re-assigning variables.
*
* @param \Illuminate\Http\Response $response
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Testing\TestResponse
*/
protected function createTestResponse($response)
protected function createTestResponse($response, $request)
{
return TestResponse::fromBaseResponse($response);
return TestResponse::fromBaseResponse($response, $request);
}

/**
* Returns a link to the specific resource using the client API.
*/
protected function link(mixed $model, string $append = null): string
protected function link(mixed $model, ?string $append = null): string
{
switch (get_class($model)) {
case Server::class:
Expand Down
11 changes: 5 additions & 6 deletions tests/Integration/Api/Client/ClientControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public function testServersAreFilteredUsingNameAndUuidInformation()
*/
public function testServersAreFilteredUsingAllocationInformation()
{
/** @var \Pterodactyl\Models\User $user */
/** @var \Pterodactyl\Models\Server $server */
/** @var User $user */
/** @var Server $server */
[$user, $server] = $this->generateTestAccount();
$server2 = $this->createServerModel(['user_id' => $user->id, 'node_id' => $server->node_id]);

Expand Down Expand Up @@ -203,7 +203,7 @@ public function testFilterOnlyOwnerServers()
*/
public function testPermissionsAreReturned()
{
/** @var \Pterodactyl\Models\User $user */
/** @var User $user */
$user = User::factory()->create();

$this->actingAs($user)
Expand Down Expand Up @@ -285,9 +285,8 @@ public function testAllServersAreReturnedToAdmin()
/**
* Test that no servers get returned if the user requests all admin level servers by using
* ?type=admin or ?type=admin-all in the request.
*
* @dataProvider filterTypeDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('filterTypeDataProvider')]
public function testNoServersAreReturnedIfAdminFilterIsPassedByRegularUser(string $type)
{
/** @var \Pterodactyl\Models\User[] $users */
Expand All @@ -309,7 +308,7 @@ public function testNoServersAreReturnedIfAdminFilterIsPassedByRegularUser(strin
*/
public function testOnlyPrimaryAllocationIsReturnedToSubuser()
{
/** @var \Pterodactyl\Models\Server $server */
/** @var Server $server */
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
$server->allocation->notes = 'Test notes';
$server->allocation->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

class AllocationAuthorizationTest extends ClientApiIntegrationTestCase
{
/**
* @dataProvider methodDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]
public function testAccessToAServersAllocationsIsRestrictedProperly(string $method, string $endpoint)
{
// The API $user is the owner of $server1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public function setUp(): void

/**
* Tests that a new allocation can be properly assigned to a server.
*
* @dataProvider permissionDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('permissionDataProvider')]
public function testNewAllocationCanBeAssignedToServer(array $permission)
{
/** @var \Pterodactyl\Models\Server $server */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ class DeleteAllocationTest extends ClientApiIntegrationTestCase
/**
* Test that an allocation is deleted from the server and the notes are properly reset
* to an empty value on assignment.
*
* @dataProvider permissionDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('permissionDataProvider')]
public function testAllocationCanBeDeletedFromServer(array $permission)
{
/** @var \Pterodactyl\Models\Server $server */
[$user, $server] = $this->generateTestAccount($permission);
$server->update(['allocation_limit' => 2]);

/** @var \Pterodactyl\Models\Allocation $allocation */
/** @var Allocation $allocation */
$allocation = Allocation::factory()->create([
'server_id' => $server->id,
'node_id' => $server->node_id,
Expand All @@ -41,7 +40,7 @@ public function testErrorIsReturnedIfUserDoesNotHavePermission()
/** @var \Pterodactyl\Models\Server $server */
[$user, $server] = $this->generateTestAccount([Permission::ACTION_ALLOCATION_CREATE]);

/** @var \Pterodactyl\Models\Allocation $allocation */
/** @var Allocation $allocation */
$allocation = Allocation::factory()->create([
'server_id' => $server->id,
'node_id' => $server->node_id,
Expand Down Expand Up @@ -73,7 +72,7 @@ public function testAllocationCannotBeDeletedIfServerLimitIsNotDefined()
{
[$user, $server] = $this->generateTestAccount();

/** @var \Pterodactyl\Models\Allocation $allocation */
/** @var Allocation $allocation */
$allocation = Allocation::factory()->forServer($server)->create(['notes' => 'Test notes']);

$this->actingAs($user)->deleteJson($this->link($allocation))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

class BackupAuthorizationTest extends ClientApiIntegrationTestCase
{
/**
* @dataProvider methodDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]
public function testAccessToAServersBackupIsRestrictedProperly(string $method, string $endpoint)
{
// The API $user is the owner of $server1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testBackupCanBeDeleted()

[$user, $server] = $this->generateTestAccount([Permission::ACTION_BACKUP_DELETE]);

/** @var \Pterodactyl\Models\Backup $backup */
/** @var Backup $backup */
$backup = Backup::factory()->create(['server_id' => $server->id]);

$this->repository->expects('setServer->delete')->with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

class DatabaseAuthorizationTest extends ClientApiIntegrationTestCase
{
/**
* @dataProvider methodDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]
public function testAccessToAServersDatabasesIsRestrictedProperly(string $method, string $endpoint)
{
// The API $user is the owner of $server1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public function testServerAllocationsAreNotReturnedWithoutPermission()

/**
* Tests that notes on an allocation can be set correctly.
*
* @dataProvider updatePermissionsDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('updatePermissionsDataProvider')]
public function testAllocationNotesCanBeUpdated(array $permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
Expand Down Expand Up @@ -96,9 +95,7 @@ public function testAllocationNotesCannotBeUpdatedByInvalidUsers()
$this->actingAs($user)->postJson($this->link($server->allocation))->assertForbidden();
}

/**
* @dataProvider updatePermissionsDataProvider
*/
#[\PHPUnit\Framework\Attributes\DataProvider('updatePermissionsDataProvider')]
public function testPrimaryAllocationCanBeModified(array $permissions)
{
[$user, $server] = $this->generateTestAccount($permissions);
Expand Down
Loading

0 comments on commit 98f228c

Please sign in to comment.