session_test.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. require 'test_helper'
  2. class SessionTest < Test::Unit::TestCase
  3. context "Session" do
  4. should "not be valid without a url" do
  5. session = ShopifyAPI::Session.new(nil, "any-token")
  6. assert_not session.valid?
  7. end
  8. should "not be valid without token" do
  9. session = ShopifyAPI::Session.new("testshop.myshopify.com")
  10. assert_not session.valid?
  11. end
  12. should "be valid with any token and any url" do
  13. session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
  14. assert session.valid?
  15. end
  16. should "ignore everything but the subdomain in the shop" do
  17. assert_equal "https://testshop.myshopify.com/admin", ShopifyAPI::Session.new("http://user:pass@testshop.notshopify.net/path", "any-token").site
  18. end
  19. should "append the myshopify domain if not given" do
  20. assert_equal "https://testshop.myshopify.com/admin", ShopifyAPI::Session.new("testshop", "any-token").site
  21. end
  22. should "not raise error without params" do
  23. assert_nothing_raised do
  24. session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
  25. end
  26. end
  27. should "raise error if params passed but signature omitted" do
  28. assert_raises(ShopifyAPI::ValidationException) do
  29. session = ShopifyAPI::Session.new("testshop.myshopify.com")
  30. session.request_token({'code' => 'any-code'})
  31. end
  32. end
  33. should "setup api_key and secret for all sessions" do
  34. ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
  35. assert_equal "My test key", ShopifyAPI::Session.api_key
  36. assert_equal "My test secret", ShopifyAPI::Session.secret
  37. end
  38. should "use 'https' protocol by default for all sessions" do
  39. assert_equal 'https', ShopifyAPI::Session.protocol
  40. end
  41. should "#temp reset ShopifyAPI::Base.site to original value" do
  42. ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
  43. session1 = ShopifyAPI::Session.new('fakeshop.myshopify.com', 'token1')
  44. ShopifyAPI::Base.activate_session(session1)
  45. ShopifyAPI::Session.temp("testshop.myshopify.com", "any-token") {
  46. @assigned_site = ShopifyAPI::Base.site
  47. }
  48. assert_equal 'https://testshop.myshopify.com/admin', @assigned_site.to_s
  49. assert_equal 'https://fakeshop.myshopify.com/admin', ShopifyAPI::Base.site.to_s
  50. end
  51. should "create_permission_url returns correct url with single scope no redirect uri" do
  52. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  53. session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
  54. scope = ["write_products"]
  55. permission_url = session.create_permission_url(scope)
  56. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products", permission_url
  57. end
  58. should "create_permission_url returns correct url with single scope and redirect uri" do
  59. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  60. session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
  61. scope = ["write_products"]
  62. permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
  63. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products&redirect_uri=http://my_redirect_uri.com", permission_url
  64. end
  65. should "create_permission_url returns correct url with dual scope no redirect uri" do
  66. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  67. session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
  68. scope = ["write_products","write_customers"]
  69. permission_url = session.create_permission_url(scope)
  70. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers", permission_url
  71. end
  72. should "create_permission_url returns correct url with no scope no redirect uri" do
  73. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  74. session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
  75. scope = []
  76. permission_url = session.create_permission_url(scope)
  77. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=", permission_url
  78. end
  79. should "raise exception if code invalid in request token" do
  80. ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
  81. session = ShopifyAPI::Session.new('http://localhost.myshopify.com')
  82. fake nil, :url => 'https://localhost.myshopify.com/admin/oauth/access_token',:method => :post, :status => 404, :body => '{"error" : "invalid_request"}'
  83. assert_raises(ShopifyAPI::ValidationException) do
  84. session.request_token(params={:code => "bad-code"})
  85. end
  86. assert_equal false, session.valid?
  87. end
  88. should "myshopify_domain supports non-standard ports" do
  89. begin
  90. ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret", :myshopify_domain => 'localhost', port: '3000')
  91. session = ShopifyAPI::Session.new('fakeshop.localhost:3000', 'token1')
  92. ShopifyAPI::Base.activate_session(session)
  93. assert_equal 'https://fakeshop.localhost:3000/admin', ShopifyAPI::Base.site.to_s
  94. session = ShopifyAPI::Session.new('fakeshop', 'token1')
  95. ShopifyAPI::Base.activate_session(session)
  96. assert_equal 'https://fakeshop.localhost:3000/admin', ShopifyAPI::Base.site.to_s
  97. ensure
  98. ShopifyAPI::Session.myshopify_domain = "myshopify.com"
  99. ShopifyAPI::Session.port = nil
  100. end
  101. end
  102. should "return site for session" do
  103. session = ShopifyAPI::Session.new("testshop.myshopify.com", "any-token")
  104. assert_equal "https://testshop.myshopify.com/admin", session.site
  105. end
  106. should "return_token_if_signature_is_valid" do
  107. ShopifyAPI::Session.secret = 'secret'
  108. params = {:code => 'any-code', :timestamp => Time.now}
  109. sorted_params = make_sorted_params(params)
  110. signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new(), ShopifyAPI::Session.secret, sorted_params)
  111. fake nil, :url => 'https://testshop.myshopify.com/admin/oauth/access_token',:method => :post, :body => '{"access_token" : "any-token"}'
  112. session = ShopifyAPI::Session.new("testshop.myshopify.com")
  113. token = session.request_token(params.merge(:hmac => signature))
  114. assert_equal "any-token", token
  115. end
  116. should "raise error if signature does not match expected" do
  117. ShopifyAPI::Session.secret = 'secret'
  118. params = {:code => "any-code", :timestamp => Time.now}
  119. sorted_params = make_sorted_params(params)
  120. signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new(), ShopifyAPI::Session.secret, sorted_params)
  121. params[:foo] = 'world'
  122. assert_raises(ShopifyAPI::ValidationException) do
  123. session = ShopifyAPI::Session.new("testshop.myshopify.com")
  124. session.request_token(params.merge(:hmac => signature))
  125. end
  126. end
  127. should "raise error if timestamp is too old" do
  128. ShopifyAPI::Session.secret = 'secret'
  129. params = {:code => "any-code", :timestamp => Time.now - 2.days}
  130. sorted_params = make_sorted_params(params)
  131. signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new(), ShopifyAPI::Session.secret, sorted_params)
  132. params[:foo] = 'world'
  133. assert_raises(ShopifyAPI::ValidationException) do
  134. session = ShopifyAPI::Session.new("testshop.myshopify.com")
  135. session.request_token(params.merge(:hmac => signature))
  136. end
  137. end
  138. should "return true when the signature is valid and the keys of params are strings" do
  139. now = Time.now
  140. params = {"code" => "any-code", "timestamp" => now}
  141. sorted_params = make_sorted_params(params)
  142. signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new(), ShopifyAPI::Session.secret, sorted_params)
  143. params = {"code" => "any-code", "timestamp" => now, "hmac" => signature}
  144. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  145. end
  146. should "return true when validating signature of params with ampersand and equal sign characters" do
  147. ShopifyAPI::Session.secret = 'secret'
  148. params = {'a' => '1&b=2', 'c=3&d' => '4'}
  149. to_sign = "a=1%26b=2&c%3D3%26d=4"
  150. params['hmac'] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), ShopifyAPI::Session.secret, to_sign)
  151. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  152. end
  153. private
  154. def make_sorted_params(params)
  155. sorted_params = params.with_indifferent_access.except(:signature, :hmac, :action, :controller).collect{|k,v|"#{k}=#{v}"}.sort.join('&')
  156. end
  157. end
  158. end