mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-21 04:07:44 +02:00
Enhance unit tests coverage
This commit is contained in:
parent
f0200e62bd
commit
13f526ccb1
61
tests/Unit/IconTest.php
Normal file
61
tests/Unit/IconTest.php
Normal 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
99
tests/Unit/OtpTest.php
Normal 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
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
22
tests/Unit/RouteTest.php
Normal 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');
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
*/
|
||||
public function testMoreThanOneUserCreation()
|
||||
public function testUserCreationWithAnExistingUser()
|
||||
{
|
||||
$response = $this->json('POST', '/api/register', [
|
||||
'name' => 'testCreate',
|
||||
@ -49,6 +64,9 @@ class UserTest extends TestCase
|
||||
*/
|
||||
public function testUserCreationWithMissingValues()
|
||||
{
|
||||
// we delete the existing user
|
||||
User::destroy(1);
|
||||
|
||||
$response = $this->json('POST', '/api/register', [
|
||||
'name' => '',
|
||||
'email' => '',
|
||||
@ -56,7 +74,7 @@ class UserTest extends TestCase
|
||||
'password_confirmation' => '',
|
||||
]);
|
||||
|
||||
$response->assertStatus(400);
|
||||
$response->assertStatus(422);
|
||||
}
|
||||
|
||||
|
||||
@ -67,6 +85,9 @@ class UserTest extends TestCase
|
||||
*/
|
||||
public function testUserCreationWithInvalidData()
|
||||
{
|
||||
// we delete the existing user
|
||||
User::destroy(1);
|
||||
|
||||
$response = $this->json('POST', '/api/register', [
|
||||
'name' => 'testInvalid',
|
||||
'email' => 'email',
|
||||
@ -74,7 +95,7 @@ class UserTest extends TestCase
|
||||
'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
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user