O'Reilly logo

Programming Phoenix by Jose Valim, Bruce Tate, Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required