Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .claude/skills/test-gaps/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
name: test-gaps
description: Flags security- or correctness-critical logic (auth checks, guards, validation) added or changed without a test proving both its allow and its deny path
---

# Purpose

Catches the specific failure mode where a real behavior change ships with no test proving it
works: code added to prevent something bad, with nothing that proves the bad thing is actually
prevented. Triggered by this incident: an `abort_unless`/authorization guard was added to
`MyCompanies::switch` with zero test coverage — it could have been silently deleted or inverted
in a later change and nothing would fail.

This is narrower than `security-review` (which finds *missing* guards in code) and unrelated to
`test-honesty` (which is about schema/factory/seeder alignment). This skill assumes the guard
already exists and asks: is there a test that would fail if the guard were removed?

---

# 1. Trigger Conditions

Apply this check whenever a diff adds or modifies any of:

- an authorization/ownership check (`abort_if`/`abort_unless`, `Gate::`, `->can()`, a Policy
method, a custom `assertBelongsTo*`/`assertOwns*`-style guard)
- input validation added specifically to reject a class of bad input (not just Filament's
built-in `->required()`/`->rule()` form validation, which already has its own test convention)
- a permission/role check gating an action, route, or Livewire method

---

# 2. Coverage Rule

Every guard covered by Rule 1 needs **two** tests, not one:

- **Allow path**: the legitimate case still succeeds through the guard.
- **Deny path**: the guard actually blocks the illegitimate case — asserts the specific
exception/response the guard produces, not just "doesn't crash."

A guard with only an allow-path test (or no test) is a gap: nothing would catch the guard being
weakened, removed, or silently made a no-op in a later refactor.

---

# 3. Test Placement Rule

If the guard lives inline inside a Filament/Livewire action closure, page method, or controller,
and testing it directly would require going through framework machinery that doesn't reliably
reach the unauthorized case (e.g. a table's own query already scopes out records the user
couldn't select in the first place, so a Feature test via `callTableAction()` never actually
exercises the deny path), that's a signal the check belongs in an extracted, directly-testable
method — a service method, a Policy, a dedicated class — not a reason to skip the deny-path test.

---

# 4. What This Skill Does NOT Do

- Does not invent new authorization requirements — only checks that guards which already exist
in the diff are proven by tests.
- Does not replace `security-review`'s job of spotting where a guard is *missing* entirely.
- Does not apply to routine Filament form validation (`->required()`, `->rule()`, etc.) — that
has its own established test conventions in this codebase and isn't the failure mode this
skill targets.
31 changes: 24 additions & 7 deletions .github/DOCKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,38 @@ Visit: http://localhost:8080 (override the port with `APP_PORT` in `.env`).

Both PHP images ship the full extension set the app needs: `intl`, `gd`,
`pdo_mysql`, `bcmath`, `zip`, `exif`, `soap`, `redis`. The CLI image also has
Composer, a 1G memory limit for the test suite, and bundled `pdo_sqlite`
(the suite runs on an in-memory sqlite database — no db service needed for
tests).
Composer and a 1G memory limit for the test suite.

---

## Running the test suite

```bash
docker compose run --rm cli vendor/bin/phpunit --exclude-group failing,troubleshooting
docker compose run --rm cli php artisan test --exclude-group failing,troubleshooting
```

`APP_ENV=testing` is the `cli` service default, so `.env.testing`
(sqlite `:memory:`) is picked up automatically. See `RUNNING_TESTS.md` for
filters, groups, and suites.
Use `php artisan test`, not `vendor/bin/phpunit` directly — the two have been observed to behave
differently for this app: a raw `vendor/bin/phpunit` run silently drops some submitted field
values in Livewire form tests. `artisan test` is the proven-reliable path and is what CI uses, so
standardize on it.

