Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Livewire/Item/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function submit()
]);

if (filled($this->private_content)) {
abort_unless(auth()->user()->hasAdminAccess(), 403);

$formState['content'] = $this->private_content;
$formState['private'] = true;
}
Expand Down
35 changes: 35 additions & 0 deletions tests/Feature/Livewire/Item/CommentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use App\Models\User;
use App\Models\Board;
use Livewire\Livewire;
use App\Enums\UserRole;
use App\Models\Comment;
use App\Models\Project;
use App\Livewire\Item\Comments;
Expand Down Expand Up @@ -62,3 +63,37 @@ function addVotedCommentThread(Item $item): void

expect($item->comments()->where('content', 'Blocked comment')->exists())->toBeFalse();
});

test('a normal user cannot create a private note through component state', function () {
$user = User::factory()->create();
$item = Item::factory()->create();

$this->actingAs($user);

Livewire::test(Comments::class, ['item' => $item])
->set('content', 'Valid public content')
->set('private_content', 'Forged private note')
->call('submit')
->assertForbidden();

expect($item->comments()->where('private', true)->exists())->toBeFalse();
});

test('staff can create a private note', function (UserRole $role) {
$staff = User::factory()->create(['role' => $role]);
$item = Item::factory()->create();

$this->actingAs($staff);

Livewire::test(Comments::class, ['item' => $item])
->set('private_content', 'Authorized private note')
->call('submit');

expect($item->comments()
->where('content', 'Authorized private note')
->where('private', true)
->exists())->toBeTrue();
})->with([
UserRole::Admin,
UserRole::Employee,
]);
Loading