AuthTest.php 2 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
class AuthTest extends TestCase {

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

    public function testAuthAttempt()
    {
15
        $user = User::create([
Jens Segers's avatar
Jens Segers committed
16 17 18
            'name'     => 'John Doe',
            'email'    => 'john@doe.com',
            'password' => Hash::make('foobar'),
19
        ]);
20

21
        $this->assertTrue(Auth::attempt(['email' => 'john@doe.com', 'password' => 'foobar'], true));
22 23 24 25 26 27
        $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
        $user = User::create([
Jens Segers's avatar
Jens Segers committed
34 35 36
            'name'     => 'John Doe',
            'email'    => 'john@doe.com',
            'password' => Hash::make('foobar'),
37
        ]);
38 39

        $mailer->shouldReceive('send')->once();
40
        $broker->sendResetLink(['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
        $this->assertEquals('john@doe.com', $reminder['email']);
        $this->assertNotNull($reminder['token']);
        $this->assertInstanceOf('MongoDate', $reminder['created_at']);

48
        $credentials = [
Jens Segers's avatar
Jens Segers committed
49 50
            'email'                 => 'john@doe.com',
            'password'              => 'foobar',
51
            'password_confirmation' => 'foobar',
Jens Segers's avatar
Jens Segers committed
52
            'token'                 => $reminder['token'],
53
        ];
54

Jens Segers's avatar
Jens Segers 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
}