session_controller_test.exs (1647B)
1 defmodule UsersWeb.SessionControllerTest do 2 use UsersWeb.ConnCase, async: true 3 @moduletag :capture_log 4 5 test "POST /login relays a successful login", %{conn: conn} do 6 Req.Test.stub(Users.Auth, fn auth_conn -> 7 {:ok, body, auth_conn} = Plug.Conn.read_body(auth_conn) 8 assert auth_conn.request_path == "/login" 9 assert Jason.decode!(body) == %{"Username" => "ana", "Password" => "segredo"} 10 Req.Test.json(auth_conn, %{"Token" => "8b47881d-650f-4ab1-a38a-bbf3e19ac0d2"}) 11 end) 12 13 conn = post(conn, ~p"/login", %{name: "ana", password: "segredo"}) 14 assert json_response(conn, 200) == %{"Token" => "8b47881d-650f-4ab1-a38a-bbf3e19ac0d2"} 15 end 16 17 test "POST /login relays a failed login unchanged", %{conn: conn} do 18 Req.Test.stub(Users.Auth, fn auth_conn -> 19 auth_conn 20 |> Plug.Conn.put_resp_content_type("text/plain") 21 |> Plug.Conn.send_resp(401, "invalid username or password\n") 22 end) 23 24 conn = post(conn, ~p"/login", %{name: "ana", password: "errada"}) 25 assert response(conn, 401) == "invalid username or password\n" 26 assert response_content_type(conn, :text) 27 end 28 29 test "POST /login without credentials answers 400", %{conn: conn} do 30 conn = post(conn, ~p"/login", %{name: "ana"}) 31 32 assert json_response(conn, 400) == %{ 33 "errors" => %{"detail" => "name and password are required"} 34 } 35 end 36 37 test "POST /login answers 503 when auth is down", %{conn: conn} do 38 Req.Test.stub(Users.Auth, &Req.Test.transport_error(&1, :econnrefused)) 39 conn = post(conn, ~p"/login", %{name: "ana", password: "a"}) 40 assert json_response(conn, 503) 41 end 42 end