akkounts/spec/services/ejabberd_manager/get_avatar_spec.rb
Râu Cao d737d9f6b8
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
Refactor ejabberd API integration
2025-05-26 14:10:27 +04:00

38 lines
1.5 KiB
Ruby

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