Merge pull request 'Add user preferences and configurable notifications' (#113) from feature/user_preferences into master
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #113
Reviewed-by: galfert <garret.alfert@gmail.com>
This commit was merged in pull request #113.
This commit is contained in:
2023-04-11 21:04:46 +00:00
33 changed files with 384 additions and 136 deletions

View File

@@ -0,0 +1,41 @@
require 'rails_helper'
RSpec.describe UserPreferences, type: :model do
let(:default_prefs) { YAML.load_file("#{Rails.root}/config/default_preferences.yml") }
describe ".load" do
it "provides default values when no preferences are stored yet" do
expect(UserPreferences.load(nil)).to eq(default_prefs)
end
it "provides default values for unset preferences" do
prefs = UserPreferences.load("lightning_notify_sats_received: xmpp")
expect(prefs[:lightning_notify_sats_received]).to eq("xmpp")
expect(prefs[:xmpp_exchange_contacts_with_invitees]).to eq(true)
end
end
describe ".process" do
it "turns all keys into strings" do
res = UserPreferences.process({ foo: "bar" })
expect(res[:foo]).to be(nil)
expect(res['foo']).to eq("bar")
end
it "converts value 'true' to boolean" do
res = UserPreferences.process({ lightning_notify_sats_received: "true" })
expect(res['lightning_notify_sats_received']).to be(true)
end
it "converts value 'false' to boolean" do
res = UserPreferences.process({ lightning_notify_sats_received: "false" })
expect(res['lightning_notify_sats_received']).to be(false)
end
it "converts value string with integer into integer" do
res = UserPreferences.process({ lightning_notify_sats_received_threshold: 1000 })
expect(res['lightning_notify_sats_received_threshold']).to be_a(Integer)
expect(res['lightning_notify_sats_received_threshold']).to eq(1000)
end
end
end

View File

@@ -109,7 +109,7 @@ RSpec.describe User, type: :model do
before do
Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now
allow_any_instance_of(User).to receive(:services_enabled).and_return(%w[ ejabberd ])
allow_any_instance_of(User).to receive(:services_enabled).and_return(%w[ xmpp ])
end
it "enqueues a job to exchange XMPP contacts between inviter and invitee" do
@@ -131,14 +131,16 @@ RSpec.describe User, type: :model do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
it "enables default services" do
expect(user).to receive(:enable_service).with(%w[ discourse ejabberd gitea mediawiki ])
expect(user).to receive(:enable_service).with(%w[ discourse gitea mediawiki xmpp ])
user.send(:devise_after_confirmation)
end
context "for invited user with ejabberd enabled" do
context "for invited user with xmpp enabled" do
let(:guest) { create :user, id: 2, cn: "isaacnewton", ou: "kosmos.org", email: "newt@example.com" }
before do
# TODO remove when defaults are implemented
user.update! preferences: { xmpp_exchange_contacts_with_invitees: true }
Invitation.create! user: user, invited_user_id: guest.id, used_at: DateTime.now
allow_any_instance_of(User).to receive(:enable_service).and_return(true)
end
@@ -147,6 +149,17 @@ RSpec.describe User, type: :model do
expect(guest).to receive(:exchange_xmpp_contact_with_inviter)
guest.send(:devise_after_confirmation)
end
context "automatic contact exchange disabled" do
before do
user.update! preferences: { xmpp_exchange_contacts_with_invitees: false }
end
it "does not exchange XMPP contacts with the inviter" do
expect(guest).to_not receive(:exchange_xmpp_contact_with_inviter)
guest.send(:devise_after_confirmation)
end
end
end
end
end

View File

@@ -55,22 +55,51 @@ RSpec.describe "Webhooks", type: :request do
before do
user.save! #FIXME this should not be necessary
post "/webhooks/lndhub", params: payload.to_json
end
it "returns a 200 status" do
post "/webhooks/lndhub", params: payload.to_json
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)
it "does not send notifications by default" do
expect(enqueued_jobs.size).to eq(0)
end
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/)
context "notification preference set to 'xmpp'" do
before do
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("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
end
end