Add tests for adding notes to assets

This commit is contained in:
Marcus Moore 2024-09-26 12:13:36 -07:00
parent b813dcd9d0
commit 47763d1e1a
No known key found for this signature in database
3 changed files with 94 additions and 1 deletions

View file

@ -31,4 +31,4 @@ class NotesController extends Controller
return response()->json(Helper::formatStandardApiResponse('success'));
}
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace Tests\Feature\Notes;
use App\Events\NoteAdded;
use App\Models\Asset;
use App\Models\User;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;
class AssetNotesTest extends TestCase
{
public function testRequiresPermission()
{
$asset = Asset::factory()->create();
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.notes.store'), [
'note' => 'New Note!',
'type' => 'asset',
'id' => $asset->id,
])
->assertForbidden();
}
public function testValidation()
{
$asset = Asset::factory()->create();
$this->actingAsForApi(User::factory()->editAssets()->create())
->postJson(route('api.notes.store'), [
// 'note' => '',
'type' => 'a_type_not_asset',
'id' => $asset->id,
])
->assertOk()
->assertStatusMessageIs('error')
->assertJsonValidationErrors(['note', 'type'], 'messages');
}
public function testRequiresExistingAsset()
{
$this->actingAsForApi(User::factory()->editAssets()->create())
->postJson(route('api.notes.store'), [
'note' => 'New Note!',
'type' => 'asset',
'id' => 999_999,
])
->assertStatusMessageIs('error')
->assertMessagesAre('Asset not found');
}
public function testCanAddNoteToAsset()
{
Event::fake([NoteAdded::class]);
$asset = Asset::factory()->create();
$user = User::factory()->editAssets()->create();
$this->actingAsForApi($user)
->postJson(route('api.notes.store'), [
'note' => 'New Note!',
'type' => 'asset',
'id' => $asset->id,
])
->assertOk()
->assertStatusMessageIs('success');
Event::assertDispatchedTimes(NoteAdded::class, 1);
Event::assertDispatched(NoteAdded::class, function (NoteAdded $event) use ($asset, $user) {
return $event->itemNoteAddedOn->is($asset)
&& $event->note === 'New Note!'
&& $event->noteAddedBy->is($user);
});
}
}

View file

@ -3,6 +3,7 @@
namespace Tests\Unit\Listeners;
use App\Events\CheckoutableCheckedOut;
use App\Events\NoteAdded;
use App\Listeners\LogListener;
use App\Models\Asset;
use App\Models\User;
@ -36,4 +37,20 @@ class LogListenerTest extends TestCase
'note' => 'A simple note...',
]);
}
public function testLogsEntryOnAssetNoteCreation()
{
$asset = Asset::factory()->create();
$noteAddedBy = User::factory()->create();
event(new NoteAdded($asset, $noteAddedBy, 'My Cool Note!'));
$this->assertDatabaseHas('action_logs', [
'action_type' => 'note_added',
'user_id' => $noteAddedBy->id,
'item_id' => $asset->id,
'item_type' => Asset::class,
'note' => 'My Cool Note!',
]);
}
}