Skip to content

Commit

Permalink
Add tests for "BackendNotifier".
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Bauch <bauch@struktur.de>
  • Loading branch information
fancycode committed Jan 30, 2018
1 parent 4498b36 commit 8600dcb
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 10 deletions.
18 changes: 11 additions & 7 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,29 @@ protected function registerInternalSignalingHooks(EventDispatcherInterface $disp
$dispatcher->addListener(Room::class . '::postSessionLeaveCall', $listener);
}

protected function getBackendNotifier() {
return $this->getContainer()->query(BackendNotifier::class);
}

protected function registerSignalingBackendHooks(EventDispatcherInterface $dispatcher) {
$dispatcher->addListener(Room::class . '::postAddUsers', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
$participants= $event->getArgument('users');
$notifier->roomInvited($room, $participants);
});
$dispatcher->addListener(Room::class . '::postSetName', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
$notifier->roomModified($room);
});
$dispatcher->addListener(Room::class . '::postSetParticipantType', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
// The type of a participant has changed, notify all participants
Expand All @@ -119,31 +123,31 @@ protected function registerSignalingBackendHooks(EventDispatcherInterface $dispa
});
$dispatcher->addListener(Room::class . '::postDeleteRoom', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
$participants = $event->getArgument('participants');
$notifier->roomDeleted($room, $participants);
});
$dispatcher->addListener(Room::class . '::postRemoveUser', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
$user = $event->getArgument('user');
$notifier->roomsDisinvited($room, [$user->getUID()]);
});
$dispatcher->addListener(Room::class . '::postSessionJoinCall', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
$sessionId = $event->getArgument('sessionId');
$notifier->roomInCallChanged($room, true, [$sessionId]);
});
$dispatcher->addListener(Room::class . '::postSessionLeaveCall', function(GenericEvent $event) {
/** @var BackendNotifier $notifier */
$notifier = $this->getContainer()->query(BackendNotifier::class);
$notifier = $this->getBackendNotifier();

$room = $event->getSubject();
$sessionId = $event->getArgument('sessionId');
Expand Down
23 changes: 20 additions & 3 deletions lib/Signaling/BackendNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\Spreed\Signaling;

use OCA\Spreed\Config;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCP\Http\Client\IClientService;
use OCP\ILogger;
Expand Down Expand Up @@ -55,6 +56,20 @@ public function __construct(Config $config,
$this->secureRandom = $secureRandom;
}

/**
* Perform actual network request to the signaling backend.
* This can be overridden in tests.
*/
protected function doRequest($url, $params) {
if (defined('PHPUNIT_RUN')) {
// Don't perform network requests when running tests.
return;
}

$client = $this->clientService->newClient();
$client->post($url, $params);
}

/**
* Perform a request to the signaling backend.
*
Expand All @@ -77,7 +92,6 @@ private function backendRequest($url, $data) {
} else if (strpos($url, 'ws://') === 0) {
$url = 'http://' . substr($url, 5);
}
$client = $this->clientService->newClient();
$body = json_encode($data);
$headers = [
'Content-Type' => 'application/json',
Expand All @@ -92,10 +106,10 @@ private function backendRequest($url, $data) {
'headers' => $headers,
'body' => $body,
];
if (!$signaling['verify']) {
if (!empty($signaling['verify'])) {
$params['verify'] = false;
}
$client->post($url, $params);
$this->doRequest($url, $params);
}

/**
Expand Down Expand Up @@ -221,6 +235,9 @@ public function roomInCallChanged($room, $inCall, $sessionIds) {
}
}
foreach ($participants['guests'] as $participant) {
if (!isset($participant['participantType'])) {
$participant['participantType'] = Participant::GUEST;
}
if ($participant['inCall']) {
$users[] = $participant;
}
Expand Down
Loading

0 comments on commit 8600dcb

Please sign in to comment.