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