|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\core\Controller; |
| 6 | + |
| 7 | +use DATE_W3C; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use SimpleSAML\Assert\AssertionFailedException; |
| 10 | +use SimpleSAML\{Configuration, Logger, Session}; |
| 11 | +use SimpleSAML\Module\core\Controller; |
| 12 | +use SimpleSAML\TestUtils\ArrayLogger; |
| 13 | +use SimpleSAML\XHTML\Template; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | + |
| 16 | +use function date; |
| 17 | +use function sprintf; |
| 18 | +use function time; |
| 19 | + |
| 20 | +/** |
| 21 | + * Set of tests for the controllers in the "core" module. |
| 22 | + * |
| 23 | + * @covers \SimpleSAML\Module\core\Controller\Exception |
| 24 | + * @package SimpleSAML\Test |
| 25 | + */ |
| 26 | +class ExceptionTest extends TestCase |
| 27 | +{ |
| 28 | + /** @var \SimpleSAML\Configuration */ |
| 29 | + protected Configuration $config; |
| 30 | + |
| 31 | + /** @var \SimpleSAML\Session */ |
| 32 | + protected Session $session; |
| 33 | + |
| 34 | + |
| 35 | + /** |
| 36 | + * Set up for each test. |
| 37 | + */ |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->config = Configuration::loadFromArray( |
| 43 | + [ |
| 44 | + 'logging.handler' => ArrayLogger::class, |
| 45 | + 'errorreporting' => false, |
| 46 | + 'module.enable' => ['core' => true], |
| 47 | + ], |
| 48 | + '[ARRAY]', |
| 49 | + 'simplesaml' |
| 50 | + ); |
| 51 | + |
| 52 | + Configuration::setPreLoadedConfig($this->config, 'config.php'); |
| 53 | + |
| 54 | + $this->session = Session::getSessionFromRequest(); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Clean up after each test |
| 60 | + */ |
| 61 | + protected function tearDown(): void |
| 62 | + { |
| 63 | + Logger::clearCapturedLog(); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * @dataProvider codeProvider |
| 69 | + * @param string $code |
| 70 | + * Test that we are presented with an 'error was reported' page |
| 71 | + */ |
| 72 | + public function testErrorURL(string $code, string $ts, string $rp, string $tid, string $ctx): void |
| 73 | + { |
| 74 | + $request = Request::create( |
| 75 | + sprintf('/error/%s?ts=%s&rp=%s&tid=%s&ctx=%s', $code, $ts, $rp, $tid, $ctx), |
| 76 | + 'GET', |
| 77 | + ); |
| 78 | + |
| 79 | + $c = new Controller\Exception($this->config, $this->session); |
| 80 | + |
| 81 | + Logger::setCaptureLog(); |
| 82 | + $response = $c->error($request, $code); |
| 83 | + Logger::setCaptureLog(false); |
| 84 | + |
| 85 | + $log = Logger::getCapturedLog(); |
| 86 | + self::assertCount(5, $log); |
| 87 | + |
| 88 | + self::assertStringContainsString( |
| 89 | + "A Service Provider reported the following error during authentication: " |
| 90 | + . sprintf( |
| 91 | + "Code: %s; Timestamp: %s; Relying party: %s; Transaction ID: %s; Context: [%s]", |
| 92 | + $code, |
| 93 | + ($ts === 'ERRORURL_TS') ? 'null' : date(DATE_W3C, intval($ts)), |
| 94 | + ($rp === 'ERRORURL_RP') ? 'null' : $rp, |
| 95 | + ($tid === 'ERRORURL_TID') ? 'null' : $tid, |
| 96 | + ($ctx === 'ERRORURL_CTX') ? '' : urldecode($ctx), |
| 97 | + ), |
| 98 | + $log[0], |
| 99 | + ); |
| 100 | + |
| 101 | + $this->assertInstanceOf(Template::class, $response); |
| 102 | + $this->assertTrue($response->isSuccessful()); |
| 103 | + $this->assertEquals('core:error.twig', $response->getTemplateName()); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + */ |
| 109 | + public static function codeProvider(): array |
| 110 | + { |
| 111 | + $codes = [ |
| 112 | + 'ERRORURL_CODE', |
| 113 | + 'IDENTIFICATION_FAILURE', |
| 114 | + 'AUTHENTICATION_FAILURE', |
| 115 | + 'AUTHORIZATION_FAILURE', |
| 116 | + 'OTHER_ERROR', |
| 117 | + ]; |
| 118 | + |
| 119 | + $tss = ['ERRORURL_TS', strval(time())]; |
| 120 | + $rps = ['ERRORURL_RP', 'phpunit']; |
| 121 | + $tids = ['ERRORURL_TID', '123456']; |
| 122 | + $ctxs = ['ERRORURL_CTX', urlencode("phpunit did it's job")]; |
| 123 | + |
| 124 | + $matrix = []; |
| 125 | + foreach ($codes as $code) { |
| 126 | + foreach ($tss as $ts) { |
| 127 | + foreach ($rps as $rp) { |
| 128 | + foreach ($tids as $tid) { |
| 129 | + foreach ($ctxs as $ctx) { |
| 130 | + $matrix[] = [$code, $ts, $rp, $tid, $ctx]; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return $matrix; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * Test that an exception was thrown when an invalid error code was used. |
| 143 | + */ |
| 144 | + public function testErrorURLInvalidCode(): void |
| 145 | + { |
| 146 | + $request = Request::create( |
| 147 | + '/error/doesNotExist', |
| 148 | + ); |
| 149 | + |
| 150 | + $c = new Controller\Exception($this->config, $this->session); |
| 151 | + |
| 152 | + $this->expectException(AssertionFailedException::class); |
| 153 | + $this->expectExceptionMessage( |
| 154 | + 'Expected one of: "IDENTIFICATION_FAILURE", "AUTHENTICATION_FAILURE",' |
| 155 | + . ' "AUTHORIZATION_FAILURE", "OTHER_ERROR". Got: "doesNotExist"' |
| 156 | + ); |
| 157 | + |
| 158 | + $c->error($request, 'doesNotExist'); |
| 159 | + } |
| 160 | +} |
0 commit comments