Skip to content

Commit 4643580

Browse files
committed
test: add test
1 parent 3c339cd commit 4643580

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

tests/system/Honeypot/HoneypotTest.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
namespace CodeIgniter\Honeypot;
1313

14+
use CodeIgniter\Config\Factories;
1415
use CodeIgniter\Config\Services;
1516
use CodeIgniter\Filters\Filters;
1617
use CodeIgniter\Honeypot\Exceptions\HoneypotException;
1718
use CodeIgniter\HTTP\CLIRequest;
1819
use CodeIgniter\HTTP\IncomingRequest;
1920
use CodeIgniter\HTTP\Response;
2021
use CodeIgniter\Test\CIUnitTestCase;
22+
use Config\App;
2123
use Config\Honeypot as HoneypotConfig;
2224

2325
/**
@@ -42,14 +44,16 @@ final class HoneypotTest extends CIUnitTestCase
4244
protected function setUp(): void
4345
{
4446
parent::setUp();
47+
4548
$this->config = new HoneypotConfig();
4649
$this->honeypot = new Honeypot($this->config);
4750

4851
unset($_POST[$this->config->name]);
4952
$_SERVER['REQUEST_METHOD'] = 'POST';
5053
$_POST[$this->config->name] = 'hey';
51-
$this->request = Services::request(null, false);
52-
$this->response = Services::response();
54+
55+
$this->request = Services::request(null, false);
56+
$this->response = Services::response();
5357
}
5458

5559
public function testAttachHoneypot()
@@ -77,6 +81,25 @@ public function testAttachHoneypotAndContainer()
7781
$this->assertSame($expected, $this->response->getBody());
7882
}
7983

84+
public function testAttachHoneypotAndContainerWithCSP()
85+
{
86+
$this->resetServices();
87+
88+
$config = new App();
89+
$config->CSPEnabled = true;
90+
Factories::injectMock('config', 'App', $config);
91+
$this->response = Services::response($config, false);
92+
93+
$this->config = new HoneypotConfig();
94+
$this->honeypot = new Honeypot($this->config);
95+
96+
$this->response->setBody('<head></head><body><form></form></body>');
97+
$this->honeypot->attachHoneypot($this->response);
98+
99+
$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';
100+
$this->assertMatchesRegularExpression($regex, $this->response->getBody());
101+
}
102+
80103
public function testHasntContent()
81104
{
82105
unset($_POST[$this->config->name]);

0 commit comments

Comments
 (0)