Added category tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-05 00:21:58 +01:00
parent d3b62aa918
commit 1f28bf14c1
4 changed files with 241 additions and 0 deletions

View file

@ -0,0 +1,71 @@
<?php
namespace Tests\Feature\Categories\Api;
use App\Models\Category;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class IndexCategoryTest extends TestCase
{
public function testViewingCategoryIndexRequiresPermission()
{
$this->actingAsForApi(User::factory()->create())
->getJson(route('api.departments.index'))
->assertForbidden();
}
public function testCategoryIndexReturnsExpectedSearchResults()
{
Category::factory()->count(10)->create();
Category::factory()->count(1)->forAssets()->create(['name' => 'My Test Category']);
$this->actingAsForApi(User::factory()->superuser()->create())
->getJson(
route('api.categories.index', [
'search' => 'My Test Category',
'sort' => 'name',
'order' => 'asc',
'offset' => '0',
'limit' => '20',
]))
->assertOk()
->assertOk()
->assertJsonStructure([
'total',
'rows',
])
->assertJson([
'total' => 1,
]);
}
public function testCategoryIndexReturnsExpectedCategories()
{
$this->markTestIncomplete('Not sure why the category factory is generating one more than expected here.');
Category::factory()->count(3)->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->getJson(
route('api.categories.index', [
'sort' => 'id',
'order' => 'asc',
'offset' => '0',
'limit' => '20',
]))
->assertOk()
->assertJsonStructure([
'total',
'rows',
])
->assertJson([
'total' => 3,
]);
}
}

View file

@ -0,0 +1,51 @@
<?php
namespace Tests\Feature\Categories\Ui;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class CreateCategoriesTest extends TestCase
{
public function testPermissionRequiredToCreateCategories()
{
$this->actingAs(User::factory()->create())
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'asset',
])
->assertForbidden();
}
public function testUserCanCreateCategories()
{
$this->assertFalse(Category::where('name', 'Test Category')->exists());
$this->actingAs(User::factory()->superuser()->create())
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'asset'
])
->assertRedirect(route('categories.index'));
$this->assertTrue(Category::where('name', 'Test Category')->exists());
}
public function testUserCannotCreateCategoriesWithInvalidType()
{
$this->assertFalse(Category::where('name', 'Test Category')->exists());
$this->actingAs(User::factory()->superuser()->create())
->from(route('categories.create'))
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'invalid'
])
->assertRedirect(route('categories.create'));
$this->assertFalse(Category::where('name', 'Test Category')->exists());
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Tests\Feature\Categories\Ui;
use App\Models\User;
use Tests\TestCase;
class IndexCategoriesTest extends TestCase
{
public function testPermissionRequiredToViewCategoryList()
{
$this->actingAs(User::factory()->create())
->get(route('categories.index'))
->assertForbidden();
}
public function testUserCanListCategories()
{
$this->actingAs(User::factory()->superuser()->create())
->get(route('categories.index'))
->assertOk();
}
}

View file

@ -0,0 +1,96 @@
<?php
namespace Tests\Feature\Categories\Ui;
use App\Models\Category;
use App\Models\Asset;
use App\Models\User;
use Tests\TestCase;
class UpdateCategoriesTest extends TestCase
{
public function testPermissionRequiredToStoreCategory()
{
$this->actingAs(User::factory()->create())
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'asset'
])
->assertStatus(403)
->assertForbidden();
}
public function testUserCanCreateCategories()
{
$this->actingAs(User::factory()->superuser()->create())
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'asset'
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('categories.index'));
$this->assertTrue(Category::where('name', 'Test Category')->exists());
}
public function testUserCanEditAssetCategory()
{
$category = Category::factory()->forAssets()->create(['name' => 'Test Category']);
$this->assertTrue(Category::where('name', 'Test Category')->exists());
$response = $this->actingAs(User::factory()->superuser()->create())
->put(route('categories.update', ['category' => $category]), [
'name' => 'Test Category Edited',
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('categories.index'));
$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Category::where('name', 'Test Category Edited')->exists());
}
public function testUserCanChangeCategoryTypeIfNoAssetsAssociated()
{
$category = Category::factory()->forAssets()->create(['name' => 'Test Category']);
$this->assertTrue(Category::where('name', 'Test Category')->exists());
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('categories.edit', ['category' => $category->id]))
->put(route('categories.update', ['category' => $category]), [
'name' => 'Test Category Edited',
'category_type' => 'accessory',
])
->assertSessionHasNoErrors()
->assertStatus(302)
->assertRedirect(route('categories.index'));
$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Category::where('name', 'Test Category Edited')->exists());
}
public function testUserCannotChangeCategoryTypeIfAssetsAreAssociated()
{
Asset::factory()->count(5)->laptopMbp()->create();
$category = Category::where('name', 'Laptops')->first();
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('categories.edit', ['category' => $category->id]))
->put(route('categories.update', ['category' => $category]), [
'name' => 'Test Category Edited',
'category_type' => 'accessory',
])
->assertSessionHasErrors(['category_type'])
->assertInvalid(['category_type'])
->assertStatus(302)
->assertRedirect(route('categories.edit', ['category' => $category->id]));
$this->followRedirects($response)->assertSee(trans('general.error'));
$this->assertFalse(Category::where('name', 'Test Category Edited')->exists());
}
}