Files
Toiminnanohjaus/tests/Feature/Modules/TimeTracking/TimeEntriesTest.php
petriandClaude Opus 5.5 1acdaa5b19 Harden the app: block project files, add rate limits and security headers
- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 06:25:26 +02:00

74 lines
3.0 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Jobs\Application\UseCases\RemoveTask;
use App\Modules\Jobs\Domain\Exceptions\TaskHasTimeEntries;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->employee = User::factory()->create();
$this->actingAs($this->employee);
$this->task = TaskModel::factory()->assignedTo([$this->employee->id])->create(['title' => 'Mainoskuvat']);
$this->monday = now()->startOfWeek()->toDateString();
});
it('saves and clears a cell of the weekly view', function () {
$this->get(route('time.week'))->assertOk()->assertSee('Mainoskuvat');
Livewire::test('time::week')
->call('saveCell', $this->task->id, $this->monday, '2,5')
->assertSee('2,5 h.')
->call('saveCell', $this->task->id, $this->monday, '1,1')
->assertHasErrors(["cells.{$this->task->id}.{$this->monday}"]);
expect((float) TimeEntryModel::query()->sole()->hours)->toBe(2.5);
Livewire::test('time::week')->call('saveCell', $this->task->id, $this->monday, '');
expect(TimeEntryModel::query()->count())->toBe(0);
});
it('takes the week only from the URL and the week buttons', function () {
$this->get(route('time.week', ['viikko' => "abc'); alert(1); ('"]))->assertOk()->assertSee('Mainoskuvat');
$week = Livewire::test('time::week');
expect(fn () => $week->set('week', '2026-05-20'))->toThrow(CannotUpdateLockedPropertyException::class)
->and(fn () => $week->set('selectedDay', "x'); alert(1); ('"))->toThrow(CannotUpdateLockedPropertyException::class);
$week->call('selectDay', "x'); alert(1); ('")->assertSet('selectedDay', now()->toDateString());
});
it('adds hours from the task page to the day entry and locks done jobs', function () {
Livewire::test('time::task', ['task' => $this->task->id])
->set('workedOn', $this->monday)
->set('hours', '2')
->call('save')
->set('hours', '1,5')
->set('note', 'Lisää kuvia')
->call('save')
->assertSee('kirjaus on nyt 3,5 h.');
expect(TimeEntryModel::query()->count())->toBe(1);
$this->task->customer_job_id && DB::table('customer_jobs')->where('id', $this->task->customer_job_id)->update(['status' => 'done']);
Livewire::test('time::task', ['task' => $this->task->id])->assertSee('tunnit on lukittu');
});
it('keeps subcontractors to their own jobs and blocks removing tasks with hours', function () {
TimeEntryModel::factory()->create(['task_id' => $this->task->id, 'user_id' => $this->employee->id]);
expect(fn () => app(RemoveTask::class)->execute($this->task->customer_job_id, $this->task->id))
->toThrow(TaskHasTimeEntries::class);
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('time.task', $this->task->id))->assertForbidden();
});