Skip to content

Commit

Permalink
Merge branch 'release/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
aronnebrivio committed Mar 21, 2021
2 parents ca12bc6 + 0a2daf2 commit c9f8ed2
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 42 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ APIs will be available at `http://localhost:PHP_HOST_PORT`, where `PHP_HOST_PORT
You can enable or disable XDebug using the `XDEBUG_MODE` environment variable.

## Test
Tests are under `/tests` folder, run it with `phpunit`.
Tests are under `/tests` folder, run it with `phpunit`:
```bash
docker-compose run --rm blog-lumen /var/www/html/vendor/bin/phpunit --coverage-html /tmp --colors=always -c /var/www/html/phpunit.xml --testsuite 'Application Test Suite'
```


## ToDo
- [x] Upgrade to Lumen 8.x
- [x] Upgrade to Composer 3
- [x] Automated PHP-CS-Fixer
- [x] Redis cache
- [x] Clean up local Docker environment
- [X] Review CI Docker environment
- [X] Use Github workflow
- [X] Setup a simple production environment
- [X] Containerized infrastructure for server
- [x] Review CI Docker environment
- [x] Use Github workflow
- [x] Setup a simple production environment
- [x] Containerized infrastructure for server
- [ ] Makefile
- [ ] Pagination

Expand Down
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"laravel",
"lumen"
],
"version": "1.1.0",
"version": "1.1.1",
"license": "MIT",
"type": "project",
"require": {
Expand Down
26 changes: 13 additions & 13 deletions composer.lock

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

12 changes: 7 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
});

$router->get('version', function () {
return response('1.1.0', 200);
return response('1.1.1', 200);
});

$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 c9f8ed2

Please sign in to comment.