Last active
July 10, 2020 03:29
-
-
Save agm1984/c96b4562a489c3a1721273465c42dc0f to your computer and use it in GitHub Desktop.
Demonstration of `resetAuth` function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests\Auth; | |
use Illuminate\Foundation\Auth\ThrottlesLogins; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
use Tests\TestCase; | |
class LoginTest extends TestCase | |
{ | |
use DatabaseTransactions, ThrottlesLogins; | |
protected $auth_guard = 'web'; | |
// ... | |
/** @test */ | |
public function it_should_throw_error_422_with_empty_form() | |
{ | |
$this->postJson(route('login'), ['email' => '', 'password' => '']) | |
->assertStatus(422) | |
->assertJsonStructure(['message', 'errors' => ['email', 'password']]); | |
$this->assertGuest($this->auth_guard); | |
$this->resetAuth(); | |
} | |
/** @test */ | |
public function it_should_throw_error_429_when_login_attempt_is_throttled() | |
{ | |
$throttledUser = factory(User::class, 1)->create()->first(); | |
// corresponds to `Route::group(['middleware' => ['guest', 'throttle:10,5']], function () {` | |
// ie: 10 attempts allowed per minute, with 5 minute cooldown | |
foreach (range(0, 9) as $attempt) { | |
$this->postJson(route('login'), ['email' => $throttledUser->email, 'password' => "{TestCase::AUTH_PASSWORD}_{$attempt}"]); | |
} | |
$this->postJson(route('login'), ['email' => $throttledUser->email, 'password' => TestCase::AUTH_PASSWORD . '6']) | |
->assertStatus(429) | |
->assertJson(['message' => 'Too Many Attempts.']); | |
$this->resetAuth(); | |
} | |
} |
Author
agm1984
commented
Jul 10, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment