firefly-iii/tests/Feature/Controllers/Auth/TwoFactorControllerTest.php

123 lines
4.4 KiB
PHP
Raw Normal View History

2017-02-12 10:58:16 -06:00
<?php
/**
* TwoFactorControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
2017-03-25 07:41:17 -05:00
declare(strict_types=1);
2017-02-12 10:58:16 -06:00
namespace Tests\Feature\Controllers\Auth;
use FireflyIII\Models\Preference;
2017-03-25 07:41:17 -05:00
use PragmaRX\Google2FA\Contracts\Google2FA;
2017-02-12 10:58:16 -06:00
use Preferences;
use Tests\TestCase;
2017-03-12 03:22:33 -05:00
/**
* Class TwoFactorControllerTest
*
* @package Tests\Feature\Controllers\Auth
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-12 03:22:33 -05:00
*/
2017-02-12 10:58:16 -06:00
class TwoFactorControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::index
*/
public function testIndex()
{
$this->be($this->user());
2017-03-25 07:41:17 -05:00
$truePref = new Preference;
$truePref->data = true;
2017-02-12 10:58:16 -06:00
$secretPreference = new Preference;
$secretPreference->data = 'BlablaSeecret';
2017-03-25 07:41:17 -05:00
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePref)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once();
2017-02-12 10:58:16 -06:00
$response = $this->get(route('two-factor.index'));
$response->assertStatus(200);
}
2017-03-25 07:41:17 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::index
*/
public function testIndexNo2FA()
{
$this->be($this->user());
$falsePreference = new Preference;
$falsePreference->data = false;
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($falsePreference)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn(null)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn(null)->once();
2017-03-25 07:41:17 -05:00
$response = $this->get(route('two-factor.index'));
$response->assertStatus(302);
$response->assertRedirect(route('index'));
}
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::index
* @expectedExceptionMessage Your two factor authentication secret is empty
*/
public function testIndexNoSecret()
{
$this->be($this->user());
$truePref = new Preference;
$truePref->data = true;
$secretPreference = new Preference;
$secretPreference->data = '';
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePref)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once();
2017-03-25 07:41:17 -05:00
$response = $this->get(route('two-factor.index'));
$response->assertStatus(500);
}
2017-02-12 10:58:16 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::lostTwoFactor
*/
public function testLostTwoFactor()
{
$this->be($this->user());
$truePreference = new Preference;
$truePreference->data = true;
$secretPreference = new Preference;
$secretPreference->data = 'BlablaSeecret';
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePreference);
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference);
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference);
2017-02-12 10:58:16 -06:00
$response = $this->get(route('two-factor.lost'));
$response->assertStatus(200);
}
2017-03-25 07:41:17 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::postIndex
*/
public function testPostIndex()
{
2017-07-07 23:28:44 -05:00
$data = ['code' => '123456'];
2017-03-25 07:41:17 -05:00
$google = $this->mock(Google2FA::class);
$google->shouldReceive('verifyKey')->andReturn(true)->once();
$this->be($this->user());
$response = $this->post(route('two-factor.post'), $data);
$response->assertStatus(302);
}
2017-02-16 15:33:32 -06:00
}