Files
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

167 lines
6.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
use App\Models\User;
use App\Modules\Customers\Application\Contracts\CustomerQueries;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Infrastructure\Persistence\Eloquent\CustomerModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(RefreshDatabase::class);
/**
* Registers a consumer with a personal identity code through the form.
*/
function registerConsumerWithCode(string $code = '131052-308T'): CustomerModel
{
Livewire::test('customers::form')
->set('form.name', 'Maija Meikäläinen')
->set('form.type', CustomerType::Consumer->value)
->set('form.personal_identity_code', $code)
->call('save')
->assertHasNoErrors();
return CustomerModel::query()->latest('id')->firstOrFail();
}
it('stores the code encrypted, with a hash that is not the code', function () {
$this->actingAs(User::factory()->create());
$customer = registerConsumerWithCode();
$row = DB::table('customers')->where('id', $customer->id)->first();
expect($row->personal_identity_code)->not->toContain('131052')
->and($row->personal_identity_code_hash)->toHaveLength(64)
->and($row->personal_identity_code_hash)->not->toContain('131052')
->and($customer->personal_identity_code)->toBe('131052-308T')
->and($customer->toArray())->not->toHaveKey('personal_identity_code');
});
it('never puts the full code in the summary other modules get', function () {
$this->actingAs(User::factory()->create());
$customer = registerConsumerWithCode();
$summary = app(CustomerQueries::class)->findSummary($customer->id);
expect($summary?->personalIdentityCodeMasked)->toBe('••••••-308T')
->and($summary?->hasPersonalIdentityCode())->toBeTrue()
->and(serialize($summary))->not->toContain('131052');
});
it('shows the full code only to admins and management', function (string $role, bool $full) {
$this->actingAs(User::factory()->create());
$customer = registerConsumerWithCode();
$response = $this->actingAs(User::factory()->{$role}()->create())->get(route('customers.show', $customer->id))->assertOk();
if ($full) {
$response->assertSee('131052-308T');
} else {
$response->assertSee('••••••-308T')->assertDontSee('131052');
}
})->with([
'admin' => ['admin', true],
'management' => ['management', true],
'employee' => ['employee', false],
'intern' => ['intern', false],
]);
it('tells when a consumer has no code yet', function () {
$this->actingAs(User::factory()->create());
$customer = CustomerModel::factory()->consumer()->create();
$this->get(route('customers.show', $customer->id))->assertSee('Ei annettu – tarvitaan laskutukseen');
});
it('never fills the form with the code, and keeps it when the field is left empty', function () {
$this->actingAs(User::factory()->create());
$customer = registerConsumerWithCode();
$this->get(route('customers.edit', $customer->id))
->assertSee('Tallennettu: ••••••-308T.')
->assertDontSee('131052');
Livewire::test('customers::form', ['customer' => $customer->id])
->assertSet('form.personal_identity_code', '')
->set('form.name', 'Maija M.')
->call('save')
->assertHasNoErrors();
expect($customer->fresh()->personal_identity_code)->toBe('131052-308T');
});
it('lets management remove the code, but not an employee', function () {
$this->actingAs(User::factory()->create());
$customer = registerConsumerWithCode();
Livewire::test('customers::form', ['customer' => $customer->id])
->assertDontSee('Poista tallennettu henkilötunnus')
->set('form.remove_personal_identity_code', true)
->call('save')
->assertForbidden();
expect($customer->fresh()->personal_identity_code)->toBe('131052-308T');
Livewire::actingAs(User::factory()->management()->create())
->test('customers::form', ['customer' => $customer->id])
->assertSee('Poista tallennettu henkilötunnus')
->set('form.remove_personal_identity_code', true)
->call('save')
->assertHasNoErrors();
expect($customer->fresh()->personal_identity_code)->toBeNull()
->and($customer->fresh()->personal_identity_code_hash)->toBeNull();
});
it('shows code errors in Finnish without repeating the code', function (string $code, string $message) {
$this->actingAs(User::factory()->create());
if ($message === 'Tällä henkilötunnuksella on jo asiakas.') {
registerConsumerWithCode();
}
Livewire::test('customers::form')
->set('form.name', 'Pekka')
->set('form.type', CustomerType::Consumer->value)
->set('form.personal_identity_code', $code)
->call('save')
->assertHasErrors(['form.personal_identity_code'])
->assertSee($message);
})->with([
'invalid' => ['131052-308U', 'Henkilötunnus ei ole kelvollinen.'],
'taken' => ['131052-308t', 'Tällä henkilötunnuksella on jo asiakas.'],
]);
it('drops the code when a consumer becomes a company', function () {
$this->actingAs(User::factory()->create());
$customer = registerConsumerWithCode();
Livewire::test('customers::form', ['customer' => $customer->id])
->set('form.type', CustomerType::Company->value)
->call('save')
->assertHasNoErrors();
expect($customer->fresh()->personal_identity_code)->toBeNull();
});
it('limits how many codes an employee can try, but not management', function (string $role, bool $limited) {
$this->actingAs(User::factory()->create(['role' => $role]));
$form = Livewire::test('customers::form')
->set('form.name', 'Maija Meikäläinen')
->set('form.type', CustomerType::Consumer->value);
foreach (range(1, 10) as $attempt) {
$form->set('form.personal_identity_code', 'väärä')->call('save')->assertHasErrors(['form.personal_identity_code']);
}
$form->set('form.personal_identity_code', '131052-308T')->call('save');
$limited
? $form->assertSee('liian monta kertaa tunnin aikana')
: $form->assertHasNoErrors();
})->with([
'employee' => ['employee', true],
'management' => ['management', false],
]);