|
| 1 | +<?php namespace Tests; |
| 2 | +/** |
| 3 | + * Copyright 2026 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | +use App\Jobs\AddUserAction; |
| 15 | +use Auth\User; |
| 16 | +use Illuminate\Support\Facades\Config; |
| 17 | +use Illuminate\Support\Facades\Queue; |
| 18 | +use models\exceptions\ValidationException; |
| 19 | + |
| 20 | +class UserPasswordShapeRegressionTest extends TestCase |
| 21 | +{ |
| 22 | + private function setPasswordPolicyForTest(): void |
| 23 | + { |
| 24 | + // Keep these aligned with your real config defaults (or just read them from config) |
| 25 | + Config::set('auth.password_min_length', 8); |
| 26 | + Config::set('auth.password_max_length', 128); |
| 27 | + Config::set('auth.password_shape_warning', 'Password does not meet complexity requirements.'); |
| 28 | + |
| 29 | + // IMPORTANT: hyphen is escaped (or move it to end of the class) |
| 30 | + Config::set( |
| 31 | + 'auth.password_shape_pattern', |
| 32 | + '^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*+\-])[A-Za-z0-9#?!@$%^&*+\-]+$' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public static function validPasswordsProvider(): array |
| 37 | + { |
| 38 | + return [ |
| 39 | + // has uppercase, lowercase, digit, and "-" special char |
| 40 | + ['Abcdef1-'], |
| 41 | + ['Zz9-aaaaa'], |
| 42 | + ['Aaaaaa1-+'], // multiple specials still ok (if + is allowed) |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider validPasswordsProvider |
| 48 | + */ |
| 49 | + public function test_set_password_allows_hyphen(string $plainPassword): void |
| 50 | + { |
| 51 | + $this->setPasswordPolicyForTest(); |
| 52 | + |
| 53 | + // Prevent actually queueing anything (setPassword dispatches AddUserAction) |
| 54 | + Queue::fake(); |
| 55 | + |
| 56 | + $user = new User(); |
| 57 | + $user->setEmail('test@example.org'); |
| 58 | + |
| 59 | + $user->setPassword($plainPassword); |
| 60 | + |
| 61 | + $this->assertTrue($user->hasPasswordSet()); |
| 62 | + $this->assertNotEmpty($user->getPassword()); |
| 63 | + |
| 64 | + // Optional: ensures we didn't break side-effect expectations |
| 65 | + Queue::assertPushed(AddUserAction::class); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_password_missing_special_character_is_rejected(): void |
| 69 | + { |
| 70 | + $this->setPasswordPolicyForTest(); |
| 71 | + Queue::fake(); |
| 72 | + |
| 73 | + $user = new User(); |
| 74 | + $user->setEmail('test@example.org'); |
| 75 | + |
| 76 | + $this->expectException(ValidationException::class); |
| 77 | + $user->setPassword('Abcdef12'); // no special char |
| 78 | + } |
| 79 | + |
| 80 | + public function test_password_with_hyphen_matches_current_regex(): void |
| 81 | + { |
| 82 | + $this->setPasswordPolicyForTest(); |
| 83 | + |
| 84 | + $pattern = Config::get('auth.password_shape_pattern'); |
| 85 | + $this->assertSame(1, preg_match("/$pattern/", 'Abcdef1-')); |
| 86 | + } |
| 87 | +} |
0 commit comments