Skip to content
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

Added controller tests for missing endpoints #23

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading