Enhance unit tests coverage

This commit is contained in:
Bubka 2020-03-02 17:11:17 +01:00
parent f0200e62bd
commit 13f526ccb1
5 changed files with 373 additions and 3 deletions

61
tests/Unit/IconTest.php Normal file
View File

@ -0,0 +1,61 @@
<?php
namespace Tests\Unit;
use Illuminate\Http\UploadedFile;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Tests\TestCase;
class IconTest extends TestCase
{
use WithoutMiddleware;
/**
* test upload icon with no missing image resource via API
*
* @test
*/
public function testIconUploadWithMissingImage()
{
$response = $this->json('POST', '/api/icon/upload', [
'icon' => '',
])
->assertStatus(422);
}
/**
* test upload icon via API
*
* @test
*/
public function testIconUpload()
{
$file = UploadedFile::fake()->image('testIcon.jpg');
$response = $this->json('POST', '/api/icon/upload', [
'icon' => $file,
])
->assertStatus(201);
}
/**
* test delete an uploaded icon via API
*
* @test
*/
public function testIconDelete()
{
$response = $this->json('DELETE', '/api/icon/delete/testIcon.jpg')
->assertStatus(204);
}
}

99
tests/Unit/OtpTest.php Normal file
View File

@ -0,0 +1,99 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Classes\OTP;
use OTPHP\TOTP;
use OTPHP\HOTP;
class OtpTest extends TestCase
{
/**
* @test
*/
public function setUp(): void
{
parent::setUp();
}
/**
* test TOTP generation (with $isPreview = false to prevent db update)
*
* @test
*/
public function testTOTPGeneration()
{
$uri = 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW&issuer=test';
$result = OTP::generate($uri, true);
$this->assertArrayHasKey('otp', $result);
$this->assertArrayHasKey('position', $result);
}
/**
* test HOTP generation (with $isPreview = false to prevent db update)
*
* @test
*/
public function testHOTPGeneration()
{
$uri = 'otpauth://hotp/test:test@test.com?counter=16&secret=A4GRFHVIRBGY7UIW';
$result = OTP::generate($uri, true);
$this->assertArrayHasKey('otp', $result);
$this->assertArrayHasKey('counter', $result);
$this->assertArrayHasKey('nextUri', $result);
}
/**
* test if provided TOTP uri is valid
*
* @test
*/
public function testTOTPUriIsValid()
{
$uri = 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW&issuer=test';
$result = OTP::get($uri);
$this->assertInstanceOf(TOTP::class, $result);
}
/**
* test if provided HOTP uri is valid
*
* @test
*/
public function testHOTPUriIsValid()
{
$uri = 'otpauth://hotp/test:test@test.com?counter=16&secret=A4GRFHVIRBGY7UIW';
$result = OTP::get($uri);
$this->assertInstanceOf(HOTP::class, $result);
}
/**
* test invalid privded uri returns a ValidationException exception
*
* @test
*/
public function testInvalidUriReturnValidationException()
{
$uri = 'otpauth://totp/';
$this->expectException(\Illuminate\Validation\ValidationException::class);
OTP::get($uri);
}
}

81
tests/Unit/QrcodeTest.php Normal file

File diff suppressed because one or more lines are too long

22
tests/Unit/RouteTest.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
class RouteTest extends TestCase
{
/**
* test return main web view
*
* @test
*/
public function testLandingViewIsReturned()
{
$response = $this->get('/');
$response->assertViewIs('landing');
}
}

View File

@ -24,12 +24,27 @@ class UserTest extends TestCase
} }
/**
* test Existing user count via API
*
* @test
*/
public function testExistingUserCount()
{
$response = $this->json('POST', '/api/checkuser')
->assertStatus(200)
->assertJson([
'userCount' => '1',
]);
}
/** /**
* test creation of another user via API * test creation of another user via API
* *
* @test * @test
*/ */
public function testMoreThanOneUserCreation() public function testUserCreationWithAnExistingUser()
{ {
$response = $this->json('POST', '/api/register', [ $response = $this->json('POST', '/api/register', [
'name' => 'testCreate', 'name' => 'testCreate',
@ -49,6 +64,9 @@ class UserTest extends TestCase
*/ */
public function testUserCreationWithMissingValues() public function testUserCreationWithMissingValues()
{ {
// we delete the existing user
User::destroy(1);
$response = $this->json('POST', '/api/register', [ $response = $this->json('POST', '/api/register', [
'name' => '', 'name' => '',
'email' => '', 'email' => '',
@ -56,7 +74,7 @@ class UserTest extends TestCase
'password_confirmation' => '', 'password_confirmation' => '',
]); ]);
$response->assertStatus(400); $response->assertStatus(422);
} }
@ -67,6 +85,9 @@ class UserTest extends TestCase
*/ */
public function testUserCreationWithInvalidData() public function testUserCreationWithInvalidData()
{ {
// we delete the existing user
User::destroy(1);
$response = $this->json('POST', '/api/register', [ $response = $this->json('POST', '/api/register', [
'name' => 'testInvalid', 'name' => 'testInvalid',
'email' => 'email', 'email' => 'email',
@ -74,7 +95,7 @@ class UserTest extends TestCase
'password_confirmation' => 'tset', 'password_confirmation' => 'tset',
]); ]);
$response->assertStatus(400); $response->assertStatus(422);
} }
@ -173,6 +194,92 @@ class UserTest extends TestCase
} }
/**
* test User update with wrong current password via API
*
* @test
*/
public function testUserUpdateWithWrongCurrentPassword()
{
$user = User::find(1);
$response = $this->actingAs($user, 'api')
->json('PATCH', '/api/user', [
'name' => 'userUpdated',
'email' => 'userUpdated@example.org',
'password' => 'wrongPassword',
]);
$response->assertStatus(400)
->assertJsonStructure(['message']);
}
/**
* test User update via API
*
* @test
*/
public function testUserUpdate()
{
$user = User::find(1);
$response = $this->actingAs($user, 'api')
->json('PATCH', '/api/user', [
'name' => 'userUpdated',
'email' => 'userUpdated@example.org',
'password' => 'password',
]);
$response->assertStatus(200)
->assertJsonFragment([
'username' => 'userUpdated'
]);
}
/**
* test User password update with wrong current password via API
*
* @test
*/
public function testUserPasswordUpdateWithWrongCurrentPassword()
{
$user = User::find(1);
$response = $this->actingAs($user, 'api')
->json('PATCH', '/api/password', [
'currentPassword' => 'wrongPassword',
'password' => 'passwordUpdated',
'password_confirmation' => 'passwordUpdated',
]);
$response->assertStatus(400)
->assertJsonStructure(['message']);
}
/**
* test User password update via API
*
* @test
*/
public function testUserPasswordUpdate()
{
$user = User::find(1);
$response = $this->actingAs($user, 'api')
->json('PATCH', '/api/password', [
'currentPassword' => 'password',
'password' => 'passwordUpdated',
'password_confirmation' => 'passwordUpdated',
]);
$response->assertStatus(200)
->assertJsonStructure(['message']);
}
/** /**
* test User creation via API * test User creation via API
* *