47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Models\WeightEntry;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Laravel\Sanctum\Sanctum;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
it('stores and lists the authenticated user weight history', function () {
|
|
$user = User::factory()->create();
|
|
Sanctum::actingAs($user);
|
|
|
|
$this->postJson('/api/weight-entries', [
|
|
'weightKg' => 69.35,
|
|
'measuredAt' => '2026-08-10T07:30:00Z',
|
|
])
|
|
->assertCreated()
|
|
->assertJsonPath('data.weightKg', 69.35);
|
|
|
|
$this->getJson('/api/weight-entries?days=365')
|
|
->assertOk()
|
|
->assertJsonPath('data.0.weightKg', 70)
|
|
->assertJsonPath('data.1.weightKg', 69.35);
|
|
});
|
|
|
|
it('does not allow deleting another users weight entry', function () {
|
|
$user = User::factory()->create();
|
|
$otherUser = User::factory()->create();
|
|
$entry = WeightEntry::factory()->for($otherUser)->create();
|
|
Sanctum::actingAs($user);
|
|
|
|
$this->deleteJson("/api/weight-entries/{$entry->id}")->assertNotFound();
|
|
$this->assertModelExists($entry);
|
|
});
|
|
|
|
it('validates weight measurements', function () {
|
|
Sanctum::actingAs(User::factory()->create());
|
|
|
|
$this->postJson('/api/weight-entries', [
|
|
'weightKg' => 10,
|
|
'measuredAt' => now()->addDay()->toISOString(),
|
|
])
|
|
->assertUnprocessable()
|
|
->assertJsonValidationErrors(['weightKg', 'measuredAt']);
|
|
});
|