Skip to content

A free open source IT asset/license management system

More tests

snipe6d1317e75275
diff --git a/app/Http/Controllers/Api/UnacceptedAcceptancesController.php b/app/Http/Controllers/Api/UnacceptedAcceptancesController.php
new file mode 100644
index 0000000000..f8f373c39b
--- /dev/null
+++ b/app/Http/Controllers/Api/UnacceptedAcceptancesController.php
@@ -0,0 +1,123 @@
+<?php
+
+namespace App\Http\Controllers\Api;
+
+use App\Actions\Acceptances\DispatchUnacceptedAssetRemindersAction;
+use App\Helpers\Helper;
+use App\Http\Controllers\Controller;
+use App\Http\Transformers\AcceptancesTransformer;
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Component;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
+use Illuminate\Http\JsonResponse;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Artisan;
+use Illuminate\Support\Facades\Gate;
+
+/**
+ * JSON endpoint powering /unaccepted-acceptances (via
+ * UnacceptedAcceptancesController's Blade view).
+ *
+ * Scoping is DB-level, not post-hoc: whereHasMorph narrows to the
+ * checkoutable types the caller can view AND lets each type's own
+ * CompanyableScope / CompanyableChildScope apply its FMCS + location
+ * filtering during the join. The paginated count and the page rows
+ * reflect the same viewer-permissioned set, so no cross-company
+ * totals leak via `total`.
+ */
+class UnacceptedAcceptancesController extends Controller
+{
+ public function index(Request $request): array
+ {
+ $this->authorize('canCheckoutAtLeastOneItemType');
+
+ // Pre-filter the morph types to just those this caller can
+ // view. Types they can't view are excluded from the query
+ // entirely, preventing even a type-shape leak.
+ $viewableTypes = array_filter(
+ [Asset::class, LicenseSeat::class, Accessory::class, Component::class, Consumable::class],
+ fn(string $type) => Gate::allows('view', $type === LicenseSeat::class ? License::class : $type),
+ );
+
+ if ($viewableTypes === []) {
+ return (new AcceptancesTransformer)->transformAcceptances(collect(), 0);
+ }
+
+ $query = CheckoutAcceptance::pending()
+ ->with([
+ 'checkoutable' => function (MorphTo $morphTo) {
+ $morphTo->withTrashed()->morphWith([
+ Asset::class => ['model.category', 'assignedTo', 'company'],
+ Accessory::class => ['category', 'checkouts', 'company'],
+ LicenseSeat::class => ['user', 'license.company', 'license.category'],
+ Component::class => ['assignedTo', 'category', 'company'],
+ Consumable::class => ['category', 'company'],
+ ]);
+ },
+ 'assignedTo' => fn($q) => $q->withTrashed(),
+ ])
+ ->whereHasMorph('checkoutable', $viewableTypes)
+ ->orderByDesc('checkout_acceptances.created_at');
+
+ if ($request->boolean('deleted')) {
+ // "Show deleted" toggle. Flips the default pending-only
+ // scope to deleted-only (not with-trashed) so the view
+ // actually surfaces the stale rows for audit without
+ // mixing them in with the live pending set.
+ $query->onlyTrashed();
+ }
+
+ $acceptances = $query->get()
+ ->filter(fn(CheckoutAcceptance $acceptance) => $acceptance->checkoutable !== null)
+ ->values();
+
+ // Pagination lives in PHP because bs-table-client-side wants
+ // the whole page body in one fetch anyway (data-side-pagination
+ // was "client" on the old /reports/unaccepted_assets view too).
+ $total = $acceptances->count();
+ $offset = max(0, (int) $request->input('offset', 0));
+ $limit = min(max(1, (int) $request->input('limit', $total ?: 1)), 500);
+ $page = $acceptances->slice($offset, $limit)->values();
+
+ return (new AcceptancesTransformer)->transformAcceptances($page, $total);
+ }
+
+ /**
+ * Send reminder emails to users with unaccepted items. No `ids`
+ * supplied: delegates to snipeit:acceptance-reminder so the
+ * send-all shape is identical to the console command
+ */
+ public function sendReminders(Request $request): JsonResponse
+ {
+ $this->authorize('canCheckoutAtLeastOneItemType');
+
+ $ids = array_values(array_unique(array_filter(array_map('intval', (array) $request->input('ids', [])))));
+
+ if ($ids === []) {
+ Artisan::call('snipeit:acceptance-reminder');
+
+ return response()->json(
+ Helper::formatStandardApiResponse(
+ 'success',
+ ['mode' => 'all'],
+ trans('admin/reports/general.reminder_sent'),
+ ),
+ );
+ }
+
+ $result = DispatchUnacceptedAssetRemindersAction::run($ids);
+
+ return response()->json(
+ Helper::formatStandardApiResponse(
+ 'success',
+ ['mode' => 'ids'] + $result,
+ trans_choice('admin/reports/general.reminder_sent_count', $result['sent'], ['count' => $result['sent']]),
+ ),
+ );
+ }
+}
diff --git a/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php b/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php
index e44dc803b3..d394b0d415 100644
--- a/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php
+++ b/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php
@@ -2,7 +2,7 @@
namespace Tests\Feature\Api\UnacceptedAcceptances;
-use App\Mail\CheckoutAssetMail;
+use App\Mail\UnacceptedAssetReminderMail;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use App\Models\Company;
@@ -80,9 +80,11 @@ class SendRemindersApiTest extends TestCase
->assertOk()
->assertJsonPath('payload.mode', 'ids')
->assertJsonPath('payload.sent', 1)
- ->assertJsonPath('payload.skipped', 0);
+ ->assertJsonPath('payload.skipped.not_found', 0)
+ ->assertJsonPath('payload.skipped.fmcs', 0)
+ ->assertJsonPath('payload.skipped.no_email', 0);
- Mail::assertSent(CheckoutAssetMail::class);
+ Mail::assertSent(UnacceptedAssetReminderMail::class);
Artisan::shouldNotHaveReceived('call');
}
@@ -102,8 +104,7 @@ class SendRemindersApiTest extends TestCase
->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => [$acceptance->id]])
->assertOk()
->assertJsonPath('payload.sent', 0)
- ->assertJsonPath('payload.skipped', 1)
- ->assertJsonPath('payload.skipped_breakdown.not_found', 1);
+ ->assertJsonPath('payload.skipped.not_found', 1);
Mail::assertNothingSent();
}
@@ -125,7 +126,7 @@ class SendRemindersApiTest extends TestCase
->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => [$acceptance->id]])
->assertOk()
->assertJsonPath('payload.sent', 0)
- ->assertJsonPath('payload.skipped_breakdown.no_email', 1);
+ ->assertJsonPath('payload.skipped.no_email', 1);
Mail::assertNothingSent();
}
@@ -155,7 +156,7 @@ class SendRemindersApiTest extends TestCase
->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => [$acceptance->id]])
->assertOk()
->assertJsonPath('payload.sent', 0)
- ->assertJsonPath('payload.skipped_breakdown.fmcs', 1);
+ ->assertJsonPath('payload.skipped.fmcs', 1);
Mail::assertNothingSent();
}
@@ -186,10 +187,9 @@ class SendRemindersApiTest extends TestCase
])
->assertOk()
->assertJsonPath('payload.sent', 1)
- ->assertJsonPath('payload.skipped', 2)
- ->assertJsonPath('payload.skipped_breakdown.no_email', 1)
- ->assertJsonPath('payload.skipped_breakdown.not_found', 1);
+ ->assertJsonPath('payload.skipped.no_email', 1)
+ ->assertJsonPath('payload.skipped.not_found', 1);
- Mail::assertSent(CheckoutAssetMail::class, 1);
+ Mail::assertSent(UnacceptedAssetReminderMail::class, 1);
}
}
diff --git a/tests/Feature/Reporting/AcceptanceReportFmcsScopeTest.php b/tests/Feature/Reporting/AcceptanceReportFmcsScopeTest.php
index d59ace56a0..5efab46e6f 100644
--- a/tests/Feature/Reporting/AcceptanceReportFmcsScopeTest.php
+++ b/tests/Feature/Reporting/AcceptanceReportFmcsScopeTest.php
@@ -40,7 +40,7 @@ class AcceptanceReportFmcsScopeTest extends TestCase
[$assetA] = $this->seedPendingAcceptanceOwnedBy($companyA);
[$assetB] = $this->seedPendingAcceptanceOwnedBy($companyB);
- $callerA = User::factory()->checkoutAccessories()->forCompany($companyA)->create();
+ $callerA = User::factory()->viewAssets()->checkoutAccessories()->forCompany($companyA)->create();
$response = $this->actingAsForApi($callerA)
->getJson(route('api.unaccepted-acceptances.index'))
@@ -48,8 +48,8 @@ class AcceptanceReportFmcsScopeTest extends TestCase
$this->assertSame(1, $response->json('total'));
$itemNames = array_map(fn ($row) => $row['item']['name'], $response->json('rows'));
- $this->assertContains($assetA->name, $itemNames);
- $this->assertNotContains($assetB->name, $itemNames);
+ $this->assertContains($assetA->display_name, $itemNames);
+ $this->assertNotContains($assetB->display_name, $itemNames);
}
public function test_api_shows_all_pending_acceptances_to_a_superuser()
@@ -68,8 +68,8 @@ class AcceptanceReportFmcsScopeTest extends TestCase
$this->assertSame(2, $response->json('total'));
$itemNames = array_map(fn ($row) => $row['item']['name'], $response->json('rows'));
- $this->assertContains($assetA->name, $itemNames);
- $this->assertContains($assetB->name, $itemNames);
+ $this->assertContains($assetA->display_name, $itemNames);
+ $this->assertContains($assetB->display_name, $itemNames);
}
public function test_fmcs_disabled_leaves_api_unscoped()
@@ -81,7 +81,7 @@ class AcceptanceReportFmcsScopeTest extends TestCase
[$assetA] = $this->seedPendingAcceptanceOwnedBy($companyA);
[$assetB] = $this->seedPendingAcceptanceOwnedBy($companyB);
- $callerA = User::factory()->checkoutAccessories()->forCompany($companyA)->create();
+ $callerA = User::factory()->viewAssets()->checkoutAccessories()->forCompany($companyA)->create();
$response = $this->actingAsForApi($callerA)
->getJson(route('api.unaccepted-acceptances.index'))
@@ -89,7 +89,7 @@ class AcceptanceReportFmcsScopeTest extends TestCase
$this->assertSame(2, $response->json('total'));
$itemNames = array_map(fn ($row) => $row['item']['name'], $response->json('rows'));
- $this->assertContains($assetA->name, $itemNames);
- $this->assertContains($assetB->name, $itemNames);
+ $this->assertContains($assetA->display_name, $itemNames);
+ $this->assertContains($assetB->display_name, $itemNames);
}
}
diff --git a/tests/Feature/Reporting/ActivityReportPageAuthTest.php b/tests/Feature/Reporting/ActivityReportPageAuthTest.php
index 2ca2c3763e..d6a0ccbdbf 100644
--- a/tests/Feature/Reporting/ActivityReportPageAuthTest.php
+++ b/tests/Feature/Reporting/ActivityReportPageAuthTest.php
@@ -80,5 +80,4 @@ class ActivityReportPageAuthTest extends TestCase
->assertOk()
->assertSee(route('reports.index').'"', false);
}
-
}
diff --git a/tests/Feature/Reporting/SentReminderAuthorizationTest.php b/tests/Feature/Reporting/SentReminderAuthorizationTest.php
index 024a8d8d54..5f2998d8a3 100644
--- a/tests/Feature/Reporting/SentReminderAuthorizationTest.php
+++ b/tests/Feature/Reporting/SentReminderAuthorizationTest.php
@@ -2,7 +2,7 @@
namespace Tests\Feature\Reporting;
-use App\Mail\CheckoutAssetMail;
+use App\Mail\UnacceptedAssetReminderMail;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use App\Models\Company;
@@ -80,7 +80,7 @@ class SentReminderAuthorizationTest extends TestCase
->assertRedirectToRoute('unaccepted-acceptances.index')
->assertSessionHas('success');
- Mail::assertSent(CheckoutAssetMail::class);
+ Mail::assertSent(UnacceptedAssetReminderMail::class);
}
public function test_checkout_user_cannot_send_reminder_for_another_company()
@@ -154,6 +154,6 @@ class SentReminderAuthorizationTest extends TestCase
->assertRedirectToRoute('unaccepted-acceptances.index')
->assertSessionHas('success');
- Mail::assertSent(CheckoutAssetMail::class);
+ Mail::assertSent(UnacceptedAssetReminderMail::class);
}
}