|
| 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 | + |
| 15 | +use Illuminate\Support\Facades\Config; |
| 16 | +use Illuminate\Support\ViewErrorBag; |
| 17 | + |
| 18 | +class PasswordPolicyRenderingTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * The regex the browser receives must classify passwords exactly as the |
| 22 | + * server-side one does. Probes are all 10-30 chars so length is never the |
| 23 | + * discriminator — only the special-character class is under test. |
| 24 | + */ |
| 25 | + public function test_rendered_signup_pattern_agrees_with_server_pattern(): void |
| 26 | + { |
| 27 | + $html = view('auth.register', [ |
| 28 | + 'countries' => [], |
| 29 | + 'client_id' => '', |
| 30 | + 'redirect_uri' => '', |
| 31 | + 'errors' => new ViewErrorBag(), |
| 32 | + ])->render(); |
| 33 | + |
| 34 | + $this->assertSame(1, preg_match('/shape_pattern:\s*(.+?),\R/', $html, $m)); |
| 35 | + |
| 36 | + // Resolve the \uXXXX escapes a JS parser would resolve in the literal. |
| 37 | + // Trim both single quotes ({{ }} format) and double quotes (Js::from format). |
| 38 | + $client = preg_replace_callback( |
| 39 | + '/\\\\u([0-9a-fA-F]{4})/', |
| 40 | + fn ($u) => mb_chr(hexdec($u[1])), |
| 41 | + trim($m[1], "\"'") |
| 42 | + ); |
| 43 | + $server = Config::get('auth.password_shape_pattern'); |
| 44 | + |
| 45 | + foreach (['Passwordma1', 'Passw0rdabc', ';Passw0rdaa', 'Valid1Pass!'] as $probe) { |
| 46 | + $this->assertSame( |
| 47 | + (bool) preg_match("/{$server}/", $probe), |
| 48 | + (bool) preg_match("/{$client}/", $probe), |
| 49 | + "rendered and server patterns disagree on '{$probe}'" |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments