refactor: Improve note ID validation and error handling

- Improve input validation and error handling in Note model
- Add test case for out-of-range note IDs in NotesController
This commit is contained in:
Jonny Barnes 2023-06-11 12:52:07 +01:00
parent d47c8c00f3
commit 71cb15d007
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
3 changed files with 15 additions and 0 deletions

View file

@ -12,6 +12,7 @@ use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -286,6 +287,13 @@ class Note extends Model
*/
public function scopeNb60(Builder $query, string $nb60id): Builder
{
$realId = resolve(Numbers::class)->b60tonum($nb60id);
// Check nb60 does not translate to ID too big for database int4 column
if ($realId > 2_147_483_647) {
abort(404);
}
return $query->where('id', resolve(Numbers::class)->b60tonum($nb60id));
}

Binary file not shown.

View file

@ -73,4 +73,11 @@ class NotesControllerTest extends TestCase
$response = $this->get('/notes/112233');
$response->assertNotFound();
}
/** @test */
public function checkNoteIdNotOutOfRange(): void
{
$response = $this->get('/notes/photou-photologo');
$response->assertNotFound();
}
}