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

Commit

Permalink
test: test Nut02 routes (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro authored Oct 21, 2024
1 parent 7c18f20 commit fa1a7e3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 0 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ config :cashubrew, Cashubrew.Web.Endpoint,
secret_key_base: "your_secret_key_base_for_tests",
server: false

# Print only warnings and errors during test
config :logger, level: :warning

# Initialize plugs at runtime for faster test compilation
Expand Down
4 changes: 3 additions & 1 deletion lib/cashubrew/NUTs/NUT-01/serde.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Cashubrew.Nuts.Nut01.Serde.GetKeysResponse do
@moduledoc """
The body of the get keys rest response
"""
@derive Jason.Encoder
@enforce_keys [:keysets]
defstruct [:keysets]

Expand All @@ -13,7 +14,7 @@ defmodule Cashubrew.Nuts.Nut01.Serde.GetKeysResponse do
accumulator
end

defp inner_from_keysets([%{id: id, unit: unit, keys: keys}, tail], accumulator) do
defp inner_from_keysets([%{id: id, unit: unit, keys: keys} | tail], accumulator) do
inner_from_keysets(tail, [
Cashubrew.Nuts.Nut01.Serde.Keyset.from_keyset(id, unit, keys) | accumulator
])
Expand Down Expand Up @@ -57,6 +58,7 @@ defmodule Cashubrew.Nuts.Nut01.Serde.Keyset do
@moduledoc """
A keyset
"""
@derive Jason.Encoder
@enforce_keys [:id, :unit, :keys]
defstruct [:id, :unit, :keys]

Expand Down
3 changes: 0 additions & 3 deletions lib/cashubrew/NUTs/NUT-02/keysets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,3 @@ defmodule Cashubrew.Nuts.Nut02.Keyset do
(:crypto.hash(:sha256, pubkeys_concat) |> Base.encode16(case: :lower) |> binary_part(0, 14))
end
end

# TODO: add logic for wallet to handle fees
# https://cashubtc.github.io/nuts/02/#fees
35 changes: 35 additions & 0 deletions test/nut01_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Cashubrew.Nuts.Nut01Test do
use Cashubrew.Test.ConnCase
alias Cashubrew.Nuts.Nut01

test "active_keysets", %{conn: conn} do
conn = get(conn, ~p"/api/v1/keys")
data = json_response(conn, 200)

keysets = Map.fetch!(data, "keysets")
assert length(keysets) == 1
[keyset | _] = keysets

id = Map.fetch!(keyset, "id")
unit = Map.fetch!(keyset, "unit")
keys = Map.fetch!(keyset, "keys")

assert unit == "sat"
assert is_bitstring(id)
assert is_map(keys)
assert Map.has_key?(keys, "1")

Enum.each(keys, fn {k, v} ->
# The keys are 1, 2, 4, 8, ...
{amount, ""} = Integer.parse(k, 10)

if amount != 1 do
assert rem(amount, 2) == 0
assert Map.has_key?(keys, "#{div(amount, 2)}")
end

# The values are valid hex numbers
{_, ""} = Integer.parse(v, 16)
end)
end
end

0 comments on commit fa1a7e3

Please sign in to comment.