Hi, during creating my own custom Pow auth I stuck during testing. I made a research on web but couldnt find solution.
For learning purpose I show token in json. I need it to paste in postman during manual testing (i dont know yet how to use postman with session/cookies auth )
@spec reset_password(Conn.t(), map()) :: Conn.t() def reset_password(conn, %{"id" => token, "user" => user_params}) do with {:ok, conn} <- PowResetPassword.Plug.load_user_by_token(conn, token), {:ok, _user, conn} <- PowResetPassword.Plug.update_user_password(conn, user_params) do json(conn, %{status: "Password changed"}) else {:error, _changeset, conn} -> json(conn, %{error: %{message: "Passwords are not the same"}}) _ -> json(conn, %{error: %{message: "Expired Token"}}) end end
Function works well, shows expected errors while providing invalid params or token. I wanted to write tests for it and then I stuck. I cant go through them and my every try is failed.
I setup up user before tests:
setup do user = %User{} |> User.changeset(%{email: "test@example.com", password: @password, password_confirmation: @password}) |> Repo.insert!() {:ok, user: user} end
First attemp:
@valid_params %{"id" => "token", "user" => %{"password" => @new_password, "password_confirmation" => @new_password}} describe "reset_password/2" do test "with valid token and passwords", %{conn: conn} do conn = post(conn, Routes.password_path(conn, :reset_password, @valid_params)) assert json = json_response(conn, 200) assert json["status"] end end
Error in terminal:
Expected truthy, got nil code: assert json["status"] arguments: # 1 %{"error" => %{"message" => "Expired Token"}} # 2 "status"
I suppose that it needs real token, not any hard coded pretending to be real.
Second attemp:
use PowApiTemplateWeb.ConnCase alias Plug.Conn alias PowApiTemplate.{Repo, Users.User} alias PowResetPassword.Plug alias Pow.Plug, as: PowPlug describe "reset_password/2" do setup %{conn: conn} do token = PowResetPassword.Plug.create_reset_token(conn, "test@example.com") {:ok, conn: conn, token: token} end test "with valid token and passwords", %{conn: conn} do conn = post(conn, Routes.password_path(conn, :reset_password, @valid_params)) assert json = json_response(conn, 200) assert json["status"] end end
It seems to be working. I use real conn, use function providing token, but there appears an error i couldn’t resolve even if I checked for solutions, check if I write in config everything like it is written in docs:
(Pow.Config.ConfigError) Pow configuration not found in connection. Please use a Pow plug that puts the Pow configuration in the plug connection.
I tried also different way of writing this func but Pow config errors still appears. I dont think my function in untestable, but there are issues I can resolve after digging in docs:
describe "reset_password/2" do test "with valid token and passwords", %{conn: conn} do token = PowResetPassword.Plug.create_reset_token(conn, "test@example.com") conn = post(conn, Routes.password_path(conn, :reset_password, %{"id" => token, "user" => %{"password" => @new_password, "password_confirmation" => @new_password}})) assert json = json_response(conn, 200) assert json["status"] end end
Thanks for any help