Skip to content

Commit

Permalink
Merge pull request #7972 from waterada/ticket-7967
Browse files Browse the repository at this point in the history
Add `IntegrationTestCase::assertCookieEncrypted()`
  • Loading branch information
markstory committed Jan 6, 2016
2 parents b152189 + e2cff0f commit 5a9ba35
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/TestSuite/IntegrationTestCase.php
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions tests/TestCase/TestSuite/CookieEncryptedUsingControllerTest.php
Expand Up @@ -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);
}
}
Expand Up @@ -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 5a9ba35

Please sign in to comment.