AuthenticationTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Tests\Feature\API;
  3. use App\Models\User;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Foundation\Testing\WithFaker;
  6. use Illuminate\Support\Facades\Hash;
  7. use Laravel\Sanctum\Sanctum;
  8. use Tests\TestCase;
  9. class AuthenticationTest extends TestCase
  10. {
  11. use RefreshDatabase, WithFaker;
  12. public function test_user_can_login(): void
  13. {
  14. $password = 'lpc..pwd';
  15. $user = User::factory()->create([
  16. 'password' => Hash::make($password),
  17. 'username' => 'lpc',
  18. ]);
  19. $fields = ['username', 'email'];
  20. foreach($fields as $field) {
  21. $response = $this->post('/api/login', [
  22. 'username' => $user->$field,
  23. 'password' => $password,
  24. ]);
  25. $response->assertStatus(200);
  26. $response->assertJsonIsObject("data");
  27. $response->assertJson([
  28. 'data' => [
  29. 'token' => true,
  30. ]
  31. ]);
  32. }
  33. }
  34. public function test_user_can_logout(): void
  35. {
  36. $user = User::factory()->create([
  37. 'username' => 'lpc'
  38. ]);
  39. Sanctum::actingAs($user);
  40. $response = $this->post('/api/logout');
  41. $response->assertStatus(204);
  42. }
  43. }