Skip to content

Commit

Permalink
(NFC) Add in unit test checking that E2E test returns sensible contac…
Browse files Browse the repository at this point in the history
…t_id when passed in a special token proving that session is working
  • Loading branch information
seamuslee001 committed Apr 17, 2019
1 parent 746d05d commit 7e6f3b4
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions tests/phpunit/E2E/Extern/RestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class E2E_Extern_RestTest extends CiviEndToEndTestCase {
protected $session_id;
protected $nocms_contact_id;
protected $old_api_keys;
protected $adminContactId;

/**
* @param $apiResult
Expand Down Expand Up @@ -272,6 +273,30 @@ public function testNotCMSUser_entityAction() {
$this->assertAPIErrorCode($result, 1);
}

/**
* Submit a request with an API key that exists but does not correspond to.
* a real user. Submit in "?entity=X&action=X" notation
*/
public function testGetCorrectUserBack() {
$client = CRM_Utils_HttpClient::singleton();

//Create contact with api_key
// The key associates with a real contact but not a real user
$params = array(
"entity" => "Contact",
"action" => "get",
"key" => $GLOBALS['_CV']['CIVI_SITE_KEY'],
"json" => "1",
"api_key" => self::getApiKey(),
"id" => "user_contact_id",
);
list($status, $data) = $client->post($this->getRestUrl(), $params);
$this->assertEquals(CRM_Utils_HttpClient::STATUS_OK, $status);
$result = json_decode($data, TRUE);
$this->assertNotNull($result);
$this->assertEquals($result['id'], $this->adminContactId);
}

/**
* Submit a request with an API key that exists but does not correspond to
* a real user. Submit in "?q=civicrm/$entity/$action" notation
Expand Down Expand Up @@ -305,13 +330,13 @@ public function testNotCMSUser_q() {

protected function updateAdminApiKey() {
/** @var int $adminContactId */
$adminContactId = civicrm_api3('contact', 'getvalue', array(
$this->adminContactId = civicrm_api3('contact', 'getvalue', array(
'id' => '@user:' . $GLOBALS['_CV']['ADMIN_USER'],
'return' => 'id',
));

$this->old_api_keys[$adminContactId] = CRM_Core_DAO::singleValueQuery('SELECT api_key FROM civicrm_contact WHERE id = %1', [
1 => [$adminContactId, 'Positive'],
$this->old_api_keys[$this->adminContactId] = CRM_Core_DAO::singleValueQuery('SELECT api_key FROM civicrm_contact WHERE id = %1', [
1 => [$this->adminContactId, 'Positive'],
]);

//$this->old_admin_api_key = civicrm_api3('Contact', 'get', array(
Expand All @@ -320,7 +345,7 @@ protected function updateAdminApiKey() {
//));

civicrm_api3('Contact', 'create', array(
'id' => $adminContactId,
'id' => $this->adminContactId,
'api_key' => self::getApiKey(),
));
}
Expand Down

0 comments on commit 7e6f3b4

Please sign in to comment.