|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright 2016 - 2026, Cake Development Corporation (https://cakedc.com) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright 2016 - 2026, Cake Development Corporation (https://cakedc.com) |
| 11 | + * @license MIT License (https://www.opensource.org/licenses/mit-license.php) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CakeDC\Api\Test\TestCase\Service\Action\Traits; |
| 15 | + |
| 16 | +use Cake\Core\Configure; |
| 17 | +use Cake\Http\ServerRequest; |
| 18 | +use Cake\Log\Log; |
| 19 | +use Cake\TestSuite\TestCase; |
| 20 | +use CakeDC\Api\Service\Action\Traits\ReCaptchaTrait; |
| 21 | + |
| 22 | +/** |
| 23 | + * Only the global setting turns reCaptcha validation off. |
| 24 | + */ |
| 25 | +class ReCaptchaTraitTest extends TestCase |
| 26 | +{ |
| 27 | + public function tearDown(): void |
| 28 | + { |
| 29 | + Configure::delete('Api.reCaptcha'); |
| 30 | + unset($_SERVER['HTTP_REFERER']); |
| 31 | + parent::tearDown(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param bool|null $success Result of the verification, or null for no reCaptcha instance. |
| 36 | + * @return object |
| 37 | + */ |
| 38 | + protected function subject(?bool $success) |
| 39 | + { |
| 40 | + $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'example.com']]); |
| 41 | + |
| 42 | + return new class ($request, $success) { |
| 43 | + use ReCaptchaTrait; |
| 44 | + |
| 45 | + public $request; |
| 46 | + |
| 47 | + public $success; |
| 48 | + |
| 49 | + public function __construct($request, $success) |
| 50 | + { |
| 51 | + $this->request = $request; |
| 52 | + $this->success = $success; |
| 53 | + } |
| 54 | + |
| 55 | + public function getService() |
| 56 | + { |
| 57 | + $request = $this->request; |
| 58 | + |
| 59 | + return new class ($request) { |
| 60 | + private $request; |
| 61 | + |
| 62 | + public function __construct($request) |
| 63 | + { |
| 64 | + $this->request = $request; |
| 65 | + } |
| 66 | + |
| 67 | + public function getRequest() |
| 68 | + { |
| 69 | + return $this->request; |
| 70 | + } |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + protected function _getReCaptchaInstance() |
| 75 | + { |
| 76 | + if ($this->success === null) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + $success = $this->success; |
| 80 | + |
| 81 | + return new class ($success) { |
| 82 | + private $success; |
| 83 | + |
| 84 | + public function __construct($success) |
| 85 | + { |
| 86 | + $this->success = $success; |
| 87 | + } |
| 88 | + |
| 89 | + public function verify($response, $ip) |
| 90 | + { |
| 91 | + $success = $this->success && $response === 'valid'; |
| 92 | + |
| 93 | + return new class ($success) { |
| 94 | + private $success; |
| 95 | + |
| 96 | + public function __construct($success) |
| 97 | + { |
| 98 | + $this->success = $success; |
| 99 | + } |
| 100 | + |
| 101 | + public function isSuccess() |
| 102 | + { |
| 103 | + return $this->success; |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + }; |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + public function testGlobalDisabledSkipsValidation(): void |
| 113 | + { |
| 114 | + Configure::write('Api.reCaptcha.disabled', true); |
| 115 | + $this->assertTrue($this->subject(false)->validateReCaptcha('any')); |
| 116 | + } |
| 117 | + |
| 118 | + public function testValidTokenPasses(): void |
| 119 | + { |
| 120 | + $this->assertTrue($this->subject(true)->validateReCaptcha('valid')); |
| 121 | + $this->assertFalse($this->subject(true)->validateReCaptcha('invalid')); |
| 122 | + } |
| 123 | + |
| 124 | + public function testDisabledForTheDomainStillValidates(): void |
| 125 | + { |
| 126 | + Configure::write('Api.reCaptcha.example$com', ['disabled' => true, 'secret' => 'secret']); |
| 127 | + $this->assertFalse($this->subject(true)->validateReCaptcha('invalid')); |
| 128 | + } |
| 129 | + |
| 130 | + public function testDisabledForAnotherDomainStillValidates(): void |
| 131 | + { |
| 132 | + Configure::write('Api.reCaptcha.dev$test', ['disabled' => true]); |
| 133 | + $_SERVER['HTTP_REFERER'] = 'https://dev.test/'; |
| 134 | + $this->assertFalse($this->subject(true)->validateReCaptcha('invalid')); |
| 135 | + } |
| 136 | + |
| 137 | + public function testNoReCaptchaInstanceFails(): void |
| 138 | + { |
| 139 | + $this->assertFalse($this->subject(null)->validateReCaptcha('valid')); |
| 140 | + } |
| 141 | + |
| 142 | + public function testGlobalDisabledMustBeTrue(): void |
| 143 | + { |
| 144 | + // a string from the environment does not turn validation off |
| 145 | + Configure::write('Api.reCaptcha.disabled', '1'); |
| 146 | + $this->assertFalse($this->subject(true)->validateReCaptcha('invalid')); |
| 147 | + } |
| 148 | + |
| 149 | + public function testNoSecretFails(): void |
| 150 | + { |
| 151 | + // the real lookup: no secret for the domain means no reCaptcha instance |
| 152 | + Configure::write('Api.reCaptcha.example$com', ['disabled' => true]); |
| 153 | + $request = new ServerRequest(['environment' => ['HTTP_HOST' => 'example.com']]); |
| 154 | + $subject = new class ($request) { |
| 155 | + use ReCaptchaTrait; |
| 156 | + |
| 157 | + public $request; |
| 158 | + |
| 159 | + public function __construct($request) |
| 160 | + { |
| 161 | + $this->request = $request; |
| 162 | + } |
| 163 | + |
| 164 | + public function getService() |
| 165 | + { |
| 166 | + $request = $this->request; |
| 167 | + |
| 168 | + return new class ($request) { |
| 169 | + private $request; |
| 170 | + |
| 171 | + public function __construct($request) |
| 172 | + { |
| 173 | + $this->request = $request; |
| 174 | + } |
| 175 | + |
| 176 | + public function getRequest() |
| 177 | + { |
| 178 | + return $this->request; |
| 179 | + } |
| 180 | + }; |
| 181 | + } |
| 182 | + }; |
| 183 | + |
| 184 | + $this->assertFalse($subject->validateReCaptcha('valid')); |
| 185 | + } |
| 186 | + |
| 187 | + public function testDisabledForTheDomainIsLogged(): void |
| 188 | + { |
| 189 | + // like the loggers of the application skeleton, which take messages without a scope |
| 190 | + Log::setConfig('recaptcha_test', ['className' => 'Array', 'levels' => ['warning'], 'scopes' => false]); |
| 191 | + Configure::write('Api.reCaptcha.example$com', ['disabled' => true]); |
| 192 | + try { |
| 193 | + $this->subject(true)->validateReCaptcha('invalid'); |
| 194 | + $logged = implode("\n", Log::engine('recaptcha_test')->read()); |
| 195 | + } finally { |
| 196 | + Log::drop('recaptcha_test'); |
| 197 | + } |
| 198 | + |
| 199 | + $this->assertStringContainsString('`Api.reCaptcha.example$com.disabled` is ignored', $logged); |
| 200 | + } |
| 201 | +} |
0 commit comments