Path: blob/1.0-develop/tests/Integration/Api/Client/SSHKeyControllerTest.php
7459 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client;34use phpseclib3\Crypt\EC;5use Pterodactyl\Models\User;6use Pterodactyl\Models\UserSSHKey;78class SSHKeyControllerTest extends ClientApiIntegrationTestCase9{10/**11* Test that only the SSH keys for the authenticated user are returned.12*/13public function testSSHKeysAreReturned()14{15$user = User::factory()->create();16$user2 = User::factory()->create();1718$key = UserSSHKey::factory()->for($user)->create();19UserSSHKey::factory()->for($user2)->rsa()->create();2021$this->actingAs($user);22$response = $this->getJson('/api/client/account/ssh-keys')23->assertOk()24->assertJsonPath('object', 'list')25->assertJsonPath('data.0.object', UserSSHKey::RESOURCE_NAME);2627$this->assertJsonTransformedWith($response->json('data.0.attributes'), $key);28}2930/**31* Test that a user's SSH key can be deleted, and that passing the fingerprint32* of another user's SSH key won't delete that key.33*/34public function testSSHKeyCanBeDeleted()35{36$user = User::factory()->create();37$user2 = User::factory()->create();3839$key = UserSSHKey::factory()->for($user)->create();40$key2 = UserSSHKey::factory()->for($user2)->create();4142$endpoint = '/api/client/account/ssh-keys/remove';4344$this->actingAs($user);45$this->postJson($endpoint)46->assertUnprocessable()47->assertJsonPath('errors.0.meta', ['source_field' => 'fingerprint', 'rule' => 'required']);4849$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();5051$this->assertSoftDeleted($key);52$this->assertNotSoftDeleted($key2);5354$this->postJson($endpoint, ['fingerprint' => $key->fingerprint])->assertNoContent();55$this->postJson($endpoint, ['fingerprint' => $key2->fingerprint])->assertNoContent();5657$this->assertNotSoftDeleted($key2);58}5960public function testDSAKeyIsRejected()61{62$user = User::factory()->create();63$key = UserSSHKey::factory()->dsa()->make();6465$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [66'name' => 'Name',67'public_key' => $key->public_key,68])69->assertUnprocessable()70->assertJsonPath('errors.0.detail', 'DSA keys are not supported.');7172$this->assertEquals(0, $user->sshKeys()->count());73}7475public function testWeakRSAKeyIsRejected()76{77$user = User::factory()->create();78$key = UserSSHKey::factory()->rsa(true)->make();7980$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [81'name' => 'Name',82'public_key' => $key->public_key,83])84->assertUnprocessable()85->assertJsonPath('errors.0.detail', 'RSA keys must be at least 2048 bytes in length.');8687$this->assertEquals(0, $user->sshKeys()->count());88}8990public function testInvalidOrPrivateKeyIsRejected()91{92$user = User::factory()->create();9394$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [95'name' => 'Name',96'public_key' => 'invalid',97])98->assertUnprocessable()99->assertJsonPath('errors.0.detail', 'The public key provided is not valid.');100101$this->assertEquals(0, $user->sshKeys()->count());102103$key = EC::createKey('Ed25519');104$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [105'name' => 'Name',106'public_key' => $key->toString('PKCS8'),107])108->assertUnprocessable()109->assertJsonPath('errors.0.detail', 'The public key provided is not valid.');110}111112public function testPublicKeyCanBeStored()113{114$user = User::factory()->create();115$key = UserSSHKey::factory()->make();116117$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [118'name' => 'Name',119'public_key' => $key->public_key,120])121->assertOk()122->assertJsonPath('object', UserSSHKey::RESOURCE_NAME)123->assertJsonPath('attributes.public_key', $key->public_key);124125$this->assertCount(1, $user->sshKeys);126$this->assertEquals($key->public_key, $user->sshKeys[0]->public_key);127}128129public function testPublicKeyThatAlreadyExistsCannotBeAddedASecondTime()130{131$user = User::factory()->create();132$key = UserSSHKey::factory()->for($user)->create();133134$this->actingAs($user)->postJson('/api/client/account/ssh-keys', [135'name' => 'Name',136'public_key' => $key->public_key,137])138->assertUnprocessable()139->assertJsonPath('errors.0.detail', 'The public key provided already exists on your account.');140141$this->assertEquals(1, $user->sshKeys()->count());142}143}144145146