Mateusz Babski
Mateusz Babski

Reputation: 11

Elixir / Pow Auth / ETS Cache - test raises error even if function works perfect

I created custom reset_password controller which works well during manual testing. Then I decided to write tests. Then problems started to appear. I read that I should mock cache and according to documentation I did step by step that configuration, but problems are not gone.

Here is my function:

@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

And here is my reset_password test:

 setup do
    user =
      %User{}
        |> User.changeset(%{email: "[email protected]", password: @password, password_confirmation: @password})
        |> Repo.insert!()

    {:ok, user: user}
  end

describe "reset_password/2" do

    test "with valid token and passwords", %{conn: conn} do
      PowApiTemplate.Test.EtsCacheMock.init()
      pow_config = [otp_app: :pow_api_template]

      {:ok, %{token: token, user: user}, conn} =
        conn
        |> Pow.Plug.put_config(Application.get_env(:pow_api_template, :pow))
        |> PowResetPassword.Plug.create_reset_token(%{"email" => "[email protected]"})

      valid_params = %{"id" => token, "user" => %{"password" => @new_password, "password_confirmation" => @new_password}}

      conn = post(conn, Routes.password_path(conn, :reset_password, valid_params))

      assert json = json_response(conn, 200)
    end
  end

Trying to test it I got error (I also tried to put some part into setup but the same error appears or it crashes all my tests):

1) test reset_password/2 with valid token and passwords (PowApiTemplateWeb.PasswordControllerTest)
    test/pow_api_template_web/controllers/password_controller_test.exs:45
    ** (ArgumentError) errors were found at the given arguments:
    
      * 2nd argument: invalid options
    
    code: PowApiTemplate.Test.EtsCacheMock.init()
    stacktrace:
      (stdlib 4.0.1) :ets.new(PowApiTemplate.Test.EtsCacheMock, [:set, :protected, :named_table])
      (pow_api_template 0.1.0) test/support/ets_cache_mock.ex:5: PowApiTemplate.Test.EtsCacheMock.init/0
      test/pow_api_template_web/controllers/password_controller_test.exs:46: (test)

Now I am stuck. Digging through internet I cant find solution to this case. It seems that problem is with init() function, but everytime someone's occur that problem solutions were different.

There are steps I did according to posts from Pow Auth creator in other issues

I updated conn_case.ex with:

  setup _tags do
    EtsCacheMock.init()

    {:ok, conn: Phoenix.ConnTest.build_conn(), ets: EtsCacheMock}
  end

Updated test config with:

config :pow_api_template, :pow,
  cache_backend: [cache_store_backend: PowApiTemplate.Test.EtsCacheMock]

Error is the same if I change it to:

config :pow_api_template, :pow,
  cache_store_backend: PowApiTemplate.Test.EtsCacheMock

And main module

I added to test folder Ets Cache Mock:

defmodule PowApiTemplate.Test.EtsCacheMock do
  @moduledoc false
  @tab __MODULE__

  def init, do: :ets.new(@tab, [:ordered_set, :protected, :named_table])

  def get(config, key) do
    ets_key = ets_key(config, key)

    @tab
    |> :ets.lookup(ets_key)
    |> case do
      [{^ets_key, value} | _rest] -> value
      []                          -> :not_found
    end
  end

  def delete(config, key) do
    :ets.delete(@tab, ets_key(config, key))

    :ok
  end

  def put(config, record_or_records) do
    records     = List.wrap(record_or_records)
    ets_records = Enum.map(records, fn {key, value} ->
      {ets_key(config, key), value}
    end)

    send(self(), {:ets, :put, records, config})
    :ets.insert(@tab, ets_records)
  end

  def all(config, match) do
    ets_key_match = ets_key(config, match)

    @tab
    |> :ets.select([{{ets_key_match, :_}, [], [:"$_"]}])
    |> Enum.map(fn {[_namespace | keys], value} -> {keys, value} end)
  end

  defp ets_key(config, key) do
    [Keyword.get(config, :namespace, "cache")] ++ List.wrap(key)
  end
end

I dont know for now where the problems is and how to solve it. I've looking for solution for many hours two days straight and still nothing :)

Thanks for any help

Upvotes: 1

Views: 134

Answers (0)

Related Questions