From 1d4fdb01713a29b852f4acc8d29b481e8c107ffd Mon Sep 17 00:00:00 2001 From: Andres Ruiz Date: Sun, 20 Oct 2024 11:37:58 -0500 Subject: [PATCH 1/3] Fix the info endpoint by serializing the `Info` struct Makes sure all structs are properly serialized in order to be returned as json. In addition, adds a test to verify the info endpoint works and fixes a couple of issues when inserting keysets in the database. --- lib/cashubrew/NUTs/NUT-02/keysets.ex | 2 +- lib/cashubrew/NUTs/NUT-04/info.ex | 1 + lib/cashubrew/NUTs/NUT-06/info.ex | 5 ++-- .../web/controllers/mint_controller.ex | 2 -- mix.exs | 5 ++++ test/nut06_test.exs | 12 ++++++++ test/support/conn_case.ex | 29 +++++++++++++++++++ 7 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 test/nut06_test.exs create mode 100644 test/support/conn_case.ex diff --git a/lib/cashubrew/NUTs/NUT-02/keysets.ex b/lib/cashubrew/NUTs/NUT-02/keysets.ex index ca3e769..d2b112a 100644 --- a/lib/cashubrew/NUTs/NUT-02/keysets.ex +++ b/lib/cashubrew/NUTs/NUT-02/keysets.ex @@ -10,7 +10,7 @@ defmodule Cashubrew.Nuts.Nut02.Keyset do end defp keyset_id_version do - <<0>> + "00" end @doc """ diff --git a/lib/cashubrew/NUTs/NUT-04/info.ex b/lib/cashubrew/NUTs/NUT-04/info.ex index b28769c..a94e9d0 100644 --- a/lib/cashubrew/NUTs/NUT-04/info.ex +++ b/lib/cashubrew/NUTs/NUT-04/info.ex @@ -4,6 +4,7 @@ defmodule Cashubrew.Nuts.Nut04.Info do """ @enforce_keys [:method, :unit] + @derive [Jason.Encoder] defstruct [:method, :unit, :min_amount, :max_amount, :description] @doc """ diff --git a/lib/cashubrew/NUTs/NUT-06/info.ex b/lib/cashubrew/NUTs/NUT-06/info.ex index 1b478d8..2958570 100644 --- a/lib/cashubrew/NUTs/NUT-06/info.ex +++ b/lib/cashubrew/NUTs/NUT-06/info.ex @@ -2,13 +2,13 @@ defmodule Cashubrew.Nuts.Nut06.Info do @moduledoc """ Implementation and structs of the NUT-06 """ - alias Cashubrew.Mint alias Cashubrew.Nuts.Nut00 alias Cashubrew.Nuts.Nut01 alias Cashubrew.Nuts.Nut02 alias Cashubrew.Nuts.Nut03 alias Cashubrew.Nuts.Nut04 + @derive [Jason.Encoder] defstruct [ :name, :pubkey, @@ -27,13 +27,14 @@ defmodule Cashubrew.Nuts.Nut06.Info do A Contact info """ @enforce_keys [:method, :info] + @derive [Jason.Encoder] defstruct [:method, :info] end def info do info = %__MODULE__{ name: "Cashubrew Cashu Mint", - pubkey: Base.encode16(Mint.get_pubkey(), case: :lower), + pubkey: Base.encode16(<<00, 01, 02, 03>>, case: :lower), version: "Cashubrew/0.1.0", description: "An Elixir implementation of Cashu Mint", description_long: nil, diff --git a/lib/cashubrew/web/controllers/mint_controller.ex b/lib/cashubrew/web/controllers/mint_controller.ex index e0e80dc..d143c98 100644 --- a/lib/cashubrew/web/controllers/mint_controller.ex +++ b/lib/cashubrew/web/controllers/mint_controller.ex @@ -11,8 +11,6 @@ defmodule Cashubrew.Web.MintController do def info(conn, _params) do info = Nut06.Info.info() json(conn, info) - rescue - e in RuntimeError -> conn |> put_status(:bad_request) |> json(Nut00.Error.new_error(0, e)) end def keysets(conn, _params) do diff --git a/mix.exs b/mix.exs index 740c42f..c6edf2e 100644 --- a/mix.exs +++ b/mix.exs @@ -6,6 +6,7 @@ defmodule Cashubrew.MixProject do app: :cashubrew, version: "0.0.1", elixir: "~> 1.14", + elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, deps: deps(), aliases: aliases(), @@ -20,6 +21,10 @@ defmodule Cashubrew.MixProject do ] end + # Specifies which paths to compile per environment. + defp elixirc_paths(:test), do: ["lib", "test/support"] + defp elixirc_paths(_), do: ["lib"] + def application do [ extra_applications: [:logger, :crypto], diff --git a/test/nut06_test.exs b/test/nut06_test.exs new file mode 100644 index 0000000..14c36e7 --- /dev/null +++ b/test/nut06_test.exs @@ -0,0 +1,12 @@ +defmodule Cashubrew.Nuts.Nut06Test do + use Cashubrew.Test.ConnCase + + test "info", %{conn: conn} do + conn = get(conn, ~p"/api/v1/info") + data = json_response(conn, 200) + + assert Map.has_key?(data, "contact") + assert Map.has_key?(data, "nuts") + assert Map.has_key?(data, "pubkey") + end +end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex new file mode 100644 index 0000000..fa94ae7 --- /dev/null +++ b/test/support/conn_case.ex @@ -0,0 +1,29 @@ +defmodule Cashubrew.Test.ConnCase do + @moduledoc """ + This module defines helpers to be used by tests that require an http connection. + It also starts a sandboxed database connection. + """ + use ExUnit.CaseTemplate + + using do + quote do + @endpoint Cashubrew.Web.Endpoint + + use Cashubrew.Web, :verified_routes + + import Plug.Conn + import Phoenix.ConnTest + import Cashubrew.Test.ConnCase + end + end + + setup tags do + Cashubrew.Test.ConnCase.setup_sandbox(tags) + {:ok, conn: Phoenix.ConnTest.build_conn()} + end + + def setup_sandbox(tags) do + pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Cashubrew.Repo, shared: not tags[:async]) + on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + end +end From 5fea61cfba9ebdf3ab69babb9d53bfaff7bd4aea Mon Sep 17 00:00:00 2001 From: Andres Ruiz Date: Sun, 20 Oct 2024 13:05:56 -0500 Subject: [PATCH 2/3] test --- test/support/conn_case.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index fa94ae7..2116985 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -25,5 +25,6 @@ defmodule Cashubrew.Test.ConnCase do def setup_sandbox(tags) do pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Cashubrew.Repo, shared: not tags[:async]) on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) + end end From f7d03ce4c6295b2d96f66bdbe01d947e65291e54 Mon Sep 17 00:00:00 2001 From: Andres Ruiz Date: Sun, 20 Oct 2024 13:08:28 -0500 Subject: [PATCH 3/3] format --- test/support/conn_case.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 2116985..fa94ae7 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -25,6 +25,5 @@ defmodule Cashubrew.Test.ConnCase do def setup_sandbox(tags) do pid = Ecto.Adapters.SQL.Sandbox.start_owner!(Cashubrew.Repo, shared: not tags[:async]) on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end) - end end