Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Feb 16, 2024
1 parent 970278f commit 4ec316c
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 115 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Admin/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function index(Request $request)
{
$locations = QueryBuilder::for(Location::query())
->withCount(['nodes', 'servers'])
->defaultSort('-id')
// @phpstan-ignore-next-line
->allowedFilters(
['short_code', AllowedFilter::custom('*', new FiltersLocationWildcard())],
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Admin/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function index(Request $request)
{
$tokens = QueryBuilder::for(PersonalAccessToken::query())
->with('tokenable')
->defaultSort('-id')
->where('personal_access_tokens.type', ApiKeyType::APPLICATION->value)
->paginate(min($request->query('per_page', 50), 100))->appends(
$request->query(),
Expand Down
9 changes: 5 additions & 4 deletions database/factories/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Database\Factories;

use Convoy\Models\Node;
use Convoy\Models\Server;
use Convoy\Models\User;
use Convoy\Services\Servers\ServerCreationService;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -32,8 +30,11 @@ public function definition(): array
'name' => $this->faker->word(),
'vmid' => rand(100, 5000),
'cpu' => 2,
'memory' => 17179869184,
'disk' => 17179869184,
'memory' => 2048 * 1024 * 1024,
'disk' => 20 * 1024 * 1024 * 1024,
'backup_limit' => 16,
'snapshot_limit' => 16,
'bandwidth_limit' => 100 * 1024 * 1024 * 1024,
];
}
}
56 changes: 56 additions & 0 deletions tests/Feature/Controllers/Admin/LocationControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Convoy\Models\Location;
use Convoy\Models\User;

it('can fetch locations', function () {
$user = User::factory()->create([
'root_admin' => true,
]);

$response = $this->actingAs($user)->getJson('/api/admin/locations');

$response->assertOk();
});

it('can create a location', function () {
$user = User::factory()->create([
'root_admin' => true,
]);

$response = $this->actingAs($user)->postJson('/api/admin/locations', [
'name' => 'Test Location',
'short_code' => 'test',
'description' => 'This is a test location.',
]);

$response->assertOk();
});

it('can update a location', function () {
$user = User::factory()->create([
'root_admin' => true,
]);

$location = Location::factory()->create();

$response = $this->actingAs($user)->putJson("/api/admin/locations/{$location->id}", [
'name' => 'Test Location',
'short_code' => 'test',
'description' => 'This is a test location.',
]);

$response->assertOk();
});

it('can delete a location', function () {
$user = User::factory()->create([
'root_admin' => true,
]);

$location = Location::factory()->create();

$response = $this->actingAs($user)->deleteJson("/api/admin/locations/{$location->id}");

$response->assertNoContent();
});
149 changes: 149 additions & 0 deletions tests/Feature/Controllers/Admin/Nodes/NodeControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php


use Convoy\Models\Location;
use Convoy\Models\Node;
use Convoy\Models\Server;
use Convoy\Models\User;

beforeEach(function () {
$this->user = User::factory()->create([
'root_admin' => true,
]);
$this->location = Location::factory()->create();
$this->node = Node::factory()->for($this->location)->create();
});

it('can fetch nodes', function () {
$response = $this->actingAs($this->user)->getJson('/api/admin/nodes');

$response->assertOk();
});

it('can fetch a node', function () {
$response = $this->actingAs($this->user)->getJson("/api/admin/nodes/{$this->node->id}");

$response->assertOk();
});

it('can create a node', function () {
$response = $this->actingAs($this->user)->postJson('/api/admin/nodes', [
'location_id' => $this->location->id,
'name' => 'Test Node',
'cluster' => 'proxmox',
'fqdn' => 'example.com',
'token_id' => 'test-token',
'secret' => 'test-secret',
'port' => 8006,
'memory' => 64 * 1024 * 1024 * 1024, // 64GB,
'memory_overallocate' => 0,
'disk' => 512 * 1024 * 1024 * 1024, // 512GB,
'disk_overallocate' => 0,
'vm_storage' => 'local-lvm',
'backup_storage' => 'local-lvm',
'iso_storage' => 'local-lvm',
'network' => 'vmbr0',
]);

$response->assertOk();
});

it('can update a node', function () {
$response = $this->actingAs($this->user)->putJson("/api/admin/nodes/{$this->node->id}", [
'location_id' => $this->location->id,
'name' => 'Test Node',
'cluster' => 'proxmox',
'fqdn' => 'example.com',
'token_id' => 'test-token',
'secret' => 'test-secret',
'port' => 8006,
'memory' => 64 * 1024 * 1024 * 1024, // 64GB,
'memory_overallocate' => 0,
'disk' => 512 * 1024 * 1024 * 1024, // 512GB,
'disk_overallocate' => 0,
'vm_storage' => 'local-lvm',
'backup_storage' => 'local-lvm',
'iso_storage' => 'local-lvm',
'network' => 'vmbr0',
]);

$response->assertOk();
});

it("can't downsize without over-allocating", function () {
$node = Node::factory()->for($this->location)->create([
'memory' => 64 * 1024 * 1024 * 1024, // 64GB,
'disk' => 512 * 1024 * 1024 * 1024, // 512GB,
]);

Server::factory()->for($node)->for($this->user)->create([
'memory' => 32 * 1024 * 1024 * 1024, // 32GB,
'disk' => 256 * 1024 * 1024 * 1024, // 256GB,
]);

$response = $this->actingAs($this->user)->putJson("/api/admin/nodes/{$node->id}", [
'location_id' => $this->location->id,
'name' => 'Test Node',
'cluster' => 'proxmox',
'fqdn' => 'example.com',
'token_id' => 'test-token',
'secret' => 'test-secret',
'port' => 8006,
'memory' => 16 * 1024 * 1024 * 1024, // 16GB,
'memory_overallocate' => 0,
'disk' => 128 * 1024 * 1024 * 1024, // 128GB,
'disk_overallocate' => 0,
'vm_storage' => 'local-lvm',
'backup_storage' => 'local-lvm',
'iso_storage' => 'local-lvm',
'network' => 'vmbr0',
]);

$response->assertStatus(422);
});

it('can update node without false positive overallocation', function () {
$node = Node::factory()->for($this->location)->create([
'memory' => 64 * 1024 * 1024 * 1024, // 64GB,
'disk' => 512 * 1024 * 1024 * 1024, // 512GB,
]);

Server::factory()->for($node)->for($this->user)->create([
'memory' => 64 * 1024 * 1024 * 1024, // 64GB,
'disk' => 256 * 1024 * 1024 * 1024, // 256GB,
]);

$response = $this->actingAs($this->user)->putJson("/api/admin/nodes/{$node->id}", [
'location_id' => $this->location->id,
'name' => 'New name',
'cluster' => 'proxmox',
'fqdn' => 'example.com',
'token_id' => 'test-token',
'secret' => 'test-secret',
'port' => 8006,
'memory' => 64 * 1024 * 1024 * 1024, // 64GB,
'memory_overallocate' => 0,
'disk' => 512 * 1024 * 1024 * 1024, // 512GB,
'disk_overallocate' => 0,
'vm_storage' => 'local-lvm',
'backup_storage' => 'local-lvm',
'iso_storage' => 'local-lvm',
'network' => 'vmbr0',
]);

$response->assertOk();
});

it('can delete a node', function () {
$response = $this->actingAs($this->user)->deleteJson("/api/admin/nodes/{$this->node->id}");

$response->assertNoContent();
});

it("can't delete a node with servers", function () {
Server::factory()->for($this->node)->for($this->user)->create();

$response = $this->actingAs($this->user)->deleteJson("/api/admin/nodes/{$this->node->id}");

$response->assertForbidden();
});

0 comments on commit 4ec316c

Please sign in to comment.