You are here

public function AcquiaSearchTest::testValidResponse in Acquia Connector 8

Covers validateResponse.

@covers ::validateResponse

File

acquia_search/tests/src/Unit/AcquiaSearchTest.php, line 125

Class

AcquiaSearchTest
Isolated tests for Acquia Search.

Namespace

Drupal\Tests\acquia_search\Unit

Code

public function testValidResponse() {

  // Generate the expected hash.
  $nonce = $this
    ->randomMachineName(32);
  $string = $this
    ->randomMachineName(32);
  $hmac = hash_hmac('sha1', $nonce . $string, $this->derivedKey);

  // Pass the expected hmac digest, API function should return TRUE.
  $valid = $this->searchSubscriber
    ->validateResponse($hmac, $nonce, $string, $this->derivedKey);
  $this
    ->assertTrue($valid, 'Response flagged as valid when the expected hash is passed.');

  // Invalidate the hmac digest, API function should return FALSE.
  $bad_hmac = $hmac . 'invalidateHash';
  $invalid_hmac = $this->searchSubscriber
    ->validateResponse($bad_hmac, $nonce, $string, $this->derivedKey);
  $this
    ->assertFalse($invalid_hmac, 'Response flagged as invalid when a malformed hash is passed.');

  // Invalidate the nonce, API function should return FALSE.
  $bad_nonce = $nonce . 'invalidateString';
  $invalid_nonce = $this->searchSubscriber
    ->validateResponse($hmac, $bad_nonce, $string, $this->derivedKey);
  $this
    ->assertFalse($invalid_nonce, 'Response flagged as invalid when a malformed nonce is passed.');

  // Invalidate the string, API function should return FALSE.
  $bad_string = $string . 'invalidateString';
  $invalid_string = $this->searchSubscriber
    ->validateResponse($hmac, $nonce, $bad_string, $this->derivedKey);
  $this
    ->assertFalse($invalid_string, 'Response flagged as invalid when a malformed string is passed.');

  // Invalidate the derived key, API function should return FALSE.
  $bad_key = $this->derivedKey . 'invalidateKey';
  $invalid_key = $this->searchSubscriber
    ->validateResponse($hmac, $nonce, $string, $bad_key);
  $this
    ->assertFalse($invalid_key, 'Response flagged as invalid when a malformed derived key is passed.');
}