Files
Verde-Web/tests/Feature/Api/V1/SuperAdmin/SuperAdminTenantTest.php

191 lines
6.2 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\SuperAdmin;
use App\Models\CityMunicipality;
use App\Models\Tenant;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Database\Seeders\SamplePsgcSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
use Tests\TestCase;
class SuperAdminTenantTest extends TestCase
{
use RefreshDatabase;
private User $superAdmin;
private User $admin;
private CityMunicipality $city;
private Polygon $sampleBoundary;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->seed(SamplePsgcSeeder::class);
$this->superAdmin = User::factory()->create([
'role' => User::ROLE_SUPER_ADMIN,
'status' => User::STATUS_ACTIVE,
]);
$this->superAdmin->assignRole(User::ROLE_SUPER_ADMIN);
$this->admin = User::factory()->create([
'role' => User::ROLE_ADMIN,
'status' => User::STATUS_ACTIVE,
]);
$this->admin->assignRole(User::ROLE_ADMIN);
$this->city = CityMunicipality::first();
$this->sampleBoundary = new Polygon([
new LineString([
new Point(13.7915, 121.0118, 4326),
new Point(13.8015, 121.0118, 4326),
new Point(13.8015, 121.0218, 4326),
new Point(13.7915, 121.0218, 4326),
new Point(13.7915, 121.0118, 4326),
]),
], 4326);
}
public function test_guests_cannot_access_tenant_endpoints(): void
{
$this->getJson('/api/v1/super-admin/tenants')->assertStatus(401);
}
public function test_lgu_admins_cannot_access_tenant_endpoints(): void
{
Sanctum::actingAs($this->admin);
$this->getJson('/api/v1/super-admin/tenants')->assertStatus(403);
}
public function test_super_admin_can_list_tenants(): void
{
Sanctum::actingAs($this->superAdmin);
Tenant::create([
'name' => 'Alfonso LGU',
'short_name' => 'ALFONSO',
'city_municipality_id' => $this->city->id,
'boundary_polygon' => $this->sampleBoundary,
'contact_email' => 'alfonso@lgu.gov.ph',
'contact_phone' => '+639171112233',
'status' => Tenant::STATUS_ACTIVE,
]);
$response = $this->getJson('/api/v1/super-admin/tenants');
$response->assertStatus(200)
->assertJsonFragment(['name' => 'Alfonso LGU']);
}
public function test_super_admin_can_onboard_tenant_and_creates_admin(): void
{
Sanctum::actingAs($this->superAdmin);
$payload = [
'name' => 'San Pascual LGU',
'short_name' => 'SAN-PASCUAL',
'city_municipality_id' => $this->city->id,
'theme_color' => '#16a34a',
'contact_email' => 'contact@sanpascual.gov.ph',
'contact_phone' => '+639171234567',
'status' => Tenant::STATUS_ACTIVE,
'boundary_polygon' => [
['lat' => 13.7915, 'lng' => 121.0118],
['lat' => 13.8015, 'lng' => 121.0118],
['lat' => 13.8015, 'lng' => 121.0218],
['lat' => 13.7915, 'lng' => 121.0218],
],
'admin_email' => 'admin@sanpascual.local',
'admin_password' => 'password123',
];
$response = $this->postJson('/api/v1/super-admin/tenants', $payload);
$response->assertCreated();
$this->assertDatabaseHas('tenants', [
'name' => 'San Pascual LGU',
'short_name' => 'SAN-PASCUAL',
]);
$tenant = Tenant::where('short_name', 'SAN-PASCUAL')->first();
$this->assertDatabaseHas('users', [
'tenant_id' => $tenant->id,
'email' => 'admin@sanpascual.local',
'role' => User::ROLE_ADMIN,
]);
$user = User::where('email', 'admin@sanpascual.local')->first();
$this->assertTrue($user->hasRole(User::ROLE_ADMIN));
}
public function test_super_admin_can_update_tenant(): void
{
Sanctum::actingAs($this->superAdmin);
$tenant = Tenant::create([
'name' => 'Old Name LGU',
'short_name' => 'OLD-SHORT',
'city_municipality_id' => $this->city->id,
'boundary_polygon' => $this->sampleBoundary,
'contact_email' => 'old@lgu.gov.ph',
'contact_phone' => '+639171112233',
'status' => Tenant::STATUS_ONBOARDING,
]);
$payload = [
'name' => 'Updated Name LGU',
'short_name' => 'UPDATED-SHORT',
'city_municipality_id' => $this->city->id,
'theme_color' => '#ef4444',
'contact_email' => 'new@lgu.gov.ph',
'contact_phone' => '+639170000000',
'status' => Tenant::STATUS_ACTIVE,
'boundary_polygon' => [
['lat' => 13.7915, 'lng' => 121.0118],
['lat' => 13.8015, 'lng' => 121.0118],
['lat' => 13.8015, 'lng' => 121.0218],
['lat' => 13.7915, 'lng' => 121.0218],
],
];
$response = $this->patchJson("/api/v1/super-admin/tenants/{$tenant->uuid}", $payload);
$response->assertStatus(200)
->assertJsonPath('data.name', 'Updated Name LGU')
->assertJsonPath('data.status', Tenant::STATUS_ACTIVE);
}
public function test_super_admin_can_soft_delete_tenant(): void
{
Sanctum::actingAs($this->superAdmin);
$tenant = Tenant::create([
'name' => 'To Delete LGU',
'short_name' => 'TO-DELETE',
'city_municipality_id' => $this->city->id,
'boundary_polygon' => $this->sampleBoundary,
'contact_email' => 'del@lgu.gov.ph',
'contact_phone' => '+639171112233',
'status' => Tenant::STATUS_ACTIVE,
]);
$response = $this->deleteJson("/api/v1/super-admin/tenants/{$tenant->uuid}");
$response->assertStatus(200);
$this->assertSoftDeleted('tenants', [
'id' => $tenant->id,
]);
}
}