Updated Tests

This commit is contained in:
Mark Gregory
2026-06-11 12:41:01 +01:00
parent 99e4bba178
commit 8b9de7426a
5 changed files with 103 additions and 213 deletions
+33 -21
View File
@@ -14,17 +14,6 @@ class WarehouseCrudTest extends TestCase
{
use RefreshDatabase;
/**
* Disable only CSRF for controller behaviour tests; auth/tenant/licence middleware still runs.
*/
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class);
$this->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class);
}
/**
* Ensure licensed tenant users can see the warehouse dashboard for their company.
*/
@@ -92,11 +81,11 @@ class WarehouseCrudTest extends TestCase
{
$user = User::factory()->create();
$this->actingAs($user)->post(route('warehouses.store'), [
$this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('warehouses.store'), $this->csrfPayload([
'code' => 'EAST',
'name' => 'East Midlands DC',
'address' => 'Main Road, Nottingham',
])->assertRedirect();
]))->assertRedirect();
$this->assertDatabaseHas('warehouses', [
'company_id' => $user->company_id,
@@ -116,10 +105,11 @@ class WarehouseCrudTest extends TestCase
$this->actingAs($user)
->from(route('warehouses.index'))
->post(route('warehouses.store'), [
->withSession(['_token' => 'test-token'])
->post(route('warehouses.store'), $this->csrfPayload([
'code' => '',
'name' => '',
])
]))
->assertRedirect(route('warehouses.index'))
->assertSessionHasErrors(['code', 'name']);
}
@@ -154,13 +144,13 @@ class WarehouseCrudTest extends TestCase
$user = User::factory()->create();
$warehouse = Warehouse::create(['company_id' => $user->company_id, 'code' => 'MAIN', 'name' => 'Main Warehouse']);
$this->actingAs($user)->post(route('warehouse-rules.store'), [
$this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('warehouse-rules.store'), $this->csrfPayload([
'warehouse_id' => $warehouse->id,
'name' => 'Require batch number',
'rule_key' => 'batch-required',
'severity' => 'blocker',
'configuration' => json_encode(['field' => 'batch_no']),
])->assertRedirect();
]))->assertRedirect();
$this->assertDatabaseHas('warehouse_validation_rules', [
'warehouse_id' => $warehouse->id,
@@ -182,13 +172,13 @@ class WarehouseCrudTest extends TestCase
$otherCompany = Company::create(['name' => 'Other Company', 'slug' => 'other-company']);
$otherWarehouse = Warehouse::create(['company_id' => $otherCompany->id, 'code' => 'OTHER', 'name' => 'Other Warehouse']);
$this->actingAs($user)->post(route('warehouse-rules.store'), [
$this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('warehouse-rules.store'), $this->csrfPayload([
'warehouse_id' => $otherWarehouse->id,
'name' => 'Invalid Rule',
'rule_key' => 'invalid-rule',
'severity' => 'error',
'configuration' => '{}',
])->assertNotFound();
]))->assertNotFound();
}
/**
@@ -200,14 +190,36 @@ class WarehouseCrudTest extends TestCase
$this->actingAs($user)
->from(route('warehouse-rules.index'))
->post(route('warehouse-rules.store'), [
->withSession(['_token' => 'test-token'])
->post(route('warehouse-rules.store'), $this->csrfPayload([
'warehouse_id' => '',
'name' => '',
'rule_key' => '',
'severity' => 'critical',
'configuration' => '{bad json',
])
]))
->assertRedirect(route('warehouse-rules.index'))
->assertSessionHasErrors(['warehouse_id', 'name', 'rule_key', 'severity', 'configuration']);
}
/**
* Return payload with a valid test CSRF token.
*
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
private function csrfPayload(array $payload = []): array
{
return ['_token' => 'test-token'] + $payload;
}
/**
* Seed the test session with the matching CSRF token.
*/
private function withCsrfSession(): self
{
$this->withSession(['_token' => 'test-token']);
return $this;
}
}