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

Add controller tests for register endpoint #15

Merged
merged 7 commits into from
Nov 17, 2024
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
14 changes: 9 additions & 5 deletions app/Http/Controllers/Api/V1/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\SiteRequest;
use App\Http\Traits\ApiResponse;
use App\Jobs\CheckSiteHealth;
use App\Models\Site;
use App\RemoteSite\Connection;
use App\Http\Traits\ApiResponse;
use Carbon\Carbon;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\App;

/**
* Class SiteController
Expand All @@ -33,15 +34,18 @@ public function register(SiteRequest $request): JsonResponse
$url = $request->string('url');
$key = $request->string('key');

$connectionService = new Connection($url, $key);
$connectionService = App::makeWith(
Connection::class,
["baseUrl" => $url, "key" => $key]
);

// Do a health check
try {
$healthResponse = $connectionService->checkHealth();
} catch (ServerException $e) {
} catch (ServerException|ClientException $e) {
return $this->error($e->getMessage(), 400);
} catch (\Exception $e) {
return $this->error($e->getMessage(), 500);
} catch (ClientException|\Exception $e) {
return $this->error($e->getMessage());
}

// If successful save site
Expand Down
6 changes: 2 additions & 4 deletions app/RemoteSite/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ public function performExtractionRequest(array $requestData): array
]
);

$responseData = $this->decodeResponse($response, $request);

return $responseData;
return $this->decodeResponse($response, $request);
}

protected function performWebserviceRequest(
public function performWebserviceRequest(
HttpMethod $method,
string $endpoint,
?array $requestData = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
return new class () extends Migration {
/**
* Run the migrations.
*/
Expand Down
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
Route::controller(SiteController::class)->group(function () {
Route::post('register', 'register');
Route::post('check', 'check');
Route::delete('delete', 'delete');
Route::post('delete', 'delete');
});
});
87 changes: 87 additions & 0 deletions tests/Feature/Api/SiteControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Tests\Api\Feature;

use App\Jobs\CheckSiteHealth;
use App\Models\Site;
use App\RemoteSite\Connection;
use App\RemoteSite\Responses\HealthCheck;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;

class SiteControllerTest extends TestCase
{
use RefreshDatabase;

public function testRegisteringASiteWithoutUrlOrKeyFails(): void
{
$response = $this->postJson(
'/api/v1/register',
);

$response->assertStatus(422);
}

public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
{
Queue::fake();

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

$mock->method('__call')->willReturn(HealthCheck::from([
"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"
]));

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

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

$response->assertStatus(200);
$row = Site::where('url', 'https://www.joomla.org')->first();

$this->assertEquals("https://www.joomla.org", $row->url);
$this->assertEquals("foobar123foobar123foobar123foobar123", $row->key);
$this->assertEquals("1.0.0", $row->php_version);

Queue::assertPushed(CheckSiteHealth::class);
}

public function testRegisteringASiteFailsWhenHealthCheckFails(): void
{
$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/register',
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
);

$response->assertStatus(500);
}

protected function getConnectionMock(HealthCheck $response)
{
$mock = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->getMock();

$mock->method('__call')->willReturn($response);

return $mock;
}
}
19 changes: 0 additions & 19 deletions tests/Feature/ExampleTest.php

This file was deleted.

Loading