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

456 lines
17 KiB
PHP
Raw Normal View History

2017-02-12 05:00:11 -06:00
<?php
/**
* ProfileControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05: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 07:42:21 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 05:00:11 -06:00
*/
2017-03-24 05:07:38 -05:00
declare(strict_types=1);
2017-02-12 05:00:11 -06:00
namespace Tests\Feature\Controllers;
use FireflyIII\Models\Preference;
2017-03-05 11:15:38 -06:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2017-02-12 06:15:23 -06:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Google2FA;
use Illuminate\Support\Collection;
2018-03-24 00:08:50 -05:00
use Log;
use Preferences;
2017-02-12 05:00:11 -06:00
use Tests\TestCase;
2017-03-05 11:15:38 -06:00
/**
* Class ProfileControllerTest
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-05 11:15:38 -06:00
*/
2017-02-12 05:00:11 -06:00
class ProfileControllerTest extends TestCase
{
/**
2018-03-24 00:08:50 -05:00
*
*/
2018-03-24 00:08:50 -05:00
public function setUp()
{
2018-03-24 00:08:50 -05:00
parent::setUp();
Log::debug(sprintf('Now in %s.', get_class($this)));
}
2018-03-24 00:08:50 -05:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::changeEmail()
*/
public function testChangeEmail()
{
$this->be($this->user());
2018-03-24 00:08:50 -05:00
$response = $this->get(route('profile.change-email'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-03-24 00:08:50 -05:00
* @covers \FireflyIII\Http\Controllers\ProfileController::changePassword
*/
2018-03-24 00:08:50 -05:00
public function testChangePassword()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$this->be($this->user());
2018-03-24 00:08:50 -05:00
$response = $this->get(route('profile.change-password'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 05:21:44 -06:00
/**
2018-03-24 00:08:50 -05:00
* @covers \FireflyIII\Http\Controllers\ProfileController::code
* @covers \FireflyIII\Http\Controllers\ProfileController::getDomain
2017-02-12 05:21:44 -06:00
*/
2018-03-24 00:08:50 -05:00
public function testCode()
2017-02-12 05:21:44 -06:00
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2018-03-24 00:08:50 -05:00
Google2FA::shouldReceive('generateSecretKey')->andReturn('secret');
Google2FA::shouldReceive('getQRCodeInline')->andReturn('long-data-url');
2017-03-05 11:15:38 -06:00
2017-02-12 05:21:44 -06:00
$this->be($this->user());
2018-03-24 00:08:50 -05:00
$response = $this->get(route('profile.code'));
2017-02-12 05:21:44 -06:00
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::confirmEmailChange()
* @expectedExceptionMessage Invalid token
*/
public function testConfirmEmailChangeNoToken()
{
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()
*/
public function testConfirmEmailWithToken()
{
$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 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::deleteAccount
*/
public function testDeleteAccount()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('profile.delete-account'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
2018-03-24 00:08:50 -05:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::deleteCode
*/
public function testDeleteCode()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$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 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::index
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\ProfileController::__construct
2017-02-12 05:21:44 -06:00
*/
public function testIndex()
{
// delete access token.
Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->delete();
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06: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
*/
public function testPostChangeEmail()
{
$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
*/
public function testPostChangeEmailExisting()
{
2018-02-28 08:50:00 -06: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
*/
public function testPostChangeEmailSame()
{
2018-02-28 08:50:00 -06: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 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangePassword
2017-03-24 05:07:38 -05:00
* @covers \FireflyIII\Http\Controllers\ProfileController::validatePassword
2017-02-12 05:21:44 -06:00
*/
public function testPostChangePassword()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06: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 05:07:38 -05:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangePassword
* @covers \FireflyIII\Http\Controllers\ProfileController::validatePassword
*/
public function testPostChangePasswordNotCorrect()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$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
*/
public function testPostChangePasswordSameNew()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$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 00:08:50 -05:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postCode
*/
public function testPostCode()
{
$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 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postDeleteAccount
*/
public function testPostDeleteAccount()
{
2017-03-05 11:15:38 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$repository = $this->mock(UserRepositoryInterface::class);
2017-03-24 05:07:38 -05:00
$repository->shouldReceive('destroy')->once();
2017-02-12 05:21:44 -06:00
$data = [
'password' => 'james',
];
$this->be($this->user());
$response = $this->post(route('profile.delete-account.post'), $data);
$response->assertStatus(302);
2017-02-12 05:32:13 -06:00
$response->assertRedirect(route('index'));
2017-02-12 05:21:44 -06:00
}
2017-03-24 05:07:38 -05:00
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postDeleteAccount
*/
public function testPostDeleteAccountWrong()
{
// mock stuff
2018-02-28 08:50:00 -06:00
$repository = $this->mock(UserRepositoryInterface::class);
2017-03-24 05:07:38 -05:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-07-07 23:28:44 -05:00
$data = [
2017-03-24 05:07:38 -05: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()
*/
public function testRegenerate()
{
$token = '';
$currentToken = Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->first();
if (!is_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);
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
*/
public function testUndoEmailChange()
{
$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 11:32:43 -06:00
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
* @expectedExceptionMessage Invalid token
*/
public function testUndoEmailChangeBadHash()
{
2018-02-28 08:50:00 -06: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 11:32:43 -06:00
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
* @expectedExceptionMessage Invalid token
*/
public function testUndoEmailChangeBadToken()
{
2018-02-28 08:50:00 -06: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 15:33:32 -06:00
}