Skip to content

Commit

Permalink
System filter testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-parry committed Jan 24, 2019
1 parent e3bbc9f commit 5016336
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 47 deletions.
46 changes: 46 additions & 0 deletions tests/system/Filters/CSRFTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\HTTP\ResponseInterface;

/**
* @backupGlobals enabled
*/
class CSRFTest extends \CIUnitTestCase
{

protected $config;
protected $request;
protected $response;

protected function setUp()
{
parent::setUp();
$this->config = new \Config\Filters();
}

//--------------------------------------------------------------------
public function testNormal()
{
$this->config->globals = [
'before' => ['csrf'],
'after' => [],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

// we expect CSRF requests to be ignored in CLI
$expected = $this->request;
$request = $filters->run($uri, 'before');
$this->assertEquals($expected, $request);
}

}
52 changes: 52 additions & 0 deletions tests/system/Filters/DebugToolbarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\HTTP\ResponseInterface;

/**
* @backupGlobals enabled
*/
class DebugToolbarTest extends \CIUnitTestCase
{

protected $request;
protected $response;

protected function setUp()
{
parent::setUp();

$this->request = Services::request();
$this->response = Services::response();
}

//--------------------------------------------------------------------

public function testDebugToolbarFilter()
{
$_SERVER['REQUEST_METHOD'] = 'GET';

$config = new FilterConfig();
$config->globals = [
'before' => ['toolbar'], // not normal; exercising its before()
'after' => ['toolbar'],
];

$filter = new DebugToolbar();

$expectedBefore = $this->request;
$expectedAfter = $this->response;

// nothing should change here, since we have no before logic
$filter->before($this->request);
$this->assertEquals($expectedBefore, $this->request);

// nothing should change here, since we are running in the CLI
$filter->after($this->request, $this->response);
$this->assertEquals($expectedAfter, $this->response);
}

}
1 change: 1 addition & 0 deletions tests/system/Filters/FiltersTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\HTTP\ResponseInterface;
Expand Down
119 changes: 119 additions & 0 deletions tests/system/Filters/HoneypotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace CodeIgniter\Filters;

use Config\Filters as FilterConfig;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Exceptions\FilterException;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\HTTP\ResponseInterface;

/**
* @backupGlobals enabled
*/
class HoneypotTest extends \CIUnitTestCase
{

protected $config;
protected $honey;
protected $request;
protected $response;

protected function setUp()
{
parent::setUp();
$this->config = new \Config\Filters();
$this->honey = new \Config\Honeypot();

unset($_POST[$this->honey->name]);
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST[$this->honey->name] = 'hey';
}

//--------------------------------------------------------------------
public function testBeforeTriggered()
{
$this->config->globals = [
'before' => ['honeypot'],
'after' => [],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$this->expectException(HoneypotException::class);
$request = $filters->run($uri, 'before');
}

//--------------------------------------------------------------------
public function testBeforeClean()
{
$this->config->globals = [
'before' => ['honeypot'],
'after' => [],
];

unset($_POST[$this->honey->name]);
$this->request = Services::request(null, false);
$this->response = Services::response();

$expected = $this->request;

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$request = $filters->run($uri, 'before');
$this->assertEquals($expected, $request);
}

//--------------------------------------------------------------------

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testAfter()
{
$this->config->globals = [
'before' => [],
'after' => ['honeypot'],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$this->response->setBody('<form></form>');
$this->response = $filters->run($uri, 'after');
$this->assertContains($this->honey->name, $this->response->getBody());
}

//--------------------------------------------------------------------

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testAfterNotApplicable()
{
$this->config->globals = [
'before' => [],
'after' => ['honeypot'],
];

$this->request = Services::request(null, false);
$this->response = Services::response();

$filters = new Filters($this->config, $this->request, $this->response);
$uri = 'admin/foo/bar';

$this->response->setBody('<div></div>');
$this->response = $filters->run($uri, 'after');
$this->assertNotContains($this->honey->name, $this->response->getBody());
}

}
6 changes: 2 additions & 4 deletions tests/system/Honeypot/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\Test\CIUnitTestCase;

require_once __DIR__ . '/fixtures/HoneyTrap.php';

/**
* @backupGlobals enabled
*/
Expand Down Expand Up @@ -90,7 +88,7 @@ public function testConfigName()
public function testHoneypotFilterBefore()
{
$config = [
'aliases' => ['trap' => 'CodeIgniter\Honeypot\fixtures\HoneyTrap'],
'aliases' => ['trap' => '\CodeIgniter\Filters\Honeypot'],
'globals' => [
'before' => ['trap'],
'after' => [],
Expand All @@ -107,7 +105,7 @@ public function testHoneypotFilterBefore()
public function testHoneypotFilterAfter()
{
$config = [
'aliases' => ['trap' => 'CodeIgniter\Honeypot\fixtures\HoneyTrap'],
'aliases' => ['trap' => '\CodeIgniter\Filters\Honeypot'],
'globals' => [
'before' => [],
'after' => ['trap'],
Expand Down
43 changes: 0 additions & 43 deletions tests/system/Honeypot/fixtures/HoneyTrap.php

This file was deleted.

0 comments on commit 5016336

Please sign in to comment.