Fixed a lot of tests and associated code.

This commit is contained in:
James Cole
2017-12-17 14:06:14 +01:00
parent 7d348f25ac
commit b08af77c98
42 changed files with 579 additions and 442 deletions

View File

@@ -22,9 +22,13 @@ declare(strict_types=1);
namespace Tests\Feature\Controllers;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Preferences;
use Tests\TestCase;
/**
@@ -36,6 +40,18 @@ use Tests\TestCase;
*/
class ProfileControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::changeEmail()
*/
public function testChangeEmail()
{
$this->be($this->user());
$response = $this->get(route('profile.change-email'));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::changePassword
*/
@@ -51,6 +67,36 @@ class ProfileControllerTest extends TestCase
$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');
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::deleteAccount
*/
@@ -72,6 +118,8 @@ class ProfileControllerTest extends TestCase
*/
public function testIndex()
{
// delete access token.
Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->delete();
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
@@ -82,6 +130,58 @@ class ProfileControllerTest extends TestCase
$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()
{
$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()
{
$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'));
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::postChangePassword
* @covers \FireflyIII\Http\Controllers\ProfileController::validatePassword
@@ -187,4 +287,92 @@ class ProfileControllerTest extends TestCase
$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'));
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
* @expectedExceptionMessage Invalid token
*/
public function testUndoEmailChangeBadHash()
{
$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);
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController::undoEmailChange()
* @expectedExceptionMessage Invalid token
*/
public function testUndoEmailChangeBadToken()
{
Preferences::shouldReceive('findByName')->once()->andReturn(new Collection);
$response = $this->get(route('profile.undo-email-change', ['token', 'some-hash']));
$response->assertStatus(500);
}
}