require 'spec_helper'

describe IcecastAdminAuthentication do

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

  it "save error" do
    admin.save.should be_false
    admin.errors[:source_pass].length.should == 2
    admin.errors[:admin_user].length.should == 2
    admin.errors[:admin_pass].length.should == 2
    admin.errors[:relay_user].length.should == 2
    admin.errors[:relay_pass].length.should == 2
  end

  it "save" do
    admin.source_pass = Faker::Lorem.characters(10)
    admin.admin_user = Faker::Lorem.characters(10)
    admin.admin_pass = Faker::Lorem.characters(10)
    admin.relay_user = Faker::Lorem.characters(10)
    admin.relay_pass = Faker::Lorem.characters(10)
    
    admin.save.should be_true

    admin.dumpXml(builder)

    output.rewind

    xml = Nokogiri::XML(output)
    xml.css('authentication source-password').text.should == admin.source_pass
    xml.css('authentication source-password').length.should == 1
    xml.css('authentication admin-user').text.should == admin.admin_user
    xml.css('authentication admin-user').length.should == 1
    xml.css('authentication relay-user').text.should == admin.relay_user
    xml.css('authentication relay-user').length.should == 1
    xml.css('authentication relay-password').text.should == admin.relay_pass
    xml.css('authentication relay-password').length.should == 1
    xml.css('authentication admin-password').text.should == admin.admin_pass
    xml.css('authentication admin-password').length.should == 1
  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.admin_auth.save!
      server.reload
      server.config_changed.should == 1
    end

    it "success when deleted via template" do
      server.template.admin_auth.destroy
      server.reload
      server.config_changed.should == 1
    end

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

    it "success when deleted via server" do
      server.admin_auth.destroy
      server.reload
      server.config_changed.should == 1
    end
  end
end
