require 'spec_helper'

describe IcecastPath do

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

  it "save default" do
    path.save.should be_true
    path.dumpXml(builder)

    output.rewind

    xml = Nokogiri::XML(output)
    xml.css('paths basedir').text.should == "./"
    xml.css('paths basedir').length.should == 1
    xml.css('paths logdir').text.should == "./logs"
    xml.css('paths logdir').length.should == 1
    xml.css('paths pidfile').text.should == "./icecast.pid"
    xml.css('paths pidfile').length.should == 1
    xml.css('paths webroot').text.should == "./web"
    xml.css('paths webroot').length.should == 1
    xml.css('paths adminroot').text.should == "./admin"
    xml.css('paths adminroot').length.should == 1
    xml.css('paths allow-ip').length.should == 0
    xml.css('paths deny-ip').length.should == 0
    xml.css('paths alias').length.should == 0
  end

  it "save set values" do
    path.base_dir = Faker::Lorem.characters(10)
    path.log_dir = Faker::Lorem.characters(10)
    path.pid_file = Faker::Lorem.characters(10)
    path.web_root = Faker::Lorem.characters(10)
    path.admin_root = Faker::Lorem.characters(10)
    path.allow_ip = Faker::Lorem.characters(10)
    path.deny_ip = Faker::Lorem.characters(10)
    path.alias_source = Faker::Lorem.characters(10)
    path.alias_dest = Faker::Lorem.characters(10)
    path.save.should be_true
    path.dumpXml(builder)

    output.rewind

    xml = Nokogiri::XML(output)
    xml.css('paths basedir').text.should == path.base_dir
    xml.css('paths logdir').text.should == path.log_dir
    xml.css('paths pidfile').text.should == path.pid_file
    xml.css('paths webroot').text.should == path.web_root
    xml.css('paths adminroot').text.should == path.admin_root
    xml.css('paths allow-ip').text.should == path.allow_ip
    xml.css('paths deny-ip').text.should == path.deny_ip
    xml.css('paths alias').first['source'] == path.alias_source
    xml.css('paths alias').first['dest'] == path.alias_dest
  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.path.save!
      server.reload
      server.config_changed.should == 1
    end

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

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

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