Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

PrijalBista/laravel-api-auth-sanctum-boilerplate

63 stars
1 code files
View PrijalBista/laravel-api-auth-sanctum-boilerplate on GitHub

tests/Feature/Api/Auth/AuthControllerTest.php

Open in GitHub
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\Sanctum;
use App\Models\User;
use Tests\TestCase;
use Notification;
 
class AuthControllerTest extends TestCase
{
use RefreshDatabase;
 
public function setUp() :void
{
parent::setUp();
 
Notification::fake();
 
User::factory()->create([
'email' => '[email protected]',
'password' => Hash::make('testpassword')
]);
 
}
 
public function test_show_validation_error_when_both_fields_empty()
{
$response = $this->json('POST', route('auth.login'), [
'email' => '',
'password' => ''
]);
 
$response->assertStatus(422)
->assertJsonValidationErrors(['email', 'password']);
}
 
public function test_show_validation_error_on_email_when_credential_donot_match()
{
$response = $this->json('POST', route('auth.login'), [
'email' => '[email protected]',
'password' => 'abcdabcd'
]);
 
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
 
public function test_return_user_and_access_token_after_successful_login()
{
$response = $this->json('POST', route('auth.login'), [
'email' =>'[email protected]',
'password' => 'testpassword',
]);
 
$response->assertStatus(200)
->assertJsonStructure(['user', 'access_token']);
}
 
public function test_non_authenticated_user_cannot_get_user_details()
{
$response = $this->json('GET', route('auth.user'));
 
$response->assertStatus(401)
->assertSee('Unauthenticated');
}
 
public function test_authenticated_user_can_get_user_details()
{
Sanctum::actingAs(
User::first(),
);
 
$response = $this->json('GET', route('auth.user'));
 
$response->assertStatus(200)
->assertJsonStructure(['name', 'email']);
}
 
public function test_non_authenticated_user_cannot_logout()
{
$response = $this->json('POST', route('auth.logout'), []);
 
$response->assertStatus(401)
->assertSee('Unauthenticated');;
}
 
public function test_authenticated_user_can_logout()
{
Sanctum::actingAs(
User::first(),
);
 
$response = $this->json('POST', route('auth.logout'), []);
 
$response->assertStatus(200);
}
 
public function test_return_validation_error_when_email_doenot_exist()
{
$response = $this->json('POST', route('password.email'), ['email' => '[email protected]']);
 
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
}
 
public function test_send_password_reset_link_if_email_exists()
{
$user = User::first();
$response = $this->json('POST', route('password.email'), ['email' => $user->email]);
 
$response->assertStatus(200)
->assertJsonStructure(['message']);
}
 
public function test_reset_password_success()
{
$user = User::first();
$token = Password::broker()->createToken($user);
$new_password = 'testpassword';
 
$response = $this->json('POST', route('password.reset'), [
'token' => $token,
'email' => $user->email,
'password' => $new_password,
'password_confirmation' => $new_password
]);
 
$response->assertStatus(200)
->assertJsonStructure(['message']);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.