|
14 | 14 | namespace Tests\Controllers; |
15 | 15 |
|
16 | 16 | use CodeIgniter\Config\Factories; |
| 17 | +use CodeIgniter\Exceptions\PageNotFoundException; |
17 | 18 | use CodeIgniter\I18n\Time; |
18 | 19 | use CodeIgniter\Shield\Authentication\Actions\EmailActivator; |
19 | 20 | use CodeIgniter\Shield\Authentication\Authenticators\Session; |
@@ -47,6 +48,14 @@ protected function setUp(): void |
47 | 48 | Services::injectMock('routes', $routes); |
48 | 49 | } |
49 | 50 |
|
| 51 | + protected function tearDown(): void |
| 52 | + { |
| 53 | + parent::tearDown(); |
| 54 | + |
| 55 | + // Clean up any robot user agent set in tests |
| 56 | + unset($_SERVER['HTTP_USER_AGENT']); |
| 57 | + } |
| 58 | + |
50 | 59 | public function testAfterLoggedInNotAllowDisplayMagicLink(): void |
51 | 60 | { |
52 | 61 | $this->user->createEmailIdentity([ |
@@ -177,4 +186,28 @@ public function testMagicLinkVerifyRedirectsIfNotAllowed(): void |
177 | 186 | lang('Auth.magicLinkDisabled'), |
178 | 187 | ); |
179 | 188 | } |
| 189 | + |
| 190 | + public function testMagicLinkVerifyReturns404ForRobotUserAgent(): void |
| 191 | + { |
| 192 | + $this->expectException(PageNotFoundException::class); |
| 193 | + |
| 194 | + /** @var User $user */ |
| 195 | + $user = fake(UserModel::class); |
| 196 | + $user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret123']); |
| 197 | + |
| 198 | + $identities = model(UserIdentityModel::class); |
| 199 | + |
| 200 | + // Insert User Identity for Magic link login |
| 201 | + $identities->insert([ |
| 202 | + 'user_id' => $user->id, |
| 203 | + 'type' => Session::ID_TYPE_MAGIC_LINK, |
| 204 | + 'secret' => 'validtoken123', |
| 205 | + 'expires' => Time::now()->addMinutes(60), |
| 206 | + ]); |
| 207 | + |
| 208 | + // Simulate a robot user agent |
| 209 | + $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'; |
| 210 | + |
| 211 | + $this->get(route_to('verify-magic-link') . '?token=validtoken123'); |
| 212 | + } |
180 | 213 | } |
0 commit comments