Refactor ejabberd API integration
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Release Drafter / Update release notes draft (pull_request) Successful in 2s

This commit is contained in:
2025-05-26 14:10:27 +04:00
parent 4bf6985b87
commit d737d9f6b8
19 changed files with 307 additions and 167 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,4 @@
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe XmppExchangeContactsJob, type: :job do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
@@ -10,19 +9,11 @@ RSpec.describe XmppExchangeContactsJob, type: :job do
described_class.perform_later(user, guest)
}
before do
stub_request(:post, "http://xmpp.example.com/api/add_rosteritem")
.to_return(status: 200, body: "", headers: {})
allow_any_instance_of(User).to receive(:services_enabled).and_return(["ejabberd"])
end
it "calls the service for exchanging contacts" do
expect(EjabberdManager::ExchangeContacts).to receive(:call)
.with(inviter: user, invitee: guest).and_return(true)
it "posts add_rosteritem commands to the ejabberd API" do
perform_enqueued_jobs { job }
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == '{"localuser":"isaacnewton","localhost":"kosmos.org","user":"willherschel","host":"kosmos.org","nick":"willherschel","group":"Buddies","subs":"both"}' }
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == '{"localuser":"willherschel","localhost":"kosmos.org","user":"isaacnewton","host":"kosmos.org","nick":"isaacnewton","group":"Buddies","subs":"both"}' }
end
after do

View File

@@ -0,0 +1,25 @@
require 'rails_helper'
RSpec.describe XmppSendMessageJob, type: :job do
let(:payload) {{
type: "normal",
from: "kosmos.org", to: "willherschel@kosmos.org",
body: "This is a test message"
}}
subject(:job) {
described_class.perform_later(payload)
}
it "calls the service for exchanging contacts" do
expect(EjabberdManager::SendMessage).to receive(:call)
.with(payload: payload).and_return(true)
perform_enqueued_jobs { job }
end
after do
clear_enqueued_jobs
clear_performed_jobs
end
end

View File

@@ -1,30 +1,17 @@
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe XmppSetDefaultBookmarksJob, type: :job do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
before do
Setting.xmpp_default_rooms = [
"Welcome <welcome@kosmos.chat>",
"Kosmos Dev <kosmos-dev@kosmos.chat>"
]
end
subject(:job) {
described_class.perform_later(user)
}
before do
stub_request(:post, "http://xmpp.example.com/api/private_set")
.to_return(status: 200, body: "", headers: {})
end
it "calls the service for setting default bookmarks" do
expect(EjabberdManager::SetDefaultBookmarks).to receive(:call)
.with(user: user).and_return(true)
it "posts a private_set command to the ejabberd API" do
perform_enqueued_jobs { job }
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/private_set")
.with { |req| req.body == '{"user":"willherschel","host":"kosmos.org","element":"\u003cstorage xmlns=\'storage:bookmarks\'\u003e\u003cconference jid=\'welcome@kosmos.chat\' name=\'Welcome\' autojoin=\'false\'\u003e\u003cnick\u003ewillherschel\u003c/nick\u003e\u003c/conference\u003e\u003cconference jid=\'kosmos-dev@kosmos.chat\' name=\'Kosmos Dev\' autojoin=\'false\'\u003e\u003cnick\u003ewillherschel\u003c/nick\u003e\u003c/conference\u003e\u003c/storage\u003e"}' }
end
after do

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe EjabberdManager::ExchangeContacts, type: :model do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:guest) { create :user, cn: "isaacnewton", ou: "kosmos.org",
id: 2, email: "hotapple42@eol.com" }
before do
stub_request(:post, "http://xmpp.example.com/api/add_rosteritem")
.to_return(status: 200, body: "", headers: {})
allow_any_instance_of(User).to receive(:services_enabled).and_return(["ejabberd"])
described_class.call(inviter: user, invitee: guest)
end
it "posts add_rosteritem commands to the ejabberd API" do
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == '{"localuser":"isaacnewton","localhost":"kosmos.org","user":"willherschel","host":"kosmos.org","nick":"willherschel","group":"Buddies","subs":"both"}' }
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/add_rosteritem")
.with { |req| req.body == '{"localuser":"willherschel","localhost":"kosmos.org","user":"isaacnewton","host":"kosmos.org","nick":"isaacnewton","group":"Buddies","subs":"both"}' }
end
end

View File

@@ -0,0 +1,37 @@
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe EjabberdManager::GetAvatar, type: :model do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
let(:img_base64) { File.read("#{Rails.root}/spec/fixtures/files/avatar-base64-png.txt").chomp }
context "when no avatar is set yet" do
before do
stub_request(:post, "http://xmpp.example.com/api/get_vcard2")
.with { |req| req.body == '{"user":"willherschel","host":"kosmos.org","name":"PHOTO","subname":"BINVAL"}' }
.to_return(status: 400, body: "", headers: {})
end
it "returns nil" do
res = described_class.call(user: user)
expect(res).to be_nil
end
end
context "when avatar exists" do
before do
stub_request(:post, "http://xmpp.example.com/api/get_vcard2")
.with { |req| req.body == '{"user":"willherschel","host":"kosmos.org","name":"PHOTO","subname":"BINVAL"}' }
.and_return(status: 200, body: { content: img_base64 }.to_json, headers: {})
stub_request(:post, "http://xmpp.example.com/api/get_vcard2")
.with { |req| req.body == '{"user":"willherschel","host":"kosmos.org","name":"PHOTO","subname":"TYPE"}' }
.and_return(status: 200, body: { content: "image/png" }.to_json, headers: {})
end
it "fetches the avatar and content type" do
res = described_class.call(user: user)
expect(res[:img_base64]).to eq(img_base64)
expect(res[:content_type]).to eq("image/png")
end
end
end

View File

@@ -0,0 +1,22 @@
require 'rails_helper'
require 'webmock/rspec'
RSpec.describe EjabberdManager::SetDefaultBookmarks, type: :model do
let(:user) { create :user, cn: "willherschel", ou: "kosmos.org" }
before do
Setting.xmpp_default_rooms = [
"Welcome <welcome@kosmos.chat>",
"Kosmos Dev <kosmos-dev@kosmos.chat>"
]
stub_request(:post, "http://xmpp.example.com/api/private_set")
.to_return(status: 200, body: "", headers: {})
described_class.call(user:)
end
it "posts a private_set command to the ejabberd API" do
expect(WebMock).to have_requested(:post, "http://xmpp.example.com/api/private_set")
.with { |req| req.body == '{"user":"willherschel","host":"kosmos.org","element":"\u003cstorage xmlns=\'storage:bookmarks\'\u003e\u003cconference jid=\'welcome@kosmos.chat\' name=\'Welcome\' autojoin=\'false\'\u003e\u003cnick\u003ewillherschel\u003c/nick\u003e\u003c/conference\u003e\u003cconference jid=\'kosmos-dev@kosmos.chat\' name=\'Kosmos Dev\' autojoin=\'false\'\u003e\u003cnick\u003ewillherschel\u003c/nick\u003e\u003c/conference\u003e\u003c/storage\u003e"}' }
end
end