snipe-it/tests/Feature/Api/Checkouts/ConsumableCheckoutTest.php

94 lines
3.1 KiB
PHP
Raw Normal View History

2024-01-29 17:56:55 -08:00
<?php
namespace Tests\Feature\Api\Checkouts;
2024-01-29 17:56:55 -08:00
use App\Models\Actionlog;
use App\Models\Consumable;
use App\Models\User;
use App\Notifications\CheckoutConsumableNotification;
use Illuminate\Support\Facades\Notification;
2024-01-29 17:56:55 -08:00
use Tests\TestCase;
class ConsumableCheckoutTest extends TestCase
{
public function testCheckingOutConsumableRequiresCorrectPermission()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.consumables.checkout', Consumable::factory()->create()))
->assertForbidden();
2024-01-29 17:56:55 -08:00
}
public function testValidationWhenCheckingOutConsumable()
{
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
->postJson(route('api.consumables.checkout', Consumable::factory()->create()), [
// missing assigned_to
])
->assertStatusMessageIs('error');
2024-01-29 17:56:55 -08:00
}
public function testConsumableMustBeAvailableWhenCheckingOut()
{
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
->postJson(route('api.consumables.checkout', Consumable::factory()->withoutItemsRemaining()->create()), [
'assigned_to' => User::factory()->create()->id,
])
->assertStatusMessageIs('error');
2024-01-29 17:56:55 -08:00
}
public function testConsumableCanBeCheckedOut()
{
$consumable = Consumable::factory()->create();
$user = User::factory()->create();
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
->postJson(route('api.consumables.checkout', $consumable), [
'assigned_to' => $user->id,
]);
$this->assertTrue($user->consumables->contains($consumable));
2024-01-29 17:56:55 -08:00
}
public function testUserSentNotificationUponCheckout()
{
Notification::fake();
$consumable = Consumable::factory()->requiringAcceptance()->create();
$user = User::factory()->create();
$this->actingAsForApi(User::factory()->checkoutConsumables()->create())
->postJson(route('api.consumables.checkout', $consumable), [
'assigned_to' => $user->id,
]);
Notification::assertSentTo($user, CheckoutConsumableNotification::class);
2024-01-29 17:56:55 -08:00
}
public function testActionLogCreatedUponCheckout()
{$consumable = Consumable::factory()->create();
$actor = User::factory()->checkoutConsumables()->create();
$user = User::factory()->create();
$this->actingAsForApi($actor)
->postJson(route('api.consumables.checkout', $consumable), [
'assigned_to' => $user->id,
'note' => 'oh hi there',
]);
$this->assertEquals(
1,
Actionlog::where([
'action_type' => 'checkout',
'target_id' => $user->id,
'target_type' => User::class,
'item_id' => $consumable->id,
'item_type' => Consumable::class,
'user_id' => $actor->id,
'note' => 'oh hi there',
])->count(),
'Log entry either does not exist or there are more than expected'
);
2024-01-29 17:56:55 -08:00
}
}