197 lines
6.5 KiB
PHP
197 lines
6.5 KiB
PHP
<?php
|
|
|
|
use App\Livewire\BackupSetPolicyPickerTable;
|
|
use App\Models\BackupSet;
|
|
use App\Models\Policy;
|
|
use App\Models\PolicyVersion;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Intune\BackupService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Livewire\Livewire;
|
|
use Mockery\MockInterface;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
test('policy picker table bulk adds selected policies to backup set', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$tenant->makeCurrent();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$backupSet = BackupSet::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'name' => 'Test backup',
|
|
]);
|
|
|
|
$policies = Policy::factory()->count(2)->create([
|
|
'tenant_id' => $tenant->id,
|
|
'ignored_at' => null,
|
|
'last_synced_at' => now(),
|
|
]);
|
|
|
|
$this->mock(BackupService::class, function (MockInterface $mock) use ($tenant, $backupSet, $policies, $user) {
|
|
$mock->shouldReceive('addPoliciesToSet')
|
|
->once()
|
|
->withArgs(function ($tenantArg, $backupSetArg, $policyIds, $actorEmail, $actorName, $includeAssignments, $includeScopeTags, $includeFoundations) use ($tenant, $backupSet, $policies, $user) {
|
|
expect($tenantArg->id)->toBe($tenant->id);
|
|
expect($backupSetArg->id)->toBe($backupSet->id);
|
|
expect($policyIds)->toBe($policies->pluck('id')->all());
|
|
expect($actorEmail)->toBe($user->email);
|
|
expect($actorName)->toBe($user->name);
|
|
expect($includeAssignments)->toBeTrue();
|
|
expect($includeScopeTags)->toBeTrue();
|
|
expect($includeFoundations)->toBeTrue();
|
|
|
|
return true;
|
|
});
|
|
});
|
|
|
|
Livewire::actingAs($user)
|
|
->test(BackupSetPolicyPickerTable::class, [
|
|
'backupSetId' => $backupSet->id,
|
|
])
|
|
->callTableBulkAction('add_selected_to_backup_set', $policies)
|
|
->assertHasNoTableBulkActionErrors();
|
|
|
|
$notifications = session('filament.notifications', []);
|
|
|
|
expect($notifications)->not->toBeEmpty();
|
|
expect(collect($notifications)->last()['title'] ?? null)->toBe('Backup items added');
|
|
expect(collect($notifications)->last()['status'] ?? null)->toBe('success');
|
|
});
|
|
|
|
test('policy picker table does not warn if failures already existed but did not increase', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$tenant->makeCurrent();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$backupSet = BackupSet::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'name' => 'Test backup',
|
|
'status' => 'partial',
|
|
'metadata' => [
|
|
'failures' => [
|
|
['policy_id' => 1, 'reason' => 'Previous failure', 'status' => 500],
|
|
],
|
|
],
|
|
]);
|
|
|
|
$policies = Policy::factory()->count(1)->create([
|
|
'tenant_id' => $tenant->id,
|
|
'ignored_at' => null,
|
|
'last_synced_at' => now(),
|
|
]);
|
|
|
|
$this->mock(BackupService::class, function (MockInterface $mock) use ($backupSet) {
|
|
$mock->shouldReceive('addPoliciesToSet')
|
|
->once()
|
|
->andReturn($backupSet);
|
|
});
|
|
|
|
Livewire::actingAs($user)
|
|
->test(BackupSetPolicyPickerTable::class, [
|
|
'backupSetId' => $backupSet->id,
|
|
])
|
|
->callTableBulkAction('add_selected_to_backup_set', $policies)
|
|
->assertHasNoTableBulkActionErrors();
|
|
|
|
$notifications = session('filament.notifications', []);
|
|
|
|
expect($notifications)->not->toBeEmpty();
|
|
expect(collect($notifications)->last()['title'] ?? null)->toBe('Backup items added');
|
|
expect(collect($notifications)->last()['status'] ?? null)->toBe('success');
|
|
});
|
|
|
|
test('policy picker table warns when new failures were added', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$tenant->makeCurrent();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$backupSet = BackupSet::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'name' => 'Test backup',
|
|
'status' => 'completed',
|
|
'metadata' => ['failures' => []],
|
|
]);
|
|
|
|
$policies = Policy::factory()->count(1)->create([
|
|
'tenant_id' => $tenant->id,
|
|
'ignored_at' => null,
|
|
'last_synced_at' => now(),
|
|
]);
|
|
|
|
$this->mock(BackupService::class, function (MockInterface $mock) use ($backupSet) {
|
|
$mock->shouldReceive('addPoliciesToSet')
|
|
->once()
|
|
->andReturnUsing(function () use ($backupSet) {
|
|
$backupSet->update([
|
|
'status' => 'partial',
|
|
'metadata' => [
|
|
'failures' => [
|
|
['policy_id' => 123, 'reason' => 'New failure', 'status' => 500],
|
|
],
|
|
],
|
|
]);
|
|
|
|
return $backupSet->refresh();
|
|
});
|
|
});
|
|
|
|
Livewire::actingAs($user)
|
|
->test(BackupSetPolicyPickerTable::class, [
|
|
'backupSetId' => $backupSet->id,
|
|
])
|
|
->callTableBulkAction('add_selected_to_backup_set', $policies)
|
|
->assertHasNoTableBulkActionErrors();
|
|
|
|
$notifications = session('filament.notifications', []);
|
|
|
|
expect($notifications)->not->toBeEmpty();
|
|
expect(collect($notifications)->last()['title'] ?? null)->toBe('Backup items added with failures');
|
|
expect(collect($notifications)->last()['status'] ?? null)->toBe('warning');
|
|
});
|
|
|
|
test('policy picker table can filter by has versions', function () {
|
|
$tenant = Tenant::factory()->create();
|
|
$tenant->makeCurrent();
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$backupSet = BackupSet::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'name' => 'Test backup',
|
|
]);
|
|
|
|
$withVersions = Policy::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'display_name' => 'With Versions',
|
|
'ignored_at' => null,
|
|
'last_synced_at' => now(),
|
|
]);
|
|
|
|
PolicyVersion::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'policy_id' => $withVersions->id,
|
|
'policy_type' => $withVersions->policy_type,
|
|
'platform' => $withVersions->platform,
|
|
]);
|
|
|
|
$withoutVersions = Policy::factory()->create([
|
|
'tenant_id' => $tenant->id,
|
|
'display_name' => 'Without Versions',
|
|
'ignored_at' => null,
|
|
'last_synced_at' => now(),
|
|
]);
|
|
|
|
Livewire::actingAs($user)
|
|
->test(BackupSetPolicyPickerTable::class, [
|
|
'backupSetId' => $backupSet->id,
|
|
])
|
|
->filterTable('has_versions', '1')
|
|
->assertSee('With Versions')
|
|
->assertDontSee('Without Versions');
|
|
});
|