Skip to content

Commit

Permalink
Added channel test, rewrote channel so it matches controller in style
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Dec 18, 2017
1 parent 4e4b025 commit ab765e3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
23 changes: 10 additions & 13 deletions lib/code_corps_web/channels/conversation_channel.ex
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
defmodule CodeCorpsWeb.ConversationChannel do
use Phoenix.Channel

alias CodeCorps.{Conversation, Repo}
alias CodeCorps.{Conversation, Policy, Repo, User}
alias Phoenix.Socket

def join("conversation:" <> id, _params, %{assigns: %{current_user: user}} = socket) do
conversation = Conversation |> Repo.get(id)
@spec join(String.t, map, Socket.t) :: {:ok, Socket.t} | {:error, map}
def join("conversation:" <> id, %{}, %Socket{} = socket) do
with %Conversation{} = conversation <- Conversation |> Repo.get(id),
%User{} = current_user <- socket.assigns[:current_user],
{:ok, :authorized} <- current_user |> Policy.authorize(:show, conversation, %{}) do

if user |> authorized?(conversation) do
{:ok, socket}
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
nil -> {:error, %{reason: "unauthenticated"}}
{:error, :not_authorized} -> {:error, %{reason: "unauthorized"}}
end
end
def join(_, _, _) do
{:error, %{reason: "unauthenticated"}}
end

def event("new:conversation-part", socket, message) do
broadcast socket, "new:conversation-part", message
Expand All @@ -30,8 +31,4 @@ defmodule CodeCorpsWeb.ConversationChannel do

CodeCorpsWeb.Endpoint.broadcast(channel, event, payload)
end

defp authorized?(user, conversation) do
CodeCorps.Policy.authorize(user, :show, conversation, %{})
end
end
52 changes: 52 additions & 0 deletions test/lib/code_corps_web/channels/conversation_channel_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule CodeCorpsWeb.ConversationChannelTest do
use CodeCorpsWeb.ChannelCase

alias CodeCorps.{Conversation, User}
alias CodeCorpsWeb.ConversationChannel

def build_socket(%Conversation{id: id}, %User{} = current_user) do
"test"
|> socket(%{current_user: current_user})
|> subscribe_and_join(ConversationChannel, "conversation:#{id}")
end

describe "conversation:id" do
test "requires authentication" do
%{id: id} = insert(:conversation)

response =
"test"
|> socket(%{})
|> subscribe_and_join(ConversationChannel, "conversation:#{id}")

assert response == {:error, %{reason: "unauthenticated"}}
end

test "ensures current user is authorized for :show on resource" do
user = insert(:user)
%{id: id} = insert(:conversation)

response =
"test"
|> socket(%{current_user: user})
|> subscribe_and_join(ConversationChannel, "conversation:#{id}")

assert response == {:error, %{reason: "unauthorized"}}
end

test "broadcasts new conversation part" do
%{id: id, user: user} = conversation = insert(:conversation)

{:ok, %{}, _socket} =
"test"
|> socket(%{current_user: user})
|> subscribe_and_join(ConversationChannel, "conversation:#{id}")

%{id: conversation_part_id} = conversation_part =
insert(:conversation_part, conversation: conversation)
ConversationChannel.broadcast_new_conversation_part(conversation_part)

assert_broadcast("new:conversation-part", %{id: ^conversation_part_id})
end
end
end
7 changes: 4 additions & 3 deletions test/support/channel_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ defmodule CodeCorpsWeb.ChannelCase do
# Import conveniences for testing with channels
use Phoenix.ChannelTest

alias CodeCorps.Repo
import Ecto
import Ecto.Changeset
import CodeCorps.Factories
import CodeCorpsWeb.ChannelCase

import Ecto.Query

alias CodeCorps.Repo

# The default endpoint for testing
@endpoint CodeCorpsWeb.Endpoint
Expand Down

0 comments on commit ab765e3

Please sign in to comment.