require 'test_helper' require 'fakeweb' class HookTest < ActiveSupport::TestCase def setup @account = Factory(:account) @subscription_url = "https://zapier.com/hooks/standard/wpGRPPcRxZt2GxBbSSeUAlWPBnhLiRWB/" @target_url = "https://zapier.com/hooks/standard/wpGRPPcRxZt2GxBbSSeUAlWPBnhLiRWB/" # Create the record before the hook is created or else the after_create Active Model hook # will trigger the REST hook prematurely during testing. @contact = Factory(:contact, :account => @account, :first => 'Ryan', :last => 'Porter', :email => 'xyz@venuedriver.com') @hook = Hook.create( { "event" => "new_contact", "account_id" => @account.id, "subscription_url" => @subscription_url, "target_url" => @target_url } ) end def test_hooks_exist assert_equal true, Hook.hooks_exist?('new_contact', @contact) Hook.destroy_all assert_equal false, Hook.hooks_exist?('new_contact', @contact) end def test_hooks hooks = Hook.hooks('new_contact', @contact) assert_equal 1, hooks.size assert_equal @hook, hooks.first second_hook = Hook.create( { "event" => "new_contact", "account_id" => @account.id, "subscription_url" => @subscription_url, "target_url" => @target_url } ) hooks = Hook.hooks('new_contact', @contact) assert_equal 2, hooks.size assert_equal second_hook, hooks.last end def test_trigger FakeWeb.register_uri( :post, @target_url, :body => 'irrelevant', :status => ['200', 'Triggered'] ) FakeWeb.allow_net_connect = false Hook.trigger('new_contact', @contact) assert_equal "POST", FakeWeb.last_request.method assert_equal HookEncoder.encode(@contact), FakeWeb.last_request.body assert_equal 1, Hook.count # The hook should not have been deleted. end def test_trigger_remove_hook_on_410_response FakeWeb.register_uri( :post, @target_url, :body => 'irrelevant', :status => ['410', 'Danger, Will Robinson!'] ) FakeWeb.allow_net_connect = false Hook.trigger('new_contact', @contact) assert_equal "POST", FakeWeb.last_request.method assert_equal HookEncoder.encode(@contact), FakeWeb.last_request.body assert_equal 0, Hook.count # The 410 response should trigger removal of the hook. end def test_resque_background_job FakeWeb.register_uri( :post, @target_url, :body => 'irrelevant', :status => ['200', 'Triggered'] ) FakeWeb.allow_net_connect = false # A Resque worker will normally do this, which should have the same effect as when the hook # is manually triggered in the test_trigger test. Hook.perform(Contact.name, @contact.id) assert_equal "POST", FakeWeb.last_request.method assert_equal HookEncoder.encode(@contact), FakeWeb.last_request.body assert_equal 1, Hook.count # The hook should not have been deleted. end end