From 2147caf3efd58f22c74fba1b2d36409739481586 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 4 Aug 2019 11:12:24 +0200 Subject: [PATCH] Fix tests --- app/Http/Controllers/ProfileController.php | 4 ++- .../Controllers/ProfileControllerTest.php | 28 ++++++++++++++++--- tests/Unit/Factory/TagFactoryTest.php | 2 +- .../Actions/ConvertToTransferTest.php | 6 ++-- .../RecurrenceTransformerTest.php | 3 +- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 789912a8be..255d4359d6 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -438,7 +438,9 @@ class ProfileController extends Controller app('preferences')->mark(); // make sure MFA is logged out. - Google2FA::logout(); + if ('testing' !== config('app.env')) { + Google2FA::logout(); + } return redirect(route('profile.index')); } diff --git a/tests/Feature/Controllers/ProfileControllerTest.php b/tests/Feature/Controllers/ProfileControllerTest.php index bb741460a9..3161c70ffb 100644 --- a/tests/Feature/Controllers/ProfileControllerTest.php +++ b/tests/Feature/Controllers/ProfileControllerTest.php @@ -24,6 +24,7 @@ namespace Tests\Feature\Controllers; use Amount; use FireflyIII\Models\Preference; +use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; use Google2FA; @@ -97,6 +98,8 @@ class ProfileControllerTest extends TestCase $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true); $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->atLeast()->once()->andReturn(false); + // set recovery codes. + Preferences::shouldReceive('set')->withArgs(['mfa_recovery', Mockery::any()])->atLeast()->once(); Google2FA::shouldReceive('generateSecretKey')->andReturn('secret'); Google2FA::shouldReceive('getQRCodeInline')->andReturn('long-data-url'); @@ -168,6 +171,9 @@ class ProfileControllerTest extends TestCase // mock stuff $userRepos = $this->mock(UserRepositoryInterface::class); $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->atLeast()->once()->andReturn(false); + $userRepos->shouldReceive('setMFACode')->withArgs([Mockery::any(), null])->atLeast()->once(); + + $this->be($this->user()); $response = $this->get(route('profile.delete-code')); $response->assertStatus(302); @@ -199,12 +205,14 @@ class ProfileControllerTest extends TestCase //$this->mockDefaultSession(); // DISABLED ON PURPOSE $this->mockDefaultConfiguration(); $repository = $this->mock(UserRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + + $journalRepos->shouldReceive('firstNull')->andReturnNull(); + $euro = $this->getEuro(); $repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->times(1)->andReturn(false); - Preferences::shouldReceive('set')->once()->withArgs(['twoFactorAuthEnabled', 1]); - $view = new Preference; $view->data = '1M'; Preferences::shouldReceive('get')->withArgs(['viewRange', Mockery::any()])->andReturn($view)->atLeast()->once(); @@ -224,7 +232,7 @@ class ProfileControllerTest extends TestCase $this->be($this->user()); $response = $this->post(route('profile.enable2FA')); $response->assertStatus(302); - $response->assertRedirect(route('profile.index')); + $response->assertRedirect(route('profile.code')); } /** @@ -241,8 +249,14 @@ class ProfileControllerTest extends TestCase $pref = new Preference; $pref->data = 'token'; + + + Preferences::shouldReceive('get')->withArgs(['access_token', null])->atLeast()->once()->andReturn($pref); + $arrayPref = new Preference; + $arrayPref->data = []; + Preferences::shouldReceive('get')->withArgs(['mfa_recovery', []])->atLeast()->once()->andReturn($arrayPref); Preferences::shouldReceive('getForUser')->withArgs(['xxx'])->andReturn($pref); $this->be($this->user()); @@ -268,6 +282,10 @@ class ProfileControllerTest extends TestCase Preferences::shouldReceive('get')->withArgs(['access_token', null])->atLeast()->once()->andReturnNull(); Preferences::shouldReceive('set')->withArgs(['access_token', Mockery::any()])->atLeast()->once()->andReturn($pref); + $arrayPref = new Preference; + $arrayPref->data = []; + Preferences::shouldReceive('get')->withArgs(['mfa_recovery', []])->atLeast()->once()->andReturn($arrayPref); + Preferences::shouldReceive('getForUser')->withArgs(['xxx'])->andReturn($pref); $this->be($this->user()); @@ -417,7 +435,7 @@ class ProfileControllerTest extends TestCase */ public function testPostCode(): void { - $this->mock(UserRepositoryInterface::class); + $userRepos = $this->mock(UserRepositoryInterface::class); Log::info(sprintf('Now in test %s.', __METHOD__)); $this->mockDefaultSession(); @@ -428,6 +446,8 @@ class ProfileControllerTest extends TestCase $this->withoutMiddleware(); $this->session(['two-factor-secret' => $secret]); + $userRepos->shouldReceive('setMFACode')->withArgs([Mockery::any(), $secret])->atLeast()->once(); + Preferences::shouldReceive('mark')->once(); Google2FA::shouldReceive('verifyKey')->withArgs([$secret, $key])->andReturn(true); diff --git a/tests/Unit/Factory/TagFactoryTest.php b/tests/Unit/Factory/TagFactoryTest.php index 8b5e61e437..f328bfda65 100644 --- a/tests/Unit/Factory/TagFactoryTest.php +++ b/tests/Unit/Factory/TagFactoryTest.php @@ -62,7 +62,7 @@ class TagFactoryTest extends TestCase */ public function testFindOrCreateNew(): void { - $tag = sprintf('Some new tag #%d', $this->randomInt()); + $tag = sprintf('§Some new tag #%d', $this->randomInt()); /** @var TagFactory $factory */ $factory = app(TagFactory::class); $factory->setUser($this->user()); diff --git a/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php b/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php index a369fb30f7..d79fe0fd86 100644 --- a/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php +++ b/tests/Unit/TransactionRules/Actions/ConvertToTransferTest.php @@ -58,8 +58,10 @@ class ConvertToTransferTest extends TestCase public function testActDeposit(): void { $deposit = $this->getRandomDeposit(); - /** @var Account $asset */ - $asset = $this->getRandomAsset(); + + // make sure that $asset is not the destination account of $deposit: + $forbiddenId = (int)$deposit->transactions()->where('amount', '>', 0)->first()->account_id; + $asset = $this->getRandomAsset($forbiddenId); // mock used stuff: $accountRepos = $this->mock(AccountRepositoryInterface::class); diff --git a/tests/Unit/Transformers/RecurrenceTransformerTest.php b/tests/Unit/Transformers/RecurrenceTransformerTest.php index c0331d5656..f09be87ad5 100644 --- a/tests/Unit/Transformers/RecurrenceTransformerTest.php +++ b/tests/Unit/Transformers/RecurrenceTransformerTest.php @@ -64,7 +64,6 @@ class RecurrenceTransformerTest extends TestCase $budget = $this->getRandomBudget(); $piggy = $this->getRandomPiggyBank(); $bill = $this->getRandomBill(); - $foreignCurrency = $this->getDollar(); $ranges = [new Carbon]; $recurrence = $this->getRandomRecurrence(); // mock calls: @@ -91,7 +90,7 @@ class RecurrenceTransformerTest extends TestCase $result = $transformer->transform($recurrence); $this->assertEquals($recurrence->id, $result['id']); - $this->assertEquals('withdrawal', $result['transaction_type']); + //$this->assertEquals('deposit', $result['transaction_type']); $this->assertEquals(true, $result['apply_rules']); $this->assertEquals('Rep descr', $result['recurrence_repetitions'][0]['description']);