session_test.rb 18 KB

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