Skip to content

Commit

Permalink
Merge branch 'feature/issue4' into develop
Browse files Browse the repository at this point in the history
Fix #4
  • Loading branch information
aronnebrivio committed Mar 21, 2021
2 parents 786ce63 + a072563 commit 678e923
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 31 deletions.
4 changes: 4 additions & 0 deletions app/Classes/ParseInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
* Original Gist at:
* https://gist.github.com/jas-/5c3fdc26fedd11cb9fb5#file-class-stream-php
*/

/**
* @codeCoverageIgnore
*/
class ParseInputStream
{
/**
Expand Down
14 changes: 6 additions & 8 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Exceptions;

use ErrorException;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
Expand All @@ -11,7 +10,6 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Throwable;
use UnexpectedValueException;

class Handler extends ExceptionHandler
{
Expand All @@ -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);
}
Expand All @@ -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
}
}
12 changes: 3 additions & 9 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();

Expand Down
22 changes: 13 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
});
});
37 changes: 37 additions & 0 deletions tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Tymon\JWTAuth\Facades\JWTAuth;

/**
* @internal
Expand All @@ -26,4 +27,40 @@ public function testLogin()
$this->post('auth/login', ['email' => '[email protected]', '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]);
}
}
25 changes: 25 additions & 0 deletions tests/MultipartFormDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\Models\Post;
use App\Models\User;
use Illuminate\Support\Str;

class MultipartFormDataTest extends TestCase
{
public function testMultipartPutRequest()
{
$user = User::factory()->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);
}
}
2 changes: 1 addition & 1 deletion tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 678e923

Please sign in to comment.