|
| 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\Session; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class TurnstileProtectedControllersTest |
| 19 | + * |
| 20 | + * Smoke tests verifying that cf-turnstile-response is always required on the |
| 21 | + * five auth endpoints that gate every submission behind Turnstile (unlike |
| 22 | + * UserController::postLogin, which only activates the rule above a threshold). |
| 23 | + */ |
| 24 | +final class TurnstileProtectedControllersTest extends BrowserKitTestCase |
| 25 | +{ |
| 26 | + protected function prepareForTests(): void |
| 27 | + { |
| 28 | + parent::prepareForTests(); |
| 29 | + Session::start(); |
| 30 | + } |
| 31 | + |
| 32 | + private function sessionHasValidationError(string $field): bool |
| 33 | + { |
| 34 | + $errors = $this->app['session']->driver()->get('errors'); |
| 35 | + return $errors !== null && $errors->has($field); |
| 36 | + } |
| 37 | + |
| 38 | + private function postWithSession(string $url, array $data = []): void |
| 39 | + { |
| 40 | + $this->call('GET', $url); |
| 41 | + $this->call('POST', $url, array_merge(['_token' => Session::token()], $data)); |
| 42 | + } |
| 43 | + |
| 44 | + // ------------------------------------------------------------------------- |
| 45 | + // RegisterController |
| 46 | + // ------------------------------------------------------------------------- |
| 47 | + |
| 48 | + public function testRegisterRequiresTurnstileToken(): void |
| 49 | + { |
| 50 | + $this->postWithSession('/auth/register', [ |
| 51 | + 'first_name' => 'Test', |
| 52 | + 'last_name' => 'User', |
| 53 | + 'email' => 'turnstile-test@example.com', |
| 54 | + 'country_iso_code'=> 'US', |
| 55 | + 'password' => 'Abcd1234!', |
| 56 | + 'password_confirmation' => 'Abcd1234!', |
| 57 | + // cf-turnstile-response intentionally omitted |
| 58 | + ]); |
| 59 | + |
| 60 | + $this->assertTrue( |
| 61 | + $this->sessionHasValidationError('cf-turnstile-response'), |
| 62 | + 'RegisterController must require cf-turnstile-response' |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + // ------------------------------------------------------------------------- |
| 67 | + // ForgotPasswordController |
| 68 | + // ------------------------------------------------------------------------- |
| 69 | + |
| 70 | + public function testForgotPasswordRequiresTurnstileToken(): void |
| 71 | + { |
| 72 | + $this->postWithSession('/auth/password/email', [ |
| 73 | + 'email' => 'anyone@example.com', |
| 74 | + // cf-turnstile-response intentionally omitted |
| 75 | + ]); |
| 76 | + |
| 77 | + $this->assertTrue( |
| 78 | + $this->sessionHasValidationError('cf-turnstile-response'), |
| 79 | + 'ForgotPasswordController must require cf-turnstile-response' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + // ------------------------------------------------------------------------- |
| 84 | + // ResetPasswordController |
| 85 | + // ------------------------------------------------------------------------- |
| 86 | + |
| 87 | + public function testResetPasswordRequiresTurnstileToken(): void |
| 88 | + { |
| 89 | + $this->postWithSession('/auth/password/reset', [ |
| 90 | + 'token' => 'any-reset-token', |
| 91 | + 'password' => 'Abcd1234!', |
| 92 | + 'password_confirmation' => 'Abcd1234!', |
| 93 | + // cf-turnstile-response intentionally omitted |
| 94 | + ]); |
| 95 | + |
| 96 | + $this->assertTrue( |
| 97 | + $this->sessionHasValidationError('cf-turnstile-response'), |
| 98 | + 'ResetPasswordController must require cf-turnstile-response' |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // ------------------------------------------------------------------------- |
| 103 | + // PasswordSetController |
| 104 | + // ------------------------------------------------------------------------- |
| 105 | + |
| 106 | + public function testPasswordSetRequiresTurnstileToken(): void |
| 107 | + { |
| 108 | + $this->postWithSession('/auth/password/set', [ |
| 109 | + 'token' => 'any-set-token', |
| 110 | + 'first_name' => 'Test', |
| 111 | + 'last_name' => 'User', |
| 112 | + 'country_iso_code' => 'US', |
| 113 | + 'password' => 'Abcd1234!', |
| 114 | + 'password_confirmation' => 'Abcd1234!', |
| 115 | + // cf-turnstile-response intentionally omitted |
| 116 | + ]); |
| 117 | + |
| 118 | + $this->assertTrue( |
| 119 | + $this->sessionHasValidationError('cf-turnstile-response'), |
| 120 | + 'PasswordSetController must require cf-turnstile-response' |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + // ------------------------------------------------------------------------- |
| 125 | + // EmailVerificationController |
| 126 | + // ------------------------------------------------------------------------- |
| 127 | + |
| 128 | + public function testEmailVerificationResendRequiresTurnstileToken(): void |
| 129 | + { |
| 130 | + $this->postWithSession('/auth/verification', [ |
| 131 | + 'email' => 'anyone@example.com', |
| 132 | + // cf-turnstile-response intentionally omitted |
| 133 | + ]); |
| 134 | + |
| 135 | + $this->assertTrue( |
| 136 | + $this->sessionHasValidationError('cf-turnstile-response'), |
| 137 | + 'EmailVerificationController must require cf-turnstile-response' |
| 138 | + ); |
| 139 | + } |
| 140 | +} |
0 commit comments