Authenticating a Test Socket

Most of our channels code relies on an authenticated user. We’ll start our tests with the socket authentication. Let’s do that now.

Create a rumbrella/apps/rumbl/test/channels/user_socket_test.exs file containing:

1: defmodule​ Rumbl.Channels.UserSocketTest ​do
use​ Rumbl.ChannelCase, ​async:​ true
alias Rumbl.UserSocket
5:  test ​"​​socket authentication with valid token"​ ​do
token = Phoenix.Token.sign(@endpoint, ​"​​user socket"​, ​"​​123"​)
assert {​:ok​, socket} = connect(UserSocket, %{​"​​token"​ => token})
assert socket.assigns.user_id == ​"​​123"
10: end
test ​"​​socket authentication with invalid ...

Get Programming Phoenix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.