Skip to content

A free open source IT asset/license management system

adds test for expiring asset notifications

Godfrey M6873244e7ee5
diff --git a/app/Console/Commands/SendExpirationAlerts.php b/app/Console/Commands/SendExpirationAlerts.php
index 31bb90de46..1220e0cd9d 100644
--- a/app/Console/Commands/SendExpirationAlerts.php
+++ b/app/Console/Commands/SendExpirationAlerts.php
@@ -50,9 +50,9 @@ class SendExpirationAlerts extends Command
$recipients = collect(explode(',', $settings->alert_email))
->map(fn($item) => trim($item)) // Trim each email
->all();
-
// Expiring Assets
$assets = Asset::getExpiringWarrantee($threshold);
+
if ($assets->count() > 0) {
$this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count' => $assets->count(), 'threshold' => $threshold]));
Mail::to($recipients)->send(new ExpiringAssetsMail($assets, $threshold));
diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php
index d5b10bde3a..d3ef733de3 100644
--- a/app/Providers/RouteServiceProvider.php
+++ b/app/Providers/RouteServiceProvider.php
@@ -25,6 +25,7 @@ class RouteServiceProvider extends ServiceProvider
$this->mapWebRoutes();
require base_path('routes/scim.php');
+ require base_path('routes/mobile.php');
});
}
diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php
index 4d6d20651c..b6408ae690 100644
--- a/database/factories/AssetFactory.php
+++ b/database/factories/AssetFactory.php
@@ -48,7 +48,7 @@ class AssetFactory extends Factory
'assigned_type' => null,
'next_audit_date' => null,
'last_checkout' => null,
- 'asset_eol_date' => null
+ 'asset_eol_date' => null,
];
}
diff --git a/routes/mobile.php b/routes/mobile.php
new file mode 100644
index 0000000000..3c9b2568c1
--- /dev/null
+++ b/routes/mobile.php
@@ -0,0 +1,39 @@
+<?php
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Route;
+use Laravel\Passport\TokenRepository;
+use Laravel\Passport\RefreshTokenRepository;
+
+
+Route::post('/mobile/login', function (Request $request) {
+ $credentials = $request->only('username', 'password');
+
+ if (Auth::attempt($credentials)) {
+ // Authentication passed...
+ $user = Auth::user();
+ $token = $user->createToken($request->device_name);
+
+ return response()->json([
+ 'token' => $token->accessToken,
+ 'token_id' => $token->token->id,
+ 'user' => $user->only('id', 'first_name', 'last_name', 'email')
+ ]);
+ } else {
+ return response()->json(['error' => 'Authentication Failed'], 401);
+ }
+})->name('mobile.login');
+
+Route::post('/mobile/logout', function (Request $request) {
+ $tokenId = $request->token_id;
+ $tokenRepository = app(TokenRepository::class);
+ $refreshTokenRepository = app(RefreshTokenRepository::class);
+
+ // Revoke an access token...
+ $tokenRepository->revokeAccessToken($tokenId);
+
+ // Revoke all of the token's refresh tokens...
+ $refreshTokenRepository->revokeRefreshTokensByAccessTokenId($tokenId);
+ return response()->json(['msg' => 'logged out'], 200);
+})->name('mobile.logout');
\ No newline at end of file
diff --git a/tests/Feature/Notifications/Email/ExpiringAlertsNotificationTest.php b/tests/Feature/Notifications/Email/ExpiringAlertsNotificationTest.php
new file mode 100644
index 0000000000..a6a6cf5561
--- /dev/null
+++ b/tests/Feature/Notifications/Email/ExpiringAlertsNotificationTest.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Tests\Feature\Notifications\Email;
+
+use App\Mail\ExpiringAssetsMail;
+use App\Models\Asset;
+use App\Models\License;
+use App\Models\Setting;
+use App\Models\User;
+use Illuminate\Support\Facades\Mail;
+use Tests\TestCase;
+
+
+class ExpiringAlertsNotificationTest extends TestCase
+{
+ public function testExpiringAssetsEmailNotification()
+ {
+ $this->markIncompleteIfSqlite();
+ Mail::fake();
+
+ $this->settings->enableAlertEmail('[email protected]');
+ $this->settings->setAlertInterval(30);
+
+ $alert_email = Setting::first()->alert_email;
+
+ $expiringAsset = Asset::factory()->create([
+ 'purchase_date' => now()->subMonths(11)->format('Y-m-d'),
+ 'warranty_months' => 12,
+ 'archived' => 0,
+ 'deleted_at' => null,
+ ]);
+// dd($expiringAsset);
+ $expiredAsset = Asset::factory()->create([
+ 'purchase_date' => now()->subMonths(13)->format('Y-m-d'),
+ 'warranty_months' => 12,
+ 'archived' => 0,
+ 'deleted_at' => null,
+ ]);
+ $notExpiringAsset = Asset::factory()->create([
+ 'purchase_date' => now()->subMonths(10)->format('Y-m-d'),
+ 'warranty_months' => 12,
+ 'archived' => 0,
+ 'deleted_at' => null,
+ ]);
+
+ $this->artisan('snipeit:expiring-alerts')->assertExitCode(0);
+
+ Mail::assertSent(ExpiringAssetsMail::class, function($mail) use ($alert_email, $expiringAsset) {
+ return $mail->hasTo($alert_email) && $mail->assets->contains($expiringAsset);
+ });
+
+ Mail::assertNotSent(ExpiringAssetsMail::class, function($mail) use ($expiredAsset, $notExpiringAsset) {
+ return $mail->assets->contains($expiredAsset) || $mail->assets->contains($notExpiringAsset);
+ });
+ }
+
+ public function testExpiringLicensesEmailNotification()
+ {
+ Mail::fake();
+ $user = User::factory()->create();
+ $license = License::factory()->create();
+ }
+}
\ No newline at end of file
diff --git a/tests/Support/Settings.php b/tests/Support/Settings.php
index e171c0ab95..09d40974c1 100644
--- a/tests/Support/Settings.php
+++ b/tests/Support/Settings.php
@@ -21,12 +21,23 @@ class Settings
public function enableAlertEmail(string $email = '[email protected]'): Settings
{
- return $this->update(['alert_email' => $email]);
+ return $this->update([
+ 'alert_email' => $email,
+ 'alerts_enabled' => 1,
+ ]);
+ }
+ public function setAlertInterval(int $days): Settings
+ {
+ return $this->update([
+ 'alert_threshold' => $days,
+ ]);
}
-
public function disableAlertEmail(): Settings
{
- return $this->update(['alert_email' => null]);
+ return $this->update([
+ 'alert_email' => null,
+ 'alerts_enabled' => 0,
+ ]);
}
public function enableMultipleFullCompanySupport(): Settings