2024-07-04 19:20:54 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\AssetModels\Api;
|
|
|
|
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
use App\Models\Category;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class CreateAssetModelsTest extends TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public function testRequiresPermissionToCreateAssetModel()
|
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->postJson(route('api.models.store'))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanCreateAssetModelWithAssetModelType()
|
|
|
|
{
|
|
|
|
$response = $this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.models.store'), [
|
|
|
|
'name' => 'Test AssetModel',
|
2024-08-16 16:27:44 -07:00
|
|
|
'category_id' => Category::factory()->assetLaptopCategory()->create()->id
|
2024-07-04 19:20:54 -07:00
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('success')
|
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
|
|
|
|
|
|
|
$this->assertTrue(AssetModel::where('name', 'Test AssetModel')->exists());
|
|
|
|
|
|
|
|
$model = AssetModel::find($response['payload']['id']);
|
|
|
|
$this->assertEquals('Test AssetModel', $model->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCannotCreateAssetModelWithoutCategory()
|
|
|
|
{
|
|
|
|
$response = $this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.models.store'), [
|
|
|
|
'name' => 'Test AssetModel',
|
|
|
|
])
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertJson([
|
|
|
|
'messages' => [
|
|
|
|
'category_id' => ['The category id field is required.'],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->json();
|
|
|
|
|
|
|
|
$this->assertFalse(AssetModel::where('name', 'Test AssetModel')->exists());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-16 14:44:03 -07:00
|
|
|
public function testUniquenessAcrossModelNameAndModelNumber()
|
|
|
|
{
|
|
|
|
AssetModel::factory()->create(['name' => 'Test Model', 'model_number'=>'1234']);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.models.store'), [
|
|
|
|
'name' => 'Test Model',
|
|
|
|
'model_number' => '1234',
|
2024-08-16 16:27:44 -07:00
|
|
|
'category_id' => Category::factory()->assetLaptopCategory()->create()->id
|
2024-08-16 14:44:03 -07:00
|
|
|
])
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertJson([
|
|
|
|
'messages' => [
|
|
|
|
'name' => ['The name must be unique across models and model number. '],
|
|
|
|
'model_number' => ['The model number must be unique across models and name. '],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->json();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-16 15:04:11 -07:00
|
|
|
public function testUniquenessAcrossModelNameAndModelNumberWithBlankModelNumber()
|
|
|
|
{
|
|
|
|
AssetModel::factory()->create(['name' => 'Test Model']);
|
|
|
|
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.models.store'), [
|
|
|
|
'name' => 'Test Model',
|
2024-08-16 16:27:44 -07:00
|
|
|
'category_id' => Category::factory()->assetLaptopCategory()->create()->id
|
2024-08-16 15:04:11 -07:00
|
|
|
])
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertJson([
|
|
|
|
'messages' => [
|
|
|
|
'name' => ['The name must be unique across models and model number. '],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->json();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-04 19:20:54 -07:00
|
|
|
}
|