Skip to content

Commit

Permalink
Merge pull request #37 from danrabinowitz/allow-custom-cookie-lifetime
Browse files Browse the repository at this point in the history
Allow custom cookie lifetime
  • Loading branch information
danrabinowitz authored Jun 7, 2018
2 parents 9e2d3ec + f11bd29 commit 0372d51
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/lockup/lockup_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def unlock
private

def set_cookie
cookies[:lockup] = { value: @codeword.to_s.downcase, expires: (Time.now + 5.years) }
cookies[:lockup] = { value: @codeword.to_s.downcase, expires: (Time.now + cookie_lifetime) }
end

def run_redirect
Expand Down
10 changes: 10 additions & 0 deletions lib/lockup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ def lockup_codeword
Rails.application.secrets.lockup_codeword.to_s.downcase
end
end

def cookie_lifetime
weeks = ENV['COOKIE_LIFETIME_IN_WEEKS'].to_f
seconds = (weeks * 1.week).to_i
if seconds > 0
seconds
else
5.years
end
end
end
26 changes: 26 additions & 0 deletions spec/controllers/lockup/lockup_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,30 @@
lambda { get 'unlock', format: 'text' }.should raise_error(ActionController::UnknownFormat)
end
end

describe "#cookie_lifetime" do
context "COOKIE_LIFETIME_IN_WEEKS is set to an integer" do
before { ENV['COOKIE_LIFETIME_IN_WEEKS'] = '52' }

it "returns the integer" do
controller.send(:cookie_lifetime).should eq(52.weeks)
end
end

context "COOKIE_LIFETIME_IN_WEEKS is not a valid integer" do
before { ENV['COOKIE_LIFETIME_IN_WEEKS'] = 'invalid value' }

it "returns the integer" do
controller.send(:cookie_lifetime).should eq(5.years)
end
end

context "COOKIE_LIFETIME_IN_WEEKS is not set" do
before { ENV.delete('COOKIE_LIFETIME_IN_WEEKS') }

it "returns the integer" do
controller.send(:cookie_lifetime).should eq(5.years)
end
end
end
end

0 comments on commit 0372d51

Please sign in to comment.