Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. defmodule PhoenixApi.AuthenticateUserTest do
  2. use PhoenixApi.ConnCase
  3.  
  4. alias PhoenixApi.{AuthenticateUser, Repo, Session, User}
  5.  
  6. @opts Authenticate.init([])
  7.  
  8. def add_token_to_header(conn, token) do
  9. conn
  10. |> put_req_header("authorization", "Token token=\"#{token}\"")
  11. end
  12.  
  13. test "should return user when token is valid", %{conn: conn} do
  14. user = Repo.insert! %User{ email: "cristiano.codelab@gmail.com", name: "Cristiano", password: "qwe@rty#$&" }
  15. session = Repo.insert!(%Session{token: "1234", user_id: user.id})
  16.  
  17. conn = conn
  18. |> add_token_to_header(session.token)
  19. |> AuthenticateUser.call(@opts)
  20.  
  21. assert conn.assigns.current_user
  22. end
  23.  
  24. test "should return 401 error when token is invalid", %{conn: conn} do
  25. conn = conn
  26. |> add_token_to_header("foo")
  27. |> AuthenticateUser.call(@opts)
  28.  
  29. assert conn.status == 401
  30. assert conn.halted
  31. end
  32.  
  33. test "should return 401 error when no token is provided", %{conn: conn} do
  34. conn = AuthenticateUser.call(conn, @opts)
  35. assert conn.status == 401
  36. assert conn.halted
  37. end
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement