-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fixed consumable checkout via API not sending notification
- Loading branch information
Showing
7 changed files
with
224 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Api\Consumables; | ||
|
||
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; | ||
|
||
class ConsumableCheckoutTest extends TestCase | ||
{ | ||
use InteractsWithSettings; | ||
|
||
public function testCheckingOutConsumableRequiresCorrectPermission() | ||
{ | ||
$this->actingAsForApi(User::factory()->create()) | ||
->postJson(route('api.consumables.checkout', Consumable::factory()->create())) | ||
->assertForbidden(); | ||
} | ||
|
||
public function testValidationWhenCheckingOutConsumable() | ||
{ | ||
$this->actingAsForApi(User::factory()->checkoutConsumables()->create()) | ||
->postJson(route('api.consumables.checkout', Consumable::factory()->create()), [ | ||
// missing assigned_to | ||
]) | ||
->assertStatusMessageIs('error'); | ||
} | ||
|
||
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'); | ||
} | ||
|
||
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)); | ||
} | ||
|
||
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); | ||
} | ||
|
||
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' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
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; | ||
|
||
class ConsumableCheckoutTest extends TestCase | ||
{ | ||
use InteractsWithSettings; | ||
|
||
public function testCheckingOutConsumableRequiresCorrectPermission() | ||
{ | ||
$this->actingAs(User::factory()->create()) | ||
->post(route('consumables.checkout.store', Consumable::factory()->create())) | ||
->assertForbidden(); | ||
} | ||
|
||
public function testValidationWhenCheckingOutConsumable() | ||
{ | ||
$this->actingAs(User::factory()->checkoutConsumables()->create()) | ||
->post(route('consumables.checkout.store', Consumable::factory()->create()), [ | ||
// missing assigned_to | ||
]) | ||
->assertSessionHas('error'); | ||
} | ||
|
||
public function testConsumableMustBeAvailableWhenCheckingOut() | ||
{ | ||
$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() | ||
{ | ||
$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() | ||
{ | ||
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() | ||
{ | ||
$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' | ||
); | ||
} | ||
} |