Skip to content

Commit 22ca8fe

Browse files
tvdijenthijskh
authored andcommitted
Further improve unit tests
1 parent b6d5ddb commit 22ca8fe

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

modules/cron/src/Controller/Cron.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function run(string $tag, string $key, string $output = 'xhtml'): Respons
135135
$key,
136136
$configKey,
137137
'Cron: Wrong key %s provided. Cron will not run.',
138-
Error\ConfigurationError::class,
138+
Error\Exception::class,
139139
);
140140

141141
$cron = new \SimpleSAML\Module\cron\Cron();

tests/modules/cron/src/Controller/CronTest.php

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SimpleSAML\Test\Module\cron\Controller;
66

77
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\TestCase;
910
use SimpleSAML\Configuration;
1011
use SimpleSAML\Error;
@@ -98,7 +99,7 @@ public function testInfo(): void
9899

99100
/**
100101
*/
101-
public function testRun(): void
102+
public function testRunCorrectKey(): void
102103
{
103104
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/verysecret';
104105

@@ -117,16 +118,75 @@ public function testRun(): void
117118

118119
/**
119120
*/
120-
public function testRunConfiguredSecret(): void
121+
public function testRunWrongKey(): void
121122
{
122-
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/secret';
123+
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/nodice';
124+
125+
$c = new Controller\Cron($this->config, $this->session);
126+
127+
$this->expectException(Error\Exception::class);
128+
$this->expectExceptionMessage('Cron: Wrong key "nodice" provided. Cron will not run.');
129+
130+
$c->run('daily', 'nodice');
131+
}
132+
133+
134+
/**
135+
*/
136+
#[DataProvider('provideDefaultSecret')]
137+
public function testRunDefaultSecret(string $secret): void
138+
{
139+
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/' . $secret;
123140

124141
$c = new Controller\Cron($this->config, $this->session);
125142

126143
$this->expectException(Error\ConfigurationError::class);
127144
$this->expectExceptionMessage('Cron: Possible malicious attempt to run cron tasks with default secret');
128145

129-
$response = $c->run('daily', 'secret');
130-
$this->assertFalse($response->isSuccessful());
146+
$c->run('daily', $secret);
147+
}
148+
149+
150+
/**
151+
*/
152+
#[DataProvider('provideDefaultSecret')]
153+
public function testRunDefaultConfigSecret(string $configKey): void
154+
{
155+
Configuration::setPreLoadedConfig(
156+
Configuration::loadFromArray(
157+
[
158+
'key' => $configKey,
159+
'allowed_tags' => ['daily'],
160+
'sendemail' => false,
161+
],
162+
'[ARRAY]',
163+
'simplesaml',
164+
),
165+
'module_cron.php',
166+
'simplesaml',
167+
);
168+
169+
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/verysecret';
170+
171+
$c = new Controller\Cron($this->config, $this->session);
172+
173+
$this->expectException(Error\ConfigurationError::class);
174+
$this->expectExceptionMessage('Cron: no proper key has been configured.');
175+
176+
$c->run('daily', 'verysecret');
177+
}
178+
179+
180+
/**
181+
* @return array<int, array{0: string}>
182+
*/
183+
public static function provideDefaultSecret(): array
184+
{
185+
return [
186+
// Config template
187+
['secret'],
188+
// Documentation inside several modules
189+
['RANDOM_KEY'],
190+
];
131191
}
132192
}

0 commit comments

Comments
 (0)