AuthTest.php 2.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
<?php

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');
        $this->app->instance('mailer', $mailer);

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

        $mailer->shouldReceive('send')->once();
        Password::remind(array('email' => 'john@doe.com'));

        $this->assertEquals(1, DB::collection('password_reminders')->count());
        $reminder = DB::collection('password_reminders')->first();
        $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']
        );

        $response = Password::reset($credentials, function($user, $password)
        {
            $user->password = Hash::make($password);
            $user->save();
        });

        $this->assertEquals('reminders.reset', $response);
        $this->assertEquals(0, DB::collection('password_reminders')->count());
    }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    public function testDeprecatedRemind()
    {
        $mailer = Mockery::mock('Illuminate\Mail\Mailer');
        $this->app->instance('mailer', $mailer);

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

        $mailer->shouldReceive('send')->once();
        Password::remind(array('email' => 'john@doe.com'));

        DB::collection('password_reminders')->update(array('created_at' => new DateTime));
        $reminder = DB::collection('password_reminders')->first();
        $this->assertTrue(is_array($reminder['created_at']));

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

        $response = Password::reset($credentials, function($user, $password)
        {
            $user->password = Hash::make($password);
            $user->save();
        });

        $this->assertEquals('reminders.reset', $response);
        $this->assertEquals(0, DB::collection('password_reminders')->count());
    }

95
}