discount_code_test.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class DiscountCodeTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. fake('price_rules/102586120/discount_codes/1002091923', body: load_fixture('discount_code'))
  7. @discount_code = ShopifyAPI::DiscountCode.find(1002091923, params: { price_rule_id: 102586120 })
  8. end
  9. def test_get_discount_code
  10. fake('price_rules/102586120/discount_codes', method: :get, status: 200, body: load_fixture('discount_code'))
  11. discount_code = ShopifyAPI::DiscountCode.find(1002091923, params: { price_rule_id: 102586120 })
  12. assert_equal(1002091923, discount_code.id)
  13. end
  14. def test_get_all_discount_codes
  15. fake('price_rules/102586120/discount_codes', method: :get, status: 200, body: load_fixture('discount_codes'))
  16. discount_codes = ShopifyAPI::DiscountCode.all(params: { price_rule_id: 102586120 })
  17. assert_equal(1, discount_codes.length)
  18. assert_equal(1002091923, discount_codes.first.id)
  19. end
  20. def test_create_discount_code
  21. fake('price_rules/102586120/discount_codes', method: :post, status: 201, body: load_fixture('discount_code'))
  22. discount_code = ShopifyAPI::DiscountCode.new
  23. discount_code.prefix_options[:price_rule_id] = 102586120
  24. discount_code.code = "SUMMERSALE10"
  25. discount_code.save
  26. assert_equal('{"discount_code":{"code":"SUMMERSALE10"}}', WebMock.last_request.body)
  27. end
  28. def test_update_discount_code
  29. discount_code_response = ActiveSupport::JSON.decode(load_fixture('discount_code'))
  30. discount_code_response['discount_code']['code'] = "WINTERSALE50"
  31. @discount_code.code = "WINTERSALE50"
  32. fake(
  33. 'price_rules/102586120/discount_codes/1002091923',
  34. method: :put,
  35. status: 200,
  36. body: ActiveSupport::JSON.encode(discount_code_response)
  37. )
  38. @discount_code.save
  39. assert_equal(discount_code_response['discount_code']['code'], @discount_code.code)
  40. end
  41. def test_delete_discount_code
  42. fake('price_rules/102586120/discount_codes/1002091923', method: :delete, body: 'destroyed')
  43. assert(@discount_code.destroy)
  44. end
  45. end