Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Fix the info endpoint by serializing the Info struct (#28)
Browse files Browse the repository at this point in the history
* 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.

* test

* format

---------

Co-authored-by: Andres Ruiz <[email protected]>
  • Loading branch information
kazluu and Andres Ruiz authored Oct 21, 2024
1 parent 7acde0f commit 92757b8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/cashubrew/NUTs/NUT-02/keysets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Cashubrew.Nuts.Nut02.Keyset do
end

defp keyset_id_version do
<<0>>
"00"
end

@doc """
Expand Down
1 change: 1 addition & 0 deletions lib/cashubrew/NUTs/NUT-04/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
5 changes: 3 additions & 2 deletions lib/cashubrew/NUTs/NUT-06/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions lib/cashubrew/web/controllers/mint_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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],
Expand Down
12 changes: 12 additions & 0 deletions test/nut06_test.exs
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 92757b8

Please sign in to comment.