require 'spec_helper'

describe IcecastLogging do

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

  it "save works by default db values" do
    logging.save.should be_true
    logging.access_log.should == 'access.log'
    logging.error_log.should == 'error.log'
  end
  
  it "save" do
    logging.access_log = Faker::Lorem.characters(10)
    logging.error_log = Faker::Lorem.characters(10)
    logging.log_level = 4
    logging.log_size = 20000
    logging.save!

    logging.dumpXml(builder)

    output.rewind
    xml = Nokogiri::XML(output)
    xml.css('logging accesslog').text.should == logging.access_log
    xml.css('logging errorlog').text.should == logging.error_log
    xml.css('logging loglevel').text.should == logging.log_level.to_s
    xml.css('logging logsize').text.should == logging.log_size.to_s
    xml.css('logging playlistlog').length.should == 0
    xml.css('logging logarchive').length.should == 0
  end

  describe "poke configs" do
    let(:server) { a = FactoryGirl.create(:icecast_server_with_overrides); a.config_updated; IcecastServer.find(a.id) }

    it "success via template" do
      server.template.logging.save!
      server.reload
      server.config_changed.should == 1
    end

    it "delete via template" do
      server.template.logging.destroy
      server.reload
      server.config_changed.should == 1
    end

    it "success via server" do
      server.logging.save!
      server.reload
      server.config_changed.should == 1
    end

    it "deete via server" do
      server.logging.destroy
      server.reload
      server.config_changed.should == 1
    end
  end
end