Added preferences for click amount and valid days on digital links. Tests for authorization are now running
This commit is contained in:
@@ -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
|
# 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?
|
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
|
end
|
||||||
|
|
||||||
# This method should be called when a download is initiated.
|
# This method should be called when a download is initiated.
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
module Spree
|
||||||
|
class SpreeDigitalConfiguration < Preferences::Configuration
|
||||||
|
preference :authorized_clicks, :integer, :default => 3
|
||||||
|
preference :authorized_days, :integer, :default => 2
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -8,7 +8,11 @@ module SpreeDigital
|
|||||||
config.generators do |g|
|
config.generators do |g|
|
||||||
g.test_framework :rspec
|
g.test_framework :rspec
|
||||||
end
|
end
|
||||||
|
|
||||||
|
initializer "spree.spree_digital.preferences", :after => "spree.environment" do |app|
|
||||||
|
Spree::DigitalConfiguration = Spree::SpreeDigitalConfiguration.new
|
||||||
|
end
|
||||||
|
|
||||||
def self.activate
|
def self.activate
|
||||||
Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
|
Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
|
||||||
Rails.application.config.cache_classes ? require(c) : load(c)
|
Rails.application.config.cache_classes ? require(c) : load(c)
|
||||||
|
|||||||
@@ -38,61 +38,39 @@ describe Spree::DigitalLink do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#context "authorization" do
|
context "authorization" do
|
||||||
# it "should increment the counter using #authorize!" do
|
it "should increment the counter using #authorize!" do
|
||||||
# link = Factory(:digital_link)
|
link = Factory(:digital_link)
|
||||||
# link.authorize!
|
link.access_counter.should == 0
|
||||||
# link.access_counter.should == 1
|
link.authorize!
|
||||||
# end
|
link.access_counter.should == 1
|
||||||
#
|
end
|
||||||
# it "should not be #authorized? when the access_counter is too high" do
|
|
||||||
# link = Factory(:digital_link)
|
it "should not be #authorized? when the access_counter is too high" do
|
||||||
# link.stub(:access_counter => 2)
|
link = Factory(:digital_link)
|
||||||
# link.authorized?.should be_true
|
link.stub(:access_counter => Spree::DigitalConfiguration[:authorized_clicks] - 1)
|
||||||
# link.stub(:access_counter => 3)
|
link.authorizable?.should be_true
|
||||||
# link.authorized?.should be_false
|
link.stub(:access_counter => Spree::DigitalConfiguration[:authorized_clicks])
|
||||||
# end
|
link.authorizable?.should be_false
|
||||||
#
|
end
|
||||||
# it "should not be #authorize! when the access_counter is too high" do
|
|
||||||
# link = Factory(:digital_link)
|
it "should not be #authorize! when the created_at date is too far in the past" do
|
||||||
# link.stub(:access_counter => 2)
|
link = Factory(:digital_link)
|
||||||
# link.authorize!.should be_true
|
link.authorize!.should be_true
|
||||||
# link.stub(:access_counter => 3)
|
link.stub(:created_at => (Spree::DigitalConfiguration[:authorized_days] * 24 - 1).hours.ago)
|
||||||
# link.authorize!.should be_false
|
link.authorize!.should be_true
|
||||||
# end
|
link.stub(:created_at => (Spree::DigitalConfiguration[:authorized_days] * 24 + 1).hours.ago)
|
||||||
#
|
link.authorize!.should be_false
|
||||||
# it "should not be #authorized? when the created_at date is too far in the past" do
|
end
|
||||||
# link = Factory(:digital_link)
|
|
||||||
# link.authorized?.should be_true
|
it "should not be #authorized? when both access_counter and created_at are invalid" do
|
||||||
# link.stub(:created_at => 23.hours.ago)
|
link = Factory(:digital_link)
|
||||||
# link.authorized?.should be_true
|
link.authorizable?.should be_true
|
||||||
# link.stub(:created_at => 25.hours.ago)
|
link.stub(:access_counter => Spree::DigitalConfiguration[:authorized_clicks], :created_at => (Spree::DigitalConfiguration[:authorized_days] * 24 + 1).hours.ago)
|
||||||
# link.authorized?.should be_false
|
link.authorizable?.should be_false
|
||||||
# end
|
end
|
||||||
#
|
|
||||||
# it "should not be #authorize! when the created_at date is too far in the past" do
|
end
|
||||||
# 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
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user