diff --git a/src/Plugin/RulesAction/Broadcaster.php b/src/Plugin/RulesAction/Broadcaster.php index 09abcd48..1d8c0784 100644 --- a/src/Plugin/RulesAction/Broadcaster.php +++ b/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); diff --git a/tests/src/Kernel/BroadcasterTest.php b/tests/src/Kernel/BroadcasterTest.php index 02b023ab..87bfaec7 100644 --- a/tests/src/Kernel/BroadcasterTest.php +++ b/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',