55namespace SimpleSAML \Test \Module \cron \Controller ;
66
77use PHPUnit \Framework \Attributes \CoversClass ;
8+ use PHPUnit \Framework \Attributes \DataProvider ;
89use PHPUnit \Framework \TestCase ;
910use SimpleSAML \Configuration ;
1011use 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