Skip to content

A free open source IT asset/license management system

Added tests

snipe51a9cc5253d9
diff --git a/tests/Feature/Console/Commands/SendCurrentInventoryToUsersTest.php b/tests/Feature/Console/Commands/SendCurrentInventoryToUsersTest.php
new file mode 100644
index 0000000000..7563c398ec
--- /dev/null
+++ b/tests/Feature/Console/Commands/SendCurrentInventoryToUsersTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Tests\Feature\Console\Commands;
+
+use App\Models\Asset;
+use App\Models\User;
+use Illuminate\Notifications\ChannelManager;
+use Illuminate\Support\Facades\Log;
+use Symfony\Component\Mailer\Exception\TransportException;
+use Tests\TestCase;
+
+class SendCurrentInventoryToUsersTest extends TestCase
+{
+ public function test_command_survives_transport_exception_and_logs_warning(): void
+ {
+ $user = User::factory()->create(['email' => '[email protected]']);
+ Asset::factory()->assignedToUser($user)->create();
+
+ $this->mock(ChannelManager::class, function ($mock) {
+ $mock->shouldReceive('send')->andThrow(new TransportException('SMTP auth failed'));
+ $mock->shouldReceive('sendNow')->andThrow(new TransportException('SMTP auth failed'));
+ });
+
+ Log::spy();
+
+ $this->artisan('snipeit:user-inventory')->assertExitCode(0);
+
+ Log::shouldHaveReceived('warning')->atLeast()->once();
+ }
+}
diff --git a/tests/Feature/Console/Commands/SendExpectedCheckinAlertsTest.php b/tests/Feature/Console/Commands/SendExpectedCheckinAlertsTest.php
new file mode 100644
index 0000000000..7c9395b3ab
--- /dev/null
+++ b/tests/Feature/Console/Commands/SendExpectedCheckinAlertsTest.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Tests\Feature\Console\Commands;
+
+use App\Models\Asset;
+use App\Models\User;
+use Carbon\Carbon;
+use Illuminate\Notifications\ChannelManager;
+use Illuminate\Support\Facades\Log;
+use Symfony\Component\Mailer\Exception\TransportException;
+use Tests\TestCase;
+
+class SendExpectedCheckinAlertsTest extends TestCase
+{
+ public function test_command_survives_transport_exception_and_logs_warning(): void
+ {
+ $user = User::factory()->create(['email' => '[email protected]']);
+ $asset = Asset::factory()->assignedToUser($user)->create([
+ 'expected_checkin' => Carbon::now()->subDay(),
+ ]);
+
+ $this->mock(ChannelManager::class, function ($mock) {
+ $mock->shouldReceive('send')->andThrow(new TransportException('SMTP auth failed'));
+ $mock->shouldReceive('sendNow')->andThrow(new TransportException('SMTP auth failed'));
+ });
+
+ Log::spy();
+
+ $this->artisan('snipeit:expected-checkin')->assertExitCode(0);
+
+ Log::shouldHaveReceived('warning')->atLeast()->once();
+ }
+
+ public function test_command_reports_success_count_on_happy_path(): void
+ {
+ $user = User::factory()->create(['email' => '[email protected]']);
+ Asset::factory()->assignedToUser($user)->create([
+ 'expected_checkin' => Carbon::now()->subDay(),
+ ]);
+
+ $this->mock(ChannelManager::class, function ($mock) {
+ $mock->shouldReceive('send')->andReturnNull();
+ $mock->shouldReceive('sendNow')->andReturnNull();
+ });
+
+ $this->artisan('snipeit:expected-checkin')
+ ->expectsOutputToContain('Sent checkin reminders to 1 users.')
+ ->assertExitCode(0);
+ }
+}
diff --git a/tests/Feature/Console/Commands/SendExpirationAlertsTest.php b/tests/Feature/Console/Commands/SendExpirationAlertsTest.php
new file mode 100644
index 0000000000..5ea8f46347
--- /dev/null
+++ b/tests/Feature/Console/Commands/SendExpirationAlertsTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Tests\Feature\Console\Commands;
+
+use App\Models\Asset;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Mail;
+use Mockery;
+use Symfony\Component\Mailer\Exception\TransportException;
+use Tests\TestCase;
+
+class SendExpirationAlertsTest extends TestCase
+{
+ public function test_survives_transport_exception_and_logs_warning(): void
+ {
+ $this->settings->set([
+ 'alerts_enabled' => 1,
+ 'alert_email' => '[email protected]',
+ 'alert_interval' => 60,
+ ]);
+
+ // AssetFactory's afterMaking hook overwrites asset_eol_date, so set it after create.
+ $asset = Asset::factory()->create(['archived' => 0]);
+ $asset->asset_eol_date = Carbon::now()->addDays(10);
+ $asset->save();
+
+ // Sanity check the fixture actually qualifies before asserting on side effects.
+ $this->assertGreaterThan(0, Asset::getExpiringWarrantyOrEol(60)->count());
+
+ $pending = Mockery::mock();
+ $pending->shouldReceive('send')->andThrow(new TransportException('SMTP auth failed'));
+ Mail::shouldReceive('to')->andReturn($pending);
+
+ Log::spy();
+
+ $this->artisan('snipeit:expiring-alerts')->assertExitCode(0);
+
+ Log::shouldHaveReceived('warning')->atLeast()->once();
+ }
+}
diff --git a/tests/Feature/Console/Commands/SendUpcomingAuditReportTest.php b/tests/Feature/Console/Commands/SendUpcomingAuditReportTest.php
new file mode 100644
index 0000000000..70fe71fe3f
--- /dev/null
+++ b/tests/Feature/Console/Commands/SendUpcomingAuditReportTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Console\Commands;
+
+use App\Models\Asset;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Mail;
+use Mockery;
+use Symfony\Component\Mailer\Exception\TransportException;
+use Tests\TestCase;
+
+class SendUpcomingAuditReportTest extends TestCase
+{
+ public function test_survives_transport_exception_and_logs_warning(): void
+ {
+ $this->settings->set([
+ 'alert_email' => '[email protected]',
+ 'audit_warning_days' => 30,
+ ]);
+
+ // AssetFactory's afterMaking hook may overwrite next_audit_date, so set it after create.
+ $asset = Asset::factory()->create();
+ $asset->next_audit_date = Carbon::now()->addDays(5);
+ $asset->save();
+
+ $pending = Mockery::mock();
+ $pending->shouldReceive('send')->andThrow(new TransportException('SMTP auth failed'));
+ Mail::shouldReceive('to')->andReturn($pending);
+
+ Log::spy();
+
+ $this->artisan('snipeit:upcoming-audits')->assertExitCode(0);
+
+ Log::shouldHaveReceived('warning')->atLeast()->once();
+ }
+}
diff --git a/tests/Feature/Console/SendAcceptanceReminderTest.php b/tests/Feature/Console/SendAcceptanceReminderTest.php
index 365ccfe4ee..e9630b3714 100644
--- a/tests/Feature/Console/SendAcceptanceReminderTest.php
+++ b/tests/Feature/Console/SendAcceptanceReminderTest.php
@@ -5,7 +5,10 @@ namespace Tests\Feature\Console;
use App\Mail\UnacceptedAssetReminderMail;
use App\Models\CheckoutAcceptance;
use App\Models\User;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
+use Mockery;
+use Symfony\Component\Mailer\Exception\TransportException;
use Tests\TestCase;
class SendAcceptanceReminderTest extends TestCase
@@ -55,4 +58,20 @@ class SendAcceptanceReminderTest extends TestCase
Mail::assertNotSent(UnacceptedAssetReminderMail::class);
}
+
+ public function test_survives_transport_exception_and_logs_warning(): void
+ {
+ $user = User::factory()->create(['email' => '[email protected]']);
+ CheckoutAcceptance::factory()->pending()->create(['assigned_to_id' => $user->id]);
+
+ $pending = Mockery::mock();
+ $pending->shouldReceive('send')->andThrow(new TransportException('SMTP auth failed'));
+ Mail::shouldReceive('to')->andReturn($pending);
+
+ Log::spy();
+
+ $this->artisan('snipeit:acceptance-reminder')->assertExitCode(0);
+
+ Log::shouldHaveReceived('warning')->atLeast()->once();
+ }
}
diff --git a/tests/Feature/Console/SendInventoryAlertsTest.php b/tests/Feature/Console/SendInventoryAlertsTest.php
index 40dc08e3e2..0e650bde10 100644
--- a/tests/Feature/Console/SendInventoryAlertsTest.php
+++ b/tests/Feature/Console/SendInventoryAlertsTest.php
@@ -3,6 +3,9 @@
namespace Tests\Feature\Console;
use App\Models\Consumable;
+use Illuminate\Notifications\ChannelManager;
+use Illuminate\Support\Facades\Log;
+use Symfony\Component\Mailer\Exception\TransportException;
use Tests\TestCase;
/**
@@ -79,4 +82,26 @@ class SendInventoryAlertsTest extends TestCase
->expectsOutputToContain('No alert email configured')
->assertExitCode(0);
}
+
+ public function test_survives_transport_exception_and_logs_warning(): void
+ {
+ $this->settings->set([
+ 'alerts_enabled' => 1,
+ 'alert_email' => '[email protected]',
+ 'alert_threshold' => 0,
+ ]);
+
+ Consumable::factory()->create(['qty' => 0, 'min_amt' => 1]);
+
+ $this->mock(ChannelManager::class, function ($mock) {
+ $mock->shouldReceive('send')->andThrow(new TransportException('SMTP auth failed'));
+ $mock->shouldReceive('sendNow')->andThrow(new TransportException('SMTP auth failed'));
+ });
+
+ Log::spy();
+
+ $this->artisan('snipeit:inventory-alerts')->assertExitCode(0);
+
+ Log::shouldHaveReceived('warning')->atLeast()->once();
+ }
}