Skip to content

A free open source IT asset/license management system

add fillable properties to rules, some tests, move authorization to request

spencerrlongg7571ff007f6a
diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php
index ec8a605caf..adbe4bc35c 100644
--- a/app/Http/Controllers/Api/DepartmentsController.php
+++ b/app/Http/Controllers/Api/DepartmentsController.php
@@ -97,9 +97,8 @@ class DepartmentsController extends Controller
*/
public function store(StoreDepartmentRequest $request): JsonResponse
{
- $this->authorize('create', Department::class);
$department = new Department;
- $department->fill($request->all());
+ $department->fill($request->validated());
$department = $request->handleImages($department);
$department->created_by = auth()->id();
diff --git a/app/Models/Department.php b/app/Models/Department.php
index 592fd840b1..4b9eb849f7 100644
--- a/app/Models/Department.php
+++ b/app/Models/Department.php
@@ -30,10 +30,13 @@ class Department extends SnipeModel
];
protected $rules = [
- 'name' => 'required|max:255|is_unique_department',
- 'location_id' => 'numeric|nullable',
- 'company_id' => 'numeric|nullable',
- 'manager_id' => 'numeric|nullable',
+ 'name' => 'required|max:255|is_unique_department',
+ 'location_id' => 'numeric|nullable|exists:locations,id',
+ 'company_id' => 'numeric|nullable|exists:companies,id',
+ 'manager_id' => 'numeric|nullable|exists:users,id',
+ 'phone' => 'string|max:255|nullable',
+ 'fax' => 'string|max:255|nullable',
+ 'notes' => 'string|max:255|nullable',
];
/**
diff --git a/tests/Feature/Departments/Api/CreateDepartmentsTest.php b/tests/Feature/Departments/Api/CreateDepartmentsTest.php
index 4b887bfc3d..0ddb25319e 100644
--- a/tests/Feature/Departments/Api/CreateDepartmentsTest.php
+++ b/tests/Feature/Departments/Api/CreateDepartmentsTest.php
@@ -8,29 +8,37 @@ use App\Models\Department;
use App\Models\Category;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
+use phpDocumentor\Reflection\Location;
use Tests\TestCase;
class CreateDepartmentsTest extends TestCase
{
- public function testRequiresPermissionToCreateDepartment()
+ public function test_requires_permission_to_create_department()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.departments.store'))
->assertForbidden();
}
- public function testCanCreateDepartment()
+ public function test_can_create_department_with_all_fields()
{
+ $company = Company::factory()->create();
+ //$location = Location::factory()->create(); ???
+ $manager = User::factory()->create();
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.departments.store'), [
- 'name' => 'Test Department',
- 'notes' => 'Test Note',
+ 'name' => 'Test Department',
+ 'company_id' => $company->id,
+ //'location_id' => $location->id,
+ 'manager_id' => $manager->id,
+ 'notes' => 'Test Note',
+ 'phone' => '1234567890',
+ 'fax' => '1234567890',
])
->assertOk()
->assertStatusMessageIs('success')
- ->assertStatus(200)
->json();
$this->assertTrue(Department::where('name', 'Test Department')->exists());
@@ -40,7 +48,27 @@ class CreateDepartmentsTest extends TestCase
$this->assertEquals('Test Note', $department->notes);
}
- public function testNoArraysAllowed()
+ public function test_name_required_for_department()
+ {
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.departments.store'), [
+ 'company_id' => Company::factory()->create()->id,
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error');
+ }
+
+ public function test_only_name_required_for_department()
+ {
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.departments.store'), [
+ 'name' => 'Test Department',
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+ }
+
+ public function test_arrays_not_allowed_for_numeric_fields()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.departments.store'), [
@@ -51,4 +79,17 @@ class CreateDepartmentsTest extends TestCase
->assertStatusMessageIs('error');
}
+ public function test_arrays_of_strings_are_not_allowed_for_numeric_fields()
+ {
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->postJson(route('api.departments.store'), [
+ 'name' => 'Test Department',
+ 'company_id' => ['1', '2'],
+ ])
+ ->assertOk()
+ ->assertStatusMessageIs('error');
+ }
+
+
+
}