198 lines
7.4 KiB
Ruby
198 lines
7.4 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe "Webhooks", type: :request do
|
|
describe "Allowed IP addresses" do
|
|
context "IP not allowed" do
|
|
it "returns a 403 status" do
|
|
post "/webhooks/lndhub"
|
|
expect(response).to have_http_status(:forbidden)
|
|
end
|
|
end
|
|
|
|
context "IP allowed" do
|
|
before do
|
|
ENV['WEBHOOKS_ALLOWED_IPS'] = '127.0.0.1'
|
|
end
|
|
|
|
it "does not return a 403 status" do
|
|
post "/webhooks/lndhub"
|
|
expect(response).not_to have_http_status(:forbidden)
|
|
end
|
|
end
|
|
end
|
|
|
|
# Webhooks from lndhub.go
|
|
describe "/webhooks/lndhub" do
|
|
before do
|
|
ENV['WEBHOOKS_ALLOWED_IPS'] = '127.0.0.1'
|
|
end
|
|
|
|
describe "Payload cannot be processed as JSON" do
|
|
before do
|
|
post "/webhooks/lndhub", params: "Foo"
|
|
end
|
|
|
|
it "returns a 422 status" do
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
end
|
|
end
|
|
|
|
describe "Valid payload for outgoing payment" do
|
|
let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/outgoing.json", File.dirname(__FILE__)))) }
|
|
|
|
before do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
end
|
|
|
|
it "returns a 204 status" do
|
|
expect(response).to have_http_status(:no_content)
|
|
end
|
|
end
|
|
|
|
describe "Valid payload for incoming payment" do
|
|
let(:user) { create :user, lndhub_username: "123456abcdef" }
|
|
let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/incoming.json", File.dirname(__FILE__)))) }
|
|
|
|
before { user.save! } #FIXME this should not be necessary
|
|
|
|
it "returns a 200 status" do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
expect(response).to have_http_status(:ok)
|
|
end
|
|
|
|
it "does not send a zap receipt" do
|
|
expect(NostrManager::PublishZapReceipt).not_to receive(:call)
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
end
|
|
|
|
context "notification preference set to 'xmpp'" do
|
|
before do
|
|
Setting.xmpp_notifications_from_address = "botka@kosmos.org"
|
|
user.update! preferences: { lightning_notify_sats_received: "xmpp" }
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
end
|
|
|
|
it "sends an XMPP message to the account owner's JID" do
|
|
expect(enqueued_jobs.size).to eq(1)
|
|
expect(enqueued_jobs.first["job_class"]).to eq("XmppSendMessageJob")
|
|
|
|
msg = enqueued_jobs.first["arguments"].first
|
|
expect(msg["type"]).to eq("normal")
|
|
expect(msg["from"]).to eq("botka@kosmos.org")
|
|
expect(msg["to"]).to eq(user.address)
|
|
expect(msg["subject"]).to eq("Sats received!")
|
|
expect(msg["body"]).to match(/^12,300 sats received/)
|
|
end
|
|
end
|
|
|
|
context "notification preference set to 'email'" do
|
|
before do
|
|
user.update! preferences: { lightning_notify_sats_received: "email" }
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
end
|
|
|
|
it "sends an email notification to the account owner" do
|
|
expect(enqueued_jobs.size).to eq(1)
|
|
expect(enqueued_jobs.first["job_class"]).to eq("ActionMailer::MailDeliveryJob")
|
|
args = enqueued_jobs.first['arguments']
|
|
expect(args[0]).to eq("NotificationMailer")
|
|
expect(args[1]).to eq("lightning_sats_received")
|
|
expect(args[3]["params"]["user"]["_aj_globalid"]).to eq("gid://akkounts/User/1")
|
|
expect(args[3]["params"]["amount_sats"]).to eq(12300)
|
|
end
|
|
end
|
|
|
|
describe "minimum threshold amount not reached" do
|
|
before do
|
|
user.update! preferences: {
|
|
lightning_notify_sats_received: "xmpp",
|
|
lightning_notify_min_sats: 21000
|
|
}
|
|
end
|
|
|
|
it "does not send a notification" do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
expect(enqueued_jobs.size).to eq(0)
|
|
end
|
|
end
|
|
|
|
describe "no memo/description/message" do
|
|
before do
|
|
user.update! preferences: {
|
|
lightning_notify_sats_received: "xmpp",
|
|
lightning_notify_only_with_message: true
|
|
}
|
|
end
|
|
|
|
it "does not send a notification" do
|
|
post "/webhooks/lndhub", params: payload.merge({ memo: "" }).to_json
|
|
expect(enqueued_jobs.size).to eq(0)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "Valid payload for zap transaction" do
|
|
let(:user) { create :user, lndhub_username: "123456abcdef" }
|
|
let(:zap) { create :zap, user: user }
|
|
let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/incoming-zap.json", File.dirname(__FILE__)))) }
|
|
let(:zap_receipt) {
|
|
Nostr::Event.new(
|
|
id: "cb66278a9add37a2f1e018826327ff15304e8055ff7b910100225baf83a9d691",
|
|
sig: "a808d6792e21824bfddc98742b6831b1070e8b21e12aa424d2bb168a09f3a95a217d4513e803f2acb6e38404f763eb09fa07a341ee9c8c4c7d18bbe3d381eb6f",
|
|
pubkey: "bdd76ce2934b2f591f9fad2ebe9da18f20d2921de527494ba00eeaa0a0efadcf",
|
|
created_at: 1673428978,
|
|
kind: 9735,
|
|
tags: [
|
|
["p", "07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3"],
|
|
["bolt11", "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu"],
|
|
["preimage", "3539663535656537343331663432653165396430623966633664656664646563"],
|
|
["description", "{\"id\":\"3cf02d7f0ccd9711c25098fc50b3a7ab880326e4e51cc8c7a7b59f147cff4fff\",\"sig\":\"e9e9bb2bac4267a107ab5c3368f504b4f11b8e3b5ae875a1d63c74d6934138d2521dc35815b6f534fc5d803cbf633736d871886368bb8f92c4ad3837a68a06f2\",\"pubkey\":\"730b43e6f62c2ab22710b046e481802c8ac1108ed2cb9c21dff808d57ba24b6c\",\"created_at\":1712487443,\"kind\":9734,\"tags\":[[\"relays\",\"wss://nostr.kosmos.org\",\"wss://relay.example.com\"],[\"p\",\"07e188a1ff87ce171d517b8ed2bb7a31b1d3453a0db3b15379ec07b724d232f3\"]],\"content\":\"\"}"]
|
|
],
|
|
content: ""
|
|
)
|
|
}
|
|
|
|
before do
|
|
user.save!
|
|
zap.save!
|
|
|
|
allow(NostrManager::CreateZapReceipt).to receive(:call)
|
|
.and_return(zap_receipt)
|
|
allow(NostrManager::PublishZapReceipt).to receive(:call)
|
|
.and_return(true)
|
|
end
|
|
|
|
it "returns a 200 status" do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
expect(response).to have_http_status(:ok)
|
|
end
|
|
|
|
it "adds the settlement date/time to the zap record" do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
expect(user.zaps.first.settled_at.to_i).to eq(1673428978)
|
|
end
|
|
|
|
it "creates and adds a zap receipt to the zap record" do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
expect(user.zaps.first.receipt).not_to be_nil
|
|
end
|
|
|
|
it "publishes the zap receipt" do
|
|
expect(NostrManager::PublishZapReceipt).to receive(:call).with(zap: zap)
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
end
|
|
|
|
context "with notifications disabled for zaps" do
|
|
before do
|
|
user.update! preferences: { lightning_notify_zap_received: "disabled" }
|
|
end
|
|
|
|
it "does not send a notification" do
|
|
post "/webhooks/lndhub", params: payload.to_json
|
|
expect(enqueued_jobs.size).to eq(0)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|