Skip to content

Commit

Permalink
Add tests for delete license endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusmoore committed Sep 16, 2024
1 parent 446e962 commit 60a54ce
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
1 change: 0 additions & 1 deletion app/Http/Controllers/Api/LicensesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public function update(Request $request, $id) : JsonResponse | array
*/
public function destroy($id) : JsonResponse
{
//
$license = License::findOrFail($id);
$this->authorize('delete', $license);

Expand Down
88 changes: 88 additions & 0 deletions tests/Feature/Licenses/Api/DeleteLicenseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Tests\Feature\Licenses\Api;

use App\Models\Company;
use App\Models\License;
use App\Models\User;
use Tests\Concerns\TestsMultipleFullCompanySupport;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;

class DeleteLicenseTest extends TestCase implements TestsMultipleFullCompanySupport, TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$license = License::factory()->create();

$this->actingAsForApi(User::factory()->create())
->deleteJson(route('api.licenses.destroy', $license))
->assertForbidden();
}

public function testAdheresToMultipleFullCompanySupportScoping()
{
[$companyA, $companyB] = Company::factory()->count(2)->create();

$licenseA = License::factory()->for($companyA)->create();
$licenseB = License::factory()->for($companyB)->create();
$licenseC = License::factory()->for($companyB)->create();

$superUser = $companyA->users()->save(User::factory()->superuser()->make());
$userInCompanyA = $companyA->users()->save(User::factory()->deleteLicenses()->make());
$userInCompanyB = $companyB->users()->save(User::factory()->deleteLicenses()->make());

$this->settings->enableMultipleFullCompanySupport();

$this->actingAsForApi($userInCompanyA)
->deleteJson(route('api.licenses.destroy', $licenseB))
->assertStatusMessageIs('error');

$this->actingAsForApi($userInCompanyB)
->deleteJson(route('api.licenses.destroy', $licenseA))
->assertStatusMessageIs('error');

$this->actingAsForApi($superUser)
->deleteJson(route('api.licenses.destroy', $licenseC))
->assertStatusMessageIs('success');

$this->assertNotSoftDeleted($licenseA);
$this->assertNotSoftDeleted($licenseB);
$this->assertSoftDeleted($licenseC);
}

public function testLicenseCannotBeDeletedIfStillAssigned()
{
$license = License::factory()->create(['seats' => 2]);
$license->freeSeat()->update(['assigned_to' => User::factory()->create()->id]);

$this->actingAsForApi(User::factory()->deleteLicenses()->create())
->deleteJson(route('api.licenses.destroy', $license))
->assertStatusMessageIs('error');

$this->assertNotSoftDeleted($license);
}

public function testCanDeleteLicense()
{
$license = License::factory()->create();

$this->actingAsForApi(User::factory()->deleteLicenses()->create())
->deleteJson(route('api.licenses.destroy', $license))
->assertStatusMessageIs('success');

$this->assertSoftDeleted($license);
}

public function testLicenseSeatsAreDeletedWhenLicenseIsDeleted()
{
$license = License::factory()->create(['seats' => 2]);

$this->assertTrue($license->fresh()->licenseseats->isNotEmpty(), 'License seats not created like expected');

$this->actingAsForApi(User::factory()->deleteLicenses()->create())
->deleteJson(route('api.licenses.destroy', $license));

$this->assertTrue($license->fresh()->licenseseats->isEmpty());
}
}

0 comments on commit 60a54ce

Please sign in to comment.