3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
27 require_once 'CiviTest/CiviSeleniumTestCase.php';
28 class WebTest_Utils_RestTest
extends CiviSeleniumTestCase
{
31 protected $session_id;
32 protected $nocms_contact_id;
34 protected function assertAPIErrorCode($apiResult, $cmpvar, $prefix = '') {
35 if (!empty($prefix)) {
38 $this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . (empty($apiResult['error_message']) ?
'' : $apiResult['error_message']));
39 //$this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . print_r($apiResult, TRUE));
42 protected function setUp() {
44 //URL should eventually be adapted for multisite
45 $this->url
= "{$this->settings->sandboxURL}/{$this->sboxPath}sites/all/modules/civicrm/extern/rest.php";
47 if (!property_exists($this->settings
, 'siteKey') ||
empty($this->settings
->siteKey
)) {
48 $this->markTestSkipped('CiviSeleniumSettings is missing siteKey');
50 if (!property_exists($this->settings
, 'adminApiKey') ||
empty($this->settings
->adminApiKey
)) {
51 $this->markTestSkipped('CiviSeleniumSettings is missing adminApiKey');
55 protected function tearDown() {
57 if (isset($this->nocms_contact_id
)) {
58 $deleteParams = array(
59 "id" => $this->nocms_contact_id
,
62 $res = $this->webtest_civicrm_api("Contact", "delete", $deleteParams);
63 unset($this->nocms_contact_id
);
68 function testValidLoginCMSUser() {
69 $client = CRM_Utils_HttpClient::singleton();
71 "q" => "civicrm/login",
72 "key" => $this->settings->siteKey,
74 "name" => $this->settings->adminUsername,
75 "pass" => $this->settings->adminPassword
77 list($status, $data) = $client->post($this->url, $params);
78 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
79 $result = json_decode($data, TRUE);
80 $this->assertNotNull($result);
81 $this->assertAPIErrorCode($result, 0);
84 function testInvalidPasswordLogin() {
85 $client = CRM_Utils_HttpClient::singleton();
86 $badPassword = $this->settings->adminPassword . "badpass";
88 "q" => "civicrm/login",
89 "key" => $this->settings->siteKey,
91 "name" => $this->settings->adminUsername,
92 "pass" => $badPassword
94 list($status, $data) = $client->post($this->url, $params);
95 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
96 $result = json_decode($data, TRUE);
97 $this->assertNotNull($result);
98 $this->assertAPIErrorCode($result, 1);
101 function testValidCallPHPSessionID() {
102 $this->_setUpAdminSessionIdAndApiKey();
103 $client = CRM_Utils_HttpClient::singleton();
105 "entity" => "Contact",
108 "PHPSESSID" => $this->session_id,
109 "api_key" => $this->api_key,
111 list($status, $data) = $client->post($this->url, $params);
112 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
113 $result = json_decode($data, TRUE);
114 $this->assertNotNull($result);
115 $this->assertAPIErrorCode($result, 0);
120 * Build a list of test cases. Each test case defines a set of REST query
121 * parameters and an expected outcome for the REST request (eg is_error=>1 or is_error=>0).
123 * @return array; each item is a list of parameters for testAPICalls
125 function apiTestCases() {
128 // entity,action: omit apiKey, valid entity+action
131 "entity" => "Contact",
133 "key" => $this->settings
->siteKey
,
139 // entity,action: valid apiKey, valid entity+action
142 "entity" => "Contact",
144 "key" => $this->settings
->siteKey
,
146 "api_key" => $this->settings
->adminApiKey
,
151 // entity,action: bad apiKey, valid entity+action
154 "entity" => "Contact",
156 "key" => $this->settings
->siteKey
,
158 "api_key" => 'garbage_' . $this->settings
->adminApiKey
,
163 // entity,action: valid apiKey, invalid entity+action
166 "entity" => "Contactses",
168 "key" => $this->settings
->siteKey
,
170 "api_key" => $this->settings
->adminApiKey
,
175 // q=civicrm/entity/action: omit apiKey, valid entity+action
178 "q" => "civicrm/contact/get",
179 "key" => $this->settings
->siteKey
,
185 // q=civicrm/entity/action: valid apiKey, valid entity+action
188 "q" => "civicrm/contact/get",
189 "key" => $this->settings
->siteKey
,
191 "api_key" => $this->settings
->adminApiKey
,
196 // q=civicrm/entity/action: invalid apiKey, valid entity+action
199 "q" => "civicrm/contact/get",
200 "key" => $this->settings
->siteKey
,
202 "api_key" => 'garbage_' . $this->settings
->adminApiKey
,
207 // q=civicrm/entity/action: valid apiKey, invalid entity+action
210 "q" => "civicrm/contactses/get",
211 "key" => $this->settings
->siteKey
,
213 "api_key" => $this->settings
->adminApiKey
,
222 * @dataProvider apiTestCases
224 function testAPICalls($query, $is_error) {
225 $client = CRM_Utils_HttpClient
::singleton();
226 list($status, $data) = $client->post($this->url
, $query);
227 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
228 $result = json_decode($data, TRUE);
229 $this->assertNotNull($result);
230 $this->assertAPIErrorCode($result, $is_error);
234 * Submit a request with an API key that exists but does not correspond to
235 * a real user. Submit in "?entity=X&action=X" notation
237 function testNotCMSUser_entityAction() {
238 $client = CRM_Utils_HttpClient
::singleton();
240 //Create contact with api_key
241 $test_key = "testing1234";
242 $contactParams = array(
243 "api_key" => $test_key,
244 "contact_type" => "Individual",
245 "first_name" => "RestTester1"
247 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
248 $this->nocms_contact_id
= $contact["id"];
250 // Use the malformed key
252 "entity" => "Contact",
254 "key" => $this->settings
->siteKey
,
256 "api_key" => $test_key
258 list($status, $data) = $client->post($this->url
, $params);
259 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
260 $result = json_decode($data, TRUE);
261 $this->assertNotNull($result);
262 $this->assertAPIErrorCode($result, 1);
266 * Submit a request with an API key that exists but does not correspond to
267 * a real user. Submit in "?q=civicrm/$entity/$action" notation
269 function testNotCMSUser_q() {
270 $client = CRM_Utils_HttpClient
::singleton();
272 //Create contact with api_key
273 $test_key = "testing1234";
274 $contactParams = array(
275 "api_key" => $test_key,
276 "contact_type" => "Individual",
277 "first_name" => "RestTester1"
279 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
280 $this->nocms_contact_id
= $contact["id"];
282 // Use the malformed key
284 "q" => "civicrm/contact/get",
285 "key" => $this->settings
->siteKey
,
287 "api_key" => $test_key
289 list($status, $data) = $client->post($this->url
, $params);
290 $this->assertEquals(CRM_Utils_HttpClient
::STATUS_OK
, $status);
291 $result = json_decode($data, TRUE);
292 $this->assertNotNull($result);
293 $this->assertAPIErrorCode($result, 1);
297 protected function _setUpAdminSessionIdAndApiKey() {
298 $client = CRM_Utils_HttpClient::singleton();
300 "q" => "civicrm/login",
301 "key" => $this->settings->siteKey,
303 "name" => $this->settings->adminUsername,
304 "pass" => $this->settings->adminPassword
306 list($status, $data) = $client->post($this->url, $params);
307 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
308 $result = json_decode($data, TRUE);
309 $this->assertAPIErrorCode($result, 0);
310 $this->api_key = $result["api_key"];
311 $this->session_id = $result["PHPSESSID"];
312 $this->assertTrue(isset($this->api_key), 'Failed to find admin API key');