require 'spec_helper'
require_dependency 'plugin/instance'

describe Plugin::Instance do

  context "find_all" do
    it "can find plugins correctly" do
      plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
      plugins.count.should == 1
      plugin =plugins[0]

      plugin.name.should == "plugin-name"
      plugin.path.should == "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
    end

    it "does not blow up on missing directory" do
      plugins = Plugin::Instance.find_all("#{Rails.root}/frank_zappa")
      plugins.count.should == 0
    end
  end

  context "activate!" do
    it "can activate plugins correctly" do
      plugin = Plugin::Instance.new
      plugin.path = "#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb"
      junk_file = "#{plugin.auto_generated_path}/junk"

      plugin.ensure_directory(junk_file)
      File.open("#{plugin.auto_generated_path}/junk", "w") {|f| f.write("junk")}
      plugin.activate!

      plugin.auth_providers.count.should == 1
      auth_provider = plugin.auth_providers[0]
      auth_provider.authenticator.name.should == 'ubuntu'

      # calls ensure_assets! make sure they are there
      plugin.assets.count.should == 1
      plugin.assets.each do |a|
        File.exists?(a).should be_true
      end

      # ensure it cleans up all crap in autogenerated directory
      File.exists?(junk_file).should be_false
    end
  end

end