|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Settings; |
| 4 | + |
| 5 | +use App\Models\User; |
| 6 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 7 | +use Illuminate\Support\Facades\Hash; |
| 8 | +use Tests\TestCase; |
| 9 | + |
| 10 | +class EmailControllerTest extends TestCase { |
| 11 | + use RefreshDatabase; |
| 12 | + |
| 13 | + private const CHANGE_EMAIL_ENDPOINT = '/api/settings/email'; |
| 14 | + |
| 15 | + public function user_can_update_email_with_correct_password() { |
| 16 | + $user = User::factory()->create([ |
| 17 | + 'email' => 'test@eccc.ca', |
| 18 | + 'password' => Hash::make('old-password'), |
| 19 | + ]); |
| 20 | + |
| 21 | + $this->actingAs($user); |
| 22 | + |
| 23 | + $response = $this->putJson(self::CHANGE_EMAIL_ENDPOINT, [ |
| 24 | + 'email' => 'test2@eccc.ca', |
| 25 | + 'password' => 'old-password', |
| 26 | + ]); |
| 27 | + |
| 28 | + $response->assertNoContent(); |
| 29 | + $this->assertTrue($user->fresh()->email === 'test2@eccc.ca'); |
| 30 | + } |
| 31 | + |
| 32 | + public function update_fails_with_incorrect_password() { |
| 33 | + $user = User::factory()->create([ |
| 34 | + 'email' => 'test@eccc.ca', |
| 35 | + 'password' => Hash::make('current-password'), |
| 36 | + ]); |
| 37 | + |
| 38 | + $this->actingAs($user); |
| 39 | + |
| 40 | + $response = $this->putJson(self::CHANGE_EMAIL_ENDPOINT, [ |
| 41 | + 'email' => fake()->unique()->safeEmail, |
| 42 | + 'password' => 'wrong-password', |
| 43 | + ]); |
| 44 | + |
| 45 | + $response->assertStatus(422); |
| 46 | + $response->assertJsonValidationErrors('password'); |
| 47 | + } |
| 48 | + |
| 49 | + public function update_fails_if_email_exists() { |
| 50 | + $testUser = $this->getFakeUser(); |
| 51 | + $otherUser = $this->getFakeUser(); |
| 52 | + |
| 53 | + $user = User::factory()->create([ |
| 54 | + 'email' => $testUser['email'], |
| 55 | + 'password' => Hash::make($testUser['password']), |
| 56 | + ]); |
| 57 | + |
| 58 | + User::factory()->create([ |
| 59 | + 'email' => $otherUser['email'], |
| 60 | + 'password' => Hash::make($otherUser['password']), |
| 61 | + ]); |
| 62 | + |
| 63 | + $this->actingAs($user); |
| 64 | + |
| 65 | + $response = $this->putJson(self::CHANGE_EMAIL_ENDPOINT, [ |
| 66 | + 'email' => $otherUser['email'], |
| 67 | + 'password' => $testUser['password'], |
| 68 | + ]); |
| 69 | + |
| 70 | + $response->assertStatus(422); |
| 71 | + $response->assertJsonValidationErrors('email'); |
| 72 | + } |
| 73 | + |
| 74 | + public function update_fails_if_email_is_the_same() { |
| 75 | + $testUser = $this->getFakeUser(); |
| 76 | + |
| 77 | + $user = User::factory()->create([ |
| 78 | + 'email' => $testUser['email'], |
| 79 | + 'password' => Hash::make('old-password'), |
| 80 | + ]); |
| 81 | + |
| 82 | + $this->actingAs($user); |
| 83 | + |
| 84 | + $response = $this->putJson(self::CHANGE_EMAIL_ENDPOINT, [ |
| 85 | + 'email' => $testUser['email'], |
| 86 | + 'password' => 'old-password', |
| 87 | + ]); |
| 88 | + |
| 89 | + $response->assertStatus(422); |
| 90 | + $response->assertJsonValidationErrors('email'); |
| 91 | + } |
| 92 | + |
| 93 | + private function getFakeUser(): array { |
| 94 | + return ["email" => fake()->unique()->safeEmail, "password" => fake()->password(8)]; |
| 95 | + } |
| 96 | +} |
0 commit comments