AuthTest.php 1.97 KB
Newer Older
1 2
<?php

duxet's avatar
duxet committed
3 4
use Illuminate\Auth\Passwords\PasswordBroker;

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
class AuthTest extends TestCase {

    public function tearDown()
    {
        User::truncate();
        DB::collection('password_reminders')->truncate();
    }

    public function testAuthAttempt()
    {
        $user = User::create(array(
            'name' => 'John Doe',
            'email' => 'john@doe.com',
            'password' => Hash::make('foobar')
        ));

        $this->assertTrue(Auth::attempt(array('email' => 'john@doe.com', 'password' => 'foobar'), true));
        $this->assertTrue(Auth::check());
    }

    public function testRemind()
    {
        $mailer = Mockery::mock('Illuminate\Mail\Mailer');
duxet's avatar
duxet committed
28 29 30 31
        $tokens = $this->app->make('auth.password.tokens');
        $users = $this->app['auth']->driver()->getProvider();

        $broker = new PasswordBroker($tokens, $users, $mailer, '');
32 33 34 35 36 37 38 39

        $user = User::create(array(
            'name' => 'John Doe',
            'email' => 'john@doe.com',
            'password' => Hash::make('foobar')
        ));

        $mailer->shouldReceive('send')->once();
duxet's avatar
duxet committed
40
        $broker->sendResetLink(array('email' => 'john@doe.com'));
41

duxet's avatar
duxet committed
42 43
        $this->assertEquals(1, DB::collection('password_resets')->count());
        $reminder = DB::collection('password_resets')->first();
44 45 46 47 48 49 50 51 52 53 54
        $this->assertEquals('john@doe.com', $reminder['email']);
        $this->assertNotNull($reminder['token']);
        $this->assertInstanceOf('MongoDate', $reminder['created_at']);

        $credentials = array(
            'email' => 'john@doe.com',
            'password' => 'foobar',
            'password_confirmation' => 'foobar',
            'token' => $reminder['token']
        );

duxet's avatar
duxet committed
55
        $response = $broker->reset($credentials, function($user, $password)
56
        {
duxet's avatar
duxet committed
57
            $user->password = bcrypt($password);
58 59 60
            $user->save();
        });

duxet's avatar
duxet committed
61 62
        $this->assertEquals('passwords.reset', $response);
        $this->assertEquals(0, DB::collection('password_resets')->count());
63 64
    }

65
}