Permalink
Browse files

added error handling to signature referral for unresolvable referral …

…codes
  • Loading branch information...
1 parent 200a75a commit d89b5e69f89e773074374f6c46fc673f9cf3318e @manderdev manderdev committed Oct 17, 2012
Showing with 91 additions and 4 deletions.
  1. +17 −4 lib/signature_referral.rb
  2. +74 −0 spec/lib/signature_referral_spec.rb
View
@@ -34,8 +34,14 @@ def trackable?
end
def referral
- return {} unless trackable?
- @referral_type == :regular ? track_regular_referral : track_facebook_referral
+ default = {}
+ begin
+ return default unless trackable?
+ @referral_type == :regular ? track_regular_referral : track_facebook_referral
+ rescue => ex
+ Rails.logger.warn "Error resolving referral: #{ex} #{ex.backtrace.join}"
+ return default
+ end
end
def self.translate_raw_referral(params={})
@@ -51,7 +57,8 @@ def self.translate_raw_referral(params={})
def track_regular_referral
if reference_type == Signature::ReferenceType::EMAIL
sent_email = SentEmail.find_by_hash(received_code)
- return unless sent_email
+ raise "SentEmail record not found for referral code #{received_code}" if not sent_email
+
sent_email.signature ||= signature
sent_email.save!
petition.experiments.email(sent_email).win!(:signature)
@@ -62,7 +69,8 @@ def track_regular_referral
}
else
referring_member = Member.find_by_hash(received_code)
-
+ raise "Member record not found for referral code #{received_code}" if not referring_member
+
{
referer: referring_member,
reference_type: reference_type,
@@ -73,20 +81,25 @@ def track_regular_referral
def code_and_member_for_facebook_share_special_case
facebook_action = Share.find_by_action_id(received_code.to_s)
+ raise "FacebookAction record not found for referral code #{received_code}" if not facebook_action
+
code = ReferralCode.where(petition_id: petition.id, member_id: facebook_action.member_id).first
return code, facebook_action.member
end
def code_and_member_for_legacy_referral_code
member = Member.find_by_hash(received_code)
+ raise "Member record not found for referral code #{received_code}" if not member
+
code = ReferralCode.where(petition_id: petition.id, member_id: member.id).first
return code, member
end
def code_and_member_for_generated_referral_code
code = ReferralCode.where(code: received_code).first
+ raise "ReferralCode record not found for referral code #{received_code}" if not code
return code, code.try(:member)
end
@@ -1,4 +1,78 @@
require 'spec_helper'
describe SignatureReferral do
+
+ let(:petition) { create :petition }
+ let(:signature) { create :signature, petition_id: petition.id }
+
+ it "return email referer given sent mail (:n) param" do
+ params = {:n => "foo"}.with_indifferent_access
+ referer_ref_type, referer_ref_code = SignatureReferral.translate_raw_referral(params)
+ referer_ref_type.should eq "email"
+ referer_ref_code.should eq "foo"
+ end
+
+ it "returns nil when translating with no relevant param " do
+ params = {"irrelevant param" => "foo"}.with_indifferent_access
+ referer_ref_type, referer_ref_code = SignatureReferral.translate_raw_referral(params)
+ referer_ref_type.should be nil
+ referer_ref_code.should be nil
+ end
+
+ it "is not trackable without relevant params" do
+ trackable = SignatureReferral.new(petition, signature).trackable?
+ trackable.should be false
+ end
+
+ it "is trackable given reference type and received code" do
+ params = {referer_ref_code: "1a2b3c", referer_ref_type: Signature::ReferenceType::EMAIL}.with_indifferent_access
+ SignatureReferral.new(petition, signature, params).trackable?.should be true
+ end
+
+ context "unresolvable referral codes" do
+
+ received_code = "1a2b3c"
+
+ it "logs warning and returns default for email referral when email unresolved" do
+ SentEmail.stub(:find_by_hash).with(received_code).and_return(nil)
+ Rails.logger.should_receive(:warn).with(/SentEmail record not found for referral code 1a2b3c/)
+ params = {referer_ref_code: received_code, referer_ref_type: Signature::ReferenceType::EMAIL}.with_indifferent_access
+ referral = SignatureReferral.new(petition, signature, params).referral
+ referral.should be_empty
+ end
+
+ it "logs warning and returns default for shared link referral when member unresolved" do
+ Member.stub(:find_by_hash).with(received_code).and_return(nil)
+ Rails.logger.should_receive(:warn).with(/Member record not found for referral code 1a2b3c/)
+ params = {referer_ref_code: received_code, referer_ref_type: Signature::ReferenceType::SHARED_LINK}.with_indifferent_access
+ referral = SignatureReferral.new(petition, signature, params).referral
+ referral.should be_empty
+ end
+
+ it "logs warning and returns default for share special case facebook referral when facebook action unresolved" do
+ Share.stub(:find_by_action_id).with(received_code).and_return(nil)
+ Rails.logger.should_receive(:warn).with(/FacebookAction record not found for referral code 1a2b3c/)
+ params = {referer_ref_code: received_code, referer_ref_type: Signature::ReferenceType::FACEBOOK_SHARE}.with_indifferent_access
+ referral = SignatureReferral.new(petition, signature, params).referral
+ referral.should be_empty
+ end
+
+ it "logs warning and returns default for legacy facebook referral when member unresolved" do
+ legacy_referral_code = "123.foo"
+ Member.stub(:find_by_hash).with(legacy_referral_code).and_return(nil)
+ Rails.logger.should_receive(:warn).with(/Member record not found for referral code 123.foo/)
+ params = {referer_ref_code: legacy_referral_code, referer_ref_type: Signature::ReferenceType::FACEBOOK_LIKE}.with_indifferent_access
+ referral = SignatureReferral.new(petition, signature, params).referral
+ referral.should be_empty
+ end
+
+ it "logs warning and returns default for generated facebook referral when referral unresolved" do
+ ReferralCode.stub(:where).with(code: received_code).and_return([])
+ Rails.logger.should_receive(:warn).with(/ReferralCode record not found for referral code 1a2b3c/)
+ params = {referer_ref_code: received_code, referer_ref_type: Signature::ReferenceType::FACEBOOK_LIKE}.with_indifferent_access
+ referral = SignatureReferral.new(petition, signature, params).referral
+ referral.should be_empty
+ end
+ end
+
end

0 comments on commit d89b5e6

Please sign in to comment.