session_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. require 'test_helper'
  2. require 'timecop'
  3. class SessionTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. ShopifyAPI::Session.secret = 'secret'
  7. end
  8. test "not be valid without a url" do
  9. session = ShopifyAPI::Session.new(domain: nil, token: "any-token", api_version: any_api_version)
  10. assert_not session.valid?
  11. end
  12. test "not be valid without token" do
  13. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  14. assert_not session.valid?
  15. end
  16. test "not be valid without an api version" do
  17. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: nil)
  18. assert_not session.valid?
  19. end
  20. test "be valid with any token, any url and version" do
  21. session = ShopifyAPI::Session.new(
  22. domain: "testshop.myshopify.com",
  23. token: "any-token",
  24. api_version: any_api_version
  25. )
  26. assert session.valid?
  27. end
  28. test "not raise error without params" do
  29. assert_nothing_raised do
  30. ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: any_api_version)
  31. end
  32. end
  33. test "ignore everything but the subdomain in the shop" do
  34. assert_equal(
  35. "https://testshop.myshopify.com",
  36. ShopifyAPI::Session.new(
  37. domain: "http://user:pass@testshop.notshopify.net/path",
  38. token: "any-token",
  39. api_version: any_api_version
  40. ).site
  41. )
  42. end
  43. test "append the myshopify domain if not given" do
  44. assert_equal(
  45. "https://testshop.myshopify.com",
  46. ShopifyAPI::Session.new(domain: "testshop", token: "any-token", api_version: any_api_version).site
  47. )
  48. end
  49. test "not raise error without params" do
  50. assert_nothing_raised do
  51. ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: any_api_version)
  52. end
  53. end
  54. test "raise error if params passed but signature omitted" do
  55. assert_raises(ShopifyAPI::ValidationException) do
  56. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  57. session.request_token({'code' => 'any-code'})
  58. end
  59. end
  60. test "setup api_key and secret for all sessions" do
  61. ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
  62. assert_equal "My test key", ShopifyAPI::Session.api_key
  63. assert_equal "My test secret", ShopifyAPI::Session.secret
  64. end
  65. test "#temp reset ShopifyAPI::Base.site to original value" do
  66. ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
  67. session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: :no_version)
  68. ShopifyAPI::Base.activate_session(session1)
  69. ShopifyAPI::Session.temp(domain: "testshop.myshopify.com", token: "any-token", api_version: :unstable) do
  70. @assigned_site = ShopifyAPI::Base.site
  71. @assigned_version = ShopifyAPI::Base.api_version
  72. end
  73. assert_equal('https://testshop.myshopify.com', @assigned_site.to_s)
  74. assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
  75. assert_equal(ShopifyAPI::ApiVersion::Unstable.new, @assigned_version)
  76. assert_equal(ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::Base.api_version)
  77. end
  78. test "create_permission_url returns correct url with single scope no redirect uri" do
  79. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  80. session = ShopifyAPI::Session.new(
  81. domain: 'http://localhost.myshopify.com',
  82. token: 'any-token',
  83. api_version: any_api_version
  84. )
  85. scope = ["write_products"]
  86. permission_url = session.create_permission_url(scope)
  87. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products", permission_url
  88. end
  89. test "create_permission_url returns correct url with single scope and redirect uri" do
  90. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  91. session = ShopifyAPI::Session.new(
  92. domain: 'http://localhost.myshopify.com',
  93. token: 'any-token',
  94. api_version: any_api_version
  95. )
  96. scope = ["write_products"]
  97. permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
  98. 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
  99. end
  100. test "create_permission_url returns correct url with dual scope no redirect uri" do
  101. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  102. session = ShopifyAPI::Session.new(
  103. domain: 'http://localhost.myshopify.com',
  104. token: 'any-token',
  105. api_version: any_api_version
  106. )
  107. scope = ["write_products","write_customers"]
  108. permission_url = session.create_permission_url(scope)
  109. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers", permission_url
  110. end
  111. test "create_permission_url returns correct url with no scope no redirect uri" do
  112. ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
  113. session = ShopifyAPI::Session.new(
  114. domain: 'http://localhost.myshopify.com',
  115. token: 'any-token',
  116. api_version: any_api_version
  117. )
  118. scope = []
  119. permission_url = session.create_permission_url(scope)
  120. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=", permission_url
  121. end
  122. test "raise exception if code invalid in request token" do
  123. ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
  124. session = ShopifyAPI::Session.new(
  125. domain: 'http://localhost.myshopify.com',
  126. token: nil,
  127. api_version: any_api_version
  128. )
  129. fake(
  130. nil,
  131. url: 'https://localhost.myshopify.com/admin/oauth/access_token',
  132. method: :post,
  133. status: 404,
  134. body: '{"error" : "invalid_request"}'
  135. )
  136. assert_raises(ShopifyAPI::ValidationException) do
  137. session.request_token(code: "bad-code")
  138. end
  139. assert_equal false, session.valid?
  140. end
  141. test "return site for session" do
  142. session = ShopifyAPI::Session.new(
  143. domain: "testshop.myshopify.com",
  144. token: "any-token",
  145. api_version: any_api_version
  146. )
  147. assert_equal "https://testshop.myshopify.com", session.site
  148. end
  149. test "return_token_if_signature_is_valid" do
  150. api_version = any_api_version
  151. fake nil,
  152. url: "https://testshop.myshopify.com/admin/oauth/access_token",
  153. method: :post,
  154. body: '{"access_token":"any-token"}'
  155. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: api_version)
  156. params = { code: 'any-code', timestamp: Time.now }
  157. token = session.request_token(params.merge(hmac: generate_signature(params)))
  158. assert_equal "any-token", token
  159. assert_equal "any-token", session.token
  160. end
  161. test "extra parameters are stored in session" do
  162. api_version = ShopifyAPI::ApiVersion::Unstable.new
  163. fake nil,
  164. url: "https://testshop.myshopify.com/admin/oauth/access_token",
  165. method: :post,
  166. body: '{"access_token":"any-token","foo":"example"}'
  167. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: api_version)
  168. params = { code: 'any-code', timestamp: Time.now }
  169. assert session.request_token(params.merge(hmac: generate_signature(params)))
  170. assert_equal ({ "foo" => "example" }), session.extra
  171. end
  172. test "expires_in is automatically converted in expires_at" do
  173. api_version = any_api_version
  174. fake nil,
  175. url: "https://testshop.myshopify.com/admin/oauth/access_token",
  176. method: :post,
  177. body: '{"access_token":"any-token","expires_in":86393}'
  178. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: api_version)
  179. Timecop.freeze do
  180. params = { code: 'any-code', timestamp: Time.now }
  181. assert session.request_token(params.merge(hmac: generate_signature(params)))
  182. expires_at = Time.now.utc + 86393
  183. assert_equal ({ "expires_at" => expires_at.to_i }), session.extra
  184. assert session.expires_at.is_a?(Time)
  185. assert_equal expires_at.to_i, session.expires_at.to_i
  186. assert_equal 86393, session.expires_in
  187. assert_equal false, session.expired?
  188. Timecop.travel(session.expires_at) do
  189. assert_equal true, session.expired?
  190. end
  191. end
  192. end
  193. test "raise error if signature does not match expected" do
  194. params = {:code => "any-code", :timestamp => Time.now}
  195. signature = generate_signature(params)
  196. params[:foo] = 'world'
  197. assert_raises(ShopifyAPI::ValidationException) do
  198. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  199. session.request_token(params.merge(:hmac => signature))
  200. end
  201. end
  202. test "raise error if timestamp is too old" do
  203. params = {:code => "any-code", :timestamp => Time.now - 2.days}
  204. signature = generate_signature(params)
  205. params[:foo] = 'world'
  206. assert_raises(ShopifyAPI::ValidationException) do
  207. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  208. session.request_token(params.merge(:hmac => signature))
  209. end
  210. end
  211. test "return true when the signature is valid and the keys of params are strings" do
  212. params = { 'code' => 'any-code', 'timestamp' => Time.now }
  213. params[:hmac] = generate_signature(params)
  214. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  215. end
  216. test "return true when validating signature of params with ampersand and equal sign characters" do
  217. ShopifyAPI::Session.secret = 'secret'
  218. params = { 'a' => '1&b=2', 'c=3&d' => '4' }
  219. to_sign = 'a=1%26b=2&c%3D3%26d=4'
  220. params[:hmac] = generate_signature(to_sign)
  221. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  222. end
  223. test "return true when validating signature of params with percent sign characters" do
  224. ShopifyAPI::Session.secret = 'secret'
  225. params = { 'a%3D1%26b' => '2%26c%3D3' }
  226. to_sign = 'a%253D1%2526b=2%2526c%253D3'
  227. params[:hmac] = generate_signature(to_sign)
  228. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  229. end
  230. test "url is aliased to domain to minimize the upgrade changes" do
  231. session = ShopifyAPI::Session.new(
  232. domain: "http://testshop.myshopify.com",
  233. token: "any-token",
  234. api_version: any_api_version
  235. )
  236. assert_equal('testshop.myshopify.com', session.url)
  237. end
  238. private
  239. def make_sorted_params(params)
  240. params.with_indifferent_access.except(
  241. :signature, :hmac, :action, :controller
  242. ).collect { |k, v| "#{k}=#{v}" }.sort.join('&')
  243. end
  244. def generate_signature(params)
  245. params = make_sorted_params(params) if params.is_a?(Hash)
  246. OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, ShopifyAPI::Session.secret, params)
  247. end
  248. def any_api_version
  249. [ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::ApiVersion::Unstable.new].sample(1).first
  250. end
  251. end