Added tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-11 15:24:56 +01:00
parent da2b64c96d
commit a6b2f5df1d
3 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,52 @@
<?php
namespace Tests\Feature\Consumables\Api;
use App\Models\Consumable;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class ConsumableUpdateTest extends TestCase
{
public function testCanUpdateConsumableViaPatchWithoutCategoryType()
{
$consumable = Consumable::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.consumables.update', $consumable), [
'name' => 'Test Consumable',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();
$consumable->refresh();
$this->assertEquals('Test Consumable', $consumable->name, 'Name was not updated');
}
public function testCannotUpdateConsumableViaPatchWithInvalidCategoryType()
{
$category = Category::factory()->create(['category_type' => 'asset']);
$consumable = Consumable::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.consumables.update', $consumable), [
'name' => 'Test Consumable',
'category_id' => $category->id,
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
$category->refresh();
$this->assertNotEquals('Test Consumable', $consumable->name, 'Name was not updated');
$this->assertNotEquals('consumable', $consumable->category_id, 'Category was not updated');
}
}

View file

@ -0,0 +1,51 @@
<?php
namespace Tests\Feature\Consumables\Api;
use App\Models\Company;
use App\Models\Consumable;
use App\Models\User;
use Tests\TestCase;
class ConsumableViewTest extends TestCase
{
public function testConsumableViewAdheresToCompanyScoping()
{
[$companyA, $companyB] = Company::factory()->count(2)->create();
$consumableA = Consumable::factory()->for($companyA)->create();
$consumableB = Consumable::factory()->for($companyB)->create();
$superUser = $companyA->users()->save(User::factory()->superuser()->make());
$userInCompanyA = $companyA->users()->save(User::factory()->viewConsumables()->make());
$userInCompanyB = $companyB->users()->save(User::factory()->viewConsumables()->make());
$this->settings->disableMultipleFullCompanySupport();
$this->actingAsForApi($superUser)
->getJson(route('api.consumables.show', $consumableA))
->assertOk();
$this->actingAsForApi($userInCompanyA)
->getJson(route('api.consumables.show', $consumableA))
->assertOk();
$this->actingAsForApi($userInCompanyB)
->getJson(route('api.consumables.show', $consumableB))
->assertOk();
$this->settings->enableMultipleFullCompanySupport();
$this->actingAsForApi($superUser)
->getJson(route('api.consumables.show', $consumableA))
->assertOk();
$this->actingAsForApi($userInCompanyA)
->getJson(route('api.consumables.index'))
->assertOk();
$this->actingAsForApi($userInCompanyB)
->getJson(route('api.consumables.index'))
->assertOk();
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Tests\Feature\Consumables\Ui;
use App\Models\Consumable;
use App\Models\User;
use Tests\TestCase;
class ConsumableViewTest extends TestCase
{
public function testPermissionRequiredToViewConsumable()
{
$consumable = Consumable::factory()->create();
$this->actingAs(User::factory()->create())
->get(route('consumables.show', $consumable))
->assertForbidden();
}
public function testUserCanListConsumables()
{
$consumable = Consumable::factory()->create();
$this->actingAs(User::factory()->superuser()->create())
->get(route('consumables.show', $consumable))
->assertOk();
}
}