|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2026 SURFnet B.V. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace OpenConext\EngineBlockBundle\Tests; |
| 22 | + |
| 23 | +use InvalidArgumentException; |
| 24 | +use Mockery; |
| 25 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 26 | +use OpenConext\EngineBlock\Service\CookieService; |
| 27 | +use OpenConext\EngineBlock\Service\TimeProvider\TimeProvider; |
| 28 | +use OpenConext\EngineBlock\Service\Wayf\RememberedIdpCookie; |
| 29 | +use OpenConext\EngineBlockBundle\Controller\ResetRememberedWayfController; |
| 30 | +use PHPUnit\Framework\Attributes\Test; |
| 31 | +use PHPUnit\Framework\TestCase; |
| 32 | +use Psr\Log\LoggerInterface; |
| 33 | +use Symfony\Component\HttpFoundation\Request; |
| 34 | +use Symfony\Component\HttpFoundation\Response; |
| 35 | + |
| 36 | +class ResetRememberedWayfControllerTest extends TestCase |
| 37 | +{ |
| 38 | + use MockeryPHPUnitIntegration; |
| 39 | + |
| 40 | + private const string REDIRECT_URL = 'https://sp.example.org/after-reset'; |
| 41 | + private const int LIFETIME = 7776000; |
| 42 | + private const int MAX_ENTRIES = 16; |
| 43 | + private const string COOKIE_DOMAIN = 'engine.example.org'; |
| 44 | + private const string COOKIE_PATH = '/'; |
| 45 | + |
| 46 | + #[Test] |
| 47 | + public function cookie_with_valid_entries_is_cleared_and_logged_with_entry_count(): void |
| 48 | + { |
| 49 | + $cookieService = Mockery::mock(CookieService::class); |
| 50 | + $cookieService->shouldReceive('clearCookieWithSameSite') |
| 51 | + ->once() |
| 52 | + ->with( |
| 53 | + RememberedIdpCookie::NAME, |
| 54 | + self::COOKIE_PATH, |
| 55 | + self::COOKIE_DOMAIN, |
| 56 | + true, |
| 57 | + true, |
| 58 | + 'None' |
| 59 | + ) |
| 60 | + ->andReturn(true); |
| 61 | + |
| 62 | + $rememberedIdpCookie = $this->buildRememberedIdpCookie($cookieService); |
| 63 | + $raw = $rememberedIdpCookie->encode([ |
| 64 | + 'https://sp1.example.org' => ['idp' => 'https://idp1.example.org', 'expires' => time() + 3600], |
| 65 | + 'https://sp2.example.org' => ['idp' => 'https://idp2.example.org', 'expires' => time() + 3600], |
| 66 | + ]); |
| 67 | + |
| 68 | + $logger = Mockery::mock(LoggerInterface::class); |
| 69 | + $logger->shouldReceive('info') |
| 70 | + ->once() |
| 71 | + ->with('WAYF-remember-my-choice cookie removed (had 2 entries)'); |
| 72 | + |
| 73 | + $controller = new ResetRememberedWayfController($rememberedIdpCookie, $logger, self::REDIRECT_URL); |
| 74 | + |
| 75 | + $response = $controller($this->buildRequest($raw)); |
| 76 | + |
| 77 | + $this->assertSame(Response::HTTP_FOUND, $response->getStatusCode()); |
| 78 | + $this->assertSame(self::REDIRECT_URL, $response->getTargetUrl()); |
| 79 | + } |
| 80 | + |
| 81 | + #[Test] |
| 82 | + public function invalid_cookie_is_still_cleared_and_logged_with_zero_entries(): void |
| 83 | + { |
| 84 | + $cookieService = Mockery::mock(CookieService::class); |
| 85 | + $cookieService->shouldReceive('clearCookieWithSameSite')->once()->andReturn(true); |
| 86 | + |
| 87 | + $rememberedIdpCookie = $this->buildRememberedIdpCookie($cookieService); |
| 88 | + |
| 89 | + $logger = Mockery::mock(LoggerInterface::class); |
| 90 | + $logger->shouldReceive('info') |
| 91 | + ->once() |
| 92 | + ->with('WAYF-remember-my-choice cookie removed (had 0 entries)'); |
| 93 | + |
| 94 | + $controller = new ResetRememberedWayfController($rememberedIdpCookie, $logger, self::REDIRECT_URL); |
| 95 | + |
| 96 | + $response = $controller($this->buildRequest('not valid base64 or deflated data!')); |
| 97 | + |
| 98 | + $this->assertSame(Response::HTTP_FOUND, $response->getStatusCode()); |
| 99 | + $this->assertSame(self::REDIRECT_URL, $response->getTargetUrl()); |
| 100 | + } |
| 101 | + |
| 102 | + #[Test] |
| 103 | + public function missing_cookie_is_not_cleared_or_logged_but_still_redirects(): void |
| 104 | + { |
| 105 | + $cookieService = Mockery::mock(CookieService::class); |
| 106 | + $rememberedIdpCookie = $this->buildRememberedIdpCookie($cookieService); |
| 107 | + |
| 108 | + $logger = Mockery::mock(LoggerInterface::class); |
| 109 | + $logger->shouldNotReceive('info'); |
| 110 | + |
| 111 | + $controller = new ResetRememberedWayfController($rememberedIdpCookie, $logger, self::REDIRECT_URL); |
| 112 | + |
| 113 | + $response = $controller($this->buildRequest(null)); |
| 114 | + |
| 115 | + $this->assertSame(Response::HTTP_FOUND, $response->getStatusCode()); |
| 116 | + $this->assertSame(self::REDIRECT_URL, $response->getTargetUrl()); |
| 117 | + } |
| 118 | + |
| 119 | + #[Test] |
| 120 | + public function constructor_rejects_an_empty_redirect_url(): void |
| 121 | + { |
| 122 | + $this->expectException(InvalidArgumentException::class); |
| 123 | + |
| 124 | + new ResetRememberedWayfController( |
| 125 | + $this->buildRememberedIdpCookie(Mockery::mock(CookieService::class)), |
| 126 | + Mockery::mock(LoggerInterface::class), |
| 127 | + '' |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + private function buildRememberedIdpCookie(CookieService $cookieService): RememberedIdpCookie |
| 132 | + { |
| 133 | + return new RememberedIdpCookie( |
| 134 | + new TimeProvider(), |
| 135 | + $cookieService, |
| 136 | + self::LIFETIME, |
| 137 | + self::MAX_ENTRIES, |
| 138 | + self::COOKIE_DOMAIN, |
| 139 | + self::COOKIE_PATH, |
| 140 | + true, |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + private function buildRequest(?string $rememberedIdpsCookie): Request |
| 145 | + { |
| 146 | + $request = Request::create('/reset-remember-wayf'); |
| 147 | + if ($rememberedIdpsCookie !== null) { |
| 148 | + $request->cookies->set(RememberedIdpCookie::NAME, $rememberedIdpsCookie); |
| 149 | + } |
| 150 | + |
| 151 | + return $request; |
| 152 | + } |
| 153 | +} |
0 commit comments