1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-01-29 19:07:48 +01:00

Add new endpoint to check an url for existing links (#6)

This commit is contained in:
Kovah 2020-06-24 17:10:10 +02:00
parent 3ffe6daadb
commit 3eb941bdb8
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use App\Models\Link;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class LinkCheckController extends Controller
{
/**
* Search for a link based on a given url
*
* @param Request $request
* @return JsonResponse
*/
public function __invoke(Request $request): JsonResponse
{
$searchedUrl = $request->input('url', false);
if (!$searchedUrl) {
return response()->json(['linksFound' => false]);
}
$linkCount = Link::byUser($request->user()->id)
->where('url', trim($searchedUrl))
->count();
return response()->json(['linksFound' => $linkCount > 0]);
}
}

View File

@ -1,5 +1,6 @@
<?php
use App\Http\Controllers\API\LinkCheckController;
use App\Http\Controllers\API\LinkController;
use App\Http\Controllers\API\LinkNotesController;
use App\Http\Controllers\API\ListController;
@ -22,6 +23,10 @@ use App\Http\Controllers\API\TagLinksController;
Route::prefix('v1')->group(function () {
Route::middleware('auth:api')->group(function () {
Route::get('links/check', LinkCheckController::class)
->name('api.links.check');
Route::apiResource('links', LinkController::class)
->names([
'index' => 'api.links.index',

View File

@ -0,0 +1,58 @@
<?php
namespace Tests\Controller\API;
use App\Models\Link;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class LinkCheckApiTest extends ApiTestCase
{
use DatabaseTransactions;
use DatabaseMigrations;
public function testUnauthorizedRequest(): void
{
$response = $this->getJson('api/v1/links/check');
$response->assertUnauthorized();
}
public function testSuccessfulLinkCheck(): void
{
factory(Link::class)->create([
'url' => 'https://example.com',
]);
$response = $this->getJson('api/v1/links/check?url=https://example.com', $this->generateHeaders());
$response->assertStatus(200)
->assertJson([
'linksFound' => true,
]);
}
public function testNegativeLinkCheck(): void
{
factory(Link::class)->create([
'url' => 'https://test.com',
]);
$response = $this->getJson('api/v1/links/check?url=https://example.com', $this->generateHeaders());
$response->assertStatus(200)
->assertJson([
'linksFound' => false,
]);
}
public function testCheckWithoutQuery(): void
{
$response = $this->getJson('api/v1/links/check', $this->generateHeaders());
$response->assertStatus(200)
->assertJson([
'linksFound' => false,
]);
}
}