require 'spec_helper'

describe IcecastServer do

  let(:server) { IcecastServer.new }
  let(:output)  { StringIO.new }
  let(:builder) { ::Builder::XmlMarkup.new(:target => output, :indent => 1) }


  it "save" do
    server = FactoryGirl.create(:icecast_server_minimal)
    server.save!
    server.reload
    server.dumpXml(output)

    output.rewind

    xml = Nokogiri::XML(output)
    xml.css('icecast hostname').text.should == server.hostname
    xml.css('icecast server-id').text.should == server.server_id
    xml.css('icecast location').text.should == server.template.location
    xml.css('icecast admin').text.should == server.template.admin_email
    xml.css('icecast fileserve').text.should == server.template.fileserve.to_s
    xml.css('icecast limits').length.should == 1
    xml.css('icecast authentication').length.should == 1
    xml.css('icecast directory').length.should == 0
    xml.css('icecast master-server').length.should == 0
    xml.css('icecast paths').length.should == 1
    xml.css('icecast logging').length.should == 1
    xml.css('icecast security').length.should == 1
    xml.css('icecast listen-socket').length.should == 1
  end

  it "xml overrides" do
    server = FactoryGirl.create(:icecast_server_minimal)
    server.save!
    server.reload
    server.dumpXml(output)

    output.rewind

    xml = Nokogiri::XML(output)
    xml.css('icecast location').text.should == server.template.location
    xml.css('icecast fileserve').text.should == server.template.fileserve.to_s
    xml.css('icecast limits').length.should == 1
    xml.css('icecast limits queue-size').text.should == server.template.limit.queue_size.to_s

    server.location = "override"
    server.fileserve = 1
    server.limit = FactoryGirl.create(:icecast_limit, :queue_size => 777)
    server.save!

    output = StringIO.new
    builder = ::Builder::XmlMarkup.new(:target => output, :indent => 1)
    server.dumpXml(builder)
    output.rewind
    xml = Nokogiri::XML(output)
    xml.css('icecast location').text.should == server.location
    xml.css('icecast fileserve').text.should == server.fileserve.to_s
    xml.css('icecast limits').length.should == 1
    xml.css('icecast limits queue-size').text.should == server.limit.queue_size.to_s
  end
end