Files
HRM-System/tests/Feature/TenantModuleIsolationRouteTest.php

120 lines
4.2 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Inquiry;
use App\Models\Tenant;
use App\Models\User;
use App\Jobs\ProvisionTenantJob;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Tests\TestCase;
class TenantModuleIsolationRouteTest extends TestCase
{
use RefreshDatabase;
public function test_designations_and_branches_blocked_when_multi_branch_not_requested(): void
{
$inquiry = Inquiry::create([
'company_name' => 'No Branch Corp',
'contact_name' => 'Admin No Branch',
'email' => 'nobranch@test.com',
'desired_subdomain' => 'no-branch-tenant',
'employee_count' => 10,
'requested_features' => ['payroll'], // NO multi_branch requested
'email_verified_at' => now(),
'status' => 'approved',
]);
$job = new ProvisionTenantJob($inquiry);
$job->handle();
$tenant = Tenant::find('no-branch-tenant');
$tenant->run(function () {
Artisan::call('migrate', ['--database' => 'tenant', '--path' => 'database/migrations/tenant', '--force' => true]);
$companyUser = User::where('email', 'nobranch@test.com')->first();
$this->actingAs($companyUser);
// Attempt to visit Designation route (Must be 403 Forbidden because multi_branch was NOT purchased)
$response = $this->get('/hr/designations');
$response->assertStatus(403);
// Attempt to visit Branch route (Must be 403 Forbidden)
$response = $this->get('/hr/branches');
$response->assertStatus(403);
// Attempt to visit Department route (Must be 403 Forbidden)
$response = $this->get('/hr/departments');
$response->assertStatus(403);
});
}
public function test_mobile_api_blocked_when_mobile_app_not_requested(): void
{
$inquiry = Inquiry::create([
'company_name' => 'No Mobile Corp',
'contact_name' => 'Admin No Mobile',
'email' => 'nomobile@test.com',
'desired_subdomain' => 'no-mobile-tenant',
'employee_count' => 10,
'requested_features' => ['payroll'], // NO mobile_app requested
'email_verified_at' => now(),
'status' => 'approved',
]);
$job = new ProvisionTenantJob($inquiry);
$job->handle();
$tenant = Tenant::find('no-mobile-tenant');
$tenant->run(function () {
Artisan::call('migrate', ['--database' => 'tenant', '--path' => 'database/migrations/tenant', '--force' => true]);
$companyUser = User::where('email', 'nomobile@test.com')->first();
$token = $companyUser->createToken('test-token')->plainTextToken;
// Attempt to access mobile API (Must be 403 Forbidden because mobile_app was NOT purchased)
$response = $this->withHeader('Authorization', 'Bearer ' . $token)
->getJson('/api/attendance/today');
$response->assertStatus(403);
});
}
public function test_designations_and_branches_allowed_when_multi_branch_requested(): void
{
$inquiry = Inquiry::create([
'company_name' => 'Branch Inc',
'contact_name' => 'Admin Branch',
'email' => 'withbranch@test.com',
'desired_subdomain' => 'with-branch-tenant',
'employee_count' => 10,
'requested_features' => ['multi_branch'], // multi_branch requested
'email_verified_at' => now(),
'status' => 'approved',
]);
$job = new ProvisionTenantJob($inquiry);
$job->handle();
$tenant = Tenant::find('with-branch-tenant');
$tenant->run(function () {
Artisan::call('migrate', ['--database' => 'tenant', '--path' => 'database/migrations/tenant', '--force' => true]);
$companyUser = User::where('email', 'withbranch@test.com')->first();
$this->actingAs($companyUser);
$response = $this->get('/hr/designations');
$response->assertStatus(200);
$response = $this->get('/hr/branches');
$response->assertStatus(200);
});
}
}