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

@@ -9,13 +9,16 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use FireflyIII\Http\Requests\TokenFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Http\Request;
use Log;
use PragmaRX\Google2FA\Contracts\Google2FA;
use Preferences;
use Session;
@@ -131,7 +134,7 @@ class PreferencesController extends Controller
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function postIndex(Request $request)
public function postIndex(Request $request, UserRepositoryInterface $repository)
{
// front page accounts
$frontPageAccounts = [];
@@ -168,7 +171,7 @@ class PreferencesController extends Controller
$twoFactorAuthEnabled = false;
$hasTwoFactorAuthSecret = false;
if (!auth()->user()->hasRole('demo')) {
if (!$repository->hasRole(auth()->user(), 'demo')) {
// two factor auth
$twoFactorAuthEnabled = intval($request->get('twoFactorAuthEnabled'));
$hasTwoFactorAuthSecret = !is_null(Preferences::get('twoFactorAuthSecret'));

View File

@@ -9,14 +9,16 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use FireflyIII\Exceptions\ValidationException;
use FireflyIII\Http\Middleware\IsLimitedUser;
use FireflyIII\Http\Requests\DeleteAccountFormRequest;
use FireflyIII\Http\Requests\ProfileFormRequest;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Hash;
use Log;
use Session;
@@ -45,6 +47,8 @@ class ProfileController extends Controller
return $next($request);
}
);
$this->middleware(IsLimitedUser::class);
}
/**
@@ -52,16 +56,6 @@ class ProfileController extends Controller
*/
public function changePassword()
{
if (intval(getenv('SANDSTORM')) === 1) {
return view('error')->with('message', strval(trans('firefly.sandstorm_not_available')));
}
if (auth()->user()->hasRole('demo')) {
Session::flash('info', strval(trans('firefly.cannot_change_demo')));
return redirect(route('profile.index'));
}
$title = auth()->user()->email;
$subTitle = strval(trans('firefly.change_your_password'));
$subTitleIcon = 'fa-key';
@@ -74,16 +68,6 @@ class ProfileController extends Controller
*/
public function deleteAccount()
{
if (intval(getenv('SANDSTORM')) === 1) {
return view('error')->with('message', strval(trans('firefly.sandstorm_not_available')));
}
if (auth()->user()->hasRole('demo')) {
Session::flash('info', strval(trans('firefly.cannot_delete_demo')));
return redirect(route('profile.index'));
}
$title = auth()->user()->email;
$subTitle = strval(trans('firefly.delete_account'));
$subTitleIcon = 'fa-trash';
@@ -111,32 +95,18 @@ class ProfileController extends Controller
*/
public function postChangePassword(ProfileFormRequest $request, UserRepositoryInterface $repository)
{
if (intval(getenv('SANDSTORM')) === 1) {
return view('error')->with('message', strval(trans('firefly.sandstorm_not_available')));
}
if (auth()->user()->hasRole('demo')) {
Session::flash('info', strval(trans('firefly.cannot_change_demo')));
return redirect(route('profile.index'));
}
// old, new1, new2
if (!Hash::check($request->get('current_password'), auth()->user()->password)) {
Session::flash('error', strval(trans('firefly.invalid_current_password')));
return redirect(route('profile.change-password'));
}
// the request has already validated both new passwords must be equal.
$current = $request->get('current_password');
$new = $request->get('new_password');
try {
$this->validatePassword($request->get('current_password'), $request->get('new_password'));
$this->validatePassword(auth()->user(), $current, $new);
} catch (ValidationException $e) {
Session::flash('error', $e->getMessage());
return redirect(route('profile.change-password'));
}
// update the user with the new password.
$repository->changePassword(auth()->user(), $request->get('new_password'));
Session::flash('success', strval(trans('firefly.password_changed')));
@@ -151,17 +121,6 @@ class ProfileController extends Controller
*/
public function postDeleteAccount(UserRepositoryInterface $repository, DeleteAccountFormRequest $request)
{
if (intval(getenv('SANDSTORM')) === 1) {
return view('error')->with('message', strval(trans('firefly.sandstorm_not_available')));
}
if (auth()->user()->hasRole('demo')) {
Session::flash('info', strval(trans('firefly.cannot_delete_demo')));
return redirect(route('profile.index'));
}
// old, new1, new2
if (!Hash::check($request->get('password'), auth()->user()->password)) {
Session::flash('error', strval(trans('firefly.invalid_password')));
@@ -182,15 +141,21 @@ class ProfileController extends Controller
}
/**
* @param string $old
* @param User $user
* @param string $current
* @param string $new
* @param string $newConfirmation
*
* @return bool
* @throws ValidationException
*/
protected function validatePassword(string $old, string $new): bool
protected function validatePassword(User $user, string $current, string $new): bool
{
if ($new === $old) {
if (!Hash::check($current, auth()->user()->password)) {
throw new ValidationException(strval(trans('firefly.invalid_current_password')));
}
if ($current === $new) {
throw new ValidationException(strval(trans('firefly.should_change')));
}

View File

@@ -26,6 +26,7 @@ use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Collection;
use Log;
use Preferences;
use Response;
use Session;
@@ -73,7 +74,7 @@ class ReportController extends Controller
public function auditReport(Collection $accounts, Carbon $start, Carbon $end)
{
if ($end < $start) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');
@@ -109,7 +110,7 @@ class ReportController extends Controller
public function budgetReport(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end)
{
if ($end < $start) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');
@@ -145,7 +146,7 @@ class ReportController extends Controller
public function categoryReport(Collection $accounts, Collection $categories, Carbon $start, Carbon $end)
{
if ($end < $start) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');
@@ -251,10 +252,9 @@ class ReportController extends Controller
/**
* @param ReportFormRequest $request
*
* @return RedirectResponse
* @throws FireflyException
* @return RedirectResponse|\Illuminate\Routing\Redirector
*/
public function postIndex(ReportFormRequest $request): RedirectResponse
public function postIndex(ReportFormRequest $request)
{
// report type:
$reportType = $request->get('report_type');
@@ -266,6 +266,7 @@ class ReportController extends Controller
$tags = join(',', $request->getTagList()->pluck('tag')->toArray());
if ($request->getAccountList()->count() === 0) {
Log::debug('Account count is zero');
Session::flash('error', trans('firefly.select_more_than_one_account'));
return redirect(route('reports.index'));
@@ -293,14 +294,7 @@ class ReportController extends Controller
return view('error')->with('message', trans('firefly.end_after_start_date'));
}
// lower threshold
if ($start < session('first')) {
$start = session('first');
}
switch ($reportType) {
default:
throw new FireflyException(sprintf('Firefly does not support the "%s"-report yet.', $reportType));
case 'category':
$uri = route('reports.report.category', [$accounts, $categories, $start, $end]);
break;
@@ -332,7 +326,7 @@ class ReportController extends Controller
public function tagReport(Collection $accounts, Collection $tags, Carbon $start, Carbon $end)
{
if ($end < $start) {
return view('error')->with('message', trans('firefly.end_after_start_date'));
return view('error')->with('message', trans('firefly.end_after_start_date')); // @codeCoverageIgnore
}
if ($start < session('first')) {
$start = session('first');

View File

@@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
@@ -255,10 +255,11 @@ class RuleController extends Controller
Preferences::mark();
if (intval($request->get('create_another')) === 1) {
// set value so create routine will not overwrite URL:
// @codeCoverageIgnoreStart
Session::put('rules.create.fromStore', true);
return redirect(route('rules.create', [$ruleGroup]))->withInput();
// @codeCoverageIgnoreEnd
}
return redirect($this->getPreviousUri('rules.create.uri'));
@@ -340,10 +341,11 @@ class RuleController extends Controller
Preferences::mark();
if (intval($request->get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
// @codeCoverageIgnoreStart
Session::put('rules.edit.fromUpdate', true);
return redirect(route('rules.edit', [$rule->id]))->withInput(['return_to_edit' => 1]);
// @codeCoverageIgnoreEnd
}
return redirect($this->getPreviousUri('rules.edit.uri'));
@@ -473,7 +475,7 @@ class RuleController extends Controller
$actions[] = view(
'rules.partials.action',
[
'oldTrigger' => $entry,
'oldAction' => $entry,
'oldValue' => $request->old('rule-action-value')[$index],
'oldChecked' => $checked,
'count' => $count,
@@ -531,7 +533,7 @@ class RuleController extends Controller
if (is_array($data['rule-triggers'])) {
foreach ($data['rule-triggers'] as $index => $triggerType) {
$data['rule-trigger-stop'][$index] = $data['rule-trigger-stop'][$index] ?? 0;
$triggers[] = [
$triggers[] = [
'type' => $triggerType,
'value' => $data['rule-trigger-values'][$index],
'stopProcessing' => intval($data['rule-trigger-stop'][$index]) === 1 ? true : false,

View File

@@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
@@ -217,10 +217,11 @@ class RuleGroupController extends Controller
Preferences::mark();
if (intval($request->get('create_another')) === 1) {
// set value so create routine will not overwrite URL:
// @codeCoverageIgnoreStart
Session::put('rule-groups.create.fromStore', true);
return redirect(route('rule-groups.create'))->withInput();
// @codeCoverageIgnoreEnd
}
return redirect($this->getPreviousUri('rule-groups.create.uri'));
@@ -261,10 +262,11 @@ class RuleGroupController extends Controller
Preferences::mark();
if (intval($request->get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
// @codeCoverageIgnoreStart
Session::put('rule-groups.edit.fromUpdate', true);
return redirect(route('rule-groups.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]);
// @codeCoverageIgnoreEnd
}
// redirect to previous URL.