snipe-it/tests/Feature/Notes/CreateNotesTest.php

71 lines
2 KiB
PHP
Raw Normal View History

2025-02-18 16:16:00 -08:00
<?php
namespace Tests\Feature\Notes;
2025-02-18 16:24:52 -08:00
use App\Models\Asset;
use App\Models\User;
2025-02-18 16:16:00 -08:00
use Tests\TestCase;
class CreateNotesTest extends TestCase
{
public function testRequiresPermission()
{
2025-02-18 16:24:52 -08:00
$this->actingAs(User::factory()->create())
->post(route('notes.store'))
->assertForbidden();
2025-02-18 16:16:00 -08:00
}
public function testValidation()
{
2025-02-20 12:49:58 -08:00
$asset = Asset::factory()->create();
$this->actingAs(User::factory()->editAssets()->create())
->post(route('notes.store'), [
'id' => $asset->id,
// should be more...
])
->assertSessionHas('errors');
}
public function testAssetMustExist()
{
$this->actingAs(User::factory()->editAssets()->create())
->post(route('notes.store'), [
'id' => 999_999,
'type' => 'asset',
'note' => 'my note',
])
2025-02-20 13:24:01 -08:00
->assertStatus(302);
2025-02-18 16:16:00 -08:00
}
2025-02-20 12:40:05 -08:00
public function testCanCreateNoteForAsset()
2025-02-18 16:16:00 -08:00
{
2025-02-18 16:24:52 -08:00
$actor = User::factory()->editAssets()->create();
$asset = Asset::factory()->create();
$this->actingAs($actor)
2025-02-20 12:40:05 -08:00
->withHeader('User-Agent', 'Custom User Agent For Test')
2025-02-18 16:24:52 -08:00
->post(route('notes.store'), [
2025-02-20 12:40:05 -08:00
'_token' => '_token-to-simulate-request-from-gui',
2025-02-18 16:24:52 -08:00
'id' => $asset->id,
'type' => 'asset',
'note' => 'my special note',
])
2025-02-20 12:41:56 -08:00
->assertRedirect(route('hardware.show', $asset->id) . '#history')
->assertSessionHas('success', trans('general.note_added'));
2025-02-18 16:24:52 -08:00
2025-02-20 12:40:05 -08:00
$this->assertDatabaseHas('action_logs', [
'created_by' => $actor->id,
'action_type' => 'note added',
'target_id' => null,
'target_type' => null,
'note' => 'my special note',
'item_type' => Asset::class,
'item_id' => $asset->id,
'action_source' => 'gui',
'user_agent' => 'Custom User Agent For Test',
]);
2025-02-18 16:16:00 -08:00
}
}