From 668891f2baea47c88288d2b095cceb8e5ff1cba0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 30 Apr 2013 18:56:42 -0700 Subject: [PATCH] RestTest - Add test cases for omitted api_key. Tweak comments. --- CRM/Utils/REST.php | 2 +- tests/phpunit/WebTest/Utils/RestTest.php | 61 ++++++++++++++---------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/CRM/Utils/REST.php b/CRM/Utils/REST.php index c58250ba96..faefd4173b 100644 --- a/CRM/Utils/REST.php +++ b/CRM/Utils/REST.php @@ -345,7 +345,7 @@ class CRM_Utils_REST { if (!$valid_user) { $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST'); if (!$api_key || strtolower($api_key) == 'null') { - return ("FATAL:mandatory param 'api_key' (user key) missing"); + return self::error("FATAL:mandatory param 'api_key' (user key) missing"); } $valid_user = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key'); } diff --git a/tests/phpunit/WebTest/Utils/RestTest.php b/tests/phpunit/WebTest/Utils/RestTest.php index 181a25566e..d6ac878592 100644 --- a/tests/phpunit/WebTest/Utils/RestTest.php +++ b/tests/phpunit/WebTest/Utils/RestTest.php @@ -36,6 +36,7 @@ class WebTest_Utils_RestTest extends CiviSeleniumTestCase { $prefix .= ': '; } $this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . (empty($apiResult['error_message']) ? '' : $apiResult['error_message'])); + //$this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . print_r($apiResult, TRUE)); } protected function setUp() { @@ -116,90 +117,102 @@ class WebTest_Utils_RestTest extends CiviSeleniumTestCase { */ /** + * Build a list of test cases. Each test case defines a set of REST query + * parameters and an expected outcome for the REST request (eg is_error=>1 or is_error=>0). + * * @return array; each item is a list of parameters for testAPICalls */ function apiTestCases() { $cases = array(); + // entity,action: omit apiKey, valid entity+action + $cases[] = array( + array( // query + "entity" => "Contact", + "action" => "get", + "key" => $this->settings->siteKey, + "json" => "1", + ), + 1, // is_error + ); + // entity,action: valid apiKey, valid entity+action $cases[] = array( - /*'query'*/ - array( + array( // query "entity" => "Contact", "action" => "get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => $this->settings->adminApiKey, ), - /*'$is_error'*/ - 0, + 0, // is_error ); // entity,action: bad apiKey, valid entity+action $cases[] = array( - /*'query'*/ - array( + array( // query "entity" => "Contact", "action" => "get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => 'garbage_' . $this->settings->adminApiKey, ), - /*'$is_error'*/ - 1, + 1, // is_error ); // entity,action: valid apiKey, invalid entity+action $cases[] = array( - /*'query'*/ - array( + array( // query "entity" => "Contactses", "action" => "get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => $this->settings->adminApiKey, ), - /*'$is_error'*/ - 1, + 1, // is_error + ); + + // q=civicrm/entity/action: omit apiKey, valid entity+action + $cases[] = array( + array( // query + "q" => "civicrm/contact/get", + "key" => $this->settings->siteKey, + "json" => "1", + ), + 1, // is_error ); // q=civicrm/entity/action: valid apiKey, valid entity+action $cases[] = array( - /*'query'*/ - array( + array( // query "q" => "civicrm/contact/get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => $this->settings->adminApiKey, ), - /*'$is_error'*/ - 0, + 0, // is_error ); // q=civicrm/entity/action: invalid apiKey, valid entity+action $cases[] = array( - /*'query'*/ - array( + array( // query "q" => "civicrm/contact/get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => 'garbage_' . $this->settings->adminApiKey, ), - /*'$is_error'*/ - 1, + 1, // is_error ); // q=civicrm/entity/action: valid apiKey, invalid entity+action $cases[] = array( - /*'query'*/ - array( + array( // query "q" => "civicrm/contactses/get", "key" => $this->settings->siteKey, "json" => "1", "api_key" => $this->settings->adminApiKey, ), - /*'$is_error'*/ - 1, + 1, // is_error ); return $cases; -- 2.25.1