Complete phpunit tests update

This commit is contained in:
Bubka 2020-11-17 21:56:06 +01:00
parent d9b48e8806
commit fc05d41120
4 changed files with 19 additions and 154 deletions

View File

@ -91,6 +91,12 @@ public function testTwoFAccountUpdateOnProtectedDB()
->json('PUT', '/api/twofaccounts/' . $this->twofaccount->id, [
'service' => 'testUpdate',
'account' => 'testUpdate@test.com',
'otpType' => 'totp',
'secret' => 'A4GRFHVVRBGY7UIW',
'secretIsBase32Encoded' => 1,
'digits' => 8,
'totpPeriod' => 30,
'algorithm' => 'sha256',
])
->assertStatus(200)
->assertJsonFragment([

View File

@ -115,7 +115,7 @@ public function test_HTTP_INTERNAL_SERVER_ERROR()
'debug'
])
->assertJsonFragment([
'message' => 'Whoops, looks like something went wrong'
'message' => 'Whoops, looks like something went wrong :('
]);
}

View File

@ -1,132 +0,0 @@
<?php
namespace Tests\Unit;
use App\User;
use Tests\TestCase;
use App\Classes\OTP;
use OTPHP\TOTP;
use OTPHP\HOTP;
use App\TwoFAccount;
class OtpTest extends TestCase
{
/** @var \App\User */
protected $user;
/**
* @test
*/
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create();
}
/**
* 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 for Preview
*
* @test
*/
public function testHOTPGenerationforPreview()
{
$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 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
*/
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);
}
}

View File

@ -62,21 +62,17 @@ public function testDecodeInvalidQrcode()
public function testDecodeValidUri()
{
$response = $this->json('POST', '/api/qrcode/decode', [
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW'
'uri' => 'otpauth://totp/service:account?secret=A4GRFHVIRBGY7UIW'
]);
$response->assertStatus(200)
->assertJsonFragment([
'service' => 'test@test.com',
'account' => '',
'options' => [
'algorithm' => 'sha1',
'digits' => 6,
'epoch' => 0,
'period' => 30,
'secret' => 'A4GRFHVIRBGY7UIW'
],
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW'
'service' => 'service',
'account' => 'account',
'algorithm' => 'sha1',
'digits' => 6,
'totpPeriod' => 30,
'secret' => 'A4GRFHVIRBGY7UIW'
]);
}
@ -100,16 +96,11 @@ public function testDecodeValidQrcode()
$response->assertStatus(200)
->assertJsonFragment([
'service' => 'test@test.com',
'account' => '',
'options' => [
'algorithm' => 'sha1',
'digits' => 6,
'epoch' => 0,
'period' => 30,
'secret' => 'A4GRFHVIRBGY7UIW'
],
'uri' => 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW'
'account' => 'test@test.com',
'algorithm' => 'sha1',
'digits' => 6,
'totpPeriod' => 30,
'secret' => 'A4GRFHVIRBGY7UIW'
]);
}