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

94 lines
3.1 KiB
PHP

<?php
namespace Tests\Feature\Api\Checkouts;
use App\Models\Accessory;
use App\Models\Actionlog;
use App\Models\User;
use App\Notifications\CheckoutAccessoryNotification;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class AccessoryCheckoutTest extends TestCase
{
public function testCheckingOutAccessoryRequiresCorrectPermission()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.accessories.checkout', Accessory::factory()->create()))
->assertForbidden();
}
public function testValidationWhenCheckingOutAccessory()
{
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
->postJson(route('api.accessories.checkout', Accessory::factory()->create()), [
// missing assigned_to
])
->assertStatusMessageIs('error');
}
public function testAccessoryMustBeAvailableWhenCheckingOut()
{
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
->postJson(route('api.accessories.checkout', Accessory::factory()->withoutItemsRemaining()->create()), [
'assigned_to' => User::factory()->create()->id,
])
->assertStatusMessageIs('error');
}
public function testAccessoryCanBeCheckedOut()
{
$accessory = Accessory::factory()->create();
$user = User::factory()->create();
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
->postJson(route('api.accessories.checkout', $accessory), [
'assigned_to' => $user->id,
]);
$this->assertTrue($accessory->users->contains($user));
}
public function testUserSentNotificationUponCheckout()
{
Notification::fake();
$accessory = Accessory::factory()->requiringAcceptance()->create();
$user = User::factory()->create();
$this->actingAsForApi(User::factory()->checkoutAccessories()->create())
->postJson(route('api.accessories.checkout', $accessory), [
'assigned_to' => $user->id,
]);
Notification::assertSentTo($user, CheckoutAccessoryNotification::class);
}
public function testActionLogCreatedUponCheckout()
{
$accessory = Accessory::factory()->create();
$actor = User::factory()->checkoutAccessories()->create();
$user = User::factory()->create();
$this->actingAsForApi($actor)
->postJson(route('api.accessories.checkout', $accessory), [
'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' => $accessory->id,
'item_type' => Accessory::class,
'user_id' => $actor->id,
'note' => 'oh hi there',
])->count(),
'Log entry either does not exist or there are more than expected'
);
}
}