From 3cf30011962e23f0c93c9d2f90be4fdc35b5ec4f Mon Sep 17 00:00:00 2001 From: Brad Sidoruk Date: Thu, 7 Jul 2016 12:58:08 -0600 Subject: [PATCH 1/2] Added some channel tests --- mix.exs | 5 +++- mix.lock | 28 ++++++++++----------- test/channels/room_channel_test.exs | 39 +++++++++++++++++++++++++++++ test/support/channel_case.ex | 10 ++++++++ 4 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 test/channels/room_channel_test.exs create mode 100644 test/support/channel_case.ex diff --git a/mix.exs b/mix.exs index 602ebb2..c98296b 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule Chat.Mixfile do [app: :chat, version: "0.0.1", elixir: "~> 1.0", - elixirc_paths: ["lib", "web"], + elixirc_paths: elixirc_paths(Mix.env), compilers: [:phoenix] ++ Mix.compilers, deps: deps] end @@ -18,6 +18,9 @@ defmodule Chat.Mixfile do applications: [:phoenix, :phoenix_html, :cowboy, :logger]] end + defp elixirc_paths(:test), do: ["lib", "web", "test/support"] + defp elixirc_paths(_), do: ["lib", "web"] + # Specifies your project dependencies # # Type `mix help deps` for examples and options diff --git a/mix.lock b/mix.lock index 035fe17..26e01d1 100644 --- a/mix.lock +++ b/mix.lock @@ -1,15 +1,15 @@ -%{"cowboy": {:hex, :cowboy, "1.0.4"}, - "cowlib": {:hex, :cowlib, "1.0.2"}, - "decimal": {:hex, :decimal, "1.1.0"}, - "ecto": {:hex, :ecto, "1.0.1"}, - "fs": {:hex, :fs, "0.9.2"}, - "phoenix": {:hex, :phoenix, "1.1.4"}, - "phoenix_ecto": {:hex, :phoenix_ecto, "1.2.0"}, - "phoenix_html": {:hex, :phoenix_html, "2.5.0"}, - "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.0"}, - "plug": {:hex, :plug, "1.1.0"}, - "poison": {:hex, :poison, "1.5.2"}, - "poolboy": {:hex, :poolboy, "1.5.1"}, - "postgrex": {:hex, :postgrex, "0.9.1"}, - "ranch": {:hex, :ranch, "1.2.1"}, +%{"cowboy": {:hex, :cowboy, "1.0.4", "a324a8df9f2316c833a470d918aaf73ae894278b8aa6226ce7a9bf699388f878", [:rebar, :make], [{:cowlib, "~> 1.0.0", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.0", [hex: :ranch, optional: false]}]}, + "cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []}, + "decimal": {:hex, :decimal, "1.1.0", "3333732f17a90ff3057d7ab8c65f0930ca2d67e15cca812a91ead5633ed472fe", [:mix], []}, + "ecto": {:hex, :ecto, "1.0.1", "cb0ef6601148c1d3fbce161a7536b2c5f7e80e03a2b72c4b98333d625dbb91ab", [:mix], [{:sbroker, "~> 0.7", [hex: :sbroker, optional: true]}, {:postgrex, "~> 0.9.1", [hex: :postgrex, optional: true]}, {:poolboy, "~> 1.4", [hex: :poolboy, optional: false]}, {:poison, "~> 1.0", [hex: :poison, optional: true]}, {:mariaex, "~> 0.4.1", [hex: :mariaex, optional: true]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, + "fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []}, + "phoenix": {:hex, :phoenix, "1.1.4", "65809fba92eb94377372a5fb5a561197654bb8406e773cc47ca1a031bbe58019", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}, {:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]}, + "phoenix_ecto": {:hex, :phoenix_ecto, "1.2.0", "3f261f1822d8fba109c23fa584fe921a7a12f69e4d45bf29aea58b04238e7a19", [:mix], [{:poison, "~> 1.3", [hex: :poison, optional: true]}, {:phoenix_html, "~> 2.2", [hex: :phoenix_html, optional: true]}, {:ecto, "~> 1.0", [hex: :ecto, optional: false]}]}, + "phoenix_html": {:hex, :phoenix_html, "2.5.0", "44377a0470e8368ff7e6180dcafabeafe5112e920cf2a72e630416cb4b6b58c1", [:mix], [{:plug, "~> 0.13 or ~> 1.0", [hex: :plug, optional: false]}]}, + "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.0", "d985d0cd1d36e89d58cae3da7117dca0724d93922613b8b6b82fef7dcad5659d", [:mix], [{:phoenix, "~> 0.16 or ~> 1.0", [hex: :phoenix, optional: false]}, {:fs, "~> 0.9.1", [hex: :fs, optional: false]}]}, + "plug": {:hex, :plug, "1.1.0", "69078a481549b81e879e169fbe099a509baf8d6a793db757f39f86258f91db07", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}]}, + "poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], []}, + "poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []}, + "postgrex": {:hex, :postgrex, "0.9.1", "a7c9d4b3cc70b4b8c73190e0d462e057e22e1597954deb422fc45d5183463d61", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, optional: false]}]}, + "ranch": {:hex, :ranch, "1.2.1", "a6fb992c10f2187b46ffd17ce398ddf8a54f691b81768f9ef5f461ea7e28c762", [:make], []}, "redo": {:hex, :redo, "2.0.1"}} diff --git a/test/channels/room_channel_test.exs b/test/channels/room_channel_test.exs new file mode 100644 index 0000000..a9be241 --- /dev/null +++ b/test/channels/room_channel_test.exs @@ -0,0 +1,39 @@ +defmodule Chat.RoomChannelTest do + use Chat.ChannelCase + alias Chat.RoomChannel + + setup do + {:ok, _, socket} = subscribe_and_join(socket(), RoomChannel, "rooms:lobby", %{"user" => "Bob"}) + {:ok, socket: socket} + end + + test "join room other than lobby", %{socket: _socket} do + ref = subscribe_and_join(socket(), RoomChannel, "rooms:other") + assert {:error, %{reason: "unauthorized"}} = ref + end + + test "join lobby", %{socket: _socket} do + assert_broadcast("user:entered", %{user: "Bob"}) + assert_push("join", %{status: "connected"}) + end + + test "ping", %{socket: socket} do + send(socket.channel_pid, :ping) + assert_push("new:msg", %{user: "SYSTEM", body: "ping"}) + end + + test "recieve new:message", %{socket: socket} do + ref = push(socket, "new:msg", %{"user" => "Bob", "body" => "Hello World"}) + socket = Phoenix.Channel.Server.socket(socket.channel_pid) #retrieve the updated socket + + assert socket.assigns[:user] == "Bob" + assert_reply(ref, :ok, %{msg: "Hello World"}) + assert_broadcast("new:msg", %{user: "Bob", body: "Hello World"}, socket) + end + + test "leaving channel", %{socket: socket} do + Process.unlink(socket.channel_pid) + ref = leave(socket) + assert_reply(ref, :ok) + end +end diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex new file mode 100644 index 0000000..9f67de7 --- /dev/null +++ b/test/support/channel_case.ex @@ -0,0 +1,10 @@ +defmodule Chat.ChannelCase do + use ExUnit.CaseTemplate + + using do + quote do + use Phoenix.ChannelTest + @endpoint Chat.Endpoint + end + end +end From 38f2749c7fd720eebdcad552dfebe60c62b2cf94 Mon Sep 17 00:00:00 2001 From: Brad Sidoruk Date: Thu, 7 Jul 2016 13:29:56 -0600 Subject: [PATCH 2/2] renamed tests --- test/channels/room_channel_test.exs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/channels/room_channel_test.exs b/test/channels/room_channel_test.exs index a9be241..143452b 100644 --- a/test/channels/room_channel_test.exs +++ b/test/channels/room_channel_test.exs @@ -7,22 +7,22 @@ defmodule Chat.RoomChannelTest do {:ok, socket: socket} end - test "join room other than lobby", %{socket: _socket} do + test "join subtopic other than lobby", %{socket: _socket} do ref = subscribe_and_join(socket(), RoomChannel, "rooms:other") assert {:error, %{reason: "unauthorized"}} = ref end - test "join lobby", %{socket: _socket} do + test "join rooms:lobby", %{socket: _socket} do assert_broadcast("user:entered", %{user: "Bob"}) assert_push("join", %{status: "connected"}) end - test "ping", %{socket: socket} do + test "ping channel", %{socket: socket} do send(socket.channel_pid, :ping) assert_push("new:msg", %{user: "SYSTEM", body: "ping"}) end - test "recieve new:message", %{socket: socket} do + test "handle new:message", %{socket: socket} do ref = push(socket, "new:msg", %{"user" => "Bob", "body" => "Hello World"}) socket = Phoenix.Channel.Server.socket(socket.channel_pid) #retrieve the updated socket