Fixed tests

This commit is contained in:
Alex Renoki 2020-09-18 12:15:49 +03:00
parent 9856fb62ed
commit 7a651d78c2
5 changed files with 222 additions and 33 deletions

View File

@ -2,7 +2,7 @@
namespace BeyondCode\LaravelWebSockets\Test\Dashboard; namespace BeyondCode\LaravelWebSockets\Test\Dashboard;
use BeyondCode\LaravelWebSockets\Test\Mocks\Message; use BeyondCode\LaravelWebSockets\Test\Mocks\SignedMessage;
use BeyondCode\LaravelWebSockets\Test\Models\User; use BeyondCode\LaravelWebSockets\Test\Models\User;
use BeyondCode\LaravelWebSockets\Test\TestCase; use BeyondCode\LaravelWebSockets\Test\TestCase;
@ -31,17 +31,12 @@ class AuthTest extends TestCase
$this->pusherServer->onOpen($connection); $this->pusherServer->onOpen($connection);
$signature = "{$connection->socketId}:private-channel"; $message = new SignedMessage([
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
$message = new Message([
'event' => 'pusher:subscribe', 'event' => 'pusher:subscribe',
'data' => [ 'data' => [
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
'channel' => 'private-channel', 'channel' => 'private-channel',
], ],
]); ], $connection, 'private-channel');
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);
@ -65,23 +60,20 @@ class AuthTest extends TestCase
$this->pusherServer->onOpen($connection); $this->pusherServer->onOpen($connection);
$channelData = [ $user = json_encode([
'user_id' => 1, 'user_id' => 1,
'user_info' => [ 'user_info' => [
'name' => 'Rick', 'name' => 'Rick',
], ],
]; ]);
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData); $message = new SignedMessage([
$message = new Message([
'event' => 'pusher:subscribe', 'event' => 'pusher:subscribe',
'data' => [ 'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => 'presence-channel', 'channel' => 'presence-channel',
'channel_data' => json_encode($channelData), 'channel_data' => $user,
], ],
]); ], $connection, 'presence-channel', $user);
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);

View File

@ -33,4 +33,24 @@ class Message extends BaseMessage
{ {
return json_encode($this->payload); return json_encode($this->payload);
} }
/**
* Get the payload as object.
*
* @return stdClass
*/
public function getPayloadAsObject()
{
return json_decode($this->getPayload());
}
/**
* Get the payload as array.
*
* @return stdClass
*/
public function getPayloadAsArray(): array
{
return $this->payload;
}
} }

View File

@ -31,16 +31,12 @@ class PrivateChannelTest extends TestCase
$this->pusherServer->onOpen($connection); $this->pusherServer->onOpen($connection);
$signature = "{$connection->socketId}:private-channel"; $message = new Mocks\SignedMessage([
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
$message = new Mocks\Message([
'event' => 'pusher:subscribe', 'event' => 'pusher:subscribe',
'data' => [ 'data' => [
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
'channel' => 'private-channel', 'channel' => 'private-channel',
], ],
]); ], $connection, 'private-channel');
$this->pusherServer->onMessage($connection, $message); $this->pusherServer->onMessage($connection, $message);

View File

@ -25,11 +25,12 @@ class ReplicationTest extends TestCase
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]); ->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
} }
public function test_events_get_replicated_across_connections() public function test_events_get_replicated_across_connections_for_public_channels()
{ {
$connection = $this->newActiveConnection(['public-channel']); $connection = $this->newActiveConnection(['public-channel']);
$receiver = $this->newActiveConnection(['public-channel']);
$message = [ $message = new Mocks\Message([
'appId' => '1234', 'appId' => '1234',
'serverId' => $this->channelManager->getServerId(), 'serverId' => $this->channelManager->getServerId(),
'event' => 'some-event', 'event' => 'some-event',
@ -37,19 +38,16 @@ class ReplicationTest extends TestCase
'channel' => 'public-channel', 'channel' => 'public-channel',
'test' => 'yes', 'test' => 'yes',
], ],
]; 'socketId' => $connection->socketId,
]);
$channel = $this->channelManager->find('1234', 'public-channel'); $channel = $this->channelManager->find('1234', 'public-channel');
$channel->broadcastToEveryoneExcept( $channel->broadcastToEveryoneExcept(
(object) $message, null, '1234', true $message->getPayloadAsObject(), $connection->socketId, '1234', true
); );
$connection->assertSentEvent('some-event', [ $receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
'data' => ['channel' => 'public-channel', 'test' => 'yes'],
]);
$this->getSubscribeClient() $this->getSubscribeClient()
->assertNothingDispatched(); ->assertNothingDispatched();
@ -57,7 +55,85 @@ class ReplicationTest extends TestCase
$this->getPublishClient() $this->getPublishClient()
->assertCalledWithArgs('publish', [ ->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'public-channel'), $this->channelManager->getRedisKey('1234', 'public-channel'),
json_encode($message), $message->getPayload()
]);
}
public function test_events_get_replicated_across_connections_for_private_channels()
{
$connection = $this->newPrivateConnection('private-channel');
$receiver = $this->newPrivateConnection('private-channel');
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
'event' => 'some-event',
'data' => [
'channel' => 'private-channel',
'test' => 'yes',
],
'socketId' => $connection->socketId,
], $connection, 'private-channel');
$channel = $this->channelManager->find('1234', 'private-channel');
$channel->broadcastToEveryoneExcept(
$message->getPayloadAsObject(), $connection->socketId, '1234', true
);
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
$this->getSubscribeClient()
->assertNothingDispatched();
$this->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'private-channel'),
$message->getPayload()
]);
}
public function test_events_get_replicated_across_connections_for_presence_channels()
{
$connection = $this->newPresenceConnection('presence-channel');
$receiver = $this->newPresenceConnection('presence-channel', ['user_id' => 2]);
$user = [
'user_id' => 1,
'user_info' => [
'name' => 'Rick',
],
];
$encodedUser = json_encode($user);
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => $this->channelManager->getServerId(),
'event' => 'some-event',
'data' => [
'channel' => 'presence-channel',
'channel_data' => $encodedUser,
'test' => 'yes',
],
'socketId' => $connection->socketId,
], $connection, 'presence-channel', $encodedUser);
$channel = $this->channelManager->find('1234', 'presence-channel');
$channel->broadcastToEveryoneExcept(
$message->getPayloadAsObject(), $connection->socketId, '1234', true
);
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
$this->getSubscribeClient()
->assertNothingDispatched();
$this->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'presence-channel'),
$message->getPayload()
]); ]);
} }
@ -186,4 +262,108 @@ class ReplicationTest extends TestCase
$this->assertCount(1, $members); $this->assertCount(1, $members);
}); });
} }
public function test_events_are_processed_by_on_message_on_public_channels()
{
$connection = $this->newActiveConnection(['public-channel']);
$message = new Mocks\Message([
'appId' => '1234',
'serverId' => 'different_server_id',
'event' => 'some-event',
'data' => [
'channel' => 'public-channel',
'test' => 'yes',
],
]);
$this->channelManager->onMessage(
$this->channelManager->getRedisKey('1234', 'public-channel'),
$message->getPayload()
);
// The message does not contain appId and serverId anymore.
$message = new Mocks\Message([
'event' => 'some-event',
'data' => [
'channel' => 'public-channel',
'test' => 'yes',
],
]);
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
}
public function test_events_are_processed_by_on_message_on_private_channels()
{
$connection = $this->newPrivateConnection('private-channel');
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => 'different_server_id',
'event' => 'some-event',
'data' => [
'channel' => 'private-channel',
'test' => 'yes',
],
], $connection, 'private-channel');
$this->channelManager->onMessage(
$this->channelManager->getRedisKey('1234', 'private-channel'),
$message->getPayload()
);
// The message does not contain appId and serverId anymore.
$message = new Mocks\SignedMessage([
'event' => 'some-event',
'data' => [
'channel' => 'private-channel',
'test' => 'yes',
],
], $connection, 'private-channel');
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
}
public function test_events_are_processed_by_on_message_on_presence_channels()
{
$user = [
'user_id' => 1,
'user_info' => [
'name' => 'Rick',
],
];
$connection = $this->newPresenceConnection('presence-channel', $user);
$encodedUser = json_encode($user);
$message = new Mocks\SignedMessage([
'appId' => '1234',
'serverId' => 'different_server_id',
'event' => 'some-event',
'data' => [
'channel' => 'presence-channel',
'channel_data' => $encodedUser,
'test' => 'yes',
],
], $connection, 'presence-channel', $encodedUser);
$this->channelManager->onMessage(
$this->channelManager->getRedisKey('1234', 'presence-channel'),
$message->getPayload()
);
// The message does not contain appId and serverId anymore.
$message = new Mocks\SignedMessage([
'event' => 'some-event',
'data' => [
'channel' => 'presence-channel',
'channel_data' => $encodedUser,
'test' => 'yes',
],
], $connection, 'presence-channel', $encodedUser);
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
}
} }

View File

@ -190,10 +190,11 @@ class TriggerEventTest extends TestCase
->assertCalledWithArgs('publish', [ ->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'public-channel'), $this->channelManager->getRedisKey('1234', 'public-channel'),
json_encode([ json_encode([
'channel' => 'public-channel',
'event' => null, 'event' => null,
'channel' => 'public-channel',
'data' => null, 'data' => null,
'appId' => '1234', 'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(), 'serverId' => $this->channelManager->getServerId(),
]), ]),
]); ]);