Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid depending on ActiveSupport #818

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/shopify_api/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ValidationException < StandardError
end

class Session
SECONDS_IN_A_DAY = 24 * 60 * 60

cattr_accessor :api_key, :secret, :myshopify_domain
self.myshopify_domain = 'myshopify.com'

Expand Down Expand Up @@ -106,7 +108,8 @@ def create_permission_url(scope, redirect_uri, options = {})
def request_token(params)
return token if token

unless self.class.validate_signature(params) && params[:timestamp].to_i > 24.hours.ago.utc.to_i
twenty_four_hours_ago = Time.now.utc.to_i - SECONDS_IN_A_DAY
unless self.class.validate_signature(params) && params[:timestamp].to_i > twenty_four_hours_ago
raise ShopifyAPI::ValidationException, "Invalid Signature: Possible malicious login"
end

Expand Down
4 changes: 3 additions & 1 deletion test/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'timecop'

class SessionTest < Test::Unit::TestCase
SECONDS_IN_A_DAY = 24 * 60 * 60

def setup
super
ShopifyAPI::Session.secret = 'secret'
Expand Down Expand Up @@ -373,7 +375,7 @@ def setup
end

test "raise error if timestamp is too old" do
params = { code: "any-code", timestamp: Time.now - 2.days }
params = { code: "any-code", timestamp: Time.now - 2 * SECONDS_IN_A_DAY }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely nitpicky, but would it make sense to use Session.SECONDS_IN_A_DAY here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple of reasons why I usually avoid referencing a implementation's constants in the tests:

  • If the constant is wrong in the implementation, and the test references that, then you lose out on the 'double-entry bookkeeping' safeguard of writing a test.
  • If I choose to rename the constant, or move it somewhere else, the tests shouldn't break since the implementation hasn't changed.

signature = generate_signature(params)
params[:foo] = 'world'
assert_raises(ShopifyAPI::ValidationException) do
Expand Down