Added preferences for click amount and valid days on digital links. Tests for authorization are now running

This commit is contained in:
Michael Bianco
2012-03-31 11:07:52 -04:00
parent c4a85b71d7
commit e652835a5f
4 changed files with 45 additions and 57 deletions

View File

@@ -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.

View File

@@ -0,0 +1,6 @@
module Spree
class SpreeDigitalConfiguration < Preferences::Configuration
preference :authorized_clicks, :integer, :default => 3
preference :authorized_days, :integer, :default => 2
end
end

View File

@@ -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)

View File

@@ -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