diff --git a/spec/factories/devise.rb b/spec/factories/devise.rb index cf486ba..a2188e9 100644 --- a/spec/factories/devise.rb +++ b/spec/factories/devise.rb @@ -2,8 +2,10 @@ FactoryBot.define do factory :user do id { 1 } cn { "jimmy" } + ou { "kosmos.org" } email { "jimmy@example.com" } password { "dis-muh-password" } confirmed_at { DateTime.now } + ln_account { "123456" } end end diff --git a/spec/fixtures/lndhub/incoming.json b/spec/fixtures/lndhub/incoming.json index fca7c8f..4de12ab 100644 --- a/spec/fixtures/lndhub/incoming.json +++ b/spec/fixtures/lndhub/incoming.json @@ -1,10 +1,10 @@ { "id": 58, "type": "incoming", - "user_login": "689e27b237798b41d123", - "amount": 100, + "user_login": "123456abcdef", + "amount": 12300, "fee": 0, - "memo": "Sats for jimmy@kosmos.org: \"Buy you some beers\"", + "memo": "Buy you some beers", "description_hash": "106af234beebd478206535486051b4f212bd31d2ed0f93e3efce7b5e7603d743", "payment_request": "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu", "destination_pubkey_hex": "024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946", diff --git a/spec/fixtures/lndhub/outgoing.json b/spec/fixtures/lndhub/outgoing.json new file mode 100644 index 0000000..fcd9acb --- /dev/null +++ b/spec/fixtures/lndhub/outgoing.json @@ -0,0 +1,19 @@ +{ + "id": 59, + "type": "outgoing", + "user_login": "123456abcdef", + "amount": 12400, + "fee": 10, + "memo": "Top up mobile phone", + "description_hash": "106af234beebd478206535486051b4f212bd31d2ed0f93e3efce7b5e7603d743", + "payment_request": "lnbc1u1p3mull3pp5qw4x46ew6kjknudypyjsg8maw935tr5kkuz7t6h7pugp3pt4msyqhp5zp40yd97a028sgr9x4yxq5d57gft6vwja58e8cl0eea4uasr6apscqzpgxqyz5vqsp53m2n8h6yeflgukv5fhwm802kur6un9w8nvycl7auk67w5g2u008q9qyyssqml8rfmxyvp32qd5939qx7uu0w6ppjuujlpwsrz28m9u0dzp799hz5j72w0xm8pg97hd4hdvwh9zxaw2hewnnmzewvc550f9y3qsfaegphmk0mu", + "destination_pubkey_hex": "024cd3be18617f39cf645851e3ba63f51fc13f0bb09e3bb25e6fd4de556486d946", + "r_hash": "03aa6aeb2ed5a569f1a40925041f7d7163458e96b705e5eafe0f10188575dc08", + "preimage": "3539663535656537343331663432653165396430623966633664656664646563", + "keysend": false, + "state": "settled", + "created_at": "2023-01-11T09:22:57.546364Z", + "expires_at": "2023-01-12T09:22:57.547209Z", + "updated_at": "2023-01-11T09:22:58.046236131Z", + "settled_at": "2023-01-11T09:22:58.046232174Z" +} diff --git a/spec/requests/webhooks_spec.rb b/spec/requests/webhooks_spec.rb index 30d28db..06c834e 100644 --- a/spec/requests/webhooks_spec.rb +++ b/spec/requests/webhooks_spec.rb @@ -10,11 +10,68 @@ RSpec.describe "Webhooks", type: :request do end context "IP allowed" do - it "returns a 403 status" 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, ln_account: "123456abcdef" } + let(:payload) { JSON.parse(File.read(File.expand_path("../fixtures/lndhub/incoming.json", File.dirname(__FILE__)))) } + + before do + user.save! #FIXME this should not be necessary + post "/webhooks/lndhub", params: payload.to_json + end + + it "returns a 200 status" do + expect(response).to have_http_status(:ok) + end + + it "sends an XMPP message to the account owner's JID" do + expect(enqueued_jobs.size).to eq(1) + + msg = enqueued_jobs.first['arguments'].first + expect(msg["type"]).to eq('normal') + expect(msg["from"]).to eq('kosmos.org') + expect(msg["to"]).to eq(user.address) + expect(msg["subject"]).to eq('Sats received!') + expect(msg["body"]).to match(/^12300 sats received/) + end + end end end