Skip to content
Permalink
Browse files

Add `IntegrationTestCase::assertCookieEncrypted()` which is to compar…

…e cookies when they are encrypted.
  • Loading branch information...
waterada committed Jan 5, 2016
1 parent b152189 commit e2cff0f2daacc4be8a9007a07586cf7803eff1a7
@@ -838,4 +838,31 @@ public function assertCookie($expected, $name, $message = '')
$result = $this->_response->cookie($name);
$this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
}
/**
* Asserts cookie values which are encrypted by the
* CookieComponent.
*
* The difference from assertCookie() is this decrypts the cookie
* value like the CookieComponent for this assertion.
*
* @param string $expected The expected contents.
* @param string $name The cookie name.
* @param string|bool $encrypt Encryption mode to use.
* @param string|null $key Encryption key used. Defaults
* to Security.salt.
* @param string $message The failure message that will be appended to the generated message.
* @return void
* @see CookieCryptTrait::_encrypt
*/
public function assertCookieEncrypted($expected, $name, $encrypt = 'aes', $key = null, $message = '')
{
if (empty($this->_response)) {
$this->fail('Not response set, cannot assert cookies.');
}
$result = $this->_response->cookie($name);
$this->_cookieEncriptionKey = $key;
$result['value'] = $this->_decrypt($result['value'], $encrypt);
$this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
}
}
@@ -109,4 +109,32 @@ public function testCanBeUsedSecuritySaltAsEncryptionKey()
$this->assertStringStartsWith('Q2FrZQ==.', $this->viewVariable('ValueFromRequest'), 'Encrypted');
$this->assertEquals('Value of Cookie', $this->viewVariable('ValueFromCookieComponent'), 'Decrypted');
}
/**
* Can AssertCookie even if the value is encrypted by
* the CookieComponent.
*/
public function testCanAssertCookieEncrypted() {
$this->get('/cookie_component_test/set_cookie');
$this->assertCookieEncrypted('abc', 'NameOfCookie');
}
/**
* Can AssertCookie even if encrypted with the aes.
*/
public function testCanAssertCookieEncryptedWithAes() {
$this->get('/cookie_component_test/set_cookie');
$this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes');
}
/**
* Can AssertCookie even if encrypted with the another
* encrypted key.
*/
public function testCanAssertCookieEncryptedWithAnotherEncryptionKey() {
$key = 'another salt xxxxxxxxxxxxxxxxxxx';
Security::salt($key);
$this->get('/cookie_component_test/set_cookie');
$this->assertCookieEncrypted('abc', 'NameOfCookie', 'aes', $key);
}
}
@@ -41,4 +41,19 @@ public function view($key = null)
$this->set('ValueFromRequest', $this->request->cookie('NameOfCookie'));
$this->set('ValueFromCookieComponent', $this->Cookie->read('NameOfCookie'));
}
/**
* action to set a cookie
*
* @param string|null $key Encryption key used. By defaults,
* CookieComponent::_config['key'].
*/
public function set_cookie($key = null)
{
$this->autoRender = false;
if (isset($key)) {
$this->Cookie->config('key', $key);
}
$this->Cookie->write('NameOfCookie', 'abc');
}
}

0 comments on commit e2cff0f

Please sign in to comment.
You can’t perform that action at this time.