Files
firefly-iii/tests/Feature/Controllers/ProfileControllerTest.php

460 lines
17 KiB
PHP
Raw Normal View History

2017-02-12 12:00:11 +01:00
<?php
/**
* ProfileControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:42:21 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 12:00:11 +01:00
*/
2017-03-24 11:07:38 +01:00
declare(strict_types=1);
2017-02-12 12:00:11 +01:00
namespace Tests\Feature\Controllers;
use FireflyIII\Models\Preference;
2017-03-05 18:15:38 +01:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2017-02-12 13:15:23 +01:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Google2FA;
use Illuminate\Support\Collection;
2018-03-24 06:08:50 +01:00
use Log;
use Preferences;
2017-02-12 12:00:11 +01:00
use Tests\TestCase;
2017-03-05 18:15:38 +01:00
/**
* Class ProfileControllerTest
*
2017-08-12 10:27:45 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-05 18:15:38 +01:00
*/
2017-02-12 12:00:11 +01:00
class ProfileControllerTest extends TestCase
{
/**
2018-03-24 06:08:50 +01:00
*
*/
2018-03-24 06:08:50 +01:00
public function setUp()
{
2018-03-24 06:08:50 +01:00
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
2018-03-24 06:08:50 +01:00
}
2018-03-24 06:08:50 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::changeEmail()
*/
2018-05-11 19:58:10 +02:00
public function testChangeEmail(): void
2018-03-24 06:08:50 +01:00
{
$this->be($this->user());
2018-03-24 06:08:50 +01:00
$response = $this->get(route('profile.change-email'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-03-24 06:08:50 +01:00
* @covers \FireflyIII\Http\Controllers\ProfileController::changePassword
*/
2018-05-11 19:58:10 +02:00
public function testChangePassword(): void
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$this->be($this->user());
2018-03-24 06:08:50 +01:00
$response = $this->get(route('profile.change-password'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 12:21:44 +01:00
/**
2018-03-24 06:08:50 +01:00
* @covers \FireflyIII\Http\Controllers\ProfileController::code
* @covers \FireflyIII\Http\Controllers\ProfileController::getDomain
2017-02-12 12:21:44 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCode(): void
2017-02-12 12:21:44 +01:00
{
2017-03-05 18:15:38 +01:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-24 06:08:50 +01:00
Google2FA::shouldReceive('generateSecretKey')->andReturn('secret');
Google2FA::shouldReceive('getQRCodeInline')->andReturn('long-data-url');
2017-03-05 18:15:38 +01:00
2017-02-12 12:21:44 +01:00
$this->be($this->user());
2018-03-24 06:08:50 +01:00
$response = $this->get(route('profile.code'));
2017-02-12 12:21:44 +01:00
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::confirmEmailChange()
* @expectedExceptionMessage Invalid token
*/
2018-05-11 19:58:10 +02:00
public function testConfirmEmailChangeNoToken(): void
{
Preferences::shouldReceive('findByName')->withArgs(['email_change_confirm_token'])->andReturn(new Collection());
// email_change_confirm_token
$response = $this->get(route('profile.confirm-email-change', ['some-fake-token']));
$response->assertStatus(500);
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::confirmEmailChange()
*/
2018-05-11 19:58:10 +02:00
public function testConfirmEmailWithToken(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('unblockUser');
$preference = new Preference;
$preference->data = 'existing-token';
/** @var \stdClass $preference */
$preference->user = $this->user();
Preferences::shouldReceive('findByName')->withArgs(['email_change_confirm_token'])->andReturn(new Collection([$preference]));
// email_change_confirm_token
$response = $this->get(route('profile.confirm-email-change', ['existing-token']));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-02-12 12:21:44 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::deleteAccount
*/
2018-05-11 19:58:10 +02:00
public function testDeleteAccount(): void
2017-02-12 12:21:44 +01:00
{
2017-03-05 18:15:38 +01:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 18:15:38 +01:00
2017-02-12 12:21:44 +01:00
$this->be($this->user());
$response = $this->get(route('profile.delete-account'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
2018-03-24 06:08:50 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::deleteCode
*/
2018-05-11 19:58:10 +02:00
public function testDeleteCode(): void
2018-03-24 06:08:50 +01:00
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2018-03-24 06:08:50 +01:00
$this->be($this->user());
$response = $this->get(route('profile.delete-code'));
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertSessionHas('info');
$response->assertRedirect(route('profile.index'));
}
2017-02-12 12:21:44 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::index
2017-02-17 20:14:38 +01:00
* @covers \FireflyIII\Http\Controllers\ProfileController::__construct
2017-02-12 12:21:44 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testIndex(): void
2017-02-12 12:21:44 +01:00
{
// delete access token.
Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->delete();
2017-03-05 18:15:38 +01:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-05 18:15:38 +01:00
2017-02-12 12:21:44 +01:00
$this->be($this->user());
$response = $this->get(route('profile.index'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangeEmail
*/
2018-05-11 19:58:10 +02:00
public function testPostChangeEmail(): void
{
$data = [
'email' => 'new@example.com',
];
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('findByEmail')->once()->andReturn(null);
$repository->shouldReceive('changeEmail')->once()->andReturn(true);
$this->be($this->user());
$response = $this->post(route('profile.change-email.post'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertRedirect(route('index'));
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangeEmail
*/
2018-05-11 19:58:10 +02:00
public function testPostChangeEmailExisting(): void
{
2018-02-28 15:50:00 +01:00
$data = [
'email' => 'existing@example.com',
];
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('findByEmail')->once()->andReturn(new User);
$this->be($this->user());
$response = $this->post(route('profile.change-email.post'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertRedirect(route('index'));
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangeEmail
*/
2018-05-11 19:58:10 +02:00
public function testPostChangeEmailSame(): void
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
$data = [
'email' => $this->user()->email,
];
$this->be($this->user());
$response = $this->post(route('profile.change-email.post'), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
$response->assertRedirect(route('profile.change-email'));
}
2017-02-12 12:21:44 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangePassword
2017-03-24 11:07:38 +01:00
* @covers \FireflyIII\Http\Controllers\ProfileController::validatePassword
2017-02-12 12:21:44 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testPostChangePassword(): void
2017-02-12 12:21:44 +01:00
{
2017-03-05 18:15:38 +01:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-02-12 12:21:44 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changePassword');
$data = [
'current_password' => 'james',
'new_password' => 'james2',
'new_password_confirmation' => 'james2',
];
$this->be($this->user());
$response = $this->post(route('profile.change-password.post'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-03-24 11:07:38 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangePassword
* @covers \FireflyIII\Http\Controllers\ProfileController::validatePassword
*/
2018-05-11 19:58:10 +02:00
public function testPostChangePasswordNotCorrect(): void
2017-03-24 11:07:38 +01:00
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-24 11:07:38 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changePassword');
$data = [
'current_password' => 'james3',
'new_password' => 'james2',
'new_password_confirmation' => 'james2',
];
$this->be($this->user());
$response = $this->post(route('profile.change-password.post'), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangePassword
* @covers \FireflyIII\Http\Controllers\ProfileController::validatePassword
*/
2018-05-11 19:58:10 +02:00
public function testPostChangePasswordSameNew(): void
2017-03-24 11:07:38 +01:00
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-03-24 11:07:38 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changePassword');
$data = [
'current_password' => 'james',
'new_password' => 'james',
'new_password_confirmation' => 'james',
];
$this->be($this->user());
$response = $this->post(route('profile.change-password.post'), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
}
2018-03-24 06:08:50 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postCode
*/
2018-05-11 19:58:10 +02:00
public function testPostCode(): void
2018-03-24 06:08:50 +01:00
{
$secret = '0123456789abcde';
$key = '123456';
$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();
Google2FA::shouldReceive('verifyKey')->withArgs([$secret, $key])->andReturn(true);
$data = [
'code' => $key,
];
$this->be($this->user());
$response = $this->post(route('profile.code.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
2017-02-12 12:21:44 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postDeleteAccount
*/
2018-05-11 19:58:10 +02:00
public function testPostDeleteAccount(): void
2017-02-12 12:21:44 +01:00
{
2017-03-05 18:15:38 +01:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-02-12 12:21:44 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
2017-03-24 11:07:38 +01:00
$repository->shouldReceive('destroy')->once();
2017-02-12 12:21:44 +01:00
$data = [
'password' => 'james',
];
$this->be($this->user());
$response = $this->post(route('profile.delete-account.post'), $data);
$response->assertStatus(302);
2017-02-12 12:32:13 +01:00
$response->assertRedirect(route('index'));
2017-02-12 12:21:44 +01:00
}
2017-03-24 11:07:38 +01:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postDeleteAccount
*/
2018-05-11 19:58:10 +02:00
public function testPostDeleteAccountWrong(): void
2017-03-24 11:07:38 +01:00
{
// mock stuff
2018-02-28 15:50:00 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
2017-03-24 11:07:38 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
2017-07-08 06:28:44 +02:00
$data = [
2017-03-24 11:07:38 +01:00
'password' => 'james2',
];
$this->be($this->user());
$response = $this->post(route('profile.delete-account.post'), $data);
$response->assertStatus(302);
$response->assertRedirect(route('profile.delete-account'));
$response->assertSessionHas('error');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::regenerate()
*/
2018-05-11 19:58:10 +02:00
public function testRegenerate(): void
{
$token = '';
$currentToken = Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->first();
2018-04-02 14:17:11 +02:00
if (null !== $currentToken) {
$token = $currentToken->data;
}
$this->be($this->user());
$response = $this->post(route('profile.regenerate'));
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertRedirect(route('profile.index'));
$newToken = Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->first();
$this->assertNotEquals($newToken->data, $token);
2018-03-31 21:05:06 +02:00
// reset token for later test:
$newToken->data = 'token';
$newToken->save();
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
*/
2018-05-11 19:58:10 +02:00
public function testUndoEmailChange(): void
{
$hash = hash('sha256', 'previous@example.com');
$tokenPreference = new Preference;
$tokenPreference->data = 'token';
/** @var \stdClass $tokenPreference */
$tokenPreference->user = $this->user();
$hashPreference = new Preference;
$hashPreference->data = 'previous@example.com';
/** @var \stdClass $hashPreference */
$hashPreference->user = $this->user();
Preferences::shouldReceive('findByName')->once()->andReturn(new Collection([$tokenPreference]));
Preferences::shouldReceive('beginsWith')->once()->andReturn(new Collection([$hashPreference]));
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changeEmail')->once();
$repository->shouldReceive('unblockUser')->once();
$response = $this->get(route('profile.undo-email-change', ['token', $hash]));
$response->assertStatus(302);
$response->assertSessionHas('success');
$response->assertRedirect(route('login'));
}
/**
2017-12-22 18:32:43 +01:00
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
* @expectedExceptionMessage Invalid token
*/
2018-05-11 19:58:10 +02:00
public function testUndoEmailChangeBadHash(): void
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
$hash = hash('sha256', 'previous@example.comX');
$tokenPreference = new Preference;
$tokenPreference->data = 'token';
/** @var \stdClass $tokenPreference */
$tokenPreference->user = $this->user();
$hashPreference = new Preference;
$hashPreference->data = 'previous@example.com';
/** @var \stdClass $hashPreference */
$hashPreference->user = $this->user();
Preferences::shouldReceive('findByName')->once()->andReturn(new Collection([$tokenPreference]));
Preferences::shouldReceive('beginsWith')->once()->andReturn(new Collection([$hashPreference]));
$response = $this->get(route('profile.undo-email-change', ['token', $hash]));
$response->assertStatus(500);
}
/**
2017-12-22 18:32:43 +01:00
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
* @expectedExceptionMessage Invalid token
*/
2018-05-11 19:58:10 +02:00
public function testUndoEmailChangeBadToken(): void
{
2018-02-28 15:50:00 +01:00
$repository = $this->mock(UserRepositoryInterface::class);
Preferences::shouldReceive('findByName')->once()->andReturn(new Collection);
$response = $this->get(route('profile.undo-email-change', ['token', 'some-hash']));
$response->assertStatus(500);
}
2017-02-16 22:33:32 +01:00
}