From e580cc9991ddfd5826323e295dc1421cec2c5786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Thu, 23 Feb 2023 17:46:36 +0800 Subject: [PATCH] Add specs for Lightning Address and lnurlpay requests --- spec/requests/lnurlpay_spec.rb | 110 +++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 spec/requests/lnurlpay_spec.rb diff --git a/spec/requests/lnurlpay_spec.rb b/spec/requests/lnurlpay_spec.rb new file mode 100644 index 0000000..33ac543 --- /dev/null +++ b/spec/requests/lnurlpay_spec.rb @@ -0,0 +1,110 @@ +require 'rails_helper' + +RSpec.describe "/lnurlpay", type: :request do + + context "Non-existent user/address" do + describe "GET /lnurlpay/:address" do + it "returns a 404" do + get lightning_address_path(address: "csw@kosmos.org") + expect(response).to have_http_status(:not_found) + end + end + + describe "GET /lnurlpay/:address/invoice" do + it "returns a 404" do + get lnurlpay_invoice_path(address: "csw@kosmos.org", params: { amount: 5000 }) + expect(response).to have_http_status(:not_found) + end + end + + describe "GET /keysend/:address/" do + it "returns a 404" do + get lightning_address_keysend_path(address: "csw@kosmos.org") + expect(response).to have_http_status(:not_found) + end + end + end + + context "Valid user/address" do + let(:user) { create :user, cn: 'satoshi', ou: 'kosmos.org', ln_account: 'abcdefg123456' } + + before do + login_as user, :scope => :user + end + + describe "GET /lnurlpay/:address" do + it "returns a formatted Lightning Address response" do + get lightning_address_path(address: "satoshi@kosmos.org") + + expect(response).to have_http_status(:ok) + + res = JSON.parse(response.body) + expect(res["status"]).to eq('OK') + expect(res["tag"]).to eq('payRequest') + expect(res["callback"]).to match(lnurlpay_invoice_path('satoshi@kosmos.org')) + expect(res["minSendable"]).to be_a(Integer) + expect(res["maxSendable"]).to be_a(Integer) + expect(res["commentAllowed"]).to be_a(Integer) + end + end + + describe "GET /lnurlpay/:address/invoice" do + before do + allow_any_instance_of(User).to receive(:ln_create_invoice).and_return("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") + end + + it "returns a formatted lnurlpay response" do + get lnurlpay_invoice_path(address: "satoshi@kosmos.org", params: { + amount: 50000, comment: "Coffee time!" + }) + + expect(response).to have_http_status(:ok) + + res = JSON.parse(response.body) + expect(res["status"]).to eq('OK') + expect(res["successAction"]["tag"]).to eq('message') + expect(res["successAction"]["message"]).to match('Thank you') + expect(res["pr"]).to eq("lnbc50u1p3lwgknpp52g78gqya5euvzjc53fc6hkmlm2rfjhcd305tcmc0g9gaestav48sdq4gdhkven9v5sx6mmwv4ujzcqzpgxqyz5vqsp5skkz4jlqr6tkvv2g9739ygrjupc4rkqd94mc7dfpj3pgx3f6w7qs9qyyssq7mf3fzcuxlmkr9nqatcch3u8uf4gjyawe052tejz8e9fqxu4pncqk3qklt8g6ylpshg09xyjquyrgtc72vcw5cp0dzcf406apyua7dgpnfn7an") + end + + context "amount too low" do + it "retuns an error" do + get lnurlpay_invoice_path(address: "satoshi@kosmos.org", params: { + amount: 5000, comment: "Coffee time!" + }) + expect(response).to have_http_status(:ok) + res = JSON.parse(response.body) + expect(res["status"]).to eq('ERROR') + expect(res["reason"]).to eq('Invalid amount') + end + end + + context "comment too long" do + it "retuns an error" do + get lnurlpay_invoice_path(address: "satoshi@kosmos.org", params: { + amount: 5000000, comment: "Coffee time is the best time, so here's some money for you to get some. May I suggest to sample some Pacamara beans from El Salvador?" + }) + expect(response).to have_http_status(:ok) + res = JSON.parse(response.body) + expect(res["status"]).to eq('ERROR') + expect(res["reason"]).to eq('Comment too long') + end + end + end + + describe "GET /keysend/:address/" do + it "returns a formatted Lightning Address keysend response" do + get lightning_address_keysend_path(address: "satoshi@kosmos.org") + + expect(response).to have_http_status(:ok) + + res = JSON.parse(response.body) + expect(res["status"]).to eq('OK') + expect(res["tag"]).to eq('keysend') + expect(res["pubkey"]).to eq(Setting.lndhub_public_key) + expect(res["customData"][0]["customKey"]).to eq('696969') + expect(res["customData"][0]["customValue"]).to eq('abcdefg123456') + end + end + end +end