|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Auth; |
| 4 | + |
| 5 | +use Illuminate\Auth\Passwords\PasswordBroker as ConcretePasswordBroker; |
| 6 | +use Illuminate\Support\Facades\Hash; |
| 7 | +use Illuminate\Support\Facades\Notification; |
| 8 | +use Illuminate\Support\Facades\Password; |
| 9 | +use ProcessMaker\Models\User; |
| 10 | +use ProcessMaker\Notifications\ResetPassword as ResetPasswordNotification; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class PasswordResetTest extends TestCase |
| 14 | +{ |
| 15 | + public function testForgotPasswordDoesNotNotifyBlockedUser(): void |
| 16 | + { |
| 17 | + Notification::fake(); |
| 18 | + |
| 19 | + $user = User::factory()->create([ |
| 20 | + 'email' => 'blocked-forgot@example.com', |
| 21 | + 'status' => 'BLOCKED', |
| 22 | + ]); |
| 23 | + |
| 24 | + $response = $this->post(route('password.email'), [ |
| 25 | + 'email' => $user->email, |
| 26 | + ]); |
| 27 | + |
| 28 | + $response->assertSessionHas('status'); |
| 29 | + Notification::assertNothingSent(); |
| 30 | + } |
| 31 | + |
| 32 | + public function testForgotPasswordDoesNotNotifyInactiveUser(): void |
| 33 | + { |
| 34 | + Notification::fake(); |
| 35 | + |
| 36 | + $user = User::factory()->create([ |
| 37 | + 'email' => 'inactive-forgot@example.com', |
| 38 | + 'status' => 'INACTIVE', |
| 39 | + ]); |
| 40 | + |
| 41 | + $response = $this->post(route('password.email'), [ |
| 42 | + 'email' => $user->email, |
| 43 | + ]); |
| 44 | + |
| 45 | + $response->assertSessionHas('status'); |
| 46 | + Notification::assertNothingSent(); |
| 47 | + } |
| 48 | + |
| 49 | + public function testForgotPasswordSendsNotificationToActiveUser(): void |
| 50 | + { |
| 51 | + Notification::fake(); |
| 52 | + |
| 53 | + $user = User::factory()->create([ |
| 54 | + 'email' => 'active-forgot@example.com', |
| 55 | + 'status' => 'ACTIVE', |
| 56 | + ]); |
| 57 | + |
| 58 | + $response = $this->post(route('password.email'), [ |
| 59 | + 'email' => $user->email, |
| 60 | + ]); |
| 61 | + |
| 62 | + $response->assertSessionHas('status'); |
| 63 | + Notification::assertSentTo($user, ResetPasswordNotification::class); |
| 64 | + } |
| 65 | + |
| 66 | + public function testShowResetFormRedirectsBlockedUserToRequestForm(): void |
| 67 | + { |
| 68 | + $user = User::factory()->create([ |
| 69 | + 'email' => 'blocked-reset-form@example.com', |
| 70 | + 'status' => 'BLOCKED', |
| 71 | + ]); |
| 72 | + |
| 73 | + $url = route('password.reset', ['token' => 'unused-token']); |
| 74 | + $response = $this->get($url . '?email=' . urlencode($user->email)); |
| 75 | + |
| 76 | + $response->assertRedirect(route('password.request')); |
| 77 | + $response->assertSessionHasErrors([ |
| 78 | + 'email' => __('passwords.blocked'), |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + public function testShowResetFormRedirectsInactiveUserToRequestForm(): void |
| 83 | + { |
| 84 | + $user = User::factory()->create([ |
| 85 | + 'email' => 'inactive-reset-form@example.com', |
| 86 | + 'status' => 'INACTIVE', |
| 87 | + ]); |
| 88 | + |
| 89 | + $url = route('password.reset', ['token' => 'unused-token']); |
| 90 | + $response = $this->get($url . '?email=' . urlencode($user->email)); |
| 91 | + |
| 92 | + $response->assertRedirect(route('password.request')); |
| 93 | + $response->assertSessionHasErrors([ |
| 94 | + 'email' => __('passwords.inactive'), |
| 95 | + ]); |
| 96 | + } |
| 97 | + |
| 98 | + public function testShowResetFormDisplaysForActiveUser(): void |
| 99 | + { |
| 100 | + $user = User::factory()->create([ |
| 101 | + 'email' => 'active-reset-form@example.com', |
| 102 | + 'status' => 'ACTIVE', |
| 103 | + 'username' => 'active_reset_user', |
| 104 | + ]); |
| 105 | + |
| 106 | + $url = route('password.reset', ['token' => 'some-token']); |
| 107 | + $response = $this->get($url . '?email=' . urlencode($user->email)); |
| 108 | + |
| 109 | + $response->assertOk(); |
| 110 | + $response->assertViewIs('auth.passwords.reset'); |
| 111 | + $response->assertViewHas('email', $user->email); |
| 112 | + $response->assertViewHas('username', $user->username); |
| 113 | + $response->assertViewHas('token', 'some-token'); |
| 114 | + } |
| 115 | + |
| 116 | + public function testResetPasswordRejectsBlockedUser(): void |
| 117 | + { |
| 118 | + $user = User::factory()->create([ |
| 119 | + 'email' => 'blocked-reset-post@example.com', |
| 120 | + 'status' => 'BLOCKED', |
| 121 | + ]); |
| 122 | + |
| 123 | + $response = $this->from(route('password.request'))->post('/password/reset', [ |
| 124 | + 'token' => 'will-not-be-used', |
| 125 | + 'email' => $user->email, |
| 126 | + 'password' => 'NewPassword123!', |
| 127 | + 'password_confirmation' => 'NewPassword123!', |
| 128 | + ]); |
| 129 | + |
| 130 | + $response->assertSessionHasErrors([ |
| 131 | + 'email' => __('passwords.blocked'), |
| 132 | + ]); |
| 133 | + } |
| 134 | + |
| 135 | + public function testResetPasswordRejectsInactiveUser(): void |
| 136 | + { |
| 137 | + $user = User::factory()->create([ |
| 138 | + 'email' => 'inactive-reset-post@example.com', |
| 139 | + 'status' => 'INACTIVE', |
| 140 | + ]); |
| 141 | + |
| 142 | + $response = $this->from(route('password.request'))->post('/password/reset', [ |
| 143 | + 'token' => 'will-not-be-used', |
| 144 | + 'email' => $user->email, |
| 145 | + 'password' => 'NewPassword123!', |
| 146 | + 'password_confirmation' => 'NewPassword123!', |
| 147 | + ]); |
| 148 | + |
| 149 | + $response->assertSessionHasErrors([ |
| 150 | + 'email' => __('passwords.inactive'), |
| 151 | + ]); |
| 152 | + } |
| 153 | + |
| 154 | + public function testResetPasswordUpdatesPasswordForActiveUser(): void |
| 155 | + { |
| 156 | + /** @var User $user */ |
| 157 | + $user = User::factory()->create([ |
| 158 | + 'email' => 'active-reset-post@example.com', |
| 159 | + 'status' => 'ACTIVE', |
| 160 | + ]); |
| 161 | + |
| 162 | + /** @var ConcretePasswordBroker $broker */ |
| 163 | + $broker = Password::broker(); |
| 164 | + $token = $broker->createToken($user); |
| 165 | + $plaintextSecret = 'NewSecurePass123!'; |
| 166 | + |
| 167 | + $response = $this->post('/password/reset', [ |
| 168 | + 'token' => $token, |
| 169 | + 'email' => $user->email, |
| 170 | + 'password' => $plaintextSecret, |
| 171 | + 'password_confirmation' => $plaintextSecret, |
| 172 | + ]); |
| 173 | + |
| 174 | + $response->assertRedirect('/password/success'); |
| 175 | + $response->assertSessionHas('status'); |
| 176 | + |
| 177 | + $user->refresh(); |
| 178 | + $this->assertTrue(Hash::check($plaintextSecret, $user->password)); |
| 179 | + $this->assertAuthenticatedAs($user, 'web'); |
| 180 | + } |
| 181 | +} |
0 commit comments