Skip to content

A free open source IT asset/license management system

Added tests

snipe71da379bdb98
diff --git a/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php b/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php
new file mode 100644
index 0000000000..e44dc803b3
--- /dev/null
+++ b/tests/Feature/Api/UnacceptedAcceptances/SendRemindersApiTest.php
@@ -0,0 +1,195 @@
+<?php
+
+namespace Tests\Feature\Api\UnacceptedAcceptances;
+
+use App\Mail\CheckoutAssetMail;
+use App\Models\Asset;
+use App\Models\CheckoutAcceptance;
+use App\Models\Company;
+use App\Models\User;
+use Illuminate\Support\Facades\Artisan;
+use Illuminate\Support\Facades\Mail;
+use PHPUnit\Framework\Attributes\Test;
+use Tests\TestCase;
+
+/**
+ * API coverage for POST /api/v1/unaccepted-acceptances/send-reminders,
+ * the scheduler-friendly surface added for #19811.
+ *
+ * Shape:
+ * - no ids in body: delegates to the snipeit:acceptance-reminder
+ * console command (one grouped email per assignee).
+ * - ids in body: iterates each, applies FMCS, dispatches per-row.
+ */
+class SendRemindersApiTest extends TestCase
+{
+ #[Test]
+ public function user_without_checkout_grant_is_rejected(): void
+ {
+ $this->actingAsForApi(User::factory()->create())
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'))
+ ->assertForbidden();
+ }
+
+ #[Test]
+ public function missing_ids_delegates_to_the_artisan_command(): void
+ {
+ Mail::fake();
+ Artisan::spy();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'))
+ ->assertOk()
+ ->assertJsonPath('status', 'success')
+ ->assertJsonPath('payload.mode', 'all');
+
+ Artisan::shouldHaveReceived('call')->with('snipeit:acceptance-reminder')->once();
+ }
+
+ #[Test]
+ public function empty_ids_array_also_delegates_to_the_artisan_command(): void
+ {
+ // Explicitly-empty array arrives from well-formed clients that
+ // always include the key. Should behave the same as missing.
+ Artisan::spy();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => []])
+ ->assertOk()
+ ->assertJsonPath('payload.mode', 'all');
+
+ Artisan::shouldHaveReceived('call')->with('snipeit:acceptance-reminder')->once();
+ }
+
+ #[Test]
+ public function ids_in_body_sends_per_row_and_bypasses_the_console_command(): void
+ {
+ Mail::fake();
+ Artisan::spy();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $asset = Asset::factory()->create();
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($asset, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => [$acceptance->id]])
+ ->assertOk()
+ ->assertJsonPath('payload.mode', 'ids')
+ ->assertJsonPath('payload.sent', 1)
+ ->assertJsonPath('payload.skipped', 0);
+
+ Mail::assertSent(CheckoutAssetMail::class);
+ Artisan::shouldNotHaveReceived('call');
+ }
+
+ #[Test]
+ public function id_pointing_at_a_non_pending_acceptance_is_counted_as_not_found(): void
+ {
+ Mail::fake();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $asset = Asset::factory()->create();
+ $acceptance = CheckoutAcceptance::factory()
+ ->for($asset, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create(['accepted_at' => now()]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->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);
+
+ Mail::assertNothingSent();
+ }
+
+ #[Test]
+ public function acceptance_without_assignee_email_is_counted_as_no_email(): void
+ {
+ Mail::fake();
+
+ $assignee = User::factory()->create(['email' => '']);
+ $asset = Asset::factory()->create();
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($asset, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => [$acceptance->id]])
+ ->assertOk()
+ ->assertJsonPath('payload.sent', 0)
+ ->assertJsonPath('payload.skipped_breakdown.no_email', 1);
+
+ Mail::assertNothingSent();
+ }
+
+ #[Test]
+ public function fmcs_blocks_cross_company_ids_without_leaking_to_the_mail_queue(): void
+ {
+ Mail::fake();
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assignee = User::factory()->create(['email' => '[email protected]']);
+ $assetInB = Asset::factory()->create(['company_id' => $companyB->id]);
+ $acceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for($assetInB, 'checkoutable')
+ ->for($assignee, 'assignedTo')
+ ->create();
+
+ $callerInA = User::factory()
+ ->checkoutAccessories()
+ ->forCompany($companyA)
+ ->create();
+
+ $this->actingAsForApi($callerInA)
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'), ['ids' => [$acceptance->id]])
+ ->assertOk()
+ ->assertJsonPath('payload.sent', 0)
+ ->assertJsonPath('payload.skipped_breakdown.fmcs', 1);
+
+ Mail::assertNothingSent();
+ }
+
+ #[Test]
+ public function mixed_batch_reports_each_outcome_separately(): void
+ {
+ Mail::fake();
+
+ $goodAssignee = User::factory()->create(['email' => '[email protected]']);
+ $noEmailAssignee = User::factory()->create(['email' => '']);
+
+ $goodAcceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for(Asset::factory(), 'checkoutable')
+ ->for($goodAssignee, 'assignedTo')
+ ->create();
+
+ $noEmailAcceptance = CheckoutAcceptance::factory()
+ ->pending()
+ ->for(Asset::factory(), 'checkoutable')
+ ->for($noEmailAssignee, 'assignedTo')
+ ->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.unaccepted-acceptances.send-reminders'), [
+ 'ids' => [$goodAcceptance->id, $noEmailAcceptance->id, 999999],
+ ])
+ ->assertOk()
+ ->assertJsonPath('payload.sent', 1)
+ ->assertJsonPath('payload.skipped', 2)
+ ->assertJsonPath('payload.skipped_breakdown.no_email', 1)
+ ->assertJsonPath('payload.skipped_breakdown.not_found', 1);
+
+ Mail::assertSent(CheckoutAssetMail::class, 1);
+ }
+}
diff --git a/tests/Feature/Reporting/UnacceptedAssetReportTest.php b/tests/Feature/Reporting/UnacceptedAssetReportTest.php
index 37ea4d7ab1..22f2226556 100644
--- a/tests/Feature/Reporting/UnacceptedAssetReportTest.php
+++ b/tests/Feature/Reporting/UnacceptedAssetReportTest.php
@@ -5,47 +5,47 @@ namespace Tests\Feature\Reporting;
use App\Models\User;
use Tests\TestCase;
+/**
+ * Index-page authorization for /unaccepted-acceptances. The surface
+ * moved out of /reports/unaccepted_assets in v8.9 and the gate
+ * tightened from reports.view (plus a widened viewer carve-out) to
+ * canCheckoutAtLeastOneItemType, so view-only users no longer reach
+ * the page. Reporter: @ShaunNeighbourSwale (#19811).
+ */
class UnacceptedAssetReportTest extends TestCase
{
- public function test_permission_required_to_view_unaccepted_asset_report()
+ public function test_permission_required_to_view_unaccepted_acceptances()
{
$this->actingAs(User::factory()->create())
- ->get(route('reports/unaccepted_assets'))
+ ->get(route('unaccepted-acceptances.index'))
->assertForbidden();
}
- public function test_user_can_list_unaccepted_assets()
+ public function test_user_can_list_unaccepted_acceptances()
{
$this->actingAs(User::factory()->superuser()->create())
- ->get(route('reports/unaccepted_assets'))
+ ->get(route('unaccepted-acceptances.index'))
->assertOk();
}
- public function test_checkoutable_viewer_can_reach_report_without_reports_view_permission()
+ public function test_view_only_user_cannot_reach_the_page()
{
- // Dashboard's Needs Attention widget links every checkoutable
- // viewer to this report. Without the widened gate the link
- // 403s for scoped viewers, which is what this test guards
- // against. See getAssetAcceptanceReport() in ReportsController.
+ // Regression pin for the gate tightening. A user who can view
+ // components but can't check anything out shouldn't reach the
+ // page, since the mutating actions on it (reminder sends,
+ // deletes) are what the page exists for.
$this->actingAs(User::factory()->viewComponents()->create())
- ->get(route('reports/unaccepted_assets'))
- ->assertOk();
+ ->get(route('unaccepted-acceptances.index'))
+ ->assertForbidden();
}
- public function test_mutating_endpoints_still_require_reports_view_for_scoped_viewer()
+ public function test_checkout_grant_on_any_type_reaches_the_page()
{
- // The GET report page was widened to accept checkoutable
- // viewers, but the CSV export (postAssetAcceptanceReport),
- // sent-reminder, and delete-acceptance endpoints all still
- // authorize reports.view directly. A scoped viewer arriving
- // via the Needs Attention widget sees a read-only list. The
- // blade hides the mutating buttons because
- // $canManageAcceptances is false, but the underlying route
- // gates are what actually enforce it and are pinned here.
- $viewer = User::factory()->viewComponents()->create();
-
- $this->actingAs($viewer)
- ->post(route('reports/export/unaccepted_assets'))
- ->assertForbidden();
+ // The gate is canCheckoutAtLeastOneItemType, so a checkout-
+ // on-components user can view the page even without broader
+ // admin permissions.
+ $this->actingAs(User::factory()->checkoutComponents()->create())
+ ->get(route('unaccepted-acceptances.index'))
+ ->assertOk();
}
}