View source
<?php
namespace Drupal\Tests\zoomapi\Functional;
use Drupal\Core\Url;
use Drupal\key\Entity\Key;
use Drupal\Tests\BrowserTestBase;
class WebhookPostTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'zoomapi',
'key',
];
protected $testKey;
protected $user;
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'administer zoom api',
'administer keys',
]);
$this
->drupalLogin($this->user);
}
public function testGetMethodBlocked() {
$url = Url::fromRoute('zoomapi.webhooks')
->setAbsolute()
->toString();
$res = $this
->getHttpClient()
->get($url, [
'http_errors' => FALSE,
]);
$this
->assertEquals($res
->getStatusCode(), 405, 'GET method not allowed.');
}
public function testWebhookPostNoAuth() {
$url = Url::fromRoute('zoomapi.webhooks')
->setAbsolute()
->toString();
$res = $this
->getHttpClient()
->post($url, [
'http_errors' => FALSE,
]);
$this
->assertEquals($res
->getStatusCode(), 403, 'Access Denied due to missing authorization header.');
}
public function testWebhookPost() {
$this
->createTestKey('zoomapi_webhook_test_key', 'authentication', '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();
$options = [
'headers' => [
'authorization' => 'taco',
],
'http_errors' => FALSE,
'json' => [
'event' => 'meeting.started',
'payload' => [],
],
];
$res = $this
->getHttpClient()
->post($url, $options);
$this
->assertEquals($res
->getStatusCode(), 200, 'Authorization header matched. Post was successful.');
$options['headers']['authorization'] = 'burrito';
$res = $this
->getHttpClient()
->post($url, $options);
$this
->assertEquals($res
->getStatusCode(), 403, 'Authorization header did not match. Post was denied.');
$options['headers']['authorization'] = 'taco';
unset($options['json']);
$res = $this
->getHttpClient()
->post($url, $options);
$this
->assertEquals($res
->getStatusCode(), 400, 'Payload was empty.');
}
protected function createTestKey($id, $type = NULL, $provider = NULL) {
$keyArgs = [
'id' => $id,
'label' => 'Zoom API Webhook Test Key',
];
if ($type != NULL) {
$keyArgs['key_type'] = $type;
}
if ($provider != NULL) {
$keyArgs['key_provider'] = $provider;
}
$this->testKey = Key::create($keyArgs);
$this->testKey
->setKeyValue('taco');
$this->testKey
->save();
return $this->testKey;
}
}