Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix abort response #6

Merged
merged 2 commits into from
Sep 19, 2023
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
3 changes: 2 additions & 1 deletion app/Actions/Challenge/CreateChallengeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\ChallengeStatus;
use App\Events\ChallengeCreated;
use App\Exceptions\UserHaveUnCompletedChallengeException;
use App\Models\Challenge;
use App\Models\User;

Expand All @@ -16,7 +17,7 @@ public function execute(User $user, array $data): Challenge
->where('status', ChallengeStatus::ONGOING->value);

if ($ongoingChallenge->exists()) {
abort(400, 'You have an uncompleted challenge!');
throw new UserHaveUnCompletedChallengeException();
}

$data = array_merge($data, [
Expand Down
3 changes: 2 additions & 1 deletion app/Actions/User/LoginUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\User;

use App\Exceptions\UserNotFoundException;
use App\Models\DeactivationRequest;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
Expand All @@ -13,7 +14,7 @@ public function execute(array $data): string
$user = User::firstWhere('email', $data['email']);

if (! $user || ! Hash::check($data['password'], $user->password)) {
abort(401, 'User not found!');
throw new UserNotFoundException();
}

// Logging into an account could save it from deletion
Expand Down
10 changes: 10 additions & 0 deletions app/Exceptions/UserHaveUnCompletedChallengeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class UserHaveUnCompletedChallengeException extends Exception
{
//
}
10 changes: 10 additions & 0 deletions app/Exceptions/UserNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Exceptions;

use Exception;

class UserNotFoundException extends Exception
{
//
}
9 changes: 8 additions & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Actions\User\CreateUserAction;
use App\Actions\User\LoginUserAction;
use App\Actions\User\LogoutUserAction;
use App\Exceptions\UserNotFoundException;
use App\Http\Requests\ChangePasswordRequest;
use App\Http\Requests\UserLoginRequest;
use App\Http\Requests\UserRegistrationRequest;
Expand All @@ -28,7 +29,13 @@ public function register(UserRegistrationRequest $request, CreateUserAction $cre

public function login(UserLoginRequest $request, LoginUserAction $loginUserAction)
{
$token = $loginUserAction->execute($request->validated());
try {
$token = $loginUserAction->execute($request->validated());
} catch (UserNotFoundException) {
return $this->unauthorized([
'message' => 'Authentication failed',
]);
}

return $this->ok(
UserTokenResource::make($token)
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Controllers/ChallengesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\Challenge\CreateChallengeAction;
use App\Actions\Challenge\StopChallengeAction;
use App\Exceptions\UserHaveUnCompletedChallengeException;
use App\Http\Requests\CreateChallengeRequest;
use App\Http\Resources\ChallengeResource;
use App\Models\Challenge;
Expand Down Expand Up @@ -32,7 +33,13 @@ public function index()

public function store(CreateChallengeRequest $request, CreateChallengeAction $createChallengeAction)
{
$challenge = $createChallengeAction->execute($request->user(), $request->validated());
try {
$challenge = $createChallengeAction->execute($request->user(), $request->validated());
} catch (UserHaveUnCompletedChallengeException) {
return $this->badRequest([
'message' => 'You have uncompleted challenge',
]);
}

return $this->created(
ChallengeResource::make($challenge)
Expand Down
10 changes: 10 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function ok($resource = null): JsonResponse
return $this->respondResourceWithStatusCode($resource, Response::HTTP_OK);
}

public function badRequest($resource = null): JsonResponse
{
return $this->respondResourceWithStatusCode($resource, Response::HTTP_BAD_REQUEST);
}

public function unauthorized($resource = null): JsonResponse
{
return $this->respondResourceWithStatusCode($resource, Response::HTTP_UNAUTHORIZED);
}

protected function respondResourceWithStatusCode($resource, $statusCode): JsonResponse
{
if (is_array($resource)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ChallengeCommentsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function test_user_leaves_a_comment()
$user = User::factory()->create([
'email' => config('hope.hope_bot_mail'),
]);

$this->signIn($user);

$response = $this->postJson('api/challenges/1/comments', [
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/ChallengeLikesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ChallengeLikesControllerTest extends TestCase
use RefreshDatabase;

private $user;

private $challenge;

public function setUp(): void
Expand All @@ -29,7 +30,7 @@ public function setUp(): void
$this->user = $this->signIn();

$this->challenge = Challenge::factory()->create([
'user_id' => (User::factory()->create())->id
'user_id' => (User::factory()->create())->id,
]);
}

Expand Down
1 change: 0 additions & 1 deletion tests/Feature/ContinueChallengeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public function test_user_completes_a_challenge()
'created_at' => '2022-01-01 21:00:00',
]);


$this->travelTo('2023-01-31 21:01:00');

$this->postJson('api/challenges/1/continue');
Expand Down
Loading