From e652835a5fa4a1854c7de6880f58d7baa940df4f Mon Sep 17 00:00:00 2001 From: Michael Bianco Date: Sat, 31 Mar 2012 11:07:52 -0400 Subject: [PATCH] Added preferences for click amount and valid days on digital links. Tests for authorization are now running --- app/models/spree/digital_link.rb | 2 +- lib/spree/spree_digital_configuration.rb | 6 ++ lib/spree_digital/engine.rb | 6 +- spec/models/digital_link_spec.rb | 88 +++++++++--------------- 4 files changed, 45 insertions(+), 57 deletions(-) create mode 100644 lib/spree/spree_digital_configuration.rb diff --git a/app/models/spree/digital_link.rb b/app/models/spree/digital_link.rb index 7304fbb..1b794bc 100644 --- a/app/models/spree/digital_link.rb +++ b/app/models/spree/digital_link.rb @@ -11,7 +11,7 @@ module Spree # Can this link stil be used? It is valid if it's less than 24 hours old and was not accessed more than 3 times def authorizable? - self.created_at > 1.day.ago and self.access_counter < 3 + self.created_at > Spree::DigitalConfiguration[:authorized_days].day.ago and self.access_counter < Spree::DigitalConfiguration[:authorized_clicks] end # This method should be called when a download is initiated. diff --git a/lib/spree/spree_digital_configuration.rb b/lib/spree/spree_digital_configuration.rb new file mode 100644 index 0000000..6a9df77 --- /dev/null +++ b/lib/spree/spree_digital_configuration.rb @@ -0,0 +1,6 @@ +module Spree + class SpreeDigitalConfiguration < Preferences::Configuration + preference :authorized_clicks, :integer, :default => 3 + preference :authorized_days, :integer, :default => 2 + end +end \ No newline at end of file diff --git a/lib/spree_digital/engine.rb b/lib/spree_digital/engine.rb index 929ac96..faf02a0 100644 --- a/lib/spree_digital/engine.rb +++ b/lib/spree_digital/engine.rb @@ -8,7 +8,11 @@ module SpreeDigital config.generators do |g| g.test_framework :rspec end - + + initializer "spree.spree_digital.preferences", :after => "spree.environment" do |app| + Spree::DigitalConfiguration = Spree::SpreeDigitalConfiguration.new + end + def self.activate Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c| Rails.application.config.cache_classes ? require(c) : load(c) diff --git a/spec/models/digital_link_spec.rb b/spec/models/digital_link_spec.rb index 878644c..eb75cf3 100644 --- a/spec/models/digital_link_spec.rb +++ b/spec/models/digital_link_spec.rb @@ -38,61 +38,39 @@ describe Spree::DigitalLink do end end - #context "authorization" do - # it "should increment the counter using #authorize!" do - # link = Factory(:digital_link) - # link.authorize! - # link.access_counter.should == 1 - # end - # - # it "should not be #authorized? when the access_counter is too high" do - # link = Factory(:digital_link) - # link.stub(:access_counter => 2) - # link.authorized?.should be_true - # link.stub(:access_counter => 3) - # link.authorized?.should be_false - # end - # - # it "should not be #authorize! when the access_counter is too high" do - # link = Factory(:digital_link) - # link.stub(:access_counter => 2) - # link.authorize!.should be_true - # link.stub(:access_counter => 3) - # link.authorize!.should be_false - # end - # - # it "should not be #authorized? when the created_at date is too far in the past" do - # link = Factory(:digital_link) - # link.authorized?.should be_true - # link.stub(:created_at => 23.hours.ago) - # link.authorized?.should be_true - # link.stub(:created_at => 25.hours.ago) - # link.authorized?.should be_false - # end - # - # it "should not be #authorize! when the created_at date is too far in the past" do - # link = Factory(:digital_link) - # link.authorize!.should be_true - # link.stub(:created_at => 23.hours.ago) - # link.authorize!.should be_true - # link.stub(:created_at => 25.hours.ago) - # link.authorize!.should be_false - # end - # - # it "should not be #authorized? when both access_counter and created_at are invalid" do - # link = Factory(:digital_link) - # link.authorized?.should be_true - # link.stub(:access_counter => 3, :created_at => 25.hours.ago) - # link.authorized?.should be_false - # end - # - # it "should not be #authorize! when both access_counter and created_at are invalid" do - # link = Factory(:digital_link) - # link.authorize!.should be_true - # link.stub(:access_counter => 3, :created_at => 25.hours.ago) - # link.authorize!.should be_false - # end - #end + context "authorization" do + it "should increment the counter using #authorize!" do + link = Factory(:digital_link) + link.access_counter.should == 0 + link.authorize! + link.access_counter.should == 1 + end + + it "should not be #authorized? when the access_counter is too high" do + link = Factory(:digital_link) + link.stub(:access_counter => Spree::DigitalConfiguration[:authorized_clicks] - 1) + link.authorizable?.should be_true + link.stub(:access_counter => Spree::DigitalConfiguration[:authorized_clicks]) + link.authorizable?.should be_false + end + + it "should not be #authorize! when the created_at date is too far in the past" do + link = Factory(:digital_link) + link.authorize!.should be_true + link.stub(:created_at => (Spree::DigitalConfiguration[:authorized_days] * 24 - 1).hours.ago) + link.authorize!.should be_true + link.stub(:created_at => (Spree::DigitalConfiguration[:authorized_days] * 24 + 1).hours.ago) + link.authorize!.should be_false + end + + it "should not be #authorized? when both access_counter and created_at are invalid" do + link = Factory(:digital_link) + link.authorizable?.should be_true + link.stub(:access_counter => Spree::DigitalConfiguration[:authorized_clicks], :created_at => (Spree::DigitalConfiguration[:authorized_days] * 24 + 1).hours.ago) + link.authorizable?.should be_false + end + + end end