You are here

public function WebhookPostTest::testWebhookPost in Zoom API 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Functional/WebhookPostTest.php \Drupal\Tests\zoomapi\Functional\WebhookPostTest::testWebhookPost()

Test Webhook Post with header.

File

tests/src/Functional/WebhookPostTest.php, line 80

Class

WebhookPostTest
Webhook post tests.

Namespace

Drupal\Tests\zoomapi\Functional

Code

public function testWebhookPost() {

  // Create a test key.
  $key = $this
    ->createTestKey('zoomapi_webhook_test_key', 'authentication', 'config');

  // Update Zoomapi config.
  $this
    ->drupalGet(Url::fromRoute('zoomapi.settings'));
  $edit = [
    'webhook_verification_token' => 'zoomapi_webhook_test_key',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save configuration');
  $url = Url::fromRoute('zoomapi.webhooks')
    ->setAbsolute()
    ->toString();

  // Mock payload.
  $options = [
    'headers' => [
      'authorization' => 'taco',
    ],
    'http_errors' => FALSE,
    'json' => [
      'event' => 'meeting.started',
      'payload' => [],
    ],
  ];

  // Post to the webhook controller.
  $res = $this
    ->getHttpClient()
    ->post($url, $options);
  $this
    ->assertEquals($res
    ->getStatusCode(), 200, 'Authorization header matched. Post was successful.');

  // Fail with an incorrect authorization header.
  $options['headers']['authorization'] = 'burrito';
  $res = $this
    ->getHttpClient()
    ->post($url, $options);
  $this
    ->assertEquals($res
    ->getStatusCode(), 403, 'Authorization header did not match. Post was denied.');

  // Empty out the payload.
  $options['headers']['authorization'] = 'taco';
  unset($options['json']);
  $res = $this
    ->getHttpClient()
    ->post($url, $options);
  $this
    ->assertEquals($res
    ->getStatusCode(), 400, 'Payload was empty.');
}