Programming Phoenix - Chapter 8: auth_test.exs "match (=) failed"

Greetings,

I am currently at chapter 8 in Programming Phoenix (and I love it so far), but I’ve encountered this strange error while testing login with a password mismatch.

match (=) failed
 code: {:error, :unauthorized, _conn} = Auth.login_by_username_and_pass(conn, "mr_dummy", "wrongsupersecret", repo: Repo)
 rhs:  [:error, :unauthorized,
        %Plug.Conn{adapter: {Plug.Adapters.Test.Conn, :...},
         assigns: %{current_user: nil},
         before_send: [#Function<0.7834419/1 in Plug.CSRFProtection.call/2>,
          #Function<4.9996867/1 in Phoenix.Controller.fetch_flash/2>,
          #Function<0.46198565/1 in Plug.Session.before_send/2>,
          #Function<1.6619757/1 in Plug.Logger.call/2>], body_params: %{},
         cookies: %{}, halted: false, host: "www.example.com",
         method: "GET", owner: #PID<0.357.0>, params: %{}, path_info: [],
         path_params: %{}, peer: {{127, 0, 0, 1}, 111317}, port: 80,
         private: %{phoenix_bypass: {Rumbl.Router, [:browser]},
           phoenix_endpoint: Rumbl.Endpoint, phoenix_flash: %{},
           phoenix_format: "html", phoenix_recycled: false,
           plug_session: %{}, plug_session_fetch: :done,
           plug_skip_csrf_protection: true}, query_params: %{},
         query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{},
         req_headers: [], request_path: "/", resp_body: nil,
         resp_cookies: %{},
         resp_headers: [{"cache-control",
           "max-age=0, private, must-revalidate"},
          {"x-request-id", "dfj104o1dkskkciudge6jcnacsn63c4j"},
          {"x-frame-options", "SAMEORIGIN"},
          {"x-xss-protection", "1; mode=block"},
          {"x-content-type-options", "nosniff"}], scheme: :http,
         script_name: [],
         secret_key_base: "ydqANDmxM7RxR2Bm1pgrbi6YqpnaHwDZFU/UbEXSoUo5L22a91LzVUN42QaOD855",
         state: :unset, status: nil}]
 stacktrace:
   test/controllers/auth_test.exs:73: (test)

I tried searching on Google but I was not able to find any results.

Here is the test code

 test "login with password mismatch", %{conn: conn} do
   _ = insert_user(username: "mr_dummy", password: "supersecret")
   assert {:error, :unauthorized, _conn} =
     Auth.login_by_username_and_pass(conn, "mr_dummy", "wrongsupersecret", repo: Repo)
 end

And here is the function that is being tested

  def login_by_username_and_pass(conn, username, given_pass, opts) do
    repo = Keyword.fetch!(opts, :repo)
    user = repo.get_by(Rumbl.User, username: username)
    cond do
      user && checkpw(given_pass, user.password_hash) ->
        {:ok, login(conn, user)}
      user ->
        [:error, :unauthorized, conn]
      true ->
        dummy_checkpw()
        {:error, :not_found, conn}
     end
   end

It looks like you expect a tuple but return a list.

2 Likes

Yup, seems like a typo there (inferring from the other return values). Need to change it to a tuple:

{:error, :unauthorized, conn}
3 Likes

Indeed, you were both right.
I can’t understand how I didn’t see that typo.

Thank you so much for your help!

1 Like