|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Feature; |
| 6 | + |
| 7 | +use Illuminate\Foundation\Testing\DatabaseTruncation; |
| 8 | +use HiEvents\Models\AccountConfiguration; |
| 9 | +use HiEvents\Models\User; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class LoginTest extends TestCase |
| 13 | +{ |
| 14 | + use DatabaseTruncation; |
| 15 | + |
| 16 | + private const API_PREFIX = ''; |
| 17 | + private const LOGIN_ROUTE = self::API_PREFIX . '/auth/login'; |
| 18 | + private const LOGOUT_ROUTE = self::API_PREFIX . '/auth/logout'; |
| 19 | + private const USERS_ME_ROUTE = self::API_PREFIX . '/users/me'; |
| 20 | + |
| 21 | + public function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + AccountConfiguration::firstOrCreate(['id' => 1], [ |
| 25 | + 'id' => 1, |
| 26 | + 'name' => 'Default', |
| 27 | + 'is_system_default' => true, |
| 28 | + 'application_fees' => json_encode(['percentage' => 1.5, 'fixed' => 0]), |
| 29 | + ]); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_login_with_valid_credentials(): void |
| 33 | + { |
| 34 | + $password = fake()->password(16); |
| 35 | + $user = User::factory()->password($password)->withAccount()->create(); |
| 36 | + |
| 37 | + $response = $this->postJson(self::LOGIN_ROUTE, [ |
| 38 | + 'email' => $user->email, |
| 39 | + 'password' => $password, |
| 40 | + ]); |
| 41 | + |
| 42 | + $response->assertCookie('token'); |
| 43 | + $response->assertHeader('X-Auth-Token'); |
| 44 | + $response->assertJsonStructure([ |
| 45 | + 'token', |
| 46 | + 'token_type', |
| 47 | + 'expires_in', |
| 48 | + 'user', |
| 49 | + 'accounts' |
| 50 | + ]); |
| 51 | + |
| 52 | + // removes warning "This test did not perform any assertions" |
| 53 | + $this->assertTrue(true); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_login_with_invalid_credentials(): void |
| 57 | + { |
| 58 | + $password = fake()->password(16); |
| 59 | + $user = User::factory()->password($password)->withAccount()->create(); |
| 60 | + |
| 61 | + $response = $this->postJson(self::LOGIN_ROUTE, [ |
| 62 | + 'email' => $user->email, |
| 63 | + 'password' => 'invalid_password', |
| 64 | + ]); |
| 65 | + |
| 66 | + $response->assertStatus(401); |
| 67 | + $response->assertCookieMissing('token'); |
| 68 | + $response->assertHeaderMissing('X-Auth-Token'); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + public function test_logout(): void |
| 73 | + { |
| 74 | + $password = fake()->password(16); |
| 75 | + $user = User::factory()->password($password)->withAccount()->create(); |
| 76 | + |
| 77 | + $response = $this->postJson(self::LOGIN_ROUTE, [ |
| 78 | + 'email' => $user->email, |
| 79 | + 'password' => $password, |
| 80 | + ]); |
| 81 | + $response->assertCookie('token'); |
| 82 | + |
| 83 | + $response2 = $this->postJson(self::LOGOUT_ROUTE, [], [ |
| 84 | + 'Authorization' => 'Bearer ' . $response->headers->get('X-Auth-Token'), |
| 85 | + ]); |
| 86 | + $response2->assertStatus(200); |
| 87 | + $response2->assertCookieExpired('token'); |
| 88 | + |
| 89 | + // try to use the expired token |
| 90 | + $response3 = $this->getJson(self::USERS_ME_ROUTE, [ |
| 91 | + 'Authorization' => 'Bearer ' . $response->headers->get('X-Auth-Token'), |
| 92 | + ]); |
| 93 | + $response3->assertStatus(401); |
| 94 | + } |
| 95 | +} |
0 commit comments