RestTest - Simplify test for property "siteKey" settings
[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
88be34fc
TO
46 if (!property_exists($this->settings, 'siteKey') || empty($this->settings->siteKey)){
47 $this->markTestSkipped('CiviSeleniumSettings is missing siteKey');
48 }
49
49626e3d
CW
50 $client = CRM_Utils_HttpClient::singleton();
51 $params = array(
52 "q" => "civicrm/login",
af8a28ef 53 "key" => $this->settings->siteKey,
49626e3d
CW
54 "json" => "1",
55 "name" => $this->settings->adminUsername,
56 "pass" => $this->settings->adminPassword
57 );
58 list($status, $data) = $client->post($this->url, $params);
59 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
60 $result = json_decode($data, TRUE);
66eec473 61 $this->assertAPIErrorCode($result, 0);
49626e3d
CW
62 $this->api_key = $result["api_key"];
63 $this->session_id = $result["PHPSESSID"];
64 if(!isset($this->api_key)){
65 $this->markTestSkipped('Admin does not have an associated API key');
66 }
67 }
68
69 protected function tearDown() {
70 parent::tearDown();
71 if(isset($this->nocms_contact_id)){
72 $deleteParams = array(
73 "id" => $this->nocms_contact_id,
74 "skip_undelete" => 1
75 );
76 $res = $this->webtest_civicrm_api("Contact", "delete", $deleteParams);
77 unset($this->nocms_contact_id);
78 }
79 }
80
81 function testValidLoginCMSUser() {
49626e3d
CW
82 $client = CRM_Utils_HttpClient::singleton();
83 $params = array(
84 "q" => "civicrm/login",
af8a28ef 85 "key" => $this->settings->siteKey,
49626e3d
CW
86 "json" => "1",
87 "name" => $this->settings->adminUsername,
88 "pass" => $this->settings->adminPassword
89 );
90 list($status, $data) = $client->post($this->url, $params);
91 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
92 $result = json_decode($data, TRUE);
93 $this->assertNotNull($result);
66eec473 94 $this->assertAPIErrorCode($result, 0);
49626e3d
CW
95 }
96
97 function testInvalidPasswordLogin() {
49626e3d
CW
98 $client = CRM_Utils_HttpClient::singleton();
99 $badPassword = $this->settings->adminPassword . "badpass";
100 $params = array(
101 "q" => "civicrm/login",
af8a28ef 102 "key" => $this->settings->siteKey,
49626e3d
CW
103 "json" => "1",
104 "name" => $this->settings->adminUsername,
105 "pass" => $badPassword
106 );
107 list($status, $data) = $client->post($this->url, $params);
108 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
109 $result = json_decode($data, TRUE);
110 $this->assertNotNull($result);
66eec473 111 $this->assertAPIErrorCode($result, 1);
49626e3d
CW
112 }
113
af8a28ef 114 function testValidCallsiteKey() {
49626e3d
CW
115 $client = CRM_Utils_HttpClient::singleton();
116 $params = array(
117 "entity" => "Contact",
118 "action" => "get",
af8a28ef 119 "key" => $this->settings->siteKey,
49626e3d
CW
120 "json" => "1",
121 "api_key" => $this->api_key
122 );
123 list($status, $data) = $client->post($this->url, $params);
124 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
125 $result = json_decode($data, TRUE);
126 $this->assertNotNull($result);
66eec473 127 $this->assertAPIErrorCode($result, 0);
49626e3d
CW
128 }
129
130 function testValidCallPHPSessionID() {
49626e3d
CW
131 $client = CRM_Utils_HttpClient::singleton();
132 $params = array(
133 "entity" => "Contact",
134 "action" => "get",
135 "json" => "1",
136 "PHPSESSID" => $this->session_id,
137 "api_key" => $this->api_key,
138 );
139 list($status, $data) = $client->post($this->url, $params);
140 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
141 $result = json_decode($data, TRUE);
142 $this->assertNotNull($result);
66eec473 143 $this->assertAPIErrorCode($result, 0);
49626e3d
CW
144 }
145
146 function testInvalidAPIKey() {
49626e3d
CW
147 $client = CRM_Utils_HttpClient::singleton();
148 $params = array(
149 "entity" => "Contact",
150 "action" => "get",
af8a28ef 151 "key" => $this->settings->siteKey,
49626e3d
CW
152 "json" => "1",
153 "api_key" => "zzzzzzzzzzzzzzaaaaaaaaaaaaaaaaabadasdasd"
154 );
155 list($status, $data) = $client->post($this->url, $params);
156 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
157 $result = json_decode($data, TRUE);
158 $this->assertNotNull($result);
66eec473 159 $this->assertAPIErrorCode($result, 1);
49626e3d
CW
160 }
161
162 function testNotCMSUser() {
49626e3d
CW
163 $client = CRM_Utils_HttpClient::singleton();
164 //Create contact with api_key
165 $test_key = "testing1234";
166 $contactParams = array(
167 "api_key" => $test_key,
168 "contact_type" => "Individual",
169 "first_name" => "RestTester1"
170 );
171 $contact = $this->webtest_civicrm_api("Contact", "create", $contactParams);
172 $this->nocms_contact_id = $contact["id"];
173
174 $params = array(
175 "entity" => "Contact",
176 "action" => "get",
af8a28ef 177 "key" => $this->settings->siteKey,
49626e3d
CW
178 "json" => "1",
179 "api_key" => $test_key
180 );
181 list($status, $data) = $client->post($this->url, $params);
182 $this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
183 $result = json_decode($data, TRUE);
184 $this->assertNotNull($result);
66eec473 185 $this->assertAPIErrorCode($result, 1);
49626e3d
CW
186 }
187
188}