Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
Signed-off-by: snipe <[email protected]>
  • Loading branch information
snipe committed Jul 11, 2024
1 parent da2b64c commit a6b2f5d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/Feature/Consumables/Api/ConsumableUpdateTest.php
Original file line number Diff line number Diff line change
@@ -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');

}

}
51 changes: 51 additions & 0 deletions tests/Feature/Consumables/Api/ConsumableViewTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
26 changes: 26 additions & 0 deletions tests/Feature/Consumables/Ui/ConsumableViewTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit a6b2f5d

Please sign in to comment.