-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added channel test, rewrote channel so it matches controller in style
- Loading branch information
Showing
3 changed files
with
66 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/lib/code_corps_web/channels/conversation_channel_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters