<commit_before><?php

namespace Nettools\Core\Tests;




use \Nettools\Core\Helpers\SecureRequestHelper;
use \Nettools\Core\Helpers\SecureRequestHelper\AbstractBrowserInterface;



class SecureRequestHelperTest extends \PHPUnit\Framework\TestCase
{
    
    public function testCSRF()
    {
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->once())->method('setCookie');
		$cookie = 'abcdef';
		$intf->method('getCookie')->will($this->returnValue($cookie));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		$sec->initializeCSRF();
		
		$this->assertEquals('_cname_', $sec->getCSRFCookieName());
		$this->assertEquals('_fcname_', $sec->getCSRFSubmittedValueName());
		$this->assertEquals($cookie, $sec->getCSRFCookie());
		$this->assertEquals("<input type=\"hidden\" name=\"_fcname_\" value=\"$cookie\">", $sec->addCSRFHiddenInput());
		
		
		$req = ['input1'=>'value1', '_fcname_'=>$cookie];
		$this->assertEquals(true, $sec->authorizeCSRF($req));
    }
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF security validation failed
	 */
	public function testAuthorizeCSRF()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->once())->method('setCookie');
		$cookie = 'abcdef';
		$intf->method('getCookie')->will($this->returnValue($cookie));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		$sec->initializeCSRF();
		$req = ['input1'=>'value1', '_fcname_'=>'dummy value'];
		$sec->authorizeCSRF($req);
	}
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF cookie has not been initialized
	 */
	public function testAuthorizeCSRFNoCookie()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->never())->method('setCookie');
		$intf->method('getCookie')->will($this->returnValue(''));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);

		// no init
		$req = ['input1'=>'value1', '_fcname_'=>'dummy value'];
		$sec->authorizeCSRF($req);
	}
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF cookie has not been initialized
	 */
	public function testNotInitializeCSRF()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->never())->method('setCookie');
		$intf->method('getCookie')->will($this->returnValue(''));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		
		// calling getCSRFCookie will throw an exception since cookie has not been initialized
		$sec->getCSRFCookie();
	}
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF cookie has not been initialized
	 */
	public function testRevokeCSRF()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->exactly(2))->method('setCookie');
		$cookie = 'abcdef';
		$intf->method('getCookie')->will($this->onConsecutiveCalls($cookie, ''));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		$sec->initializeCSRF();
		$this->assertEquals($cookie, $sec->getCSRFCookie());	// returning cookie
		
		$sec->revokeCSRF();
		
		// calling getCSRFCookie will throw an exception since cookie has been revoked
		$sec->getCSRFCookie();									// returning ''
	}

    
}

?><commit_msg>tests<commit_after><?php

namespace Nettools\Core\Tests;




use \Nettools\Core\Helpers\SecureRequestHelper;
use \Nettools\Core\Helpers\SecureRequestHelper\AbstractBrowserInterface;



class SecureRequestHelperTest extends \PHPUnit\Framework\TestCase
{
    
    public function testCSRF()
    {
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->once())->method('setCookie');
		$cookie = 'abcdef';
		$intf->method('getCookie')->will($this->returnValue($cookie));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		$sec->initializeCSRF();
		
		$this->assertEquals('_cname_', $sec->getCSRFCookieName());
		$this->assertEquals('_fcname_', $sec->getCSRFSubmittedValueName());
		$this->assertEquals($cookie, $sec->getCSRFCookie());
		$this->assertEquals("<input type=\"hidden\" name=\"_fcname_\" value=\"$cookie\">", $sec->addCSRFHiddenInput());
		
		
		$req = ['input1'=>'value1', '_fcname_'=>$cookie];
		$this->assertEquals(true, $sec->authorizeCSRF($req));
    }
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF security validation failed
	 */
	public function testAuthorizeCSRF()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->once())->method('setCookie');
		$cookie = 'abcdef';
		$intf->method('getCookie')->will($this->returnValue($cookie));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		$sec->initializeCSRF();
		$req = ['input1'=>'value1', '_fcname_'=>'dummy value'];
		$sec->authorizeCSRF($req);
	}
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF cookie has not been initialized
	 */
	public function testAuthorizeCSRFNoCookie()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->never())->method('setCookie');
		$intf->method('getCookie')->will($this->returnValue(''));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);

		// no init
		$req = ['input1'=>'value1', '_fcname_'=>'dummy value'];
		$sec->authorizeCSRF($req);
	}
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF cookie has not been initialized
	 */
	public function testNotInitializeCSRF()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->never())->method('setCookie');
		$intf->method('getCookie')->will($this->returnValue(''));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		
		// calling getCSRFCookie will throw an exception since cookie has not been initialized
		$sec->getCSRFCookie();
	}
	
	
	
	/**
	 * @expectedException \Nettools\Core\Helpers\SecureRequestHelper\CSRFException
	 * @expectedExceptionMessage CSRF cookie has not been initialized
	 */
	public function testRevokeCSRF()
	{
		$intf = $this->getMockForAbstractClass(AbstractBrowserInterface::class);
		$intf->expects($this->once())->method('setCookie');
		$intf->expects($this->once())->method('deleteCookie');
		$cookie = 'abcdef';
		$intf->method('getCookie')->will($this->onConsecutiveCalls($cookie, ''));
		
		$sec = new SecureRequestHelper('_cname_', '_fcname_');
		$sec->setBrowserInterface($intf);
		$sec->initializeCSRF();
		$this->assertEquals($cookie, $sec->getCSRFCookie());	// returning cookie
		
		$sec->revokeCSRF();
		
		// calling getCSRFCookie will throw an exception since cookie has been revoked
		$sec->getCSRFCookie();									// returning ''
	}

    
}

?>
{'fraction_non_alphanumeric': 0.17563397289682006, 'fraction_numerical': 0.0017442640547430566, 'mean_word_length': 5.431406384814495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 142, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4895129', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2299, 'n_words': 347}