Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #68 from joselfonseca/feature/improve-test-coverage
Browse files Browse the repository at this point in the history
Improve testing coverage.
  • Loading branch information
joselfonseca authored Oct 2, 2020
2 parents 9825b5c + 4e13115 commit c41c464
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 14 deletions.
1 change: 0 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Exceptions;

use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

Expand Down
5 changes: 1 addition & 4 deletions app/Http/Controllers/Api/Assets/UploadFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ protected function validateMime($mime)
*/
protected function validateBodySize($contentLength, $content)
{
if ($contentLength > config('files.maxsize', 1000000)) {
throw new BodyTooLargeException();
}
if (mb_strlen($content) > config('files.maxsize', 1000000)) {
if ($contentLength > config('files.maxsize', 1000000) || mb_strlen($content) > config('files.maxsize', 1000000)) {
throw new BodyTooLargeException();
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Kernel extends HttpKernel
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]
],
];

/**
Expand Down
1 change: 0 additions & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class Authenticate extends Middleware
{

}
2 changes: 1 addition & 1 deletion app/Http/Middleware/PreventRequestsDuringMaintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class PreventRequestsDuringMaintenance extends Middleware
protected $except = [
//
];
}
}
3 changes: 1 addition & 2 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/**
* @codeCoverageIgnore
* Class RedirectIfAuthenticated
* @package App\Http\Middleware
*/
class RedirectIfAuthenticated
{
Expand All @@ -33,4 +32,4 @@ public function handle($request, Closure $next, ...$guards)

return $next($request);
}
}
}
1 change: 1 addition & 0 deletions app/Providers/BroadcastServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class BroadcastServiceProvider extends ServiceProvider
{
/**
* @codeCoverageIgnore
* Bootstrap any application services.
*
* @return void
Expand Down
4 changes: 0 additions & 4 deletions app/Providers/ErrorHandlerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Dingo\Api\Exception\ResourceException;
use Dingo\Api\Routing\Helpers;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;

Expand Down Expand Up @@ -35,9 +34,6 @@ public function register()
app('Dingo\Api\Exception\Handler')->register(function (AuthenticationException $exception) {
return $this->response->errorUnauthorized('Unauthenticated.');
});
app('Dingo\Api\Exception\Handler')->register(function (ModelNotFoundException $exception) {
return $this->response->errorNotFound('404 Not Found');
});
app('Dingo\Api\Exception\Handler')->register(function (BodyTooLargeException $exception) {
return $this->response->error('The body is too large', 413);
});
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/Assets/UploadImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature\Assets;

use Illuminate\Http\UploadedFile;
use Tests\TestCase;
use App\Entities\User;
use Laravel\Passport\Passport;
Expand Down Expand Up @@ -153,4 +154,21 @@ function test_it_validates_url()
$this->assertEquals(422, $response->getStatusCode());
$this->assertArrayHasKey('Content-Type', $jsonResponse['errors']);
}

function test_it_validates_size_using_multipart_file()
{
Storage::fake();
Passport::actingAs(
factory(User::class)->create()
);
config()->set('files.maxsize', 10);
$file = UploadedFile::fake()->image('avatar.jpg')->size(1000);
$response = $this->post('api/assets', [
'file' => $file
]);
$jsonResponse = json_decode($response->getContent(), true);
$this->assertEquals(413, $response->getStatusCode());
$this->assertArrayHasKey('message', $jsonResponse);
$this->assertEquals('The body is too large', $jsonResponse['message']);
}
}
10 changes: 10 additions & 0 deletions tests/Feature/PingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ public function test_it_returns_ping()
$response->assertStatus(200);
$response->assertJson(['status' => 'ok']);
}

/**
*
*/
public function test_it_returns_404()
{
$response = $this->json('GET', 'api/non-existing-resource');
$response->assertStatus(404);
$response->assertJson(['message' => '404 Not Found', 'status_code' => 404]);
}
}
10 changes: 10 additions & 0 deletions tests/Feature/Users/UsersEndpointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,4 +311,14 @@ function test_it_deletes_users_roles_if_empty_array_sent()
$this->assertCount(0, $user->fresh()->roles);
}

function test_it_responds_404_if_user_not_found()
{
Passport::actingAs(User::first());
$roles = factory(Role::class, 2)->create();
$user = factory(User::class)->create()->syncRoles($roles);
$this->assertCount(2, $user->roles);
$response = $this->get('api/users/some-id-that-is-not-here');
$response->assertStatus(404);
}

}

0 comments on commit c41c464

Please sign in to comment.