Added basic Department UI tests

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-07-05 07:53:02 +01:00
parent 1d65781d8d
commit 0a7e053985
6 changed files with 186 additions and 1 deletions

View file

@ -0,0 +1,23 @@
<?php
namespace Tests\Feature\Department\Api;
use App\Models\AssetModel;
use App\Models\Department;
use App\Models\Category;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class CreateDepartmentsTest extends TestCase
{
public function testRequiresPermissionToCreateDepartment()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.departments.store'))
->assertForbidden();
}
}

View file

@ -8,7 +8,7 @@ use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;
class DepartmentIndexTest extends TestCase
class DepartmentsIndexTest extends TestCase
{
public function testViewingDepartmentIndexRequiresAuthentication()
{

View file

@ -0,0 +1,41 @@
<?php
namespace Tests\Feature\Departments\Api;
use App\Models\Department;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class UpdateDepartmentsTest extends TestCase
{
public function testRequiresPermissionToEditDepartment()
{
$department = Department::factory()->create();
$this->actingAsForApi(User::factory()->create())
->patchJson(route('api.departments.update', $department))
->assertForbidden();
}
public function testCanUpdateDepartmentViaPatch()
{
$department = Department::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.departments.update', $department), [
'name' => 'Test Department',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();
$department->refresh();
$this->assertEquals('Test Department', $department->name, 'Name was not updated');
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Tests\Feature\Departments\Ui;
use App\Models\Department;
use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
class CreateDepartmentsTest extends TestCase
{
public function testPermissionRequiredToCreateDepartment()
{
$this->actingAs(User::factory()->create())
->post(route('departments.store'), [
'name' => 'Test Department',
'company_id' => Company::factory()->create()->id
])
->assertForbidden();
}
public function testUserCanCreateDepartments()
{
$this->assertFalse(Department::where('name', 'Test Department')->exists());
$this->actingAs(User::factory()->superuser()->create())
->post(route('departments.store'), [
'name' => 'Test Department',
'company_id' => Company::factory()->create()->id
])
->assertRedirect(route('departments.index'));
$this->assertTrue(Department::where('name', 'Test Department')->exists());
}
}

View file

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

View file

@ -0,0 +1,61 @@
<?php
namespace Tests\Feature\Departments\Ui;
use App\Models\Department;
use App\Models\Category;
use App\Models\User;
use Tests\TestCase;
class UpdateDepartmentsTest extends TestCase
{
public function testPermissionRequiredToStoreDepartment()
{
$this->actingAs(User::factory()->create())
->post(route('departments.store'), [
'name' => 'Test Department',
'category_id' => Category::factory()->create()->id
])
->assertStatus(403)
->assertForbidden();
}
public function testUserCanCreateDepartments()
{
$this->assertFalse(Department::where('name', 'Test Department')->exists());
$this->actingAs(User::factory()->superuser()->create())
->post(route('departments.store'), [
'name' => 'Test Department',
'category_id' => Category::factory()->create()->id
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('departments.index'));
$this->assertTrue(Department::where('name', 'Test Department')->exists());
}
public function testUserCanEditDepartments()
{
$category = Category::factory()->forAssets()->create();
$department = Department::factory()->create(['name' => 'Test Department']);
$this->assertTrue(Department::where('name', 'Test Department')->exists());
$response = $this->actingAs(User::factory()->superuser()->create())
->put(route('departments.update', ['department' => $department]), [
'name' => 'Test Department Edited',
'category_id' => $department->category_id,
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('departments.index'));
$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Department::where('name', 'Test Department Edited')->exists());
}
}