Expanded test coverage.

This commit is contained in:
James Cole
2017-03-24 11:07:38 +01:00
parent 398cf0b312
commit 222b3008d5
14 changed files with 651 additions and 121 deletions

View File

@@ -7,7 +7,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace Tests\Feature\Controllers;
@@ -15,8 +15,10 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use PragmaRX\Google2FA\Contracts\Google2FA;
use Preferences;
use Tests\TestCase;
/**
@@ -81,6 +83,34 @@ class PreferencesControllerTest extends TestCase
$response->assertSee('<ol class="breadcrumb">');
}
/**
*
*/
public function testPostCode()
{
$secret = '0123456789abcde';
$key = '123456';
$google = $this->mock(Google2FA::class);
$this->withoutMiddleware();
$this->session(['two-factor-secret' => $secret]);
Preferences::shouldReceive('set')->withArgs(['twoFactorAuthEnabled', 1])->once();
Preferences::shouldReceive('set')->withArgs(['twoFactorAuthSecret', $secret])->once();
Preferences::shouldReceive('mark')->once();
$google->shouldReceive('verifyKey')->withArgs([$secret, $key])->andReturn(true);
$data = [
'code' => $key,
];
$this->be($this->user());
$response = $this->post(route('preferences.code.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\PreferencesController::postIndex
*/
@@ -88,11 +118,13 @@ class PreferencesControllerTest extends TestCase
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$userRepos->shouldReceive('hasRole')->andReturn(false);
$data = [
'fiscalYearStart' => '2016-01-01',
'frontPageAccounts' => [],
'frontPageAccounts' => [1],
'viewRange' => '1M',
'customFiscalYear' => 0,
'showDepositsFrontpage' => 0,
@@ -109,4 +141,80 @@ class PreferencesControllerTest extends TestCase
$response->assertRedirect(route('preferences.index'));
}
/**
* User wants 2FA and has secret already.
*
* @covers \FireflyIII\Http\Controllers\PreferencesController::postIndex
*/
public function testPostIndexWith2FA()
{
$this->withoutMiddleware();
// mock stuff
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->andReturn(false);
// mock preferences (in a useful way?)
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn('12345');
Preferences::shouldReceive('set');
Preferences::shouldReceive('mark');
$data = [
'fiscalYearStart' => '2016-01-01',
'frontPageAccounts' => [1],
'viewRange' => '1M',
'customFiscalYear' => 0,
'showDepositsFrontpage' => 0,
'transactionPageSize' => 100,
'twoFactorAuthEnabled' => 1,
'language' => 'en_US',
'tj' => [],
];
$this->be($this->user());
$response = $this->post(route('preferences.update'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
// go to code to get a secret.
$response->assertRedirect(route('preferences.index'));
}
/**
* User wants 2FA and has no secret.
*
* @covers \FireflyIII\Http\Controllers\PreferencesController::postIndex
*/
public function testPostIndexWithEmpty2FA()
{
$this->withoutMiddleware();
// mock stuff
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->andReturn(false);
// mock preferences (in a useful way?)
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn(null);
Preferences::shouldReceive('set');
Preferences::shouldReceive('mark');
$data = [
'fiscalYearStart' => '2016-01-01',
'frontPageAccounts' => [1],
'viewRange' => '1M',
'customFiscalYear' => 0,
'showDepositsFrontpage' => 0,
'transactionPageSize' => 100,
'twoFactorAuthEnabled' => 1,
'language' => 'en_US',
'tj' => [],
];
$this->be($this->user());
$response = $this->post(route('preferences.update'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
// go to code to get a secret.
$response->assertRedirect(route('preferences.code'));
}
}