|
| 1 | +<?php |
| 2 | + |
| 3 | +use CraftCms\Cms\Auth\Concerns\ConfirmsPasswords; |
| 4 | +use CraftCms\Cms\User\Elements\User; |
| 5 | +use Illuminate\Support\Facades\Date; |
| 6 | +use Illuminate\Support\Facades\Session; |
| 7 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 8 | + |
| 9 | +use function Pest\Laravel\actingAs; |
| 10 | +use function Pest\Laravel\travel; |
| 11 | + |
| 12 | +class TestConfirmsPasswords |
| 13 | +{ |
| 14 | + use ConfirmsPasswords; |
| 15 | + |
| 16 | + public function __call(string $name, array $arguments) |
| 17 | + { |
| 18 | + return $this->$name(...$arguments); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +beforeEach(function () { |
| 23 | + actingAs(User::find()->first()); |
| 24 | +}); |
| 25 | + |
| 26 | +test('it can mark a password as confirmed', function () { |
| 27 | + Date::setTestNow(now()); |
| 28 | + |
| 29 | + expect(Session::get('auth.password_confirmed_at'))->toBeNull(); |
| 30 | + expect(new TestConfirmsPasswords()->isPasswordConfirmed())->toBeFalse(); |
| 31 | + |
| 32 | + new TestConfirmsPasswords()->confirmPassword(); |
| 33 | + |
| 34 | + expect(Session::get('auth.password_confirmed_at'))->toBe(now()->unix()); |
| 35 | + expect(new TestConfirmsPasswords()->isPasswordConfirmed())->toBeTrue(); |
| 36 | +}); |
| 37 | + |
| 38 | +test('it can require password to be confirmed', function () { |
| 39 | + $this->expectException(HttpException::class); |
| 40 | + |
| 41 | + new TestConfirmsPasswords()->requireConfirmedPassword(); |
| 42 | +}); |
| 43 | + |
| 44 | +test('timeout returns seconds until confirmation is required', function () { |
| 45 | + Date::setTestNow(now()); |
| 46 | + $timeout = config('auth.password_timeout'); |
| 47 | + |
| 48 | + expect(new TestConfirmsPasswords()->confirmedPasswordTimeout())->toBe(0); |
| 49 | + |
| 50 | + new TestConfirmsPasswords()->confirmPassword(); |
| 51 | + |
| 52 | + expect(new TestConfirmsPasswords()->confirmedPasswordTimeout())->toBe($timeout); |
| 53 | + |
| 54 | + travel(5)->seconds(); |
| 55 | + |
| 56 | + expect(new TestConfirmsPasswords()->confirmedPasswordTimeout())->toBe($timeout - 5); |
| 57 | +}); |
| 58 | + |
| 59 | +test('password confirmation can be disabled', function () { |
| 60 | + expect(new TestConfirmsPasswords()->confirmedPasswordTimeout())->toBe(0); |
| 61 | + expect(new TestConfirmsPasswords()->isPasswordConfirmed())->toBeFalse(); |
| 62 | + |
| 63 | + config()->set('auth.password_timeout', -1); |
| 64 | + |
| 65 | + expect(new TestConfirmsPasswords()->confirmedPasswordTimeout())->toBeFalse(); |
| 66 | + expect(new TestConfirmsPasswords()->isPasswordConfirmed())->toBeTrue(); |
| 67 | +}); |
0 commit comments