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

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

Jens Segers's avatar
Jens Segers committed
6 7
class AuthTest extends TestCase
{
8 9 10 11 12 13 14 15
    public function tearDown()
    {
        User::truncate();
        DB::collection('password_reminders')->truncate();
    }

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

22
        $this->assertTrue(Auth::attempt(['email' => 'john@doe.com', 'password' => 'foobar'], true));
23 24 25
        $this->assertTrue(Auth::check());
    }

26
    public function testRemindOld()
27
    {
28 29 30 31
        if (Application::VERSION >= '5.2') {
            return;
        }

32
        $mailer = Mockery::mock('Illuminate\Mail\Mailer');
duxet's avatar
duxet committed
33 34 35 36
        $tokens = $this->app->make('auth.password.tokens');
        $users = $this->app['auth']->driver()->getProvider();

        $broker = new PasswordBroker($tokens, $users, $mailer, '');
37

38
        $user = User::create([
Jens Segers's avatar
Jens Segers committed
39 40 41
            'name'     => 'John Doe',
            'email'    => 'john@doe.com',
            'password' => Hash::make('foobar'),
42
        ]);
43 44

        $mailer->shouldReceive('send')->once();
45
        $broker->sendResetLink(['email' => 'john@doe.com']);
46

duxet's avatar
duxet committed
47 48
        $this->assertEquals(1, DB::collection('password_resets')->count());
        $reminder = DB::collection('password_resets')->first();
49 50
        $this->assertEquals('john@doe.com', $reminder['email']);
        $this->assertNotNull($reminder['token']);
51
        $this->assertInstanceOf('MongoDB\BSON\UTCDateTime', $reminder['created_at']);
52

53
        $credentials = [
Jens Segers's avatar
Jens Segers committed
54 55
            'email'                 => 'john@doe.com',
            'password'              => 'foobar',
56
            'password_confirmation' => 'foobar',
Jens Segers's avatar
Jens Segers committed
57
            'token'                 => $reminder['token'],
58
        ];
59

Jens Segers's avatar
Jens Segers committed
60
        $response = $broker->reset($credentials, function ($user, $password) {
duxet's avatar
duxet committed
61
            $user->password = bcrypt($password);
62 63 64
            $user->save();
        });

duxet's avatar
duxet committed
65 66
        $this->assertEquals('passwords.reset', $response);
        $this->assertEquals(0, DB::collection('password_resets')->count());
67
    }
68
}