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

Commit

Permalink
test: add rust integration tests using cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro committed Oct 24, 2024
1 parent 7e876c3 commit 09fe884
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 18 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ jobs:

- name: Check for security vulnerabilities
run: mix sobelow --config

- name: Integration tests
uses: Swatinem/rust-cache@v2
with:
shared-key: "cache"
save-if: false
run: |-
mix phx.server &
CASHUBREW_SERVER_PID=$!
cd integration-tests && cargo test
kill $CASHUBREW_SERVER_PID
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ priv/static/

benchmarks/output

.env
.env

# Rust related
debug/
target/
Cargo.lock
*.pdb
17 changes: 17 additions & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "integration-tests"
version = "0.1.0"
edition = "2021"

[dependencies]
cdk = { git = "https://github.com/cashubtc/cdk", tag = "v0.4.0" }
rand = "0.8.5"
assert_matches = "1.5.0"
tokio = "1.41.0"
url = "2.5.2"
secp256k1 = {version = "0.30.0", features = ["rand", "hashes", "global-context"]}

[[test]]
name = "nut06"
path = "nut06.rs"

72 changes: 72 additions & 0 deletions integration-tests/nut06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
extern crate integration_tests;

use assert_matches::assert_matches;
use integration_tests::init_wallet;

use cdk::nuts::{
ContactInfo, CurrencyUnit, MintInfo, MintMethodSettings, MintVersion, NUT04Settings, Nuts,
PaymentMethod, PublicKey,
};

#[tokio::test]
pub async fn info() {
const URL: &str = "http://localhost:4000";
let wallet = init_wallet(URL, CurrencyUnit::Sat).unwrap();
let mint_info = wallet.get_mint_info().await.unwrap().unwrap();

let expected_nuts = Nuts::new()
.nut04(NUT04Settings::new(
[MintMethodSettings {
method: PaymentMethod::Bolt11,
unit: CurrencyUnit::Sat,
min_amount: None,
max_amount: None,
}]
.to_vec(),
false,
))
.nut05(cdk::nuts::NUT05Settings {
methods: [].to_vec(),
disabled: true,
});
println!("{:#?}", expected_nuts);
let expected_name = "Cashubrew Cashu Mint";
let expected_pubkey =
PublicKey::from_hex("0381094f72790bb014504dfc9213bd3c8450440f5d220560075dbf2f8113e9fa3e")
.unwrap();
let expected_version = MintVersion::new("Cashubrew".to_string(), "0.1.0".to_string());
let expected_description = "An Elixir implementation of Cashu Mint";
let expected_contact = [
ContactInfo::new("twitter".to_string(), "@dimahledba".to_string()),
ContactInfo::new(
"nostr".to_string(),
"npub1hr6v96g0phtxwys4x0tm3khawuuykz6s28uzwtj5j0zc7lunu99snw2e29".to_string(),
),
];
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();

assert_matches!(
mint_info,
MintInfo {
name: Some(name) ,
pubkey: Some(pubkey),
version: Some(version),
description: Some(description),
description_long: None,
contact: Some(contact),
nuts,
icon_url: None,
motd: None,
time: Some(time)
} if name == expected_name
&& pubkey == expected_pubkey
&& version == expected_version
&& description == expected_description
&& contact == expected_contact
&& nuts == expected_nuts
&& time <= now
);
}
4 changes: 4 additions & 0 deletions integration-tests/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.82.0"
components = ["rustfmt", "clippy", "rust-analyzer"]
profile = "minimal"
12 changes: 12 additions & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use cdk::cdk_database::WalletMemoryDatabase;
use cdk::nuts::CurrencyUnit;
use cdk::wallet::Wallet;
use rand::Rng;
use std::sync::Arc;

pub fn init_wallet(mint_url: &str, unit: CurrencyUnit) -> Result<Wallet, cdk::Error> {
let seed = rand::thread_rng().gen::<[u8; 32]>();

let localstore = WalletMemoryDatabase::default();
Wallet::new(mint_url, unit, Arc::new(localstore), &seed, None)
}
2 changes: 1 addition & 1 deletion lib/cashubrew/NUTs/NUT-04/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Cashubrew.Nuts.Nut04.Info do
%{
methods: [
%__MODULE__{
method: "bolt111",
method: "bolt11",
unit: "sat"
}
],
Expand Down
13 changes: 11 additions & 2 deletions lib/cashubrew/NUTs/NUT-06/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Cashubrew.Nuts.Nut06.Info do
def info do
info = %__MODULE__{
name: "Cashubrew Cashu Mint",
pubkey: Base.encode16(<<00, 01, 02, 03>>, case: :lower),
pubkey: "0381094f72790bb014504dfc9213bd3c8450440f5d220560075dbf2f8113e9fa3e",
version: "Cashubrew/0.1.0",
description: "An Elixir implementation of Cashu Mint",
description_long: nil,
Expand All @@ -46,7 +46,16 @@ defmodule Cashubrew.Nuts.Nut06.Info do
],
time: System.os_time(:second),
nuts: %{
"4": Nut04.Info.info()
"4": Nut04.Info.info(),
"5": %{methods: [], disabled: true},
"7": %{supported: false},
"8": %{supported: false},
"9": %{supported: false},
"10": %{supported: false},
"11": %{supported: false},
"12": %{supported: false},
"14": %{supported: false},
"15": %{methods: []},
},
motd: nil
}
Expand Down
1 change: 1 addition & 0 deletions lib/cashubrew/core/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Cashubrew.Mint do
"""

use GenServer
require Logger
alias Cashubrew.Nuts.Nut00.{BDHKE, BlindSignature}
alias Cashubrew.Nuts.Nut02

Expand Down
8 changes: 6 additions & 2 deletions lib/cashubrew/web/controllers/mint_controller.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Cashubrew.Web.MintController do
use Cashubrew.Web, :controller
require :logger
require Logger
alias Cashubrew.Mint
alias Cashubrew.Nuts.Nut00
alias Cashubrew.Nuts.Nut01
Expand All @@ -9,8 +11,10 @@ defmodule Cashubrew.Web.MintController do
alias Cashubrew.Nuts.Nut06

def info(conn, _params) do
info = Nut06.Info.info()
json(conn, info)
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
3 changes: 3 additions & 0 deletions lib/cashubrew/web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ defmodule Cashubrew.Web.Router do
post("/v1/melt/quote/bolt11", MintController, :melt_quote)
post("/v1/melt/bolt11", MintController, :melt_tokens)

end

scope "/", Cashubrew.Web do
# NUT-06
get("/v1/info", MintController, :info)
end
Expand Down
12 changes: 0 additions & 12 deletions test/nut06_test.exs

This file was deleted.

0 comments on commit 09fe884

Please sign in to comment.