require 'spec_helper' require 'captivus/notifier' describe Captivus::Notifier do describe "notify" do before do Timecop.freeze Time.utc(2012, 11, 28, 2, 46, 4) @config = double 'config', :scheme => 'http', :host => 'api.captiv.us', :api_key => 'api-key', :api_secret_key => 'secret', :environment => 'production', :development_environments => %w[development test] end after { Timecop.return } context "when the environment is a development environment" do it "doesn't post" do @config.stub(:environment) { 'development' } Captivus::Notifier.new(@config).notify({}) expect(captivus_api.requests.size).to eq 0 @config.stub(:environment) { 'test' } Captivus::Notifier.new(@config).notify({}) expect(captivus_api.requests.size).to eq 0 end end it "posts the given payload to the endpoint in the config" do Captivus::Notifier.new(@config).notify({:a => 1, :b => '2'}) expect(captivus_api.requests.size).to eq 1 headers = { "CONTENT_TYPE"=>"application/json; charset=UTF-8", "HTTP_ACCEPT" => "*/*", "HTTP_USER_AGENT" => "Ruby", "HTTP_DATE" => "Wed, 28 Nov 2012 02:46:04 GMT", "HTTP_AUTHORIZATION" => "Captivus api-key:IgBW675pSKkCuD4McYnX9QQMI5g=", "PATH_INFO" => "/events", "QUERY_STRING" => "", "REQUEST_METHOD"=>"POST", "SCRIPT_NAME" => "", "SERVER_NAME" => "api.captiv.us", "SERVER_PORT" => "80", "rack.url_scheme" => "http" } last_request = captivus_api.requests[0] expect(last_request.values_at(*headers.keys)).to eq headers.values expect(last_request['rack.input'].read).to eq '{"a":1,"b":"2"}' end end describe "#==" do it "is true given another notifier with the same config" do config = double 'config' expect(Captivus::Notifier.new(config) == Captivus::Notifier.new(config)).to be_true end it "is false given another notifier with different config" do expect(Captivus::Notifier.new(double('config')) == Captivus::Notifier.new(double('config'))).to be_false end it "is false given a non notifier" do config = double 'config' expect(Captivus::Notifier.new(config) == double('other', :config => config)).to be_false end end end