Added category tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-05 01:46:09 +01:00
parent 96d1aea54a
commit 4089b4cd68
2 changed files with 116 additions and 0 deletions

View file

@ -0,0 +1,60 @@
<?php
namespace Tests\Feature\Categories\Api;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class CreateCategoriesTest extends TestCase
{
public function testRequiresPermissionToCreateCategory()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.categories.store'))
->assertForbidden();
}
public function testCanCreateCategoryWithCategoryType()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.categories.store'), [
'name' => 'Test Category',
'eula_text' => 'Test EULA',
'category_type' => 'accessory',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();
$this->assertTrue(Category::where('name', 'Test Category')->exists());
$category = Category::find($response['payload']['id']);
$this->assertEquals('Test Category', $category->name);
$this->assertEquals('Test EULA', $category->eula_text);
$this->assertEquals('accessory', $category->category_type);
}
public function testCannotCreateCategoryWithoutCategoryType()
{
$this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.categories.store'), [
'name' => 'Test Category',
'eula_text' => 'Test EULA',
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
$this->assertFalse(Category::where('name', 'Test Category')->exists());
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace Tests\Feature\Categories\Api;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class UpdateCategoriesTest extends TestCase
{
public function testCanUpdateCategoryViaPatchWithoutCategoryType()
{
$category = Category::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.categories.update', $category), [
'name' => 'Test Category',
'eula_text' => 'Test EULA',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();
//dd($response);
$category->refresh();
$this->assertEquals('Test Category', $category->name, 'Name was not updated');
$this->assertEquals('Test EULA', $category->eula_text, 'EULA was not updated');
}
public function testCannotUpdateCategoryViaPatchWithCategoryType()
{
$category = Category::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.categories.update', $category), [
'name' => 'Test Category',
'eula_text' => 'Test EULA',
'category_type' => 'accessory',
])
->assertOk()
->assertStatusMessageIs('error')
->assertStatus(200)
->json();
//dd($response);
$category->refresh();
$this->assertNotEquals('Test Category', $category->name, 'Name was not updated');
$this->assertNotEquals('Test EULA', $category->eula_text, 'EULA was not updated');
$this->assertNotEquals('accessory', $category->category_type, 'EULA was not updated');
}
}