Skip to content

Commit

Permalink
user only can follow others, not himself
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadmp97 committed Sep 18, 2023
1 parent 8c541e5 commit 0d685a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/Actions/User/FollowUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

use App\Models\User;
use App\Notifications\NewFollowerNotification;
use Symfony\Component\HttpKernel\Exception\HttpException;

class FollowUserAction
{
public function execute(User $follower, User $following): void
{
if ($follower->is($following)) {
throw new HttpException(400, 'No one follows himself!');
}

$userIsFollowed = $follower
->following()
->where('following_id', $following->id)
Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/UserFollowersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ public function test_user_unfollows_following_user()

$this->assertDatabaseEmpty('followers');
}

public function test_user_cannot_follow_himself()
{
$this
->postJson('api/users/1/followers')
->assertStatus(400);
}
}

0 comments on commit 0d685a8

Please sign in to comment.