From 50163360dff0d503ccba5b57ac39de2cfda118d6 Mon Sep 17 00:00:00 2001 From: Jim Parry Date: Thu, 24 Jan 2019 11:32:42 -0800 Subject: [PATCH] System filter testing --- tests/system/Filters/CSRFTest.php | 46 +++++++ tests/system/Filters/DebugToolbarTest.php | 52 ++++++++ tests/system/Filters/FiltersTest.php | 1 + tests/system/Filters/HoneypotTest.php | 119 +++++++++++++++++++ tests/system/Honeypot/HoneypotTest.php | 6 +- tests/system/Honeypot/fixtures/HoneyTrap.php | 43 ------- 6 files changed, 220 insertions(+), 47 deletions(-) create mode 100644 tests/system/Filters/CSRFTest.php create mode 100644 tests/system/Filters/DebugToolbarTest.php create mode 100644 tests/system/Filters/HoneypotTest.php delete mode 100644 tests/system/Honeypot/fixtures/HoneyTrap.php diff --git a/tests/system/Filters/CSRFTest.php b/tests/system/Filters/CSRFTest.php new file mode 100644 index 000000000000..697c24534818 --- /dev/null +++ b/tests/system/Filters/CSRFTest.php @@ -0,0 +1,46 @@ +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); + } + +} diff --git a/tests/system/Filters/DebugToolbarTest.php b/tests/system/Filters/DebugToolbarTest.php new file mode 100644 index 000000000000..5d8f7a774a4b --- /dev/null +++ b/tests/system/Filters/DebugToolbarTest.php @@ -0,0 +1,52 @@ +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); + } + +} diff --git a/tests/system/Filters/FiltersTest.php b/tests/system/Filters/FiltersTest.php index e6f0449cc930..a6bdbf175f88 100644 --- a/tests/system/Filters/FiltersTest.php +++ b/tests/system/Filters/FiltersTest.php @@ -1,6 +1,7 @@ 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('
'); + $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('
'); + $this->response = $filters->run($uri, 'after'); + $this->assertNotContains($this->honey->name, $this->response->getBody()); + } + +} diff --git a/tests/system/Honeypot/HoneypotTest.php b/tests/system/Honeypot/HoneypotTest.php index b7346f45b8ee..38bb8ebe7cbc 100644 --- a/tests/system/Honeypot/HoneypotTest.php +++ b/tests/system/Honeypot/HoneypotTest.php @@ -7,8 +7,6 @@ use CodeIgniter\Honeypot\Exceptions\HoneypotException; use CodeIgniter\Test\CIUnitTestCase; -require_once __DIR__ . '/fixtures/HoneyTrap.php'; - /** * @backupGlobals enabled */ @@ -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' => [], @@ -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'], diff --git a/tests/system/Honeypot/fixtures/HoneyTrap.php b/tests/system/Honeypot/fixtures/HoneyTrap.php deleted file mode 100644 index 8cc3aceef95e..000000000000 --- a/tests/system/Honeypot/fixtures/HoneyTrap.php +++ /dev/null @@ -1,43 +0,0 @@ -hasContent($request)) - { - throw HoneypotException::isBot(); - } - } - - /** - * Attach a honypot to the current response. - * - * @param CodeIgniter\HTTP\RequestInterface $request - * @param CodeIgniter\HTTP\ResponseInterface $response - * @return mixed - */ - public function after(RequestInterface $request, ResponseInterface $response) - { - $honeypot = new Honeypot(new \Config\Honeypot()); - $honeypot->attachHoneypot($response); - } - -}