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 "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
    clear_enqueued_jobs
    clear_performed_jobs
  end
end
