mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Add socket authentication tests
This commit is contained in:
parent
95b565231a
commit
b500ac5068
@ -29,6 +29,7 @@ defmodule ChessWeb.UserSocket do
|
||||
:error
|
||||
end
|
||||
end
|
||||
def connect(%{}, socket), do: :error
|
||||
|
||||
# Socket id's are topics that allow you to identify all sockets for a given user:
|
||||
#
|
||||
|
||||
20
test/chess_web/channels/user_socket_test.exs
Normal file
20
test/chess_web/channels/user_socket_test.exs
Normal file
@ -0,0 +1,20 @@
|
||||
defmodule ChessWeb.UserSocketTest do
|
||||
use ChessWeb.ChannelCase, async: true
|
||||
|
||||
alias ChessWeb.UserSocket
|
||||
|
||||
test "authenticate with valid token" do
|
||||
token = Phoenix.Token.sign(@endpoint, "game socket", 42)
|
||||
|
||||
assert {:ok, socket} = connect(UserSocket, %{"token" => token})
|
||||
assert socket.assigns.current_user_id == 42
|
||||
end
|
||||
|
||||
test "cannot authenticate with invalid token" do
|
||||
assert :error = connect(UserSocket, %{"token" => "invalid-token"})
|
||||
end
|
||||
|
||||
test "cannot authenticate with no token" do
|
||||
assert :error = connect(UserSocket, %{})
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user