Add channel to be joined
This commit is contained in:
parent
1a8d9cd840
commit
3afaf346c7
6 changed files with 221 additions and 0 deletions
81
test/birdy_chat_web/channels/server_channel_test.exs
Normal file
81
test/birdy_chat_web/channels/server_channel_test.exs
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
defmodule BirdyChatWeb.ServerChannelTest do
|
||||
use BirdyChatWeb.ChannelCase
|
||||
|
||||
setup do
|
||||
server_id = "test2"
|
||||
token = Phoenix.Token.sign(BirdyChatWeb.Endpoint, "serverAuth", server_id)
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test-user.txt"])
|
||||
on_exit(fn -> File.rm(path) end)
|
||||
|
||||
{:ok, _, socket} =
|
||||
BirdyChatWeb.ServerSocket
|
||||
|> socket("server_socket", %{server_id: server_id})
|
||||
|> subscribe_and_join(BirdyChatWeb.ServerChannel, "server:#{server_id}", %{token: token})
|
||||
|
||||
%{socket: socket}
|
||||
end
|
||||
|
||||
describe "authorization" do
|
||||
test "requires a signed token to join a channel" do
|
||||
server_id = "test2"
|
||||
token = Phoenix.Token.sign(BirdyChatWeb.Endpoint, "fakeAuth", server_id)
|
||||
|
||||
result =
|
||||
BirdyChatWeb.ServerSocket
|
||||
|> socket("server_socket", %{server_id: server_id})
|
||||
|> subscribe_and_join(BirdyChatWeb.ServerChannel, "server:#{server_id}", %{token: token})
|
||||
|
||||
assert result == {:error, %{reason: "unauthorised"}}
|
||||
end
|
||||
|
||||
test "requires server id in token and in channel to match" do
|
||||
server_id = "test2"
|
||||
token = Phoenix.Token.sign(BirdyChatWeb.Endpoint, "serverAuth", server_id)
|
||||
|
||||
result =
|
||||
BirdyChatWeb.ServerSocket
|
||||
|> socket("server_socket", %{server_id: server_id})
|
||||
|> subscribe_and_join(BirdyChatWeb.ServerChannel, "server:fake", %{token: token})
|
||||
|
||||
assert result == {:error, %{reason: "unauthorised"}}
|
||||
end
|
||||
end
|
||||
|
||||
describe "new_message" do
|
||||
test "receives message from other server and saves it locally", %{socket: socket} do
|
||||
message = %{from: "test2-user", to: "test-user", message: "123"}
|
||||
bin = :erlang.term_to_binary(message)
|
||||
ref = push(socket, "new_message", {:binary, bin})
|
||||
assert_reply ref, :ok, {:binary, ^bin}
|
||||
|
||||
path = Application.app_dir(:birdy_chat, ["priv", "messages", "test-user.txt"])
|
||||
contents = File.read!(path)
|
||||
assert contents == "test2-user: 123\n"
|
||||
end
|
||||
|
||||
test "blows up when sent payload is not safe to decode", %{socket: socket} do
|
||||
# This unsafe binary is taken from erlang docs directly:
|
||||
bin = <<131, 119, 8, "tjenixen">>
|
||||
Process.flag(:trap_exit, true)
|
||||
push(socket, "new_message", {:binary, bin})
|
||||
|
||||
assert_receive {:EXIT, _pid,
|
||||
{:badarg,
|
||||
[
|
||||
{:erlang, :binary_to_term,
|
||||
[<<131, 119, 8, 116, 106, 101, 110, 105, 120, 101, 110>>, [:safe]],
|
||||
[error_info: %{module: :erl_erts_errors}]},
|
||||
{BirdyChatWeb.ServerChannel, :handle_in, 3,
|
||||
[file: ~c"lib/birdy_chat_web/channels/server_channel.ex", line: 15]},
|
||||
{Phoenix.Channel.Server, :handle_info, 2,
|
||||
[file: ~c"lib/phoenix/channel/server.ex", line: 332]},
|
||||
{:gen_server, :try_handle_info, 3,
|
||||
[file: ~c"gen_server.erl", line: 2434]},
|
||||
{:gen_server, :handle_msg, 3, [file: ~c"gen_server.erl", line: 2420]},
|
||||
{:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 333]}
|
||||
]}},
|
||||
100
|
||||
end
|
||||
end
|
||||
end
|
||||
34
test/support/channel_case.ex
Normal file
34
test/support/channel_case.ex
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
defmodule BirdyChatWeb.ChannelCase do
|
||||
@moduledoc """
|
||||
This module defines the test case to be used by
|
||||
channel tests.
|
||||
|
||||
Such tests rely on `Phoenix.ChannelTest` and also
|
||||
import other functionality to make it easier
|
||||
to build common data structures and query the data layer.
|
||||
|
||||
Finally, if the test case interacts with the database,
|
||||
we enable the SQL sandbox, so changes done to the database
|
||||
are reverted at the end of every test. If you are using
|
||||
PostgreSQL, you can even run database tests asynchronously
|
||||
by setting `use BirdyChatWeb.ChannelCase, async: true`, although
|
||||
this option is not recommended for other databases.
|
||||
"""
|
||||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
using do
|
||||
quote do
|
||||
# Import conveniences for testing with channels
|
||||
import Phoenix.ChannelTest
|
||||
import BirdyChatWeb.ChannelCase
|
||||
|
||||
# The default endpoint for testing
|
||||
@endpoint BirdyChatWeb.Endpoint
|
||||
end
|
||||
end
|
||||
|
||||
setup _tags do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue