Skip to content

A free open source IT asset/license management system

Implement tests around consumable checkout

Marcus Moore13c37e708ff5
diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
index 0cac973415..f7a297aeee 100644
--- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
+++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php
@@ -76,7 +76,6 @@ class ConsumableCheckoutController extends Controller
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable'));
}
-
$admin_user = Auth::user();
$assigned_to = e($request->input('assigned_to'));
diff --git a/database/factories/ConsumableFactory.php b/database/factories/ConsumableFactory.php
index 18a116418b..b5c9b58f84 100644
--- a/database/factories/ConsumableFactory.php
+++ b/database/factories/ConsumableFactory.php
@@ -91,4 +91,22 @@ class ConsumableFactory extends Factory
];
});
}
+
+ public function withoutItemsRemaining()
+ {
+ return $this->state(function () {
+ return [
+ 'qty' => 1,
+ ];
+ })->afterCreating(function (Consumable $consumable) {
+ $user = User::factory()->create();
+
+ $consumable->users()->attach($consumable->id, [
+ 'consumable_id' => $consumable->id,
+ 'user_id' => $user->id,
+ 'assigned_to' => $user->id,
+ 'note' => '',
+ ]);
+ });
+ }
}
diff --git a/tests/Feature/Checkouts/ConsumableCheckoutTest.php b/tests/Feature/Checkouts/ConsumableCheckoutTest.php
index bbbba9b7e0..5785d0572b 100644
--- a/tests/Feature/Checkouts/ConsumableCheckoutTest.php
+++ b/tests/Feature/Checkouts/ConsumableCheckoutTest.php
@@ -2,6 +2,11 @@
namespace Tests\Feature\Checkouts;
+use App\Models\Actionlog;
+use App\Models\Consumable;
+use App\Models\User;
+use App\Notifications\CheckoutConsumableNotification;
+use Illuminate\Support\Facades\Notification;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
@@ -11,31 +16,81 @@ class ConsumableCheckoutTest extends TestCase
public function testCheckingOutConsumableRequiresCorrectPermission()
{
- $this->markTestIncomplete();
+ $this->actingAs(User::factory()->create())
+ ->post(route('consumables.checkout.store', Consumable::factory()->create()))
+ ->assertForbidden();
}
public function testValidationWhenCheckingOutConsumable()
{
- $this->markTestIncomplete();
+ $this->actingAs(User::factory()->checkoutConsumables()->create())
+ ->post(route('consumables.checkout.store', Consumable::factory()->create()), [
+ // missing assigned_to
+ ])
+ ->assertSessionHas('error');
}
public function testConsumableMustBeAvailableWhenCheckingOut()
{
- $this->markTestIncomplete();
+ $this->actingAs(User::factory()->checkoutConsumables()->create())
+ ->post(route('consumables.checkout.store', Consumable::factory()->withoutItemsRemaining()->create()), [
+ 'assigned_to' => User::factory()->create()->id,
+ ])
+ ->assertSessionHas('error');
}
public function testConsumableCanBeCheckedOut()
{
- $this->markTestIncomplete();
+ $consumable = Consumable::factory()->create();
+ $user = User::factory()->create();
+
+ $this->actingAs(User::factory()->checkoutConsumables()->create())
+ ->post(route('consumables.checkout.store', $consumable), [
+ 'assigned_to' => $user->id,
+ ]);
+
+ $this->assertTrue($user->consumables->contains($consumable));
}
public function testUserSentNotificationUponCheckout()
{
- $this->markTestIncomplete();
+ Notification::fake();
+
+ $consumable = Consumable::factory()->create();
+ $user = User::factory()->create();
+
+ $this->actingAs(User::factory()->checkoutConsumables()->create())
+ ->post(route('consumables.checkout.store', $consumable), [
+ 'assigned_to' => $user->id,
+ ]);
+
+ Notification::assertSentTo($user, CheckoutConsumableNotification::class);
}
public function testActionLogCreatedUponCheckout()
{
- $this->markTestIncomplete();
+ $consumable = Consumable::factory()->create();
+ $actor = User::factory()->checkoutConsumables()->create();
+ $user = User::factory()->create();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.checkout.store', $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'
+ );
}
}