Skip to content

Commit

Permalink
Add tests for delete companies endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusmoore committed Sep 12, 2024
1 parent 8ce2512 commit 910f13c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
15 changes: 10 additions & 5 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,6 @@ public function viewComponents()
return $this->appendPermission(['components.view' => '1']);
}

public function createCompanies()
{
return $this->appendPermission(['companies.create' => '1']);
}

public function createComponents()
{
return $this->appendPermission(['components.create' => '1']);
Expand All @@ -276,6 +271,16 @@ public function checkoutComponents()
return $this->appendPermission(['components.checkout' => '1']);
}

public function createCompanies()
{
return $this->appendPermission(['companies.create' => '1']);
}

public function deleteCompanies()
{
return $this->appendPermission(['companies.delete' => '1']);
}

public function viewUsers()
{
return $this->appendPermission(['users.view' => '1']);
Expand Down
49 changes: 49 additions & 0 deletions tests/Feature/Companies/Api/DeleteCompaniesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Tests\Feature\Companies\Api;

use App\Models\Asset;
use App\Models\Company;
use App\Models\User;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;

class DeleteCompaniesTest extends TestCase implements TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$company = Company::factory()->create();

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

public function testCanDeleteCompany()
{
$company = Company::factory()->create();

$this->actingAsForApi(User::factory()->deleteCompanies()->create())
->deleteJson(route('api.companies.destroy', $company))
->assertStatusMessageIs('success');

$this->assertDatabaseMissing('companies', ['id' => $company->id]);
}

public function testCannotDeleteCompanyThatHasAssociatedItems()
{
$companyWithAssets = Company::factory()->hasAssets()->create();
$companyWithAccessories = Company::factory()->hasAccessories()->create();
$companyWithConsumables = Company::factory()->hasConsumables()->create();
$companyWithComponents = Company::factory()->hasComponents()->create();
$companyWithUsers = Company::factory()->hasUsers()->create();

$actor = $this->actingAsForApi(User::factory()->deleteCompanies()->create());

$actor->deleteJson(route('api.companies.destroy', $companyWithAssets))->assertStatusMessageIs('error');
$actor->deleteJson(route('api.companies.destroy', $companyWithAccessories))->assertStatusMessageIs('error');
$actor->deleteJson(route('api.companies.destroy', $companyWithConsumables))->assertStatusMessageIs('error');
$actor->deleteJson(route('api.companies.destroy', $companyWithComponents))->assertStatusMessageIs('error');
$actor->deleteJson(route('api.companies.destroy', $companyWithUsers))->assertStatusMessageIs('error');
}
}

0 comments on commit 910f13c

Please sign in to comment.