require 'spec_helper'

describe GenericState do

  def database_env (env)
    singleton = GenericState.singleton
    singleton.env = env
    singleton.save!
  end

  def rails_env (env)
    JamRuby::Environment.should_receive(:mode).any_number_of_times.and_return(env)
  end

  describe "allow_emails?" do

    it "allows emails if database is production and env is production, with build number" do
      database_env('production')
      rails_env('production')
      stub_const("ENV", {'BUILD_NUMBER' => 1})

      GenericState.allow_emails?.should be_true
    end

    it "no emails if database is production and env is production, no build number" do
      database_env('production')
      rails_env('production')
      stub_const("ENV", {'BUILD_NUMBER' => nil})

      GenericState.allow_emails?.should be_false
    end

    it "allows emails if database is development and env is development" do
      database_env('development')
      rails_env('development')

      GenericState.allow_emails?.should be_true
    end

    it "no emails if database development, and environment is test" do
      database_env('development')
      rails_env('test')
      GenericState.allow_emails?.should be_false
    end

    it "no emails if database production, and environment is development" do
      database_env('production')
      rails_env('development')
      stub_const("ENV", {'BUILD_NUMBER' => 1})

      GenericState.allow_emails?.should be_false
    end

    it "no emails if database production, and environment is test" do
      database_env('production')
      rails_env('development')

      GenericState.allow_emails?.should be_false
    end
  end
end