You are here

public function AcquiaSearchTest::testCalculateAuthCookie in Acquia Connector 8

Covers calculateAuthCookie.

@covers ::calculateAuthCookie

File

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

Class

AcquiaSearchTest
Isolated tests for Acquia Search.

Namespace

Drupal\Tests\acquia_search\Unit

Code

public function testCalculateAuthCookie() {

  // Generate the expected hash.
  $time = 1577635946;
  $nonce = $this
    ->randomMachineName(32);
  $string = $time . $nonce . $this
    ->randomMachineName();
  $hmac = hash_hmac('sha1', $time . $nonce . $string, $this->derivedKey);
  $calculateAuthCookie = $this
    ->getMockBuilder('Drupal\\acquia_search\\EventSubscriber\\SearchSubscriber')
    ->setMethods([
    'getDerivedKey',
  ])
    ->getMock();
  $calculateAuthCookie
    ->expects($this
    ->any())
    ->method('getDerivedKey')
    ->willReturn($this->derivedKey);
  $authenticator = $calculateAuthCookie
    ->calculateAuthCookie($string, $nonce, $time, $this->derivedKey, $time);
  preg_match('/acquia_solr_hmac=([a-zA-Z0-9]{40});/', $authenticator, $matches);
  $this
    ->assertEquals($hmac, $matches[1], 'HMAC API function generates the expected hmac hash.');
  preg_match('/acquia_solr_time=([0-9]{10});/', $authenticator, $matches);
  $this
    ->assertNotNull($matches, 'HMAC API function generates a timestamp.', 'Acquia Search');
  preg_match('/acquia_solr_nonce=([a-zA-Z0-9]{32});/', $authenticator, $matches);
  $this
    ->assertEquals($nonce, $matches[1], 'HMAC API function generates the expected nonce.');
}