Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule PhoenixApi.AuthenticateUserTest do
- use PhoenixApi.ConnCase
- alias PhoenixApi.{AuthenticateUser, Repo, Session, User}
- @opts Authenticate.init([])
- def add_token_to_header(conn, token) do
- conn
- |> put_req_header("authorization", "Token token=\"#{token}\"")
- end
- test "should return user when token is valid", %{conn: conn} do
- user = Repo.insert! %User{ email: "cristiano.codelab@gmail.com", name: "Cristiano", password: "qwe@rty#$&" }
- session = Repo.insert!(%Session{token: "1234", user_id: user.id})
- conn = conn
- |> add_token_to_header(session.token)
- |> AuthenticateUser.call(@opts)
- assert conn.assigns.current_user
- end
- test "should return 401 error when token is invalid", %{conn: conn} do
- conn = conn
- |> add_token_to_header("foo")
- |> AuthenticateUser.call(@opts)
- assert conn.status == 401
- assert conn.halted
- end
- test "should return 401 error when no token is provided", %{conn: conn} do
- conn = AuthenticateUser.call(conn, @opts)
- assert conn.status == 401
- assert conn.halted
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement