Fixed tests

This commit is contained in:
snipe
2026-04-30 09:53:52 +01:00
parent a0bf7a018c
commit f4b9736862
9 changed files with 49 additions and 66 deletions
@@ -53,7 +53,7 @@ class AcceptanceController extends Controller
$currentUser = auth()->user();
if (! $currentUser instanceof User) {
abort(403, trans('general.insufficient_permissions'));
return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions'));
}
$acceptance = CheckoutAcceptance::find($id);
@@ -78,7 +78,7 @@ class AcceptanceController extends Controller
}
if (! Company::isCurrentUserHasAccess($acceptance->checkoutable)) {
return redirect()->route('account.accept')->with('error', trans('general.error_user_company'));
return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions'));
}
$checkedOutAt = Helper::getFormattedDateObject($acceptance->created_at, 'datetime', false);
@@ -92,19 +92,15 @@ class AcceptanceController extends Controller
*
* @param int $id
*/
public function store(AcceptSignatureRequest $request, $id): RedirectResponse
public function store(AcceptSignatureRequest $request, CheckoutAcceptance $acceptance): RedirectResponse
{
$currentUser = auth()->user();
if (! $currentUser instanceof User) {
abort(403, trans('general.insufficient_permissions'));
return redirect()->route('account.accept')->with('error', trans('general.insufficient_permissions'));
}
$acceptance = CheckoutAcceptance::find($id);
if (! $acceptance) {
return redirect()->route('account.accept')->with('error', trans('admin/hardware/message.does_not_exist'));
}
$assignedUser = User::find($acceptance->assigned_to_id);
$settings = Setting::getSettings();
@@ -48,4 +48,17 @@ class AcceptSignatureRequest extends FormRequest
// ...existing validation rules...
];
}
protected function failedAuthorization()
{
$user = Auth::user();
$acceptance = $this->route('acceptance');
// If user is logged in and acceptance exists, treat as business logic error
if ($user && $acceptance) {
$redirectResponse = redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted'));
throw new \Illuminate\Validation\ValidationException($this->getValidatorInstance(), $redirectResponse);
}
// Otherwise, use default 403
parent::failedAuthorization();
}
}
+1 -1
View File
@@ -127,7 +127,7 @@ class CheckoutAcceptance extends Model
*/
public function isCheckedOutTo(User $user)
{
return $this->assignedTo?->is($user);
return $this->assigned_to_id === $user->id;
}
/**
@@ -125,13 +125,12 @@
<div class="row">
<div class="col-md-7">
@if ($acceptance->assignedTo?->email)
@if (config('app.always_send_email')!='true')
<div class="col-md-12">
{{ trans('general.acceptance_email_always_sent') }}
({{ $acceptance->assignedTo->email }})
@if (config('app.always_send_email'))
<div class="col-md-12" id="emailInfoBox">
{{ trans('general.acceptance_email_always_sent') }} ({{ $acceptance->assignedTo->email }})
</div>
@else
<div class="col-md-12" style="display: none;" id="showEmailBox">
<div class="col-md-12" id="showEmailBox">
<label class="form-control">
<input type="checkbox" value="1" name="send_copy" id="send_copy" checked="checked" aria-label="send_copy">
{{ trans('mail.send_pdf_copy') }} ({{ $acceptance->assignedTo->email }})
@@ -202,16 +201,16 @@
@endif
$('[name="asset_acceptance"]').on('change', function() {
if ($(this).is(':checked') && $(this).attr('id') === 'declined') {
$("#showEmailBox").hide();
$("#emailInfoBox").hide();
$("#showSubmit").show();
$("#submit-button").removeClass("btn-success").addClass("btn-danger").show();
$("#submitIcon").removeClass("fa-check").addClass("fa-times");
$("#buttonText").text('{{ trans_choice('general.i_decline_item', $acceptance->qty ?? 1) }}');
$("#note").prop('required', true);
} else if ($(this).is(':checked') && $(this).attr('id') === 'accepted') {
$("#emailInfoBox").show();
$("#showEmailBox").show();
$("#showSubmit").show();
$("#submit-button").removeClass("btn-danger").addClass("btn-success").show();
@@ -36,7 +36,8 @@ class AcceptanceAuthorizationTest extends TestCase
'asset_acceptance' => 'accepted',
'note' => 'no',
]);
$response->assertForbidden();
$response->assertRedirectToRoute('account.accept');
$response->assertSessionHas('error');
$this->assertNull($acceptance->fresh()->accepted_at);
}
}
@@ -150,11 +150,15 @@ class AccessoryAcceptanceTest extends TestCase
{
Notification::fake();
$assignee = User::factory()->create();
$otherUser = User::factory()->create();
$accessory = Accessory::factory()->create();
$acceptance = CheckoutAcceptance::factory()
->pending()
->for(Asset::factory()->laptopMbp(), 'checkoutable')
->for($assignee, 'assignedTo')
->for($accessory, 'checkoutable')
->create();
$this->actingAs($otherUser)
@@ -257,7 +257,8 @@ class AssetAcceptanceTest extends TestCase
->post(route('account.store-acceptance', $checkoutAcceptance), [
'asset_acceptance' => 'accepted',
])
->assertRedirect(route('users.show', $assignee));
->assertRedirectToRoute('account.accept')
->assertSessionHas('error');
}
public function test_stale_sign_in_place_post_with_missing_assignee_does_not_throw_route_error()
@@ -33,7 +33,7 @@ class AcceptanceReminderTest extends TestCase
Mail::fake();
$this->admin = User::factory()->canViewReports()->create();
$this->admin = User::factory()->admin()->canViewReports()->create();
$this->assignee = User::factory()->create();
}
@@ -46,81 +46,50 @@ class UnacceptedAssetReportTest extends TestCase
$this->actingAs(User::factory()->create())
->get(route('reports/unaccepted_items'))
->assertForbidden();
}
public function test_user_can_list_unaccepted_assets()
{
$this->actingAs(User::factory()->superuser()->create())
$this->actingAs(User::factory()->canViewReports()->create())
->get(route('reports/unaccepted_items'))
->assertOk();
}
public function test_regular_user_does_not_see_actions_column_or_buttons()
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->get(route('reports/unaccepted_items'));
$response->assertOk();
$response->assertDontSee('Actions');
$response->assertDontSee('Send Reminder');
$response->assertDontSee('Delete');
}
public function test_admin_sees_actions_column_and_buttons()
{
$admin = User::factory()->admin()->create();
$response = $this->actingAs($admin)
->get(route('reports/unaccepted_items'));
$response->assertOk();
$response->assertSee('Actions');
$response->assertSee('Send Reminder');
$response->assertSee('Delete');
}
public function test_superuser_sees_actions_column_and_buttons()
{
$superuser = User::factory()->superuser()->create();
$response = $this->actingAs($superuser)
->get(route('reports/unaccepted_items'));
$response->assertOk();
$response->assertSee('Actions');
$response->assertSee('Send Reminder');
$response->assertSee('Delete');
}
public function test_regular_user_cannot_perform_reminder_or_delete()
{
$user = User::factory()->create();
$acceptanceId = 1; // Use a valid acceptance ID in your test DB or factory
$user = User::factory()->canViewReports()->create();
$acceptance = \App\Models\CheckoutAcceptance::factory()->pending()->create();
$this->actingAs($user)
->post(route('reports/unaccepted_items_sent_reminder'), ['acceptance_id' => $acceptanceId])
->post(route('reports/unaccepted_items_sent_reminder'), ['acceptance_id' => $acceptance->id])
->assertForbidden();
$this->actingAs($user)
->delete(route('reports/unaccepted_items_delete', $acceptanceId))
->delete(route('reports/unaccepted_items_delete', $acceptance->id))
->assertForbidden();
}
public function test_admin_can_perform_reminder_and_delete()
{
$admin = User::factory()->admin()->create();
$acceptanceId = 1; // Use a valid acceptance ID in your test DB or factory
$admin = User::factory()->admin()->canViewReports()->create();
$acceptance = \App\Models\CheckoutAcceptance::factory()->pending()->create();
$this->actingAs($admin)
->post(route('reports/unaccepted_items_sent_reminder'), ['acceptance_id' => $acceptanceId])
->post(route('reports/unaccepted_items_sent_reminder'), ['acceptance_id' => $acceptance->id])
->assertStatus(302); // Or whatever is appropriate (redirect, etc)
$this->actingAs($admin)
->delete(route('reports/unaccepted_items_delete', $acceptanceId))
->delete(route('reports/unaccepted_items_delete', $acceptance->id))
->assertStatus(302);
}
public function test_superuser_can_perform_reminder_and_delete()
{
$superuser = User::factory()->superuser()->create();
$acceptanceId = 1; // Use a valid acceptance ID in your test DB or factory
$superuser = User::factory()->superuser()->canViewReports()->create();
$acceptance = \App\Models\CheckoutAcceptance::factory()->pending()->create();
$this->actingAs($superuser)
->post(route('reports/unaccepted_items_sent_reminder'), ['acceptance_id' => $acceptanceId])
->post(route('reports/unaccepted_items_sent_reminder'), ['acceptance_id' => $acceptance->id])
->assertStatus(302);
$this->actingAs($superuser)
->delete(route('reports/unaccepted_items_delete', $acceptanceId))
->delete(route('reports/unaccepted_items_delete', $acceptance->id))
->assertStatus(302);
}
}