diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index c8c8e1df2..66c256774 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -26,6 +26,7 @@ namespace Icinga\Web; use Icinga\Exception\ProgrammingError; +use Icinga\Web\Form\InvalidCSRFTokenException; use Zend_Form_Exception; use Zend_View_Interface; @@ -68,16 +69,55 @@ abstract class Form extends \Zend_Form */ private $created = false; + /** + * Session id required for CSRF token generation + * @var numeric|bool + */ + private $sessionId = false; + + /** + * Returns the session ID stored in this form instance + * @return mixed + */ + public function getSessionId() + { + if (!$this->sessionId) { + $this->sessionId = session_id(); + } + return $this->sessionId; + } + + /** + * Overwrites the currently set session id to a user + * provided one, helpful when testing + * + * @param $sessionId The session id to use for CSRF generation + */ + public function setSessionId($sessionId) + { + $this->sessionId = $sessionId; + } + /** * @see Zend_Form::init */ public function init() { - if (!$this->tokenDisabled) { - $this->initCsrfToken(); - } + } + /** + * Returns the html-element name of the CSRF token + * field + * + * @return string + */ + public function getTokenElementName() + { + return $this->tokenElementName; + } + + /** * Render the form to html * @param Zend_View_Interface $view @@ -86,7 +126,6 @@ abstract class Form extends \Zend_Form public function render(Zend_View_Interface $view = null) { // Elements must be there to render the form - $this->buildForm(); return parent::render($view); } @@ -126,6 +165,7 @@ abstract class Form extends \Zend_Form public function buildForm() { if ($this->created === false) { + $this->initCsrfToken(); $this->create(); // Empty action if not safe @@ -137,17 +177,6 @@ abstract class Form extends \Zend_Form } } - /** - * Overridden to assert form creation - * @param array $data - * @return bool - */ - public function isValid($data) - { - $this->buildForm(); - $this->preValidation($data); - return parent::isValid($data); - } /** @@ -163,24 +192,23 @@ abstract class Form extends \Zend_Form } $checkData = $this->getRequest()->getParams(); + + $this->buildForm(); + $this->assertValidCsrfToken($checkData); + $this->preValidation($checkData); return parent::isValid($checkData); } - /** - * Enable CSRF counter measure - */ - final public function enableCsrfToken() - { - $this->tokenDisabled = false; - } + /** * Disable CSRF counter measure and remove its field if already added */ - final public function disableCsrfToken() + final public function setTokenDisabled($value) { - $this->tokenDisabled = true; - $this->removeElement($this->tokenElementName); + $this->tokenDisabled = $value; + if ($value == true) + $this->removeElement($this->tokenElementName); } /** @@ -191,62 +219,89 @@ abstract class Form extends \Zend_Form if ($this->tokenDisabled || $this->getElement($this->tokenElementName)) { return; } - list($seed, $token) = $this->generateCsrfToken($this->tokenTimeout); $this->addElement( 'hidden', $this->tokenElementName, array( - 'value' => sprintf('%s\|/%s', $seed, $token), + 'value' => $this->generateCsrfTokenAsString(), 'decorators' => array('ViewHelper') ) ); } + /** + * Tests the submitted data for a correct CSRF token, if needed + * + * @param Array $checkData The POST data send by the user + * @throws Form\InvalidCSRFTokenException When CSRF Validation fails + */ + final public function assertValidCsrfToken(array $checkData) + { + if ($this->tokenDisabled) { + return; + } + + if (!isset($checkData[$this->tokenElementName]) || !$this->hasValidCsrfToken($checkData[$this->tokenElementName])) { + throw new InvalidCSRFTokenException(); + } + } + /** * Check whether the form's CSRF token-field has a valid value * * @param int $maxAge Max allowed token age - * @param string $sessionId A specific session id * * @return bool */ - final private function hasValidCsrfToken($maxAge, $sessionId = null) + final private function hasValidCsrfToken($checkData) { - if ($this->tokenDisabled) { - return true; - } if ($this->getElement($this->tokenElementName) === null) { return false; } - $elementValue = $this->getElement($this->tokenElementName)->getValue(); - list($seed, $token) = explode($elementValue, '\|/'); + $elementValue = $checkData; + if (strpos($elementValue, '|') === false) { + return false; + } + + + list($seed, $token) = explode('|', $elementValue); if (!is_numeric($seed)) { return false; } - $seed -= intval(time() / $maxAge) * $maxAge; - $sessionId = $sessionId ? $sessionId : session_id(); - return $token === hash('sha256', $sessionId . $seed); + $seed -= intval(time() / $this->tokenTimeout) * $this->tokenTimeout; + + return $token === hash('sha256', $this->getSessionId() . $seed); } /** * Generate a new (seed, token) pair * * @param int $maxAge Max allowed token age - * @param string $sessionId A specific session id * * @return array */ - final private function generateCsrfToken($maxAge, $sessionId = null) + final public function generateCsrfToken() { - $sessionId = $sessionId ? $sessionId : session_id(); $seed = mt_rand(); - $hash = hash('sha256', $sessionId . $seed); - $seed += intval(time() / $maxAge) * $maxAge; + $hash = hash('sha256', $this->getSessionId() . $seed); + $seed += intval(time() / $this->tokenTimeout) * $this->tokenTimeout; + return array($seed, $hash); } + + /** + * Returns the string representation of the CSRF seed/token pair + * + * @return string + */ + final public function generateCsrfTokenAsString() + { + list ($seed, $token) = $this->generateCsrfToken($this->getSessionId()); + return sprintf('%s|%s', $seed, $token); + } } diff --git a/library/Icinga/Web/Form/InvalidCSRFTokenException.php b/library/Icinga/Web/Form/InvalidCSRFTokenException.php new file mode 100644 index 000000000..1b33f4fb6 --- /dev/null +++ b/library/Icinga/Web/Form/InvalidCSRFTokenException.php @@ -0,0 +1,8 @@ +getElement('expiretime'); $expireTime->setRequired(true); $expireTime->addValidator($this->createDateTimeValidator(), true); diff --git a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php index 259187907..1c03717b2 100644 --- a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php @@ -1,126 +1,116 @@ setRequest($this->getRequest()); - $form->buildForm(); + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->buildForm(); - $this->assertCount(11, $form->getElements()); - } + $this->assertCount(11, $form->getElements()); + } - public function testValidation1() - { - $form = new AcknowledgeForm(); - $form->setRequest($this->getRequest()); - $this->assertTrue( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '0', - 'expiretime' => '', - 'sticky' => '0', - 'notify' => '0' - ) - ) - ); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => '', - 'persistent' => '0', - 'expire' => '0', - 'expiretime' => '', - 'sticky' => '0', - 'notify' => '0' - ) - ) - ); + public function testValidateCorrectForm() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => 'test comment', + 'persistent' => '0', + 'expire' => '0', + 'expiretime' => '', + 'sticky' => '0', + 'notify' => '0' + ), self::FORMCLASS); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '1', - 'expiretime' => '', - 'sticky' => '0', - 'notify' => '0' - ) - ) - ); + $this->assertTrue( + $form->isPostAndValid(), + "Asserting a correct form to be validated correctly" + ); + } - $this->assertFalse( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '1', - 'expiretime' => 'NOT A DATE', - 'sticky' => '0', - 'notify' => '0' - ) - ) - ); + public function testDetectMissingAcknowledgementComment() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => '', + 'persistent' => '0', + 'expire' => '0', + 'expiretime' => '', + 'sticky' => '0', + 'notify' => '0', + ), self::FORMCLASS); + $this->assertFalse( + $form->isPostAndValid(), + "Asserting a missing comment text to cause validation errors" + ); + } - $this->assertTrue( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => 'test comment', - 'persistent' => '0', - 'expire' => '1', - 'expiretime' => '2013-07-10 17:32:16', - 'sticky' => '0', - 'notify' => '0' - ) - ) - ); - } + public function testValidateMissingExpireTime() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => 'test comment', + 'persistent' => '0', + 'expire' => '1', + 'expiretime' => '', + 'sticky' => '0', + 'notify' => '0' + ), self::FORMCLASS); + $this->assertFalse( + $form->isPostAndValid(), + "Asserting a missing expire time to cause validation errors when expire is 1" + ); + } + + public function testValidateIncorrectExpireTime() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => 'test comment', + 'persistent' => '0', + 'expire' => '1', + 'expiretime' => 'NOT A DATE', + 'sticky' => '0', + 'notify' => '0' + ), self::FORMCLASS); + $this->assertFalse( + $form->isPostAndValid(), + "Assert incorrect dates to be recognized when validating expiretime" + ); + } + + public function testValidateCorrectAcknowledgementWithExpireTime() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => 'test comment', + 'persistent' => '0', + 'expire' => '1', + 'expiretime' => '2013-07-10 17:32:16', + 'sticky' => '0', + 'notify' => '0' + ), self::FORMCLASS); + $this->assertTrue( + $form->isPostAndValid(), + "Assert that correct expire time acknowledgement is considered valid" + ); } } + diff --git a/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php b/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php new file mode 100644 index 000000000..ce79c7a11 --- /dev/null +++ b/modules/monitoring/test/php/application/forms/Command/BaseFormTest.php @@ -0,0 +1,56 @@ +setSessionId("test"); + $form->initCsrfToken(); + $request = $this->getRequest(); + $data[$form->getTokenElementName()] = $form->getValue($form->getTokenElementName()); + + $request->setMethod("POST")->setPost($data); + $form->setRequest($request); + + return $form; + } + } + +} \ No newline at end of file diff --git a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php index 9e47955bc..e5e242fa5 100644 --- a/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CommentFormTest.php @@ -1,86 +1,68 @@ setRequest($this->getRequest()); - $form->buildForm(); + $form = new CommentForm(); + $form->setRequest($this->getRequest()); + $form->buildForm(); - $this->assertCount(6, $form->getElements()); - } + $this->assertCount(6, $form->getElements()); + } - public function testValidation() - { - $form = new CommentForm(); - $form->setRequest($this->getRequest()); - $this->assertTrue( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => 'test2', - 'sticky' => '0' - ) - ) - ); + public function testCorrectCommentValidation() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => 'test2', + 'sticky' => '0' + ), self::FORMCLASS); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'test1', - 'comment' => '', - 'sticky' => '0' - ) - ) - ); + $this->assertTrue( + $form->isPostAndValid(), + "Asserting correct comment form to be considered valid" + ); + } - $this->assertFalse( - $form->isValid( - array( - 'author' => '', - 'comment' => 'test2', - 'sticky' => '0' - ) - ) - ); - } + public function testRecognizeMissingCommentText() + { + $form = $this->getRequestForm(array( + 'author' => 'test1', + 'comment' => '', + 'sticky' => '0' + ), self::FORMCLASS); + $this->assertFalse( + $form->isPostAndValid(), + "Asserting missing comment text in comment form to cause validation errors" + ); + } + + public function testRecognizeMissingCommentAuthor() + { + $form = $this->getRequestForm(array( + 'author' => '', + 'comment' => 'test2', + 'sticky' => '0' + ), self::FORMCLASS); + $this->assertFalse( + $form->isPostAndValid(), + "Asserting missing comment author to cause validation errors" + ); } } + diff --git a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php index 2ff9123af..3a35d48ba 100644 --- a/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ConfirmationFormTest.php @@ -1,12 +1,7 @@ addNote('444 NOTE 1'); $form->addNote('555 NOTE 2'); - + $form->buildForm(); $content = $form->render($view); $this->assertContains('', $content); @@ -42,7 +37,7 @@ class ConfirmationFormTest extends Zend_Test_PHPUnit_ControllerTestCase $this->assertContains('555 NOTE 2', $content); } - public function testNotes1() + public function testFormNotes() { $form = new ConfirmationForm(); $form->addNote('test1'); diff --git a/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php b/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php index ca2784675..8ba7f0602 100644 --- a/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ConfirmationWithIdentifierFormTest.php @@ -1,102 +1,79 @@ setRequest($this->getRequest()); - $form->setSubmitLabel('DING DING'); - $form->buildForm(); + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->setSubmitLabel('DING DING'); + $form->buildForm(); - $this->assertCount(4, $form->getElements()); - } + $this->assertCount(4, $form->getElements()); + } - public function testValidation() - { - $form = new ConfirmationWithIdentifierForm(); - $form->setRequest($this->getRequest()); - $form->setFieldLabel('Test1'); - $form->setFieldName('testval'); - $form->setSubmitLabel('DING DING'); + public function testCorrectFormValidation() + { - $this->assertTrue( - $form->isValid( - array( - 'testval' => 123 - ) - ) - ); + $form = $this->getRequestForm(array( + 'testval' => 123 + ), self::FORMCLASS); - $this->assertFalse( - $form->isValid( - array( - 'testval' => '' - ) - ) - ); + $form->setFieldLabel('Test1'); + $form->setFieldName('testval'); + $form->setSubmitLabel('DING DING'); - $this->assertFalse( - $form->isValid( - array( - 'testval' => 'NaN' - ) - ) - ); - } + $this->assertTrue( + $form->isPostAndValid(), + "Asserting correct confirmation with id to be valid" + ); + } - public function testRequestBridge() - { - $this->getRequest()->setMethod('POST'); - $this->getRequest()->setPost( - array( - 'objectid' => 123123666 - ) - ); + public function testInvalidValueValidationErrors() + { + $form = $this->getRequestForm(array( + 'testval' => '' + ), self::FORMCLASS); - $form = new ConfirmationWithIdentifierForm(); - $form->setRequest($this->getRequest()); - $form->buildForm(); + $this->assertFalse( + $form->isPostAndValid(), + "Asserting an invalid (empty) value to cause validation errors" + ); + } - $this->assertTrue($form->isPostAndValid()); + public function testNonNumericValueValidationErrors() + { + $form = $this->getRequestForm(array( + 'testval' => 'NaN' + ), self::FORMCLASS); - $this->assertEquals('123123666', $form->getElement('objectid')->getValue()); - } + $this->assertFalse( + $form->isPostAndValid(), + "Asserting an non numeric value to cause validation errors" + ); + } + + public function testRequestBridge() + { + $form = $this->getRequestForm(array( + 'objectid' => 123123666 + ), self::FORMCLASS); + $form->buildForm(); + + $this->assertTrue($form->isPostAndValid()); + + $this->assertEquals('123123666', $form->getElement('objectid')->getValue()); } } diff --git a/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php index db8175ceb..cdeb1a886 100644 --- a/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/CustomNotificationFormTest.php @@ -1,55 +1,28 @@ getRequest()->setMethod('POST'); - $this->getRequest()->setPost( - array( - 'comment' => 'TEST COMMENT', - 'author' => 'LAOLA' - ) - ); + $form = $this->getRequestForm(array( + 'comment' => 'TEST COMMENT', + 'author' => 'LAOLA' + ), "Monitoring\Form\Command\CustomNotificationForm"); + $form->buildForm(); - $form = new CustomNotificationForm(); - $form->setRequest($this->getRequest()); - $form->buildForm(); - - $this->assertCount(7, $form->getElements()); - $this->assertTrue($form->isPostAndValid()); - } + $this->assertCount(7, $form->getElements()); + $this->assertTrue($form->isPostAndValid()); } } + diff --git a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php index c4a34e122..856b4bd31 100644 --- a/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/DelayNotificationFormTest.php @@ -1,84 +1,55 @@ getRequest()->setMethod('POST'); - $this->getRequest()->setPost( - array( - 'minutes' => 12 - ) - ); + $form = $this->getRequestForm(array( + 'minutes' => 12 + ), 'Monitoring\Form\Command\DelayNotificationForm'); - $form = new DelayNotificationForm(); + $form->buildForm(); + $this->assertCount(5, $form->getElements()); - $form->setRequest($this->getRequest()); - $form->buildForm(); + $element = $form->getElement('minutes'); + $this->assertInstanceOf('Zend_Form_Element_Text', $element); + $this->assertEquals('0', $element->getValue(), "Assert a correct default value in minutes"); + $this->assertTrue($element->isRequired(), "Assert minutes to be declared as required"); - $this->assertCount(5, $form->getElements()); + $this->assertTrue( + $form->isPostAndValid(), + "Assert a correct DelayNotificationForm to be considered valid" + ); - $element = $form->getElement('minutes'); - $this->assertInstanceOf('Zend_Form_Element_Text', $element); - $this->assertEquals('0', $element->getValue()); - $this->assertTrue($element->isRequired()); - - $this->assertTrue($form->isPostAndValid()); - - $this->assertEquals('12', $form->getValue('minutes')); - } - - public function testValidation() - { - $this->getRequest()->setMethod('POST'); - $this->getRequest()->setPost( - array( - 'minutes' => 'SCHAHH-LAHH-LAHH' - ) - ); - - $form = new DelayNotificationForm(); - - $form->setRequest($this->getRequest()); - $form->buildForm(); - - $this->assertFalse($form->isPostAndValid()); - - $errors = $form->getErrors('minutes'); - $this->assertEquals('notBetween', $errors[0]); - } + $this->assertEquals('12', $form->getValue('minutes'), "Assert the minutes field to be correctly populated"); } + public function testInvalidMinuteValue() + { + $form = $this->getRequestForm(array( + 'minutes' => 'SCHAHH-LAHH-LAHH' + ), 'Monitoring\Form\Command\DelayNotificationForm'); + + $form->buildForm(); + + $this->assertFalse( + $form->isPostAndValid(), + "Asserting invalid minutes (NaN) to cause validation errors" + ); + + $errors = $form->getErrors('minutes'); + $this->assertEquals('notBetween', $errors[0], "Assert correct error message"); + } } + diff --git a/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php b/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php index 0e2001100..ff52d69ca 100644 --- a/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/RescheduleNextCheckFormTest.php @@ -1,120 +1,101 @@ getRequest()->setPost( - array( - ) - ); + $form = $this->getRequestForm(array( + 'checktime' => '2013-10-19 17:30:00', + 'forcecheck' => 1 + ), self::FORMCLASS); + $form->buildForm(); - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->buildForm(); + $this->assertCount(6, $form->getElements()); - $this->assertCount(6, $form->getElements()); + $this->assertTrue( + $form->isPostAndValid(), + 'Asserting a reschedule form with correct time and forececheck=1 to be valid' + ); + $form = $this->getRequestForm(array( + 'checktime' => '2013-10-19 17:30:00', + 'forcecheck' => 0 + ), self::FORMCLASS); - $this->assertTrue( - $form->isValid( - array( - 'checktime' => '2013-10-19 17:30:00', - 'forcecheck' => 1 - ) - ) - ); + $this->assertTrue( + $form->isPostAndValid(), + 'Asserting a reschedule form with correct time and forecheck=0 to be valid' + ); + } - $this->assertTrue( - $form->isValid( - array( - 'checktime' => '2013-10-19 17:30:00', - 'forcecheck' => 0 - ) - ) - ); + public function testInValidRescheduleChecktimeSubmissions() + { + $form = $this->getRequestForm(array( + 'checktime' => '2013-24-12 17:30:00', + 'forcecheck' => 1 + ), self::FORMCLASS); - $this->assertFalse( - $form->isValid( - array( - 'checktime' => '2013-24-12 17:30:00', - 'forcecheck' => 1 - ) - ) - ); + $this->assertFalse( + $form->isPostAndValid(), + 'Asserting an logically invalid checktime to be considered as invalid reschedule data' + ); - $this->assertFalse( - $form->isValid( - array( - 'checktime' => 'AHAHA', - 'forcecheck' => 1 - ) - ) - ); - } + $form = $this->getRequestForm(array( + 'checktime' => 'AHAHA', + 'forcecheck' => 1 + ), self::FORMCLASS); - public function testChildrenFlag() - { - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(true); - $form->buildForm(); - $notes1 = $form->getNotes(); - $form = null; + $this->assertFalse( + $form->isPostAndValid(), + 'Asserting an invalid non-numeric checktime to be considered as invalid reschedule data' + ); + } - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(false); - $form->buildForm(); - $notes2 = $form->getNotes(); - $form = null; + public function testChildrenFlag() + { - $form = new RescheduleNextCheckForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(); - $form->buildForm(); - $notes3 = $form->getNotes(); - $form = null; + $form = new RescheduleNextCheckForm(); + $form->setRequest($this->getRequest()); + $form->setWithChildren(true); + $form->buildForm(); + $notes1 = $form->getNotes(); + $form = null; - $this->assertEquals($notes1, $notes3); - $this->assertNotEquals($notes1, $notes2); - } + $form = new RescheduleNextCheckForm(); + $form->setRequest($this->getRequest()); + $form->setWithChildren(false); + $form->buildForm(); + $notes2 = $form->getNotes(); + $form = null; + + $form = new RescheduleNextCheckForm(); + $form->setRequest($this->getRequest()); + $form->setWithChildren(); + $form->buildForm(); + $notes3 = $form->getNotes(); + $form = null; + + $this->assertEquals($notes1, $notes3); + $this->assertNotEquals($notes1, $notes2); } } + diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php index 74d942aa3..7ae842296 100644 --- a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -1,317 +1,318 @@ getRequest()->setPost( - array( + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->buildForm(); - ) - ); + $this->assertCount(13, $form->getElements()); - $form = new ScheduleDowntimeForm(); - $form->setRequest($this->getRequest()); - $form->buildForm(); + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->setWithChildren(true); + $form->buildForm(); - $this->assertCount(13, $form->getElements()); - - $form = new ScheduleDowntimeForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(true); - $form->buildForm(); - - $this->assertCount(12, $form->getElements()); - } + $this->assertCount(12, $form->getElements()); + } - public function testFormValidation1() - { - $this->getRequest()->setPost( - array( + public function testCorrectValidationWithChildrend() + { + $form = $this->getRequestForm(array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); - ) - ); - $form = new ScheduleDowntimeForm(); - $form->setRequest($this->getRequest()); - $form->setWithChildren(true); + $form->setWithChildren(true); - $this->assertTrue( - $form->isValid( - array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); + $this->assertTrue( + $form->isPostAndValid(), + 'Asserting a correct fixed downtime form to be considered valid' + ); + $form = $this->getRequestForm(array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '10', + 'minutes' => '10', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); + $this->assertTrue( + $form->isPostAndValid(), + 'Asserting a correct flexible downtime form to be considered valid' + ); - $this->assertTrue( - $form->isValid( - array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, - 'hours' => '10', - 'minutes' => '10', - // 'childobjects' => '', - ) - ) - ); + } - $this->assertFalse( - $form->isValid( - array( - 'author' => '', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); + public function testMissingFlexibleDurationRecognition() + { + $form = $this->getRequestForm(array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'OK', - 'comment' => '', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); + $this->assertFalse( + $form->isPostAndValid(), + 'Assert missing hours and minutes in downtime form to cause failing validation' + ); + } - $this->assertFalse( - $form->isValid( - array( - 'author' => 'OK', - 'comment' => 'OK', - 'triggered' => 'HAHA', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); + public function testMissingAuthorRecognition() + { - $this->assertFalse( - $form->isValid( - array( - 'author' => 'OK', - 'comment' => 'OK', - 'triggered' => '123', - 'starttime' => '2013-07-17', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); + $form = $this->getRequestForm(array( + 'author' => '', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'OK', - 'comment' => 'OK', - 'triggered' => '123', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => 'DING', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - // 'childobjects' => '', - ) - ) - ); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'OK', - 'comment' => 'OK', - 'triggered' => '123', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 09:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, - 'hours' => '-1', - 'minutes' => '12', - // 'childobjects' => '', - ) - ) - ); + $this->assertFalse( + $form->isPostAndValid(), + 'Assert missing author to cause validation errors in fixed downtime' + ); + } - $this->assertFalse( - $form->isValid( - array( - 'author' => 'OK', - 'comment' => 'OK', - 'triggered' => '123', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 09:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, - 'hours' => '12', - 'minutes' => 'DING', - // 'childobjects' => '', - ) - ) - ); + public function testMissingCommentRecognition() + { + $form = $this->getRequestForm(array( + 'author' => 'OK', + 'comment' => '', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - } - public function testFormValidation2() - { - $this->getRequest()->setPost( - array( + $this->assertFalse( + $form->isPostAndValid(), + 'Assert missing comment to cause validation errors in fixed downtime' + ); + } - ) - ); + public function testInvalidTriggeredFieldValueRecognition() + { + $form = $this->getRequestForm(array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => 'HAHA', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - $form = new ScheduleDowntimeForm(); - $form->setWithChildren(false); - $form->setRequest($this->getRequest()); + $this->assertFalse( + $form->isPostAndValid(), + 'Assert invalid trigger field to cause validation to fail' + ); + } - $this->assertTrue( - $form->isValid( - array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - 'childobjects' => '0', - ) - ) - ); + public function testInvalidStartTimeRecognition() + { + $form = $this->getRequestForm(array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '123', + 'starttime' => '2013-07-17', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - $this->assertFalse( - $form->isValid( - array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - 'childobjects' => 'AHA', - ) - ) - ); + $this->assertFalse( + $form->isPostAndValid(), + 'Assert incorrect start time to cause validation errors in fixed downtime' + ); + } - $this->assertFalse( - $form->isValid( - array( - 'author' => 'TEST_AUTHOR', - 'comment' => 'DING DING', - 'triggered' => '4', - 'starttime' => '2013-07-17 10:30:00', - 'endtime' => '2013-07-17 10:30:00', - 'type' => ScheduleDowntimeForm::TYPE_FIXED, - 'hours' => '', - 'minutes' => '', - 'childobjects' => '4', - ) - ) - ); - } + public function testInvalidEndTimeRecognition() + { - public function testTimeRange() - { - $this->getRequest()->setPost( - array( + $form = $this->getRequestForm(array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '123', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => 'DING', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - ) - ); + $this->assertFalse( + $form->isPostAndValid(), + 'Assert invalid endtime to cause validation errors in fixed downtime' + ); + } - $form = new ScheduleDowntimeForm(); - $form->setWithChildren(false); - $form->setRequest($this->getRequest()); - $form->buildForm(); - $time1 = strtotime($form->getElement('starttime')->getValue()); - $time2 = strtotime($form->getElement('endtime')->getValue()); + public function testInvalidHoursValueRecognitionInFlexibleDowntime() + { + $form = $this->getRequestForm(array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '123', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 09:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '-1', + 'minutes' => '12', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); - $this->assertEquals(3600, ($time2 - $time1)); - } + $this->assertFalse( + $form->isPostAndValid(), + 'Assert negative hours to cause validation errors in flexible downtime' + ); + } + + public function testInvalidMinutesValueRecognitionInFlexibleDowntime() + { + $form = $this->getRequestForm(array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '123', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 09:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '12', + 'minutes' => 'DING', + // 'childobjects' => '', + ), self::FORMCLASS); + $form->setWithChildren(true); + + $this->assertFalse( + $form->isPostAndValid(), + 'Assert non numeric valud to cause validation errors in flexible downtime ' + ); + + } + + public function testCorrectScheduleDowntimeWithoutChildrenForm() + { + $form = $this->getRequestForm(array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => '0', + ), self::FORMCLASS); + $form->setWithChildren(false); + + + $this->assertTrue( + $form->isPostAndValid(), + "Assert a correct schedule downtime without children form to be considered valid" + ); + } + + public function testIncorrectChildObjectsRecognition() { + $form = $this->getRequestForm(array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => 'AHA', + ), self::FORMCLASS); + $form->setWithChildren(false); + + $this->assertFalse( + $form->isPostAndValid(), + "Assert and incorrect (non-numeric) childobjects value to cause validation errors" + ); + + $form = $this->getRequestForm(array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '4', + 'starttime' => '2013-07-17 10:30:00', + 'endtime' => '2013-07-17 10:30:00', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => '4', + ), self::FORMCLASS); + $form->setWithChildren(false); + + $this->assertFalse( + $form->isPostAndValid(), + "Assert and incorrect (numeric) childobjects value to cause validation errors" + ); + } + + public function testTimeRange() + { + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->buildForm(); + + $time1 = strtotime($form->getElement('starttime')->getValue()); + $time2 = strtotime($form->getElement('endtime')->getValue()); + + $this->assertEquals(3600, ($time2 - $time1)); } } + diff --git a/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php b/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php index cb655e5d6..019e9768a 100644 --- a/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php +++ b/modules/monitoring/test/php/application/forms/Command/SubmitPassiveCheckResultTest.php @@ -1,120 +1,103 @@ setRequest($this->getRequest()); + $form = $this->getRequestForm(array(), self::FORMCLASS); - $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); - $options = $form->getOptions(); - $this->assertCount(4, $options); - $this->assertEquals('OK', $options[0]); - $this->assertEquals('WARNING', $options[1]); - $this->assertEquals('CRITICAL', $options[2]); - $this->assertEquals('UNKNOWN', $options[3]); + $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); + $options = $form->getOptions(); + $this->assertCount(4, $options, "Assert correct number of states in service passive checks form"); + $this->assertEquals('OK', $options[0], "Assert OK state to be available in service passive check form"); + $this->assertEquals('WARNING', $options[1], "Assert WARNING state to be available in service passive check form"); + $this->assertEquals('CRITICAL', $options[2], "Assert CRITICAL state to be available in service passive check form"); + $this->assertEquals('UNKNOWN', $options[3], "Assert UNKNOWN state to be available in service passive check form"); - $form->setType(SubmitPassiveCheckResultForm::TYPE_HOST); - $options = $form->getOptions(); - $this->assertCount(3, $options); - $this->assertEquals('UP', $options[0]); - $this->assertEquals('DOWN', $options[1]); - $this->assertEquals('UNREACHABLE', $options[2]); - } + $form->setType(SubmitPassiveCheckResultForm::TYPE_HOST); + $options = $form->getOptions(); + $this->assertCount(3, $options, "Assert correct number of states in host passive checks form"); + $this->assertEquals('UP', $options[0], "Assert UP state to be available in host passive check form"); + $this->assertEquals('DOWN', $options[1], "Assert DOWN state to be available in host passive check form"); + $this->assertEquals('UNREACHABLE', $options[2], "Assert UNREACHABLE state to be available in host passive check form"); + } - /** - * @expectedException Icinga\Exception\ProgrammingError - * @expectedExceptionMessage Type is not valid - */ - public function testForm1() - { - $form = new SubmitPassiveCheckResultForm(); - $form->setRequest($this->getRequest()); - $form->buildForm(); - } + /** + * @expectedException Icinga\Exception\ProgrammingError + * @expectedExceptionMessage Type is not valid + */ + public function testMissingTypeThrowingException() + { + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->buildForm(); + } - public function testForm2() - { - $form = new SubmitPassiveCheckResultForm(); - $form->setRequest($this->getRequest()); - $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); - $form->buildForm(); + public function testCorrectFormCreation() + { + $form = $this->getRequestForm(array(), self::FORMCLASS); + $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); + $form->buildForm(); - $this->assertCount(6, $form->getElements()); - } + $this->assertCount(6, $form->getElements(), "Assert correct number of elements in form"); + } - public function testValidation1() - { - $form = new SubmitPassiveCheckResultForm(); - $form->setRequest($this->getRequest()); - $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); + public function testCorrectServicePassiveCheckSubmission() + { + $form = $this->getRequestForm(array( + 'pluginstate' => 0, + 'checkoutput' => 'DING', + 'performancedata' => '' + ), self::FORMCLASS); - $this->assertTrue( - $form->isValid( - array( - 'pluginstate' => 0, - 'checkoutput' => 'DING', - 'performancedata' => '' - ) - ) - ); + $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); - $this->assertFalse( - $form->isValid( - array( - 'pluginstate' => 0, - 'checkoutput' => '', - 'performancedata' => '' - ) - ) - ); + $this->assertTrue( + $form->isPostAndValid(), + "Assert a correct passive service check form to pass form validation" + ); + } - $this->assertFalse( - $form->isValid( - array( - 'pluginstate' => 'LA', - 'checkoutput' => 'DING', - 'performancedata' => '' - ) - ) - ); - } + public function testIncorrectCheckoutputRecognition() + { + $form = $this->getRequestForm(array( + 'pluginstate' => 0, + 'checkoutput' => '', + 'performancedata' => '' + ), self::FORMCLASS); + $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); + + $this->assertFalse( + $form->isPostAndValid(), + "Assert empty checkoutput to cause validation errors in passive service check " + ); + } + + public function testIncorrectStateRecognition() + { + $form = $this->getRequestForm(array( + 'pluginstate' => 'LA', + 'checkoutput' => 'DING', + 'performancedata' => '' + ), self::FORMCLASS); + $form->setType(SubmitPassiveCheckResultForm::TYPE_SERVICE); + + $this->assertFalse( + $form->isPostAndValid(), + "Assert invalid (non-numeric) state to cause validation errors in passive service check" + ); } } +