-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathAuthTest.php
83 lines (67 loc) · 2.39 KB
/
AuthTest.php
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace MongoDB\Laravel\Tests;
use Carbon\Carbon;
use Illuminate\Auth\Passwords\PasswordBroker;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use MongoDB\Laravel\Tests\Models\User;
use function bcrypt;
class AuthTest extends TestCase
{
public function tearDown(): void
{
parent::setUp();
User::truncate();
DB::table('password_reset_tokens')->truncate();
}
public function testAuthAttempt()
{
User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => Hash::make('foobar'),
]);
$this->assertTrue(Auth::attempt(['email' => '[email protected]', 'password' => 'foobar'], true));
$this->assertTrue(Auth::check());
}
public function testRemindOld()
{
$broker = $this->app->make('auth.password.broker');
$user = User::create([
'name' => 'John Doe',
'email' => '[email protected]',
'password' => Hash::make('foobar'),
]);
$token = null;
$this->assertSame(
PasswordBroker::RESET_LINK_SENT,
$broker->sendResetLink(
['email' => '[email protected]'],
function ($actualUser, $actualToken) use ($user, &$token) {
$this->assertEquals($user->id, $actualUser->id);
// Store token for later use
$token = $actualToken;
},
),
);
$this->assertEquals(1, DB::table('password_reset_tokens')->count());
$reminder = DB::table('password_reset_tokens')->first();
$this->assertEquals('[email protected]', $reminder->email);
$this->assertNotNull($reminder->token);
$this->assertInstanceOf(Carbon::class, $reminder->created_at);
$credentials = [
'email' => '[email protected]',
'password' => 'foobar',
'password_confirmation' => 'foobar',
'token' => $token,
];
$response = $broker->reset($credentials, function ($user, $password) {
$user->password = bcrypt($password);
$user->save();
});
$this->assertEquals('passwords.reset', $response);
$this->assertEquals(0, DB::table('password_resets')->count());
}
}