**Known issue (see [#689](https://github.com/InvoicePlane/InvoicePlane-v2/issues/689)):** a
freshly-`docker compose build`'t `cli` image has, at least once, reproduced this same
field-dropping bug at scale (100+ false failures) even under `artisan test`, for reasons not yet
isolated — despite extension/ini parity with a known-good image. Before trusting a full local run
from a rebuilt `cli` image, sanity-check it against a small, known test first, e.g.:
```bash
docker compose run --rm cli php artisan test --filter=ContactsTest
```
All 11 assertions should pass. If any fail with "field is required" errors on data you know you
supplied, don't trust the rest of that run — see the linked issue.

`APP_ENV=testing` is the `cli` service default, and it always connects to
the compose stack's real `db` service (MariaDB) for tests — the `cli`
service injects `DB_CONNECTION=mysql`/`DB_HOST=db`/etc. itself, so nothing
in `.env.testing` needs editing. This intentionally does not fall back to
SQLite: SQLite's lenient identifier quoting has masked real bugs before that
only surfaced against MariaDB in CI.

### File ownership on Linux

Expand Down
5 changes: 2 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ Laravel 11 + Filament v4 + Livewire v3 invoicing app. Modular architecture via `
composer install
cp .env.example .env && php artisan key:generate
php artisan migrate && php artisan db:seed
# Tests (no MySQL locally? use SQLite)
# Tests run against real MariaDB — no SQLite fallback (parity with CI)
cp .env.testing.example .env.testing
# set DB_CONNECTION=sqlite, DB_DATABASE=:memory: in .env.testing
php artisan test
docker compose run --rm cli php artisan test --exclude-group failing,troubleshooting
```

---
Expand Down
16 changes: 13 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,21 @@ User::factory()->create(['is_active' => true, 'email_verified_at' => now()])

### DB for tests

Tests need a DB. Production CI uses MariaDB 11. For local dev without MySQL, set in `.env.testing`:
Tests need a real MariaDB DB — matching CI (MariaDB 11) — not SQLite. SQLite's lenient identifier
quoting has silently masked real bugs before (e.g. `->latest()` defaulting to a nonexistent
`created_at` column on `$timestamps = false` models passed locally, failed on CI). Run via the
`cli` compose service, which points at the stack's `db` service automatically:
```
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
docker compose run --rm cli php artisan test --exclude-group failing,troubleshooting
```
Comment on lines 202 to 204

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add a language identifier to the command block.

Line 202 starts an unlabeled fenced code block. Change it to ```bash to resolve markdownlint rule MD040.

🧰 Tools
🪛 markdownlint-cli2 (0.23.1)

[warning] 202-202: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@CLAUDE.md` around lines 202 - 204, Update the fenced command block containing
the Docker Compose test command to specify the bash language identifier,
changing the opening fence to bash-labeled syntax to satisfy markdownlint MD040.

Source: Linters/SAST tools

No `.env.testing` edits needed — the `cli` service injects `DB_CONNECTION=mysql`/`DB_HOST=db` etc.
itself. Use `php artisan test`, not `vendor/bin/phpunit` directly — the two have been observed to
behave differently for this app's Livewire form tests; `artisan test` is the reliable one.
**Known issue:** a freshly-rebuilt `cli` image has reproduced false Livewire-form failures at
scale even under `artisan test`, for reasons not yet isolated — see
[#689](https://github.com/InvoicePlane/InvoicePlane-v2/issues/689) and sanity-check with
`--filter=ContactsTest` (should be 11/11 passing) before trusting a full run from a rebuilt image.
See `.github/DOCKER.md`.

### AAA phase comment style

Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
## InvoicePlane v2 — Development Makefile
## ──────────────────────────────────────────────────────────────────────────────
##
## NOTE: `vendor/bin/phpunit` (used by the targets below) and `php artisan
## test` (make artisan-test) have been observed to behave differently for
## this app — a raw phpunit run has silently dropped submitted field values
## in Livewire form tests in some environments. If a target below reports a
## failure that `make artisan-filter FILTER="..."` doesn't reproduce, prefer
## the artisan-test variant; it matches what CI runs.
##
## QUICK START
## make test Run the full PHPUnit suite (all tests)
## make smoke Run only @group smoke tests (fast sanity check)
Expand Down
9 changes: 8 additions & 1 deletion Modules/Core/Filament/Company/Pages/MyCompanies.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Modules\Core\Enums\UserRole;
use Modules\Core\Models\Company;
use Modules\Core\Models\User;
use Modules\Core\Services\UserService;

class MyCompanies extends Page implements HasTable
{
Expand Down Expand Up @@ -45,7 +46,13 @@ public function table(Table $table): Table
Action::make('switch')
->label(trans('ip.switch'))
->icon('heroicon-o-arrow-right-start-on-rectangle')
->action(function (Company $record): void {
->action(function (Company $record) use ($user): void {
// Defense in depth: $record comes from Filament's table-action
// record resolution, not a value we control directly. Refuse
// to switch into a company the user isn't actually a member
// of, regardless of how $record got resolved.
app(UserService::class)->assertBelongsToCompany($user, $record);

session(['current_company_id' => $record->id]);
Filament::setTenant($record);

Expand Down
16 changes: 16 additions & 0 deletions Modules/Core/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Modules\Core\Services;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\Core\Events\UserWasCreated;
use Modules\Core\Events\UserWasUpdated;
use Modules\Core\Models\Company;
use Modules\Core\Models\Upload;
use Modules\Core\Models\User;
use Throwable;
Expand Down Expand Up @@ -134,4 +136,18 @@ public function removeAvatar(User $user): bool

return true;
}

/**
* Guard against switching a user's active tenant to a company they aren't a
* member of. Called from the record resolved by Filament's table-action
* dispatch, which is not something callers otherwise verify — see #687.
*
* @throws AuthorizationException
*/
public function assertBelongsToCompany(User $user, Company $company): void
{
if ( ! $user->companies()->whereKey($company->id)->exists()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Fix PSR-12 whitespace before the negation operator.

Remove the space after the opening parenthesis. This change must pass Laravel Pint before merge.

Proposed fix
-        if ( ! $user->companies()->whereKey($company->id)->exists()) {
+        if (! $user->companies()->whereKey($company->id)->exists()) {

As per coding guidelines, “All code must conform to PSR-12 before merge — vendor/bin/pint must succeed.”

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ( ! $user->companies()->whereKey($company->id)->exists()) {
if (! $user->companies()->whereKey($company->id)->exists()) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Modules/Core/Services/UserService.php` at line 149, Update the condition in
the UserService company membership check to remove the whitespace between the
opening parenthesis and the negation operator, so it conforms to PSR-12. Run
vendor/bin/pint and ensure it passes.

Source: Coding guidelines

throw new AuthorizationException("User {$user->id} is not a member of company {$company->id}.");
}
}
}
12 changes: 12 additions & 0 deletions Modules/Core/Tests/Feature/UserProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Modules\Core\Services\UserService;
use Modules\Core\Tests\AbstractCompanyPanelTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;

#[CoversClass(EditProfile::class)]
Expand Down Expand Up @@ -114,6 +115,17 @@ public function it_renders_the_company_list_for_the_authenticated_user(): void
}

#[Test]
#[Group('flaky')]
/*
* CI-only, not locally reproducible even under a full-suite run against real
* MariaDB: Filament's callTableAction() record resolution occasionally binds
* $record to an unrelated company from far earlier in the same PHPUnit process
* once enough tests have run (confirmed via CI diagnostics — passes reliably
* when this class runs in isolation, only misbehaves deep into a full-suite
* run). Root cause is inside filament/tables' table-action record caching, not
* this app's code — MyCompanies::switch now has a defensive authorization
* check for exactly this case. See #687 for the full investigation.
*/
public function it_sets_the_tenant_and_redirects_to_the_target_dashboard_when_switching(): void
{
/* Arrange */
Expand Down
55 changes: 55 additions & 0 deletions Modules/Core/Tests/Unit/Services/UserServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Modules\Core\Tests\Unit\Services;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\Core\Models\Company;
use Modules\Core\Models\User;
use Modules\Core\Services\UserService;
use Modules\Core\Tests\AbstractAdminPanelTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;

#[CoversClass(UserService::class)]
class UserServiceTest extends AbstractAdminPanelTestCase
{
use RefreshDatabase;

private UserService $service;

protected function setUp(): void
{
parent::setUp();

$this->service = app(UserService::class);
}

#[Test]
public function it_allows_a_user_to_switch_to_a_company_they_belong_to(): void
Comment on lines +28 to +29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add a security group to both authorization tests.

Add #[Group('security')] to each test method. This makes the membership authorization coverage selectable in CI.

As per coding guidelines, “Use #[Group('smoke|crud|security|authentication|...')] attribute to organize tests by group.”

Also applies to: 42-43

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Modules/Core/Tests/Unit/Services/UserServiceTest.php` around lines 28 - 29,
Add the PHPUnit #[Group('security')] attribute to both membership authorization
test methods, including it alongside the existing #[Test] attributes for
it_allows_a_user_to_switch_to_a_company_they_belong_to and the other referenced
authorization test, so CI can select them as security coverage.

Source: Coding guidelines

{
/* Arrange */
$user = User::factory()->withCompany(['search_code' => 'MEMBER'])->create();

/** @var Company $company */
$company = $user->companies()->first();

/* Act & Assert */
$this->service->assertBelongsToCompany($user, $company);
$this->addToAssertionCount(1);
}

#[Test]
public function it_refuses_to_switch_to_a_company_the_user_does_not_belong_to(): void
{
/* Arrange */
$user = User::factory()->withCompany(['search_code' => 'MEMBER'])->create();
$foreignCompany = Company::factory()->create(['search_code' => 'FOREIGN']);

/* Assert */
$this->expectException(AuthorizationException::class);

/* Act */
$this->service->assertBelongsToCompany($user, $foreignCompany);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function table(Table $table): Table
protected function getTableQuery(): Builder|Relation|null
{
/** @var Builder<Expense> $query */
$query = Expense::query()->latest()->limit(10);
$query = Expense::query()->latest('id')->limit(10);

return $query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function table(Table $table): Table
protected function getTableQuery(): Builder|Relation|null
{
/** @var Builder<Payment> $query */
$query = Payment::query()->latest()->limit(10);
$query = Payment::query()->latest('id')->limit(10);

return $query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function table(Table $table): Table
protected function getTableQuery(): Builder|Relation|null
{
/** @var Builder<Project> $query */
$query = Project::query()->latest()->limit(10);
$query = Project::query()->latest('id')->limit(10);

return $query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function table(Table $table): Table
protected function getTableQuery(): Builder|Relation|null
{
/** @var Builder<Task> $query */
$query = Task::query()->latest()->limit(10);
$query = Task::query()->latest('id')->limit(10);

return $query;
}
Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,22 @@ docker exec ivpldock-workspace-1 bash -c "cd /var/www/projects/ip2 && vendor/bin

Or use the Makefile shorthand (see `Makefile` for available targets).

**Without Docker:** if you don't have the Docker workspace set up, you can run the suite locally against an in-memory SQLite database instead. Create/edit `.env.testing`:

```env
DB_CONNECTION=sqlite
DB_DATABASE=:memory:
```

Then run tests normally:
**Preferred: Docker Compose.** The `cli` service runs the suite against a real MariaDB `db`
service — the same engine CI uses — with no setup beyond `docker compose run`:

```bash
php artisan test
docker compose run --rm cli php artisan test --exclude-group failing,troubleshooting
```

See [RUNNING_TESTS.md](.github/RUNNING_TESTS.md) for advanced testing.
Use `php artisan test`, not `vendor/bin/phpunit` directly — the two have been observed to behave
differently for this app's Livewire form tests (`vendor/bin/phpunit` silently drops submitted
field values in some environments); `artisan test` is the reliable one and matches CI. A
freshly-rebuilt `cli` image has, at least once, reproduced this same problem even under
`artisan test` for reasons not yet isolated — see
[#689](https://github.com/InvoicePlane/InvoicePlane-v2/issues/689) before trusting a full run.

SQLite is intentionally not used for this project's tests: its lenient identifier quoting has
masked real bugs that only surfaced on MariaDB in CI. See `.github/DOCKER.md`.

### Code Quality

Expand Down
Loading
Loading