Finished testing user controller

This commit is contained in:
James Cole 2014-07-03 08:50:21 +02:00
parent 614c556c96
commit 516fe54bf0
3 changed files with 135 additions and 6 deletions

View File

@ -75,9 +75,11 @@ class UserController extends BaseController
if ($user) { if ($user) {
if (Config::get('auth.verify_reset') === true) { if (Config::get('auth.verify_reset') === true) {
$this->email->sendResetVerification($user); $this->email->sendResetVerification($user);
return View::make('user.verification-pending');
} }
if (Config::get('auth.verify_reset') === false) { if (Config::get('auth.verify_reset') === false) {
$this->email->sendPasswordMail($user); $this->email->sendPasswordMail($user);
return View::make('user.registered');
} }
} }
Session::flash('error', 'No good!'); Session::flash('error', 'No good!');

View File

@ -94,6 +94,35 @@ class UserControllerTest extends TestCase
} }
public function mock($class)
{
$mock = Mockery::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
/**
* Register and verify FAILED:
*/
public function testPostRegisterAllowedFailed()
{
// no mock for config! :(
Config::set('auth.verify_mail', true);
Config::set('auth.allow_register', true);
// mock repository:
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$email = $this->mock('Firefly\Helper\Email\EmailHelper');
$repository->shouldReceive('register')->once()->andReturn(null);
// test
$crawler = $this->client->request('POST', '/register');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Register")'));
}
/** /**
* Register and NO verify: * Register and NO verify:
*/ */
@ -116,17 +145,59 @@ class UserControllerTest extends TestCase
// test // test
$crawler = $this->client->request('POST', '/register', $data); $crawler = $this->client->request('POST', '/register', $data);
$this->assertTrue($this->client->getResponse()->isOk()); $this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Registered!")')); $this->assertCount(1, $crawler->filter('h1:contains("Password sent")'));
} }
public function mock($class) public function testLogout()
{ {
$mock = Mockery::mock($class);
$this->app->instance($class, $mock); Auth::shouldReceive('logout');
return $mock; $this->call('GET', '/logout');
}
public function testRemindme()
{
$this->call('GET', '/remindme');
$this->assertResponseOk();
}
public function testPostRemindmeWithVerification()
{
Config::set('auth.verify_reset', true);
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$email = $this->mock('Firefly\Helper\Email\EmailHelper');
$repository->shouldReceive('findByEmail')->once()->andReturn(new User);
$email->shouldReceive('sendResetVerification')->once()->andReturn(true);
$this->call('POST', '/remindme');
$this->assertResponseOk();
}
public function testPostRemindmeWithoutVerification()
{
Config::set('auth.verify_reset', false);
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$email = $this->mock('Firefly\Helper\Email\EmailHelper');
$repository->shouldReceive('findByEmail')->once()->andReturn(new User);
$email->shouldReceive('sendPasswordMail')->once()->andReturn(true);
$this->call('POST', '/remindme');
$this->assertResponseOk();
}
public function testPostRemindmeFails()
{
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$repository->shouldReceive('findByEmail')->once()->andReturn(null);
$this->call('POST', '/remindme');
$this->assertResponseOk();
$this->assertSessionHas('error');
} }
/** /**
@ -150,6 +221,62 @@ class UserControllerTest extends TestCase
} }
public function testVerification()
{
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$email = $this->mock('Firefly\Helper\Email\EmailHelper');
$repository->shouldReceive('findByVerification')->once()->andReturn(new User);
$email->shouldReceive('sendPasswordMail')->once()->andReturn(true);
// test
$crawler = $this->client->request('GET', '/verify/blabla');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Password sent")'));
}
public function testVerificationFails()
{
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$repository->shouldReceive('findByVerification')->once()->andReturn(null);
// test
$crawler = $this->client->request('GET', '/verify/blabla');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Error")'));
$this->assertViewHas('message');
}
public function testReset()
{
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$email = $this->mock('Firefly\Helper\Email\EmailHelper');
$repository->shouldReceive('findByReset')->once()->andReturn(new User);
$email->shouldReceive('sendPasswordMail')->once()->andReturn(true);
// test
$crawler = $this->client->request('GET', '/reset/blabla');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Password sent")'));
}
public function testResetFails()
{
$repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
$repository->shouldReceive('findByReset')->once()->andReturn(null);
// test
$crawler = $this->client->request('GET', '/reset/blabla');
$this->assertTrue($this->client->getResponse()->isOk());
$this->assertCount(1, $crawler->filter('h1:contains("Error")'));
$this->assertViewHas('message');
}
public function tearDown() public function tearDown()
{ {
Mockery::close(); Mockery::close();

View File

@ -3,7 +3,7 @@
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly<br/> <h1>Firefly<br/>
<small>Registered!</small> <small>Password sent!</small>
</h1> </h1>
</div> </div>
</div> </div>