Merge pull request #15975 from marcusmoore/testing/license-checkin

Added tests around license checkin
This commit is contained in:
snipe 2024-12-19 22:33:03 +00:00 committed by GitHub
commit dc5dedd5a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 122 additions and 1 deletions

View file

@ -71,7 +71,7 @@ class LicenseCheckinController extends Controller
if (! $license->reassignable) {
// Not allowed to checkin
Session::flash('error', 'License not reassignable.');
Session::flash('error', trans('admin/licenses/message.checkin.not_reassignable') . '.');
return redirect()->back()->withInput();
}

View file

@ -4,6 +4,7 @@ namespace Database\Factories;
use App\Models\Asset;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@ -33,4 +34,18 @@ class LicenseSeatFactory extends Factory
];
});
}
public function reassignable()
{
return $this->afterMaking(function (LicenseSeat $seat) {
$seat->license->update(['reassignable' => true]);
});
}
public function notReassignable()
{
return $this->afterMaking(function (LicenseSeat $seat) {
$seat->license->update(['reassignable' => false]);
});
}
}

View file

@ -50,6 +50,7 @@ return array(
'checkin' => array(
'error' => 'There was an issue checking in the license. Please try again.',
'not_reassignable' => 'License not reassignable',
'success' => 'The license was checked in successfully'
),

View file

@ -2,8 +2,11 @@
namespace Tests\Feature\Checkins\Ui;
use App\Events\CheckoutableCheckedIn;
use App\Models\Asset;
use App\Models\LicenseSeat;
use App\Models\User;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
class LicenseCheckinTest extends TestCase
@ -17,10 +20,112 @@ class LicenseCheckinTest extends TestCase
->assertForbidden();
}
public function testCannotCheckinNonReassignableLicense()
{
$licenseSeat = LicenseSeat::factory()
->notReassignable()
->assignedToUser()
->create();
$this->actingAs(User::factory()->checkoutLicenses()->create())
->post(route('licenses.checkin.save', $licenseSeat), [
'notes' => 'my note',
'redirect_option' => 'index',
])
->assertSessionHas('error', trans('admin/licenses/message.checkin.not_reassignable') . '.');
$this->assertNotNull($licenseSeat->fresh()->assigned_to);
}
public function testCannotCheckinLicenseThatIsNotAssigned()
{
$licenseSeat = LicenseSeat::factory()
->reassignable()
->create();
$this->assertNull($licenseSeat->assigned_to);
$this->assertNull($licenseSeat->asset_id);
$this->actingAs(User::factory()->checkoutLicenses()->create())
->post(route('licenses.checkin.save', $licenseSeat), [
'notes' => 'my note',
'redirect_option' => 'index',
])
->assertSessionHas('error', trans('admin/licenses/message.checkin.error'));
}
public function testCanCheckInLicenseAssignedToAsset()
{
Event::fake([CheckoutableCheckedIn::class]);
$asset = Asset::factory()->create();
$licenseSeat = LicenseSeat::factory()
->reassignable()
->assignedToAsset($asset)
->create();
$actor = User::factory()->checkoutLicenses()->create();
$this->actingAs($actor)
->post(route('licenses.checkin.save', $licenseSeat), [
'notes' => 'my note',
'redirect_option' => 'index',
])
->assertRedirect(route('licenses.index'));
$this->assertNull($licenseSeat->fresh()->asset_id);
$this->assertNull($licenseSeat->fresh()->assigned_to);
$this->assertEquals('my note', $licenseSeat->fresh()->notes);
Event::assertDispatchedTimes(CheckoutableCheckedIn::class, 1);
Event::assertDispatched(CheckoutableCheckedIn::class, function (CheckoutableCheckedIn $event) use ($actor, $asset, $licenseSeat) {
return $event->checkoutable->is($licenseSeat)
&& $event->checkedOutTo->is($asset)
&& $event->checkedInBy->is($actor)
&& $event->note === 'my note';
});
}
public function testCanCheckInLicenseAssignedToUser()
{
Event::fake([CheckoutableCheckedIn::class]);
$user = User::factory()->create();
$licenseSeat = LicenseSeat::factory()
->reassignable()
->assignedToUser($user)
->create();
$actor = User::factory()->checkoutLicenses()->create();
$this->actingAs($actor)
->post(route('licenses.checkin.save', $licenseSeat), [
'notes' => 'my note',
'redirect_option' => 'index',
])
->assertRedirect(route('licenses.index'));
$this->assertNull($licenseSeat->fresh()->asset_id);
$this->assertNull($licenseSeat->fresh()->assigned_to);
$this->assertEquals('my note', $licenseSeat->fresh()->notes);
Event::assertDispatchedTimes(CheckoutableCheckedIn::class, 1);
Event::assertDispatched(CheckoutableCheckedIn::class, function (CheckoutableCheckedIn $event) use ($actor, $licenseSeat, $user) {
return $event->checkoutable->is($licenseSeat)
&& $event->checkedOutTo->is($user)
&& $event->checkedInBy->is($actor)
&& $event->note === 'my note';
});
}
public function testPageRenders()
{
$this->actingAs(User::factory()->superuser()->create())
->get(route('licenses.checkin', LicenseSeat::factory()->assignedToUser()->create()->id))
->assertOk();
}
}