Skip to content
Snippets Groups Projects
Unverified Commit 8db8e116 authored by Tim van Dijen's avatar Tim van Dijen Committed by GitHub
Browse files

Add tests for the cron-controller (#1361)

parent 6918dc34
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ declare(strict_types=1); ...@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace SimpleSAML\Module\cron\Controller; namespace SimpleSAML\Module\cron\Controller;
use PHPMailer\PHPMailer\Exception as PHPMailerException;
use SimpleSAML\Auth; use SimpleSAML\Auth;
use SimpleSAML\Auth\AuthenticationFactory; use SimpleSAML\Auth\AuthenticationFactory;
use SimpleSAML\Configuration; use SimpleSAML\Configuration;
...@@ -15,6 +16,7 @@ use SimpleSAML\Session; ...@@ -15,6 +16,7 @@ use SimpleSAML\Session;
use SimpleSAML\Utils; use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template; use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
/** /**
...@@ -35,6 +37,12 @@ class Cron ...@@ -35,6 +37,12 @@ class Cron
/** @var \SimpleSAML\Session */ /** @var \SimpleSAML\Session */
protected $session; protected $session;
/**
* @var \SimpleSAML\Utils\Auth|string
* @psalm-var \SimpleSAML\Utils\Auth|class-string
*/
protected $authUtils = Utils\Auth::class;
/** /**
* Controller constructor. * Controller constructor.
...@@ -57,18 +65,30 @@ class Cron ...@@ -57,18 +65,30 @@ class Cron
} }
/**
* Inject the \SimpleSAML\Utils\Auth dependency.
*
* @param \SimpleSAML\Utils\Auth $authUtils
*/
public function setAuthUtils(Utils\Auth $authUtils): void
{
$this->authUtils = $authUtils;
}
/** /**
* Show cron info. * Show cron info.
* *
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \SimpleSAML\XHTML\Template * @return \SimpleSAML\XHTML\Template
* An HTML template or a redirection if we are not authenticated. * An HTML template or a redirection if we are not authenticated.
*/ */
public function info(): Template public function info(Request $request): Template
{ {
Utils\Auth::requireAdmin(); $this->authUtils::requireAdmin();
$key = $this->cronconfig->getValue('key', 'secret'); $key = $this->cronconfig->getValue('key', 'secret');
$tags = $this->cronconfig->getValue('allowed_tags'); $tags = $this->cronconfig->getValue('allowed_tags', []);
$def = [ $def = [
'weekly' => "22 0 * * 0", 'weekly' => "22 0 * * 0",
...@@ -98,6 +118,7 @@ class Cron ...@@ -98,6 +118,7 @@ class Cron
* *
* This controller will start a cron operation * This controller will start a cron operation
* *
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $tag The tag * @param string $tag The tag
* @param string $key The secret key * @param string $key The secret key
* @param string $output The output format, defaulting to xhtml * @param string $output The output format, defaulting to xhtml
...@@ -107,7 +128,7 @@ class Cron ...@@ -107,7 +128,7 @@ class Cron
* *
* @throws \SimpleSAML\Error\Exception * @throws \SimpleSAML\Error\Exception
*/ */
public function run(string $tag, string $key, string $output = 'xhtml'): Response public function run(Request $request, string $tag, string $key, string $output = 'xhtml'): Response
{ {
$configKey = $this->cronconfig->getValue('key', 'secret'); $configKey = $this->cronconfig->getValue('key', 'secret');
if ($key !== $configKey) { if ($key !== $configKey) {
...@@ -132,7 +153,7 @@ class Cron ...@@ -132,7 +153,7 @@ class Cron
$mail->setData(['url' => $url, 'tag' => $croninfo['tag'], 'summary' => $croninfo['summary']]); $mail->setData(['url' => $url, 'tag' => $croninfo['tag'], 'summary' => $croninfo['summary']]);
try { try {
$mail->send(); $mail->send();
} catch (\PHPMailer\PHPMailer\Exception $e) { } catch (PHPMailerException $e) {
Logger::warning("Unable to send cron report; " . $e->getMessage()); Logger::warning("Unable to send cron report; " . $e->getMessage());
} }
} }
......
...@@ -59,6 +59,7 @@ class Cron ...@@ -59,6 +59,7 @@ class Cron
Logger::debug('Cron - Summary: ' . $s); Logger::debug('Cron - Summary: ' . $s);
} }
/** @psalm-suppress NullableReturnStatement */
return $croninfo; return $croninfo;
} }
......
<?php
namespace SimpleSAML\Module\cron;
use SimpleSAML\Configuration;
use SimpleSAML\Session;
use Symfony\Component\HttpFoundation\Request;
$config = Configuration::getInstance();
$session = Session::getSessionFromRequest();
$request = Request::createFromGlobals();
$tag = $request->get('tag');
$key = $request->get('key');
$output = $request->get('output');
$controller = new Controller\Cron($config, $session);
$response = $controller->run($tag, $key, $output);
$response->send();
<?php
/**
* The _include script registers a autoloader for the SimpleSAMLphp libraries. It also
* initializes the SimpleSAMLphp config class with the correct path.
*/
namespace SimpleSAML\Module\cron;
use SimpleSAML\Configuration;
use SimpleSAML\Session;
$config = Configuration::getInstance();
$session = Session::getSessionFromRequest();
$controller = new Controller\Cron($config, $session);
$response = $controller->info();
$response->send();
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
<directory suffix=".php">./lib/</directory> <directory suffix=".php">./lib/</directory>
<directory suffix=".php">./modules/admin/lib/</directory> <directory suffix=".php">./modules/admin/lib/</directory>
<directory suffix=".php">./modules/core/lib/</directory> <directory suffix=".php">./modules/core/lib/</directory>
<directory suffix=".php">./modules/cron/lib/</directory>
<directory suffix=".php">./modules/saml/lib/</directory> <directory suffix=".php">./modules/saml/lib/</directory>
<exclude> <exclude>
<directory>./vendor/</directory> <directory>./vendor/</directory>
......
<?php
declare(strict_types=1);
namespace SimpleSAML\Test\Module\cron\Controller;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
//use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Module\cron\Controller;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Set of tests for the controllers in the "cron" module.
*
* @package SimpleSAML\Test
*/
class CronTest extends TestCase
{
/** @var \SimpleSAML\Configuration */
protected $config;
/** @var \SimpleSAML\Session */
protected $session;
/** @var \SimpleSAML\Utils\Auth */
protected $authUtils;
/**
* Set up for each test.
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->config = Configuration::loadFromArray(
[
'module.enable' => ['cron' => true],
'secretsalt' => 'defaultsecretsalt'
],
'[ARRAY]',
'simplesaml'
);
$this->session = Session::getSessionFromRequest();
$this->authUtils = new class () extends Utils\Auth {
public static function requireAdmin(): void
{
// stub
}
};
Configuration::setPreLoadedConfig(
Configuration::loadFromArray(
[
'key' => 'secret',
'allowed_tags' => ['daily'],
'sendemail' => false,
],
'[ARRAY]',
'simplesaml'
),
'module_cron.php',
'simplesaml'
);
}
/**
* @return void
*/
public function testInfo(): void
{
$_SERVER['REQUEST_URI'] = '/module.php/cron/info';
$request = Request::create(
'/info',
'GET'
);
$c = new Controller\Cron($this->config, $this->session);
$c->setAuthUtils($this->authUtils);
$response = $c->info($request);
$this->assertTrue($response->isSuccessful());
}
/**
* @return void
*/
public function testRun(): void
{
$_SERVER['REQUEST_URI'] = '/module.php/cron/run/daily/secret';
$request = Request::create(
'/run/daily/secret',
'GET'
);
$c = new Controller\Cron($this->config, $this->session);
$response = $c->run($request, 'daily', 'secret');
$this->assertTrue($response->isSuccessful());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment