-
-
Notifications
You must be signed in to change notification settings - Fork 314
Add Application API tests for nodes, servers, mounts, allocations, and database hosts #2565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| it('redirects panel routes to the installer when not installed', function () { | ||
| config(['app.installed' => false]); | ||
|
|
||
| $this->get('/admin')->assertRedirect(route('installer')); | ||
| }); | ||
|
|
||
| it('does not redirect to the installer when installed', function () { | ||
| config(['app.installed' => true]); | ||
|
|
||
| // A guest hits the auth layer (401) instead of being sent to the installer. | ||
| $response = $this->get('/admin'); | ||
|
|
||
| $response->assertUnauthorized(); | ||
| expect($response->headers->get('Location'))->not->toBe(route('installer')); | ||
| }); | ||
|
|
||
| it('allows the installer route itself when not installed', function () { | ||
| config(['app.installed' => false]); | ||
|
|
||
| $this->get(route('installer'))->assertOk(); | ||
| }); | ||
81 changes: 81 additions & 0 deletions
81
tests/Integration/Api/Application/AllocationControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| namespace App\Tests\Integration\Api\Application; | ||
|
|
||
| use App\Models\Allocation; | ||
| use App\Models\Node; | ||
| use App\Services\Acl\Api\AdminAcl; | ||
| use Illuminate\Http\Response; | ||
|
|
||
| class AllocationControllerTest extends ApplicationApiIntegrationTestCase | ||
| { | ||
| private Node $node; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->node = Node::factory()->create(); | ||
| } | ||
|
|
||
| public function test_list_node_allocations(): void | ||
| { | ||
| $allocations = Allocation::factory(2)->create(['node_id' => $this->node->id]); | ||
|
|
||
| $response = $this->getJson("/api/application/nodes/{$this->node->id}/allocations"); | ||
| $response->assertStatus(Response::HTTP_OK); | ||
| $response->assertJsonCount(2, 'data'); | ||
|
|
||
| foreach ($allocations as $allocation) { | ||
| $response->assertJsonFragment(['id' => $allocation->id, 'port' => $allocation->port]); | ||
| } | ||
| } | ||
|
|
||
| public function test_create_allocations_for_single_ports_and_ranges(): void | ||
| { | ||
| $this->postJson("/api/application/nodes/{$this->node->id}/allocations", [ | ||
| 'ip' => '10.0.0.1', | ||
| 'ports' => ['25565', '25570-25572'], | ||
| ])->assertStatus(Response::HTTP_NO_CONTENT); | ||
|
|
||
| $this->assertEqualsCanonicalizing( | ||
| [25565, 25570, 25571, 25572], | ||
| Allocation::query()->where('node_id', $this->node->id)->where('ip', '10.0.0.1')->pluck('port')->all(), | ||
| ); | ||
| } | ||
|
|
||
| public function test_create_allocations_requires_ports(): void | ||
| { | ||
| $this->postJson("/api/application/nodes/{$this->node->id}/allocations", [ | ||
| 'ip' => '10.0.0.1', | ||
| ])->assertUnprocessable(); | ||
| } | ||
|
|
||
| public function test_delete_unassigned_allocation(): void | ||
| { | ||
| $allocation = Allocation::factory()->create(['node_id' => $this->node->id, 'server_id' => null]); | ||
|
|
||
| $this->deleteJson("/api/application/nodes/{$this->node->id}/allocations/{$allocation->id}") | ||
| ->assertStatus(Response::HTTP_NO_CONTENT); | ||
|
|
||
| $this->assertDatabaseMissing('allocations', ['id' => $allocation->id]); | ||
| } | ||
|
|
||
| public function test_assigned_allocation_cannot_be_deleted(): void | ||
| { | ||
| $server = $this->createServerModel(['node_id' => $this->node->id]); | ||
|
|
||
| $this->deleteJson("/api/application/nodes/{$this->node->id}/allocations/{$server->allocation_id}") | ||
| ->assertStatus(Response::HTTP_BAD_REQUEST); | ||
|
|
||
| $this->assertDatabaseHas('allocations', ['id' => $server->allocation_id]); | ||
| } | ||
|
|
||
| public function test_error_returned_if_no_permission(): void | ||
| { | ||
| $this->createNewDefaultApiKey($this->getApiUser(), [Allocation::RESOURCE_NAME => AdminAcl::NONE]); | ||
|
|
||
| $response = $this->getJson("/api/application/nodes/{$this->node->id}/allocations"); | ||
| $this->assertAccessDeniedJson($response); | ||
| } | ||
| } |
148 changes: 148 additions & 0 deletions
148
tests/Integration/Api/Application/DatabaseHostControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| <?php | ||
|
|
||
| namespace App\Tests\Integration\Api\Application; | ||
|
|
||
| use App\Data\Api\Application\DatabaseHostData; | ||
| use App\Models\DatabaseHost; | ||
| use App\Services\Acl\Api\AdminAcl; | ||
| use Exception; | ||
| use Illuminate\Database\Connection; | ||
| use Illuminate\Http\Response; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Mockery; | ||
| use PDO; | ||
| use PDOException; | ||
|
|
||
| class DatabaseHostControllerTest extends ApplicationApiIntegrationTestCase | ||
| { | ||
| /** | ||
| * The host services confirm access via DatabaseHost->buildConnection()->getPdo(), | ||
| * so fake the remote connection while leaving the panel's own connection real. | ||
| */ | ||
| private function fakeRemoteDatabaseConnection(?Exception $exception = null): void | ||
| { | ||
| $connection = Mockery::mock(Connection::class); | ||
|
|
||
| if ($exception) { | ||
| $connection->shouldReceive('getPdo')->andThrow($exception); | ||
| } else { | ||
| $connection->shouldReceive('getPdo')->andReturn(Mockery::mock(PDO::class)); | ||
| } | ||
|
|
||
| $manager = Mockery::mock(app('db')); | ||
| $manager->shouldReceive('build')->andReturn($connection); | ||
| DB::swap($manager); | ||
| $this->app->instance('db', $manager); | ||
| } | ||
|
|
||
| public function test_list_all_database_hosts(): void | ||
| { | ||
| $host = DatabaseHost::factory()->create(); | ||
|
|
||
| $response = $this->getJson('/api/application/database-hosts'); | ||
| $response->assertStatus(Response::HTTP_OK); | ||
| $response->assertJsonCount(1, 'data'); | ||
| $response->assertJsonFragment(['id' => $host->id]); | ||
| } | ||
|
|
||
| public function test_return_single_database_host(): void | ||
| { | ||
| $host = DatabaseHost::factory()->create(); | ||
|
|
||
| $response = $this->getJson('/api/application/database-hosts/' . $host->id); | ||
| $response->assertStatus(Response::HTTP_OK); | ||
| $response->assertJson([ | ||
| 'object' => 'database_host', | ||
| 'attributes' => $this->getExpectedData(DatabaseHostData::class, $host), | ||
| ], true); | ||
| } | ||
|
|
||
| public function test_create_database_host(): void | ||
| { | ||
| $this->fakeRemoteDatabaseConnection(); | ||
|
|
||
| $response = $this->postJson('/api/application/database-hosts', [ | ||
| 'name' => 'api-db-host', | ||
| 'host' => '127.0.0.1', | ||
| 'port' => 3306, | ||
| 'username' => 'pelicanuser', | ||
| 'password' => 'secret1234', | ||
| ]); | ||
|
|
||
| $response->assertStatus(Response::HTTP_CREATED); | ||
| $this->assertDatabaseHas('database_hosts', ['name' => 'api-db-host', 'host' => '127.0.0.1']); | ||
| } | ||
|
|
||
| public function test_database_host_is_not_saved_when_connection_fails(): void | ||
| { | ||
| $this->fakeRemoteDatabaseConnection(new PDOException('Connection refused')); | ||
|
|
||
| $this->postJson('/api/application/database-hosts', [ | ||
| 'name' => 'unreachable-host', | ||
| 'host' => '10.0.0.99', | ||
| 'port' => 3306, | ||
| 'username' => 'pelicanuser', | ||
| 'password' => 'secret1234', | ||
| ])->assertServerError(); | ||
|
|
||
| $this->assertDatabaseMissing('database_hosts', ['name' => 'unreachable-host']); | ||
| } | ||
|
|
||
| public function test_create_database_host_requires_host(): void | ||
| { | ||
| $this->postJson('/api/application/database-hosts', [ | ||
| 'name' => 'api-db-host', | ||
| 'port' => 3306, | ||
| 'username' => 'pelicanuser', | ||
| ])->assertUnprocessable(); | ||
| } | ||
|
|
||
| public function test_update_database_host(): void | ||
| { | ||
| $this->fakeRemoteDatabaseConnection(); | ||
|
|
||
| $host = DatabaseHost::factory()->create(); | ||
|
|
||
| $response = $this->patchJson('/api/application/database-hosts/' . $host->id, [ | ||
| 'name' => 'renamed-db-host', | ||
| 'host' => $host->host, | ||
| 'port' => $host->port, | ||
| 'username' => $host->username, | ||
| ]); | ||
|
|
||
| $response->assertStatus(Response::HTTP_OK); | ||
| $this->assertDatabaseHas('database_hosts', ['id' => $host->id, 'name' => 'renamed-db-host']); | ||
| } | ||
|
|
||
| public function test_delete_database_host(): void | ||
| { | ||
| $host = DatabaseHost::factory()->create(); | ||
|
|
||
| $this->deleteJson('/api/application/database-hosts/' . $host->id) | ||
| ->assertStatus(Response::HTTP_NO_CONTENT); | ||
|
|
||
| $this->assertDatabaseMissing('database_hosts', ['id' => $host->id]); | ||
| } | ||
|
|
||
| public function test_error_returned_if_no_permission(): void | ||
| { | ||
| $host = DatabaseHost::factory()->create(); | ||
| $this->createNewDefaultApiKey($this->getApiUser(), [DatabaseHost::RESOURCE_NAME => AdminAcl::NONE]); | ||
|
|
||
| $response = $this->getJson('/api/application/database-hosts/' . $host->id); | ||
| $this->assertAccessDeniedJson($response); | ||
| } | ||
|
|
||
| public function test_api_key_without_write_permissions_cannot_create(): void | ||
| { | ||
| $this->createNewDefaultApiKey($this->getApiUser(), [DatabaseHost::RESOURCE_NAME => AdminAcl::READ]); | ||
|
|
||
| $response = $this->postJson('/api/application/database-hosts', [ | ||
| 'name' => 'api-db-host', | ||
| 'host' => '127.0.0.1', | ||
| 'port' => 3306, | ||
| 'username' => 'pelicanuser', | ||
| ]); | ||
| $this->assertAccessDeniedJson($response); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.