CiviSeleniumSettings - Add adminApiKey
[civicrm-core.git] / tests / phpunit / WebTest / Utils / RestTest.php
CommitLineData
49626e3d
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
25*/
26
27require_once 'CiviTest/CiviSeleniumTestCase.php';
28class WebTest_Utils_RestTest extends CiviSeleniumTestCase {
29 protected $url;
30 protected $api_key;
31 protected $session_id;
32 protected $nocms_contact_id;
33
66eec473 34 protected function assertAPIErrorCode($apiResult, $cmpvar, $prefix = '') {
49626e3d
CW
35 if (!empty($prefix)) {
36 $prefix .= ': ';
37 }
38 $this->assertEquals($cmpvar, $apiResult['is_error'], $prefix . (empty($apiResult['error_message']) ? '' : $apiResult['error_message']));
39 }
40
41 protected function setUp() {
42 parent::setUp();
43 //URL should eventually be adapted for multisite
44 $this->url = "{$this->settings->sandboxURL}/{$this->sboxPath}sites/all/modules/civicrm/extern/rest.php";
45
016157f0 46 if (!property_exists($this->settings, 'siteKey') || empty($this->settings->siteKey)) {
88be34fc
TO
47 $this->markTestSkipped('CiviSeleniumSettings is missing siteKey');
48 }
eb87411f
TO
49 if (!property_exists($this->settings, 'adminApiKey') || empty($this->settings->adminApiKey)) {
50 $this->markTestSkipped('CiviSeleniumSettings is missing adminApiKey');
51 }
49626e3d
CW
52 }
53
54 protected function tearDown() {
55 parent::tearDown();
016157f0 56 if (isset($this->nocms_contact_id)) {
49626e3d
CW
57 $deleteParams = array(
58 "id" => $this->nocms_contact_id,
59 "skip_undelete" => 1
60 );
61 $res = $this->webtest_civicrm_api("Contact", "delete", $deleteParams);
62 unset($this->nocms_contact_id);
63 }
64 }
65
eb87411f 66 /*
49626e3d 67 function testValidLoginCMSUser() {
016157f0
TO
68 $client = CRM_Utils_HttpClient::singleton();
69 $params = array(
70 "q" => "civicrm/login",
71 "key" => $this->settings->siteKey,
72 "json" => "1",
73 "name" => $this->settings->adminUsername,
74 "pass" => $this->settings->adminPassword
75 );
76 list($status, $data) = $client->post($this->url, $params);
77 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
78 $result = json_decode($data, TRUE);
79 $this->assertNotNull($result);
80 $this->assertAPIErrorCode($result, 0);
49626e3d
CW
81 }
82
83 function testInvalidPasswordLogin() {
016157f0
TO
84 $client = CRM_Utils_HttpClient::singleton();
85 $badPassword = $this->settings->adminPassword . "badpass";
86 $params = array(
87 "q" => "civicrm/login",
88 "key" => $this->settings->siteKey,
89 "json" => "1",
90 "name" => $this->settings->adminUsername,
91 "pass" => $badPassword
92 );
93 list($status, $data) = $client->post($this->url, $params);
94 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
95 $result = json_decode($data, TRUE);
96 $this->assertNotNull($result);
97 $this->assertAPIErrorCode($result, 1);
49626e3d
CW
98 }
99
eb87411f 100 function testValidCallPHPSessionID() {
5700921b 101 $this->_setUpAdminSessionIdAndApiKey();
016157f0
TO
102 $client = CRM_Utils_HttpClient::singleton();
103 $params = array(
104 "entity" => "Contact",
105 "action" => "get",
016157f0 106 "json" => "1",
eb87411f
TO
107 "PHPSESSID" => $this->session_id,
108 "api_key" => $this->api_key,
016157f0
TO
109 );
110 list($status, $data) = $client->post($this->url, $params);
111 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
112 $result = json_decode($data, TRUE);
113 $this->assertNotNull($result);
114 $this->assertAPIErrorCode($result, 0);
49626e3d 115 }
eb87411f 116 */
49626e3d 117
eb87411f 118 function testValidCallAPIKey() {
016157f0
TO
119 $client = CRM_Utils_HttpClient::singleton();
120 $params = array(
121 "entity" => "Contact",
122 "action" => "get",
eb87411f 123 "key" => $this->settings->siteKey,
016157f0 124 "json" => "1",
eb87411f 125 "api_key" => $this->settings->adminApiKey,
016157f0
TO
126 );
127 list($status, $data) = $client->post($this->url, $params);
128 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
129 $result = json_decode($data, TRUE);
130 $this->assertNotNull($result);
131 $this->assertAPIErrorCode($result, 0);
49626e3d
CW
132 }
133
134 function testInvalidAPIKey() {
016157f0
TO
135 $client = CRM_Utils_HttpClient::singleton();
136 $params = array(
137 "entity" => "Contact",
138 "action" => "get",
139 "key" => $this->settings->siteKey,
140 "json" => "1",
eb87411f 141 "api_key" => 'garbage_' . $this->settings->adminApiKey,
016157f0
TO
142 );
143 list($status, $data) = $client->post($this->url, $params);
144 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
145 $result = json_decode($data, TRUE);
146 $this->assertNotNull($result);
147 $this->assertAPIErrorCode($result, 1);
49626e3d
CW
148 }
149
150 function testNotCMSUser() {
016157f0
TO
151 $client = CRM_Utils_HttpClient::singleton();
152 //Create contact with api_key
153 $test_key = "testing1234";
154 $contactParams = array(
155 "api_key" => $test_key,
156 "contact_type" => "Individual",
157 "first_name" => "RestTester1"
158 );
159 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
160 $this->nocms_contact_id = $contact["id"];
49626e3d 161
016157f0
TO
162 $params = array(
163 "entity" => "Contact",
164 "action" => "get",
165 "key" => $this->settings->siteKey,
166 "json" => "1",
167 "api_key" => $test_key
168 );
169 list($status, $data) = $client->post($this->url, $params);
170 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
171 $result = json_decode($data, TRUE);
172 $this->assertNotNull($result);
173 $this->assertAPIErrorCode($result, 1);
49626e3d
CW
174 }
175
eb87411f 176 /*
5700921b
TO
177 protected function _setUpAdminSessionIdAndApiKey() {
178 $client = CRM_Utils_HttpClient::singleton();
179 $params = array(
180 "q" => "civicrm/login",
181 "key" => $this->settings->siteKey,
182 "json" => "1",
183 "name" => $this->settings->adminUsername,
184 "pass" => $this->settings->adminPassword
185 );
186 list($status, $data) = $client->post($this->url, $params);
187 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
188 $result = json_decode($data, TRUE);
189 $this->assertAPIErrorCode($result, 0);
190 $this->api_key = $result["api_key"];
191 $this->session_id = $result["PHPSESSID"];
eb87411f 192 $this->assertTrue(isset($this->api_key), 'Failed to find admin API key');
5700921b 193 return $result;
eb87411f 194 } // */
49626e3d 195}