diff --git a/app/Classes/ParseInputStream.php b/app/Classes/ParseInputStream.php index e691315..a62c398 100644 --- a/app/Classes/ParseInputStream.php +++ b/app/Classes/ParseInputStream.php @@ -24,6 +24,10 @@ * Original Gist at: * https://gist.github.com/jas-/5c3fdc26fedd11cb9fb5#file-class-stream-php */ + +/** + * @codeCoverageIgnore + */ class ParseInputStream { /** diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 8725fb8..a86c82e 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,7 +2,6 @@ namespace App\Exceptions; -use ErrorException; use Exception; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -11,7 +10,6 @@ use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Throwable; -use UnexpectedValueException; class Handler extends ExceptionHandler { @@ -38,9 +36,12 @@ class Handler extends ExceptionHandler */ public function report(Throwable $e) { + // Ignoring this block because only applies on production environment + // @codeCoverageIgnoreStart if (app()->environment('production') && app()->bound('sentry') && $this->shouldReport($e)) { app('sentry')->captureException($e); } + // @codeCoverageIgnoreEnd parent::report($e); } @@ -61,19 +62,16 @@ public function render($request, Throwable $e) if ($e instanceof MethodNotAllowedHttpException) { return response('Method Not Allowed.', 405); } - if ($e instanceof UnexpectedValueException) { - return response('Unexpected value.', 422); - } if ($e instanceof ModelNotFoundException) { return response('The resource you are looking for is not available or does not belong to you.', 404); } - if ($e instanceof ErrorException) { - return response('Unprocessable. Please provide all inputs and retry.', 422); - } if ($e instanceof AuthorizationException) { return response($e->getMessage(), 401); } + // Ignoring this block because only applies if an error is not handled (like 500 server errors) + // @codeCoverageIgnoreStart return response($e->getMessage(), $e->getCode() ?: 500); + // @codeCoverageIgnoreEnd } } diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 6a4f1a7..19e9919 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -9,14 +9,6 @@ class AuthController extends BaseController { - /** - * Create a new AuthController instance. - */ - public function __construct() - { - $this->middleware('auth:api', ['except' => ['login']]); - } - /** * Get a JWT via given credentials. * @@ -53,9 +45,11 @@ public function me() /** * Log the user out (Invalidate the token). * + * @param Request $request + * * @return JsonResponse */ - public function logout() + public function logout(Request $request) { Auth::logout(); diff --git a/composer.lock b/composer.lock index 3a89f8f..8bdaeb6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "403ada2e5348aef3aafa558e21c09cc8", + "content-hash": "74b814e128f39a6a69664353d3de7e4f", "packages": [ { "name": "brick/math", @@ -6827,16 +6827,16 @@ }, { "name": "tymon/jwt-auth", - "version": "1.0.2", + "version": "dev-develop", "source": { "type": "git", "url": "https://github.com/tymondesigns/jwt-auth.git", - "reference": "e588cb719539366c0e2f6017f975379cb73e9680" + "reference": "ab00f2d7cce5f043067aef7849cdc792de2df635" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/e588cb719539366c0e2f6017f975379cb73e9680", - "reference": "e588cb719539366c0e2f6017f975379cb73e9680", + "url": "https://api.github.com/repos/tymondesigns/jwt-auth/zipball/ab00f2d7cce5f043067aef7849cdc792de2df635", + "reference": "ab00f2d7cce5f043067aef7849cdc792de2df635", "shasum": "" }, "require": { @@ -6847,15 +6847,17 @@ "lcobucci/jwt": "<3.4", "namshi/jose": "^7.0", "nesbot/carbon": "^1.0|^2.0", - "php": "^5.5.9|^7.0" + "php": "^7.2|^8.0" }, "require-dev": { "illuminate/console": "^5.2|^6|^7|^8", "illuminate/database": "^5.2|^6|^7|^8", "illuminate/routing": "^5.2|^6|^7|^8", "mockery/mockery": ">=0.9.9", - "phpunit/phpunit": "~4.8|~6.0" + "phpunit/phpunit": "^8.5|^9.4", + "yoast/phpunit-polyfills": "^0.2.0" }, + "default-branch": true, "type": "library", "extra": { "branch-alias": { @@ -6907,7 +6909,7 @@ "type": "patreon" } ], - "time": "2020-11-27T12:32:42+00:00" + "time": "2021-02-02T14:44:28+00:00" }, { "name": "vlucas/phpdotenv", @@ -10074,7 +10076,9 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": [], + "stability-flags": { + "tymon/jwt-auth": 20 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/routes/web.php b/routes/web.php index d6fab4e..6fad3a2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -17,11 +17,7 @@ $router->group(['prefix' => 'auth'], function () use ($router) { $router->post('register', UserController::class . '@new'); - $router->post('login', AuthController::class . '@login'); - $router->post('logout', AuthController::class . '@logout'); - $router->post('refresh', AuthController::class . '@refresh'); - $router->get('me', AuthController::class . '@me'); }); $router->group(['prefix' => 'users'], function () use ($router) { @@ -54,4 +50,10 @@ $router->group(['prefix' => 'users'], function () use ($router) { $router->put('{id}', UserController::class . '@update'); }); + + $router->group(['prefix' => 'auth'], function () use ($router) { + $router->post('logout', AuthController::class . '@logout'); + $router->post('refresh', AuthController::class . '@refresh'); + $router->get('me', AuthController::class . '@me'); + }); }); diff --git a/tests/AuthTest.php b/tests/AuthTest.php index f590a96..776f166 100644 --- a/tests/AuthTest.php +++ b/tests/AuthTest.php @@ -2,6 +2,7 @@ use App\Models\User; use Illuminate\Support\Facades\Hash; +use Tymon\JWTAuth\Facades\JWTAuth; /** * @internal @@ -26,4 +27,40 @@ public function testLogin() $this->post('auth/login', ['email' => 'wrong@email.com', 'password' => $password]) ->seeStatusCode(401); } + + public function testLogout() + { + $this->refreshApplication(); + + $user = User::factory()->create(); + // NOTE: in order to make logout() function working we have to pass the JWT token -> can't use standard actingAs function + $token = JWTAuth::fromUser($user); + + $this->post('auth/logout', [], ['Authorization' => 'Bearer ' . $token]) + ->seeStatusCode(200) + ->seeJson(['message' => 'Successfully logged out']); + } + + public function testRefresh() + { + $this->refreshApplication(); + + $user = User::factory()->create(); + // NOTE: in order to make logout() function working we have to pass the JWT token -> can't use standard actingAs function + $token = JWTAuth::fromUser($user); + + $this->post('auth/refresh', [], ['Authorization' => 'Bearer ' . $token]) + ->seeStatusCode(200) + ->seeJson(['token_type' => 'bearer']); + } + + public function testMe() + { + $user = User::factory()->create(); + $this->actingAs($user); + + $this->get('auth/me') + ->seeStatusCode(200) + ->seeJson(['id' => $user->id, 'email' => $user->email]); + } } diff --git a/tests/MultipartFormDataTest.php b/tests/MultipartFormDataTest.php new file mode 100644 index 0000000..dfefa50 --- /dev/null +++ b/tests/MultipartFormDataTest.php @@ -0,0 +1,25 @@ +create(); + $post = Post::factory()->create([ + 'user_id' => $user->id, + ]); + $newText = Str::random(300); + + $this->actingAs($user); + $this->put( + 'posts/' . $post->id, + ['text' => $newText], + ['Content-Type' => 'multipart/form-data'] + ) + ->seeStatusCode(200); + } +} diff --git a/tests/UserTest.php b/tests/UserTest.php index a32c74f..66251a6 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -47,7 +47,7 @@ public function testUserUpdate() // NOTE: in order to make logout() function working we have to pass the JWT token -> can't use standard actingAs function $token = JWTAuth::fromUser($user); - $this->put('users/' . $user->id . '?token=' . $token, ['email' => $email, 'password' => $password]) + $this->put('users/' . $user->id, ['email' => $email, 'password' => $password], ['Authorization' => 'Bearer ' . $token]) ->seeStatusCode(200); }