session_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: ShopifyAPI::ApiVersion::NullVersion)
  20. assert_not session.valid?
  21. end
  22. test "be valid with any token, any url and version" do
  23. session = ShopifyAPI::Session.new(
  24. domain: "testshop.myshopify.com",
  25. token: "any-token",
  26. api_version: any_api_version
  27. )
  28. assert session.valid?
  29. end
  30. test "not raise error without params" do
  31. assert_nothing_raised do
  32. ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: any_api_version)
  33. end
  34. end
  35. test "ignore everything but the subdomain in the shop" do
  36. assert_equal(
  37. "https://testshop.myshopify.com",
  38. ShopifyAPI::Session.new(
  39. domain: "http://user:pass@testshop.notshopify.net/path",
  40. token: "any-token",
  41. api_version: any_api_version
  42. ).site
  43. )
  44. end
  45. test "append the myshopify domain if not given" do
  46. assert_equal(
  47. "https://testshop.myshopify.com",
  48. ShopifyAPI::Session.new(domain: "testshop", token: "any-token", api_version: any_api_version).site
  49. )
  50. end
  51. test "not raise error without params" do
  52. assert_nothing_raised do
  53. ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: "any-token", api_version: any_api_version)
  54. end
  55. end
  56. test "raise error if params passed but signature omitted" do
  57. assert_raises(ShopifyAPI::ValidationException) do
  58. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  59. session.request_token({'code' => 'any-code'})
  60. end
  61. end
  62. test "setup api_key and secret for all sessions" do
  63. ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
  64. assert_equal "My test key", ShopifyAPI::Session.api_key
  65. assert_equal "My test secret", ShopifyAPI::Session.secret
  66. end
  67. test "#temp reset ShopifyAPI::Base.site to original value" do
  68. session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: '2019-01')
  69. ShopifyAPI::Base.activate_session(session1)
  70. ShopifyAPI::Session.temp(domain: "testshop.myshopify.com", token: "any-token", api_version: :unstable) do
  71. @assigned_site = ShopifyAPI::Base.site
  72. @assigned_version = ShopifyAPI::Base.api_version
  73. end
  74. assert_equal('https://testshop.myshopify.com', @assigned_site.to_s)
  75. assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
  76. assert_equal(ShopifyAPI::ApiVersion.new(handle: :unstable), @assigned_version)
  77. assert_equal(ShopifyAPI::ApiVersion.new(handle: '2019-01'), ShopifyAPI::Base.api_version)
  78. end
  79. test "#with_session activates the session for the duration of the block" do
  80. session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: '2019-01')
  81. ShopifyAPI::Base.activate_session(session1)
  82. other_session = ShopifyAPI::Session.new(
  83. domain: "testshop.myshopify.com",
  84. token: "any-token",
  85. api_version: :unstable
  86. )
  87. ShopifyAPI::Session.with_session(other_session) do
  88. @assigned_site = ShopifyAPI::Base.site
  89. @assigned_version = ShopifyAPI::Base.api_version
  90. end
  91. assert_equal('https://testshop.myshopify.com', @assigned_site.to_s)
  92. assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
  93. assert_equal(ShopifyAPI::ApiVersion.new(handle: :unstable), @assigned_version)
  94. assert_equal(ShopifyAPI::ApiVersion.new(handle: '2019-01'), ShopifyAPI::Base.api_version)
  95. end
  96. test "#with_session resets the activated session even if there an exception during the block" do
  97. session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: '2019-01')
  98. ShopifyAPI::Base.activate_session(session1)
  99. other_session = ShopifyAPI::Session.new(
  100. domain: "testshop.myshopify.com",
  101. token: "any-token",
  102. api_version: :unstable
  103. )
  104. assert_raises StandardError do
  105. ShopifyAPI::Session.with_session(other_session) { raise StandardError, "" }
  106. end
  107. assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
  108. assert_equal(ShopifyAPI::ApiVersion.new(handle: '2019-01'), ShopifyAPI::Base.api_version)
  109. end
  110. test "#with_version will adjust the actvated api version for the duration of the block" do
  111. session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: '2019-01')
  112. ShopifyAPI::Base.activate_session(session1)
  113. ShopifyAPI::Session.with_version(:unstable) do
  114. @assigned_site = ShopifyAPI::Base.site
  115. @assigned_version = ShopifyAPI::Base.api_version
  116. end
  117. assert_equal('https://fakeshop.myshopify.com', @assigned_site.to_s)
  118. assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
  119. assert_equal(ShopifyAPI::ApiVersion.new(handle: :unstable), @assigned_version)
  120. assert_equal(ShopifyAPI::ApiVersion.new(handle: '2019-01'), ShopifyAPI::Base.api_version)
  121. end
  122. test "create_permission_url requires redirect_uri" 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: 'any-token',
  127. api_version: any_api_version
  128. )
  129. scope = ["write_products"]
  130. assert_raises(ArgumentError) do
  131. session.create_permission_url(scope)
  132. end
  133. end
  134. test "create_permission_url returns correct url with single scope and redirect uri" do
  135. ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
  136. session = ShopifyAPI::Session.new(
  137. domain: 'http://localhost.myshopify.com',
  138. token: 'any-token',
  139. api_version: any_api_version
  140. )
  141. scope = ["write_products"]
  142. permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
  143. 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
  144. end
  145. test "create_permission_url returns correct url with dual scope" do
  146. ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
  147. session = ShopifyAPI::Session.new(
  148. domain: 'http://localhost.myshopify.com',
  149. token: 'any-token',
  150. api_version: any_api_version
  151. )
  152. scope = ["write_products","write_customers"]
  153. permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
  154. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=write_products,write_customers&redirect_uri=http://my_redirect_uri.com", permission_url
  155. end
  156. test "create_permission_url returns correct url with no scope" do
  157. ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
  158. session = ShopifyAPI::Session.new(
  159. domain: 'http://localhost.myshopify.com',
  160. token: 'any-token',
  161. api_version: any_api_version
  162. )
  163. scope = []
  164. permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com")
  165. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=&redirect_uri=http://my_redirect_uri.com", permission_url
  166. end
  167. test "create_permission_url returns correct url with state" do
  168. ShopifyAPI::Session.setup(api_key: "My_test_key", secret: "My test secret")
  169. session = ShopifyAPI::Session.new(
  170. domain: 'http://localhost.myshopify.com',
  171. token: 'any-token',
  172. api_version: any_api_version
  173. )
  174. scope = []
  175. permission_url = session.create_permission_url(scope, "http://my_redirect_uri.com", state: "My nonce")
  176. assert_equal "https://localhost.myshopify.com/admin/oauth/authorize?client_id=My_test_key&scope=&redirect_uri=http://my_redirect_uri.com&state=My%20nonce", permission_url
  177. end
  178. test "raise exception if code invalid in request token" do
  179. ShopifyAPI::Session.setup(:api_key => "My test key", :secret => "My test secret")
  180. session = ShopifyAPI::Session.new(
  181. domain: 'http://localhost.myshopify.com',
  182. token: nil,
  183. api_version: any_api_version
  184. )
  185. fake(
  186. nil,
  187. url: 'https://localhost.myshopify.com/admin/oauth/access_token',
  188. method: :post,
  189. status: 404,
  190. body: '{"error" : "invalid_request"}'
  191. )
  192. assert_raises(ShopifyAPI::ValidationException) do
  193. session.request_token(code: "bad-code")
  194. end
  195. assert_equal false, session.valid?
  196. end
  197. test "return site for session" do
  198. session = ShopifyAPI::Session.new(
  199. domain: "testshop.myshopify.com",
  200. token: "any-token",
  201. api_version: any_api_version
  202. )
  203. assert_equal "https://testshop.myshopify.com", session.site
  204. end
  205. test "return_token_if_signature_is_valid" do
  206. api_version = any_api_version
  207. fake nil,
  208. url: "https://testshop.myshopify.com/admin/oauth/access_token",
  209. method: :post,
  210. body: '{"access_token":"any-token"}'
  211. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: api_version)
  212. params = { code: 'any-code', timestamp: Time.now }
  213. token = session.request_token(params.merge(hmac: generate_signature(params)))
  214. assert_equal "any-token", token
  215. assert_equal "any-token", session.token
  216. end
  217. test "extra parameters are stored in session" do
  218. api_version = ShopifyAPI::ApiVersion.new(handle: :unstable)
  219. fake nil,
  220. url: "https://testshop.myshopify.com/admin/oauth/access_token",
  221. method: :post,
  222. body: '{"access_token":"any-token","foo":"example"}'
  223. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: api_version)
  224. params = { code: 'any-code', timestamp: Time.now }
  225. assert session.request_token(params.merge(hmac: generate_signature(params)))
  226. assert_equal ({ "foo" => "example" }), session.extra
  227. end
  228. test "expires_in is automatically converted in expires_at" do
  229. api_version = any_api_version
  230. fake nil,
  231. url: "https://testshop.myshopify.com/admin/oauth/access_token",
  232. method: :post,
  233. body: '{"access_token":"any-token","expires_in":86393}'
  234. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: api_version)
  235. Timecop.freeze do
  236. params = { code: 'any-code', timestamp: Time.now }
  237. assert session.request_token(params.merge(hmac: generate_signature(params)))
  238. expires_at = Time.now.utc + 86393
  239. assert_equal ({ "expires_at" => expires_at.to_i }), session.extra
  240. assert session.expires_at.is_a?(Time)
  241. assert_equal expires_at.to_i, session.expires_at.to_i
  242. assert_equal 86393, session.expires_in
  243. assert_equal false, session.expired?
  244. Timecop.travel(session.expires_at) do
  245. assert_equal true, session.expired?
  246. end
  247. end
  248. end
  249. test "raise error if signature does not match expected" do
  250. params = {:code => "any-code", :timestamp => Time.now}
  251. signature = generate_signature(params)
  252. params[:foo] = 'world'
  253. assert_raises(ShopifyAPI::ValidationException) do
  254. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  255. session.request_token(params.merge(:hmac => signature))
  256. end
  257. end
  258. test "raise error if timestamp is too old" do
  259. params = {:code => "any-code", :timestamp => Time.now - 2.days}
  260. signature = generate_signature(params)
  261. params[:foo] = 'world'
  262. assert_raises(ShopifyAPI::ValidationException) do
  263. session = ShopifyAPI::Session.new(domain: "testshop.myshopify.com", token: nil, api_version: any_api_version)
  264. session.request_token(params.merge(:hmac => signature))
  265. end
  266. end
  267. test "return true when the signature is valid and the keys of params are strings" do
  268. params = { 'code' => 'any-code', 'timestamp' => Time.now }
  269. params[:hmac] = generate_signature(params)
  270. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  271. end
  272. test "return true when validating signature of params with ampersand and equal sign characters" do
  273. ShopifyAPI::Session.secret = 'secret'
  274. params = { 'a' => '1&b=2', 'c=3&d' => '4' }
  275. to_sign = 'a=1%26b=2&c%3D3%26d=4'
  276. params[:hmac] = generate_signature(to_sign)
  277. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  278. end
  279. test "return true when validating signature of params with percent sign characters" do
  280. ShopifyAPI::Session.secret = 'secret'
  281. params = { 'a%3D1%26b' => '2%26c%3D3' }
  282. to_sign = 'a%253D1%2526b=2%2526c%253D3'
  283. params[:hmac] = generate_signature(to_sign)
  284. assert_equal true, ShopifyAPI::Session.validate_signature(params)
  285. end
  286. test "url is aliased to domain to minimize the upgrade changes" do
  287. session = ShopifyAPI::Session.new(
  288. domain: "http://testshop.myshopify.com",
  289. token: "any-token",
  290. api_version: any_api_version
  291. )
  292. assert_equal('testshop.myshopify.com', session.url)
  293. end
  294. test "#hash returns the same value for equal Sessions" do
  295. session = ShopifyAPI::Session.new(
  296. domain: "http://testshop.myshopify.com",
  297. token: "any-token",
  298. api_version: '2019-01',
  299. extra: { foo: "bar" }
  300. )
  301. other_session = ShopifyAPI::Session.new(
  302. domain: "http://testshop.myshopify.com",
  303. token: "any-token",
  304. api_version: '2019-01',
  305. extra: { foo: "bar" }
  306. )
  307. assert_equal(session.hash, other_session.hash)
  308. end
  309. test "equality verifies domain" do
  310. session = ShopifyAPI::Session.new(
  311. domain: "http://testshop.myshopify.com",
  312. token: "any-token",
  313. api_version: '2019-01',
  314. extra: { foo: "bar" }
  315. )
  316. other_session = ShopifyAPI::Session.new(
  317. domain: "http://testshop.myshopify.com",
  318. token: "any-token",
  319. api_version: '2019-01',
  320. extra: { foo: "bar" }
  321. )
  322. different_session = ShopifyAPI::Session.new(
  323. domain: "http://another_testshop.myshopify.com",
  324. token: "any-token",
  325. api_version: '2019-01',
  326. extra: { foo: "bar" }
  327. )
  328. assert_equal(session, other_session)
  329. refute_equal(session, different_session)
  330. end
  331. test "equality verifies token" do
  332. session = ShopifyAPI::Session.new(
  333. domain: "http://testshop.myshopify.com",
  334. token: "any-token",
  335. api_version: '2019-01',
  336. extra: { foo: "bar" }
  337. )
  338. different_session = ShopifyAPI::Session.new(
  339. domain: "http://testshop.myshopify.com",
  340. token: "very-different-token",
  341. api_version: '2019-01',
  342. extra: { foo: "bar" }
  343. )
  344. refute_equal(session, different_session)
  345. end
  346. test "equality verifies api_version" do
  347. session = ShopifyAPI::Session.new(
  348. domain: "http://testshop.myshopify.com",
  349. token: "any-token",
  350. api_version: '2019-01',
  351. extra: { foo: "bar" }
  352. )
  353. different_session = ShopifyAPI::Session.new(
  354. domain: "http://testshop.myshopify.com",
  355. token: "any-token",
  356. api_version: :unstable,
  357. extra: { foo: "bar" }
  358. )
  359. refute_equal(session, different_session)
  360. end
  361. test "equality verifies extra" do
  362. session = ShopifyAPI::Session.new(
  363. domain: "http://testshop.myshopify.com",
  364. token: "any-token",
  365. api_version: '2019-01',
  366. extra: { foo: "bar" }
  367. )
  368. different_session = ShopifyAPI::Session.new(
  369. domain: "http://testshop.myshopify.com",
  370. token: "any-token",
  371. api_version: '2019-01',
  372. extra: { bar: "other-bar" }
  373. )
  374. refute_equal(session, different_session)
  375. end
  376. test "equality verifies other is a Session" do
  377. session = ShopifyAPI::Session.new(
  378. domain: "http://testshop.myshopify.com",
  379. token: "any-token",
  380. api_version: '2019-01',
  381. extra: { foo: "bar" }
  382. )
  383. different_session = nil
  384. refute_equal(session, different_session)
  385. end
  386. test "#eql? and #hash are implemented" do
  387. session = ShopifyAPI::Session.new(
  388. domain: "http://testshop.myshopify.com",
  389. token: "any-token",
  390. api_version: '2019-01',
  391. extra: { foo: "bar" }
  392. )
  393. other_session = ShopifyAPI::Session.new(
  394. domain: "http://testshop.myshopify.com",
  395. token: "any-token",
  396. api_version: '2019-01',
  397. extra: { foo: "bar" }
  398. )
  399. different_session = ShopifyAPI::Session.new(
  400. domain: "http://another_testshop.myshopify.com",
  401. token: "any-token",
  402. api_version: '2019-01',
  403. extra: { foo: "bar" }
  404. )
  405. assert_equal([session, different_session], [session, other_session, different_session].uniq)
  406. end
  407. private
  408. def make_sorted_params(params)
  409. params.with_indifferent_access.except(
  410. :signature, :hmac, :action, :controller
  411. ).collect { |k, v| "#{k}=#{v}" }.sort.join('&')
  412. end
  413. def generate_signature(params)
  414. params = make_sorted_params(params) if params.is_a?(Hash)
  415. OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, ShopifyAPI::Session.secret, params)
  416. end
  417. def any_api_version
  418. version_name = ['2019-01', :unstable].sample(1).first
  419. ShopifyAPI::ApiVersion.find_version(version_name)
  420. end
  421. end