Skip to content

Commit

Permalink
Added controller tests for missing endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperSister committed Jan 18, 2025
1 parent ecdd86c commit 967220f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public function getUrlAttribute(string $value): string

public function getConnectionAttribute(): Connection
{
return new Connection($this->url, $this->key);
return App::makeWith(
Connection::class,
["baseUrl" =>$this->url, "key" => $this->key]
);
}

public function getFrontendStatus(): int
Expand Down
97 changes: 97 additions & 0 deletions tests/Feature/Api/SiteControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Site;
use App\RemoteSite\Connection;
use App\RemoteSite\Responses\HealthCheck;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
Expand Down Expand Up @@ -74,6 +75,82 @@ public function testRegisteringASiteFailsWhenHealthCheckFails(): void
$response->assertStatus(500);
}

public function testCheckingASiteReturnsSuccessIfCheckIsSuccessful(): void
{
$site = $this->createMockSiteInDb();

$mock = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();

$mock->method('__call')->willReturn(HealthCheck::from($site->toArray()));

$this->app->bind(Connection::class, fn () => $mock);

$response = $this->postJson(
'/api/v1/check',
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(200);
}

public function testCheckingASiteReturnErrorIfCheckIsUnsuccessful(): void
{
$this->createMockSiteInDb();

$mock = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();

$mock->method('__call')->willThrowException(new \Exception());

$this->app->bind(Connection::class, fn () => $mock);

$response = $this->postJson(
'/api/v1/check',
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(500);
}

public function testCheckingASiteReturns404ForInvalidSite(): void
{
$response = $this->postJson(
'/api/v1/check',
["url" => "https://www.joomlaf.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(404);
}

public function testDeleteASiteReturns404ForInvalidSite(): void
{
$response = $this->postJson(
'/api/v1/delete',
["url" => "https://www.joomlaf.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(404);
}

public function testDeleteASiteRemovesRow(): void
{
$this->createMockSiteInDb();

$this->assertEquals(1, Site::get()->count());

$response = $this->postJson(
'/api/v1/delete',
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(200);

$this->assertEquals(0, Site::get()->count());
}

protected function getConnectionMock(HealthCheck $response)
{
$mock = $this->getMockBuilder(Connection::class)
Expand All @@ -84,4 +161,24 @@ protected function getConnectionMock(HealthCheck $response)

return $mock;
}

protected function createMockSiteInDb(): Site
{
$site = new Site([
"php_version" => "1.0.0",
"db_type" => "mysqli",
"db_version" => "1.0.0",
"cms_version" => "1.0.0",
"server_os" => "Joomla OS 1.0.0",
"last_seen" => Carbon::now()
]);

$site->key = 'foobar123foobar123foobar123foobar123';
$site->url = 'https://www.joomla.org';
$site->last_seen = Carbon::now();

$site->save();

return $site;
}
}

0 comments on commit 967220f

Please sign in to comment.