Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 27, 2022
1 parent 3c339cd commit 4643580
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions tests/system/Honeypot/HoneypotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace CodeIgniter\Honeypot;

use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Filters;
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use Config\Honeypot as HoneypotConfig;

/**
Expand All @@ -42,14 +44,16 @@ final class HoneypotTest extends CIUnitTestCase
protected function setUp(): void
{
parent::setUp();

$this->config = new HoneypotConfig();
$this->honeypot = new Honeypot($this->config);

unset($_POST[$this->config->name]);
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST[$this->config->name] = 'hey';
$this->request = Services::request(null, false);
$this->response = Services::response();

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

public function testAttachHoneypot()
Expand Down Expand Up @@ -77,6 +81,25 @@ public function testAttachHoneypotAndContainer()
$this->assertSame($expected, $this->response->getBody());
}

public function testAttachHoneypotAndContainerWithCSP()
{
$this->resetServices();

$config = new App();
$config->CSPEnabled = true;
Factories::injectMock('config', 'App', $config);
$this->response = Services::response($config, false);

$this->config = new HoneypotConfig();
$this->honeypot = new Honeypot($this->config);

$this->response->setBody('<head></head><body><form></form></body>');
$this->honeypot->attachHoneypot($this->response);

$regex = '!<head><style nonce="[0-9a-f]+">#hpc { display:none }</style></head><body><form><div style="display:none" id="hpc"><label>Fill This Field</label><input type="text" name="honeypot" value=""></div></form></body>!u';
$this->assertMatchesRegularExpression($regex, $this->response->getBody());
}

public function testHasntContent()
{
unset($_POST[$this->config->name]);
Expand Down

0 comments on commit 4643580

Please sign in to comment.