Update API handling

This commit is contained in:
DariusIII
2026-07-11 10:00:01 +02:00
parent b0c781421c
commit ccb79f13b9
18 changed files with 768 additions and 240 deletions
+75
View File
@@ -118,6 +118,81 @@ class ApiRequestMatrixTest extends TestCase
->assertJsonPath('error', 'Incorrect parameter (maxage must be numeric)');
}
public function test_v2_search_reuses_cached_release_rows(): void
{
$token = (string) DB::table('users')->value('api_token');
$request = Request::create('/api/v2/search', 'GET', [
'api_token' => $token,
'id' => 'ubuntu',
]);
$releaseSearchService = Mockery::mock(ReleaseSearchService::class);
$releaseSearchService->shouldReceive('apiSearch')
->once()
->with('ubuntu', -1, 0, 100, -1, [5030], [-1], 0, 'posted_desc')
->andReturn(collect());
$releaseBrowseService = Mockery::mock(ReleaseBrowseService::class);
$releaseBrowseService->shouldNotReceive('getBrowseRangeForApi');
$controller = new ApiV2Controller(app(ApiController::class), $releaseSearchService, $releaseBrowseService);
$firstResponse = $controller->apiSearch($request);
$secondResponse = $controller->apiSearch($request);
$this->assertSame(200, $firstResponse->getStatusCode());
$this->assertSame(200, $secondResponse->getStatusCode());
$this->assertSame([], $firstResponse->getData(true)['results']);
$this->assertSame([], $secondResponse->getData(true)['results']);
}
public function test_v1_search_reuses_cached_release_rows(): void
{
$token = (string) DB::table('users')->value('api_token');
$request = Request::create('/api/v1/api', 'GET', [
't' => 'search',
'apikey' => $token,
'q' => 'ubuntu',
]);
$releaseSearchService = Mockery::mock(ReleaseSearchService::class);
$releaseSearchService->shouldReceive('apiSearch')
->once()
->with('ubuntu', -1, 0, 100, -1, [5030], [-1], 0, 'posted_desc')
->andReturn(collect());
$releaseBrowseService = Mockery::mock(ReleaseBrowseService::class);
$releaseBrowseService->shouldNotReceive('getBrowseRangeForApi');
$controller = new class($releaseSearchService, $releaseBrowseService) extends ApiController
{
/**
* @var array{data:mixed,params:array<string,mixed>,xml:bool,offset:int,type:string}|null
*/
public ?array $capturedOutput = null;
public function output(mixed $data, array $params, bool $xml, int $offset, string $type = '', array $headers = [])
{
$this->capturedOutput = [
'data' => $data,
'params' => $params,
'xml' => $xml,
'offset' => $offset,
'type' => $type,
];
}
};
$controller->api($request);
$this->assertNotNull($controller->capturedOutput);
$this->assertSame('api', $controller->capturedOutput['type']);
$this->assertSame(1, DB::table('user_requests')->count());
$controller->api($request);
$this->assertSame('api', $controller->capturedOutput['type']);
$this->assertSame(2, DB::table('user_requests')->count());
}
public function test_v1_movie_without_search_params_returns_recent_movie_feed(): void
{
$token = (string) DB::table('users')->value('api_token');
@@ -5,6 +5,7 @@ namespace Tests\Feature;
use App\Models\Category;
use App\Models\User;
use App\Models\UserExcludedCategory;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;
@@ -266,6 +267,32 @@ final class UserExcludedCategoryTest extends TestCase
$this->assertContains(2070, $exclusions);
}
public function test_cached_category_exclusions_are_invalidated_when_syncing(): void
{
$this->user->syncExcludedCategories([2070]);
$this->assertContains(2070, User::getCachedCategoryExclusionById($this->user->id));
$this->user->syncExcludedCategories([2040]);
$exclusions = User::getCachedCategoryExclusionById($this->user->id);
$this->assertNotContains(2070, $exclusions);
$this->assertContains(2040, $exclusions);
}
public function test_cached_category_exclusions_are_invalidated_when_rows_change_directly(): void
{
Cache::put(User::categoryExclusionCacheKey($this->user->id), [2070], 300);
UserExcludedCategory::create([
'users_id' => $this->user->id,
'categories_id' => 2040,
]);
$this->assertNull(Cache::get(User::categoryExclusionCacheKey($this->user->id)));
}
public function test_clearing_exclusions_works(): void
{
// First add exclusions