A free open source IT asset/license management system
Add tests for adding notes to assets
Marcus Moore47763d1e1a96
diff --git a/app/Http/Controllers/Api/NotesController.php b/app/Http/Controllers/Api/NotesController.phpindex f1f8f62144..1206445c37 100644--- a/app/Http/Controllers/Api/NotesController.php+++ b/app/Http/Controllers/Api/NotesController.php@@ -31,4 +31,4 @@ class NotesController extends Controllerreturn response()->json(Helper::formatStandardApiResponse('success'));}-}\ No newline at end of file+}diff --git a/tests/Feature/Notes/AssetNotesTest.php b/tests/Feature/Notes/AssetNotesTest.phpnew file mode 100644index 0000000000..c666cb9e3f--- /dev/null+++ b/tests/Feature/Notes/AssetNotesTest.php@@ -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);+ });+ }+}diff --git a/tests/Unit/Listeners/LogListenerTest.php b/tests/Unit/Listeners/LogListenerTest.phpindex 011a5c51a4..e4460c2c24 100644--- a/tests/Unit/Listeners/LogListenerTest.php+++ b/tests/Unit/Listeners/LogListenerTest.php@@ -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!',+ ]);+ }}