Commit c5eecbccd117261c8b2d8e05b4c3b328cb1468f8
1 parent
51e3a693
Exists in
master
and in
1 other branch
Skip fraud detection
Showing
2 changed files
with
31 additions
and
0 deletions
Show diff stats
app/models/visitor.rb
| ... | ... | @@ -33,6 +33,16 @@ class Visitor < ActiveRecord::Base |
| 33 | 33 | options.merge!(:appearance => @appearance) |
| 34 | 34 | end |
| 35 | 35 | end |
| 36 | + | |
| 37 | + if options.delete(:skip_fraud_protection) | |
| 38 | + last_answered_appearance = self.appearances.find(:first, | |
| 39 | + :conditions => ["appearances. question_id = ? AND appearances.answerable_id IS NOT NULL", prompt.question_id], | |
| 40 | + :order => 'id DESC') | |
| 41 | + if last_answered_appearance && last_answered_appearance.answerable_type == "Skip" | |
| 42 | + options.merge!(:valid_record => false) | |
| 43 | + options.merge!(:validity_information => "Fraud protection: last visitor action was a skip") | |
| 44 | + end | |
| 45 | + end | |
| 36 | 46 | |
| 37 | 47 | choice = prompt.choices[ordinality] #we need to guarantee that the choices are in the right order (by position) |
| 38 | 48 | other_choices = prompt.choices - [choice] | ... | ... |
spec/models/visitor_spec.rb
| ... | ... | @@ -198,6 +198,27 @@ describe Visitor do |
| 198 | 198 | @rc.losses.should == prev_loser_losses + 1 |
| 199 | 199 | @rc.wins.should == prev_winner_wins |
| 200 | 200 | end |
| 201 | + | |
| 202 | + it "should invalidate vote after skips when :skip_fraud_protection option passed" do | |
| 203 | + | |
| 204 | + # If a visitor skips a prompt, the vote after should be conisdered invalid | |
| 205 | + @appearance = @aoi_clone.record_appearance(@visitor, @prompt) | |
| 206 | + @visitor.skip!(@required_skip_params.merge({ :appearance_lookup => @appearance.lookup})) | |
| 207 | + | |
| 208 | + @appearance_2 = @aoi_clone.record_appearance(@visitor, @prompt) | |
| 209 | + @optional_vote_params = {:appearance_lookup => @appearance_2.lookup, :skip_fraud_protection => true } | |
| 210 | + | |
| 211 | + vote = @visitor.vote_for! @required_vote_params.merge(@optional_vote_params) | |
| 212 | + vote.valid_record.should be_false | |
| 213 | + | |
| 214 | + @appearance_3 = @aoi_clone.record_appearance(@visitor, @prompt) | |
| 215 | + @optional_vote_params = {:appearance_lookup => @appearance_3.lookup, :skip_fraud_protection => true } | |
| 216 | + | |
| 217 | + vote_2 = @visitor.vote_for! @required_vote_params.merge(@optional_vote_params) | |
| 218 | + | |
| 219 | + vote_2.valid_record.should be_true | |
| 220 | + | |
| 221 | + end | |
| 201 | 222 | |
| 202 | 223 | |
| 203 | 224 | end | ... | ... |