Enhance test code coverage

This commit is contained in:
Bubka 2020-03-08 20:16:56 +01:00
parent 8927a4c7c0
commit 98c02a48dc
7 changed files with 179 additions and 5 deletions

View File

@ -101,10 +101,10 @@ private function handleApiException($request, Exception $exception)
*/ */
private function customApiResponse($exception, $debug) private function customApiResponse($exception, $debug)
{ {
$statusCode = 500;
if (method_exists($exception, 'getStatusCode')) { if (method_exists($exception, 'getStatusCode')) {
$statusCode = $exception->getStatusCode(); $statusCode = $exception->getStatusCode();
} else {
$statusCode = 500;
} }
$response = []; $response = [];

View File

@ -11,6 +11,8 @@ class BroadcastServiceProvider extends ServiceProvider
* Bootstrap any application services. * Bootstrap any application services.
* *
* @return void * @return void
*
* @codeCoverageIgnore unused, see config/app::Providers
*/ */
public function boot() public function boot()
{ {

View File

@ -54,6 +54,8 @@ protected static function boot()
* *
* @param string $value * @param string $value
* @return string * @return string
*
* @codeCoverageIgnore
*/ */
public function getIconAttribute($value) public function getIconAttribute($value)
{ {
@ -73,6 +75,8 @@ public function getIconAttribute($value)
* *
* @param string $value * @param string $value
* @return string * @return string
*
* @codeCoverageIgnore
*/ */
public function setIconAttribute($value) public function setIconAttribute($value)
{ {

View File

@ -84,7 +84,11 @@ public function testUserLoginWithMissingValues()
'password' => '' 'password' => ''
]); ]);
$response->assertStatus(422); $response->assertStatus(422)
->assertJsonValidationErrors([
'email',
'password'
]);
} }

View File

@ -0,0 +1,119 @@
<?php
namespace Tests\Unit;
use App\User;
use Tests\TestCase;
use App\Http\Controllers\TwoFAccountController;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\RequestGuard;
use Symfony\Component\HttpKernel\Exception\HttpException;
class ApiExceptionTest extends TestCase
{
/** @var \App\User */
protected $user;
/**
* @test
*/
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create();
}
/**
* test Unauthorized
*
* @test
*/
public function test_HTTP_UNAUTHORIZED()
{
$response = $this->json('GET', '/api/profile/settings')
->assertStatus(401)
->assertJson([
'message' => 'Unauthorized'
]);
}
/**
* test Unauthorized
*
* @test
*/
public function test_HTTP_FORBIDDEN()
{
}
/**
* test Not Found
*
* @test
*/
public function test_HTTP_NOT_FOUND()
{
$response = $this->actingAs($this->user, 'api')
->json('GET', '/api/twofaccounts/1000')
->assertStatus(404)
->assertJson([
'message' => 'Not Found'
]);
}
/**
* test Method Not Allowed
*
* @test
*/
public function test_HTTP_METHOD_NOT_ALLOWED()
{
$response = $this->actingAs($this->user, 'api')
->json('PATCH', '/api/profile/settings')
->assertStatus(405)
->assertJson([
'message' => 'Method Not Allowed'
]);
}
/**
* test Unprocessable entity
*
* @test
*/
public function test_HTTP_UNPROCESSABLE_ENTITY()
{
$response = $this->json('POST', '/api/login')
->assertStatus(422)
->assertJsonStructure([
'message',
'errors'
])
->assertJsonValidationErrors([
'email',
'password'
]);
}
/**
* test Internal Server error
*
* @test
*/
public function test_HTTP_INTERNAL_SERVER_ERROR()
{
}
}

View File

@ -2,20 +2,28 @@
namespace Tests\Unit; namespace Tests\Unit;
use App\User;
use Tests\TestCase; use Tests\TestCase;
use App\Classes\OTP; use App\Classes\OTP;
use OTPHP\TOTP; use OTPHP\TOTP;
use OTPHP\HOTP; use OTPHP\HOTP;
use App\TwoFAccount;
class OtpTest extends TestCase class OtpTest extends TestCase
{ {
/** @var \App\User */
protected $user;
/** /**
* @test * @test
*/ */
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->user = factory(User::class)->create();
} }
@ -36,11 +44,11 @@ public function testTOTPGeneration()
/** /**
* test HOTP generation (with $isPreview = false to prevent db update) * test HOTP generation for Preview
* *
* @test * @test
*/ */
public function testHOTPGeneration() public function testHOTPGenerationforPreview()
{ {
$uri = 'otpauth://hotp/test:test@test.com?counter=16&secret=A4GRFHVIRBGY7UIW'; $uri = 'otpauth://hotp/test:test@test.com?counter=16&secret=A4GRFHVIRBGY7UIW';
@ -52,6 +60,31 @@ public function testHOTPGeneration()
} }
/**
* test HOTP generation for existing HOTP account
*
* @test
*/
public function testHOTPGenerationForExistingAccount()
{
$response = $this->actingAs($this->user, 'api')
->json('POST', '/api/twofaccounts', [
'service' => 'hotp',
'account' => 'test.com',
'uri' => 'otpauth://hotp/test@test.com?counter=1&secret=A4GRFHZVRBGY7UIW',
'icon' => 'test.png',
]);
$testedAccount = TwoFAccount::where('service', 'hotp')->first();
$result = OTP::generate($testedAccount->uri, false);
$testedAccount->refresh();
$this->assertEquals($testedAccount->uri, 'otpauth://hotp/test@test.com?counter=2&secret=A4GRFHZVRBGY7UIW');
}
/** /**
* test if provided TOTP uri is valid * test if provided TOTP uri is valid
* *

View File

@ -20,4 +20,16 @@ public function testLandingViewIsReturned()
->assertViewIs('landing'); ->assertViewIs('landing');
} }
/**
* test return main web view
*
* @test
*/
public function testExceptionHandlerWithWebRoute()
{
$response = $this->post('/');
$response->assertStatus(405);
}
} }