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

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