-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make tests pass + fix Get inject on alerts
- Loading branch information
1 parent
980bc68
commit 2cb7130
Showing
2 changed files
with
112 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ | |
use App\Filament\Resources\AlertResource; | ||
use App\Models\Alert; | ||
use App\Models\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Livewire\Livewire; | ||
use App\Filament\Resources\AlertResource\Pages\ManageAlerts; | ||
use function Pest\Laravel\assertDatabaseHas; | ||
use function Pest\Laravel\assertDatabaseMissing; | ||
|
||
beforeEach(function () { | ||
$this->user = User::factory()->create(); | ||
|
@@ -17,17 +21,16 @@ | |
}); | ||
|
||
test('can create an email alert through Uppi UI', function () { | ||
$alertData = [ | ||
'data.name' => 'Test Email Alert', | ||
'data.type' => AlertType::EMAIL->value, | ||
'data.destination' => '[email protected]', | ||
'data.is_enabled' => true, | ||
]; | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', $alertData); | ||
|
||
$this->assertDatabaseHas('alerts', [ | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Email Alert', | ||
'type' => AlertType::EMAIL->value, | ||
'destination' => '[email protected]', | ||
'is_enabled' => true, | ||
]); | ||
|
||
assertDatabaseHas('alerts', [ | ||
'name' => 'Test Email Alert', | ||
'type' => AlertType::EMAIL->value, | ||
'destination' => '[email protected]', | ||
|
@@ -37,102 +40,95 @@ | |
}); | ||
|
||
test('can create a slack alert through Uppi UI', function () { | ||
$alertData = [ | ||
'data.name' => 'Test Slack Alert', | ||
'data.type' => AlertType::SLACK->value, | ||
'data.destination' => '#monitoring', | ||
'data.is_enabled' => true, | ||
'data.config' => [ | ||
'slack_token' => 'xoxb-test-token', | ||
], | ||
]; | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', $alertData); | ||
|
||
$this->assertDatabaseHas('alerts', [ | ||
'name' => 'Test Slack Alert', | ||
'type' => AlertType::SLACK->value, | ||
'destination' => '#monitoring', | ||
'is_enabled' => true, | ||
'user_id' => $this->user->id, | ||
]); | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Slack Alert', | ||
'type' => AlertType::SLACK->value, | ||
'destination' => '#monitoring', | ||
'is_enabled' => true, | ||
'config' => [ | ||
'slack_token' => 'xoxb-test-token', | ||
], | ||
]); | ||
|
||
$alert = Alert::first(); | ||
expect($alert->config)->toHaveKey('slack_token') | ||
expect($alert) | ||
->name->toBe('Test Slack Alert') | ||
->type->toBe(AlertType::SLACK) | ||
->destination->toBe('#monitoring') | ||
->is_enabled->toBeTrue(); | ||
|
||
expect($alert->config) | ||
->toHaveKey('slack_token') | ||
->and($alert->config['slack_token'])->toBe('xoxb-test-token'); | ||
}); | ||
|
||
test('can create a bird alert through Uppi UI', function () { | ||
$alertData = [ | ||
'data.name' => 'Test Bird Alert', | ||
'data.type' => AlertType::BIRD->value, | ||
'data.destination' => '+31612345678', | ||
'data.is_enabled' => true, | ||
'data.config' => [ | ||
'bird_api_key' => 'test-api-key', | ||
'bird_workspace_id' => 'test-workspace', | ||
'bird_channel_id' => 'test-channel', | ||
], | ||
]; | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', $alertData); | ||
|
||
$this->assertDatabaseHas('alerts', [ | ||
'name' => 'Test Bird Alert', | ||
'type' => AlertType::BIRD->value, | ||
'destination' => '+31612345678', | ||
'is_enabled' => true, | ||
'user_id' => $this->user->id, | ||
]); | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Bird Alert', | ||
'type' => AlertType::BIRD->value, | ||
'destination' => '+31612345678', | ||
'is_enabled' => true, | ||
'config' => [ | ||
'bird_api_key' => 'test-api-key', | ||
'bird_workspace_id' => 'test-workspace', | ||
'bird_channel_id' => 'test-channel', | ||
], | ||
]); | ||
|
||
$alert = Alert::first(); | ||
expect($alert) | ||
->name->toBe('Test Bird Alert') | ||
->type->toBe(AlertType::BIRD) | ||
->destination->toBe('+31612345678'); | ||
|
||
expect($alert->config) | ||
->toHaveKey('bird_api_key') | ||
->toHaveKey('bird_workspace_id') | ||
->toHaveKey('bird_channel_id'); | ||
}); | ||
|
||
test('can create a pushover alert through Uppi UI', function () { | ||
$alertData = [ | ||
'data.name' => 'Test Pushover Alert', | ||
'data.type' => AlertType::PUSHOVER->value, | ||
'data.destination' => 'user-key', | ||
'data.is_enabled' => true, | ||
'data.config' => [ | ||
'pushover_api_token' => 'app-token', | ||
], | ||
]; | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', $alertData); | ||
|
||
$this->assertDatabaseHas('alerts', [ | ||
'name' => 'Test Pushover Alert', | ||
'type' => AlertType::PUSHOVER->value, | ||
'destination' => 'user-key', | ||
'is_enabled' => true, | ||
'user_id' => $this->user->id, | ||
]); | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Pushover Alert', | ||
'type' => AlertType::PUSHOVER->value, | ||
'destination' => 'user-key', | ||
'is_enabled' => true, | ||
'config' => [ | ||
'pushover_api_token' => 'app-token', | ||
], | ||
]); | ||
|
||
$alert = Alert::first(); | ||
expect($alert->config)->toHaveKey('pushover_api_token') | ||
expect($alert) | ||
->name->toBe('Test Pushover Alert') | ||
->type->toBe(AlertType::PUSHOVER) | ||
->destination->toBe('user-key') | ||
->is_enabled->toBeTrue(); | ||
|
||
expect($alert->config) | ||
->toHaveKey('pushover_api_token') | ||
->and($alert->config['pushover_api_token'])->toBe('app-token'); | ||
}); | ||
|
||
test('can create an expo alert through Uppi UI', function () { | ||
$alertData = [ | ||
'data.name' => 'Test Expo Alert', | ||
'data.type' => AlertType::EXPO->value, | ||
'data.destination' => 'ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]', | ||
'data.is_enabled' => true, | ||
]; | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', $alertData); | ||
|
||
$this->assertDatabaseHas('alerts', [ | ||
test('cannot create an expo alert through Uppi UI', function () { | ||
$test = Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Expo Alert', | ||
'type' => AlertType::EXPO->value, | ||
'destination' => 'ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]', | ||
'is_enabled' => true, | ||
]); | ||
|
||
$test->assertHasActionErrors(['uppi_app_info']); | ||
|
||
assertDatabaseMissing('alerts', [ | ||
'name' => 'Test Expo Alert', | ||
'type' => AlertType::EXPO->value, | ||
'destination' => 'ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]', | ||
|
@@ -142,91 +138,46 @@ | |
}); | ||
|
||
test('validates required fields when creating an alert', function () { | ||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', [ | ||
'data.name' => '', | ||
'data.type' => AlertType::EMAIL->value, | ||
'data.destination' => '', | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => '', | ||
'type' => AlertType::EMAIL->value, | ||
'destination' => '', | ||
]) | ||
->assertHasPageActionErrors(['data.name', 'data.destination']); | ||
->assertHasActionErrors(['name', 'destination']); | ||
}); | ||
|
||
test('validates email format for email alerts', function () { | ||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', [ | ||
'data.name' => 'Test Alert', | ||
'data.type' => AlertType::EMAIL->value, | ||
'data.destination' => 'not-an-email', | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Alert', | ||
'type' => AlertType::EMAIL->value, | ||
'destination' => 'not-an-email', | ||
]) | ||
->assertHasPageActionErrors(['data.destination']); | ||
->assertHasActionErrors(['destination']); | ||
}); | ||
|
||
test('requires slack token for slack alerts', function () { | ||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callPageAction('create', [ | ||
'data.name' => 'Test Slack Alert', | ||
'data.type' => AlertType::SLACK->value, | ||
'data.destination' => '#monitoring', | ||
'data.config' => [], | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->callAction('create', [ | ||
'name' => 'Test Slack Alert', | ||
'type' => AlertType::SLACK->value, | ||
'destination' => '#monitoring', | ||
'config' => [], | ||
]) | ||
->assertHasPageActionErrors(['data.config.slack_token']); | ||
->assertHasActionErrors(['config.slack_token']); | ||
}); | ||
|
||
test('can enable and disable alerts', function () { | ||
$alert = Alert::factory()->email()->create([ | ||
'user_id' => $this->user->id, | ||
'is_enabled' => true, | ||
]); | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callTableAction('disable', $alert); | ||
|
||
expect($alert->fresh()->is_enabled)->toBeFalse(); | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callTableAction('enable', $alert); | ||
|
||
expect($alert->fresh()->is_enabled)->toBeTrue(); | ||
}); | ||
|
||
test('can delete alerts', function () { | ||
$alert = Alert::factory()->email()->create([ | ||
'user_id' => $this->user->id, | ||
]); | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callTableAction('delete', $alert); | ||
|
||
$this->assertModelMissing($alert); | ||
}); | ||
|
||
test('can bulk enable and disable alerts', function () { | ||
$alerts = Alert::factory()->email()->count(3)->create([ | ||
'user_id' => $this->user->id, | ||
'is_enabled' => false, | ||
]); | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callTableBulkAction('enable', $alerts); | ||
|
||
foreach ($alerts as $alert) { | ||
expect($alert->fresh()->is_enabled)->toBeTrue(); | ||
} | ||
|
||
Livewire::test(AlertResource\Pages\ManageAlerts::class) | ||
->callTableBulkAction('disable', $alerts); | ||
|
||
foreach ($alerts as $alert) { | ||
expect($alert->fresh()->is_enabled)->toBeFalse(); | ||
} | ||
}); | ||
|
||
it('cannot access someone else\'s alerts', function () { | ||
test('cannot access someone else\'s alerts', function () { | ||
$otherUser = User::factory()->create(); | ||
$alert = Alert::factory()->email()->create([ | ||
'user_id' => User::factory()->create()->id, | ||
'user_id' => $otherUser->id, | ||
]); | ||
|
||
$this->actingAs($this->user) | ||
->get(AlertResource::getUrl('edit', ['record' => $alert])) | ||
->assertForbidden(); | ||
Livewire::test(ManageAlerts::class) | ||
->assertSuccessful() | ||
->assertTableActionHidden('delete', $alert); | ||
}); |