Browse Source

PHP has a odd notion of truth (#48)

* PHP has a odd notion of truth

* Exiting early

* Mocking token generator

* Standards

* Forgot about Bearer
pull/756/head
dannylamb 8 years ago committed by Jonathan Green
parent
commit
cfcc16e337
  1. 10
      src/Plugin/RulesAction/Broadcaster.php
  2. 11
      tests/src/Kernel/BroadcasterTest.php

10
src/Plugin/RulesAction/Broadcaster.php

@ -118,19 +118,19 @@ class Broadcaster extends RulesActionBase implements ContainerFactoryPluginInter
// Include a token for later authentication in the message.
$token = $this->auth->generateToken();
if ($token !== NULL) {
$headers['Authorization'] = "Bearer $token";
}
else {
if (empty($token)) {
// JWT isn't properly configured. Log and notify user.
\Drupal::logger('islandora')->error(
'Error getting JWT token for message: @msg', ['@msg' => $e->getMessage()]
'Error getting JWT token for message: @msg', ['@msg' => $message]
);
drupal_set_message(
t('Error getting JWT token for message. Check JWT Configuration.'), 'error'
);
return;
}
$headers['Authorization'] = "Bearer $token";
// Transform message from string into a proper message object.
$message = new Message($message, $headers);

11
tests/src/Kernel/BroadcasterTest.php

@ -5,6 +5,7 @@ namespace Drupal\Tests\islandora\Kernel;
use Stomp\Exception\StompException;
use Stomp\StatefulStomp;
use Drupal\islandora\Plugin\RulesAction\Broadcaster;
use Drupal\jwt\Authentication\Provider\JwtAuth;
/**
* Broadcaster tests.
@ -87,6 +88,10 @@ class BroadcasterTest extends IslandoraKernelTestBase {
strcmp($headers['IslandoraBroadcastRecipients'], 'activemq:queue:foo,activemq:queue:bar') == 0,
"IslandoraBroadcastRecipients header must be a comma separated list of endpoints"
);
$this->assertTrue(
strcmp($headers['Authorization'], 'Bearer some_token') == 0,
"Authorization header must be set"
);
$stomp->unsubscribe();
}
catch (StompException $e) {
@ -106,10 +111,14 @@ class BroadcasterTest extends IslandoraKernelTestBase {
protected function createBroadcaster(StatefulStomp $stomp) {
// Pull the plugin definition out of the plugin system.
$actionManager = $this->container->get('plugin.manager.rules_action');
$jwt = $this->container->get('jwt.authentication.jwt');
$definitions = $actionManager->getDefinitions();
$pluginDefinition = $definitions['islandora_broadcast'];
// Mock a JWT generator.
$prophecy = $this->prophesize(JwtAuth::class);
$prophecy->generateToken()->willReturn("some_token");
$jwt = $prophecy->reveal();
$action = new Broadcaster(
[],
'islandora_broadcast',

Loading…
Cancel
Save