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:
parent
d47c8c00f3
commit
71cb15d007
3 changed files with 15 additions and 0 deletions
|
@ -12,6 +12,7 @@ use GuzzleHttp\Client;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
@ -286,6 +287,13 @@ class Note extends Model
|
||||||
*/
|
*/
|
||||||
public function scopeNb60(Builder $query, string $nb60id): Builder
|
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));
|
return $query->where('id', resolve(Numbers::class)->b60tonum($nb60id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -73,4 +73,11 @@ class NotesControllerTest extends TestCase
|
||||||
$response = $this->get('/notes/112233');
|
$response = $this->get('/notes/112233');
|
||||||
$response->assertNotFound();
|
$response->assertNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function checkNoteIdNotOutOfRange(): void
|
||||||
|
{
|
||||||
|
$response = $this->get('/notes/photou-photologo');
|
||||||
|
$response->assertNotFound();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue