Add tenant lifecycle view and limit controls
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Tests\Feature;
|
||||
use App\Filament\Resources\TenantResource\Pages\ListTenants;
|
||||
use App\Jobs\AnonymizeAccount;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantLifecycleEvent;
|
||||
use App\Models\User;
|
||||
use Filament\Actions\Testing\TestAction;
|
||||
use Filament\Facades\Filament;
|
||||
@@ -45,6 +46,10 @@ class TenantLifecycleActionsTest extends TestCase
|
||||
$plannedDeletion->toDateTimeString(),
|
||||
$tenant->pending_deletion_at?->toDateTimeString()
|
||||
);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'deletion_scheduled')
|
||||
->exists());
|
||||
|
||||
Livewire::test(ListTenants::class)
|
||||
->callAction(TestAction::make('cancel_deletion')->table($tenant));
|
||||
@@ -53,6 +58,10 @@ class TenantLifecycleActionsTest extends TestCase
|
||||
|
||||
$this->assertNull($tenant->pending_deletion_at);
|
||||
$this->assertNull($tenant->deletion_warning_sent_at);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'deletion_cancelled')
|
||||
->exists());
|
||||
}
|
||||
|
||||
public function test_superadmin_can_toggle_tenant_status_flags(): void
|
||||
@@ -70,24 +79,40 @@ class TenantLifecycleActionsTest extends TestCase
|
||||
|
||||
$tenant->refresh();
|
||||
$this->assertFalse((bool) $tenant->is_active);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'deactivated')
|
||||
->exists());
|
||||
|
||||
Livewire::test(ListTenants::class)
|
||||
->callAction(TestAction::make('activate')->table($tenant));
|
||||
|
||||
$tenant->refresh();
|
||||
$this->assertTrue((bool) $tenant->is_active);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'activated')
|
||||
->exists());
|
||||
|
||||
Livewire::test(ListTenants::class)
|
||||
->callAction(TestAction::make('suspend')->table($tenant));
|
||||
|
||||
$tenant->refresh();
|
||||
$this->assertTrue((bool) $tenant->is_suspended);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'suspended')
|
||||
->exists());
|
||||
|
||||
Livewire::test(ListTenants::class)
|
||||
->callAction(TestAction::make('unsuspend')->table($tenant));
|
||||
|
||||
$tenant->refresh();
|
||||
$this->assertFalse((bool) $tenant->is_suspended);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'unsuspended')
|
||||
->exists());
|
||||
}
|
||||
|
||||
public function test_superadmin_can_dispatch_tenant_anonymization(): void
|
||||
@@ -105,6 +130,10 @@ class TenantLifecycleActionsTest extends TestCase
|
||||
Queue::assertPushed(AnonymizeAccount::class, function (AnonymizeAccount $job) use ($tenant) {
|
||||
return $job->tenantId() === $tenant->id;
|
||||
});
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'anonymize_requested')
|
||||
->exists());
|
||||
}
|
||||
|
||||
private function bootSuperAdminPanel(User $user): void
|
||||
|
||||
121
tests/Feature/TenantLifecycleManagementTest.php
Normal file
121
tests/Feature/TenantLifecycleManagementTest.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Filament\Resources\TenantResource\Pages\ViewTenantLifecycle;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantLifecycleEvent;
|
||||
use App\Models\User;
|
||||
use Filament\Actions\Testing\TestAction;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantLifecycleManagementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_superadmin_can_update_limits(): void
|
||||
{
|
||||
$user = User::factory()->create(['role' => 'super_admin']);
|
||||
$tenant = Tenant::factory()->create([
|
||||
'max_photos_per_event' => 500,
|
||||
'max_storage_mb' => 1024,
|
||||
]);
|
||||
|
||||
$this->bootSuperAdminPanel($user);
|
||||
|
||||
Livewire::test(ViewTenantLifecycle::class, ['record' => $tenant->id])
|
||||
->callAction(TestAction::make('update_limits'), [
|
||||
'max_photos_per_event' => 750,
|
||||
'max_storage_mb' => 2048,
|
||||
'note' => 'adjusted for onboarding',
|
||||
]);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertSame(750, $tenant->max_photos_per_event);
|
||||
$this->assertSame(2048, $tenant->max_storage_mb);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'limits_updated')
|
||||
->exists());
|
||||
}
|
||||
|
||||
public function test_superadmin_can_set_and_clear_grace_period(): void
|
||||
{
|
||||
$user = User::factory()->create(['role' => 'super_admin']);
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
$this->bootSuperAdminPanel($user);
|
||||
|
||||
$graceUntil = now()->addDays(14)->startOfDay();
|
||||
|
||||
Livewire::test(ViewTenantLifecycle::class, ['record' => $tenant->id])
|
||||
->callAction(TestAction::make('set_grace_period'), [
|
||||
'grace_period_ends_at' => $graceUntil->toDateTimeString(),
|
||||
'note' => 'billing exception',
|
||||
]);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertSame(
|
||||
$graceUntil->toDateTimeString(),
|
||||
$tenant->grace_period_ends_at?->toDateTimeString()
|
||||
);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'grace_period_set')
|
||||
->exists());
|
||||
|
||||
Livewire::test(ViewTenantLifecycle::class, ['record' => $tenant->id])
|
||||
->callAction(TestAction::make('clear_grace_period'));
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertNull($tenant->grace_period_ends_at);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'grace_period_cleared')
|
||||
->exists());
|
||||
}
|
||||
|
||||
public function test_superadmin_can_update_subscription_expiry(): void
|
||||
{
|
||||
$user = User::factory()->create(['role' => 'super_admin']);
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
$this->bootSuperAdminPanel($user);
|
||||
|
||||
$expiresAt = now()->addMonths(3)->startOfDay();
|
||||
|
||||
Livewire::test(ViewTenantLifecycle::class, ['record' => $tenant->id])
|
||||
->callAction(TestAction::make('update_subscription_expires_at'), [
|
||||
'subscription_expires_at' => $expiresAt->toDateTimeString(),
|
||||
'note' => 'manual extension',
|
||||
]);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertSame(
|
||||
$expiresAt->toDateTimeString(),
|
||||
$tenant->subscription_expires_at?->toDateTimeString()
|
||||
);
|
||||
$this->assertTrue(TenantLifecycleEvent::query()
|
||||
->where('tenant_id', $tenant->id)
|
||||
->where('type', 'subscription_expires_at_updated')
|
||||
->exists());
|
||||
}
|
||||
|
||||
private function bootSuperAdminPanel(User $user): void
|
||||
{
|
||||
$panel = Filament::getPanel('superadmin');
|
||||
|
||||
$this->assertNotNull($panel);
|
||||
|
||||
Filament::setCurrentPanel($panel);
|
||||
Filament::bootCurrentPanel();
|
||||
Filament::auth()->login($user);
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class TenantLifecycleViewTest extends TestCase
|
||||
|
||||
$this->actingAs($user, 'super_admin');
|
||||
|
||||
$url = TenantResource::getUrl('view', ['record' => $tenant], panel: 'superadmin');
|
||||
$url = TenantResource::getUrl('lifecycle', ['record' => $tenant], panel: 'superadmin');
|
||||
|
||||
$this->get($url)
|
||||
->assertOk()
|
||||
|
||||
93
tests/Feature/TenantLimitEnforcementTest.php
Normal file
93
tests/Feature/TenantLimitEnforcementTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Event;
|
||||
use App\Models\EventMediaAsset;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\MediaStorageTarget;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\Packages\PackageLimitEvaluator;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantLimitEnforcementTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_photo_upload_blocks_when_tenant_photo_limit_reached(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'max_photos_per_event' => 2,
|
||||
'max_storage_mb' => 0,
|
||||
]);
|
||||
$event = Event::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$package = Package::factory()->create([
|
||||
'max_photos' => 10,
|
||||
'type' => 'endcustomer',
|
||||
]);
|
||||
|
||||
EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => 0,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 2,
|
||||
]);
|
||||
|
||||
$violation = app(PackageLimitEvaluator::class)
|
||||
->assessPhotoUpload($tenant, $event->id, $event);
|
||||
|
||||
$this->assertNotNull($violation);
|
||||
$this->assertSame('tenant_photo_limit_exceeded', $violation['code']);
|
||||
}
|
||||
|
||||
public function test_photo_upload_blocks_when_tenant_storage_limit_reached(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'max_photos_per_event' => 0,
|
||||
'max_storage_mb' => 1,
|
||||
]);
|
||||
$event = Event::factory()->create(['tenant_id' => $tenant->id]);
|
||||
$package = Package::factory()->create([
|
||||
'max_photos' => 10,
|
||||
'type' => 'endcustomer',
|
||||
]);
|
||||
|
||||
EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => 0,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 0,
|
||||
]);
|
||||
|
||||
$storageTarget = MediaStorageTarget::create([
|
||||
'key' => 'local',
|
||||
'name' => 'Local Storage',
|
||||
'driver' => 'local',
|
||||
'config' => [],
|
||||
'is_hot' => true,
|
||||
'is_default' => true,
|
||||
'is_active' => true,
|
||||
'priority' => 1,
|
||||
]);
|
||||
|
||||
EventMediaAsset::create([
|
||||
'event_id' => $event->id,
|
||||
'media_storage_target_id' => $storageTarget->id,
|
||||
'variant' => 'original',
|
||||
'disk' => 'local',
|
||||
'path' => 'events/'.$event->id.'/photos/test.jpg',
|
||||
'size_bytes' => 1024 * 1024,
|
||||
'status' => 'hot',
|
||||
]);
|
||||
|
||||
$violation = app(PackageLimitEvaluator::class)
|
||||
->assessPhotoUpload($tenant, $event->id, $event, 0);
|
||||
|
||||
$this->assertNotNull($violation);
|
||||
$this->assertSame('tenant_storage_limit_exceeded', $violation['code']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user