1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "buy_account_credit_form"
  5require "customer"
  6require "credit_card_sale"
  7
  8CustomerFinancials::BRAINTREE = Minitest::Mock.new
  9CustomerFinancials::REDIS = Minitest::Mock.new
 10TrustLevelRepo::REDIS = Minitest::Mock.new
 11TrustLevelRepo::DB = Minitest::Mock.new
 12
 13class BuyAccountCreditFormTest < Minitest::Test
 14	def setup
 15		@payment_method = OpenStruct.new(card_type: "Test", last_4: "1234")
 16		@max_top_up_amount = 130
 17		@form = BuyAccountCreditForm.new(
 18			BigDecimal("15.1234"),
 19			PaymentMethods.new([@payment_method]),
 20			@max_top_up_amount
 21		)
 22	end
 23
 24	def test_for
 25		braintree_customer = Minitest::Mock.new
 26		CustomerFinancials::BRAINTREE.expect(:customer, braintree_customer)
 27		CustomerFinancials::REDIS.expect(:smembers, [], ["block_credit_cards"])
 28		braintree_customer.expect(
 29			:find,
 30			EMPromise.resolve(OpenStruct.new(payment_methods: [])),
 31			["test"]
 32		)
 33
 34		TrustLevelRepo::REDIS.expect(
 35			:get,
 36			EMPromise.resolve("Customer"),
 37			["jmp_customer_trust_level-test"]
 38		)
 39		TrustLevelRepo::DB.expect(
 40			:query_one,
 41			EMPromise.resolve({}),
 42			[String, "test"], default: {}
 43		)
 44
 45		assert_kind_of(
 46			BuyAccountCreditForm,
 47			BuyAccountCreditForm.for(customer).sync
 48		)
 49
 50		assert_mock TrustLevelRepo::REDIS
 51		assert_mock TrustLevelRepo::DB
 52	end
 53	em :test_for
 54
 55	def test_form
 56		iq_form = @form.form
 57		assert_equal :form, iq_form.type
 58		assert_equal "Buy Account Credit", iq_form.title
 59		assert_equal(
 60			[
 61				Blather::Stanza::X::Field.new(
 62					type: "fixed",
 63					label: "Current balance",
 64					value: "$15.12"
 65				),
 66				Blather::Stanza::X::Field.new(
 67					type: "list-single",
 68					var: "payment_method",
 69					label: "Credit card to pay with",
 70					required: true,
 71					options: [{ label: "Test 1234", value: "0" }]
 72				),
 73				Blather::Stanza::X::Field.new(
 74					var: "amount",
 75					label: "Amount of credit to buy",
 76					required: true
 77				)
 78			],
 79			iq_form.fields
 80		)
 81	end
 82
 83	def test_parse_amount
 84		iq_form = Blather::Stanza::X.new
 85		iq_form.fields = [{ var: "amount", value: "123" }]
 86		assert_equal "123", @form.parse(iq_form)[:amount]
 87	end
 88
 89	def test_parse_payment_method
 90		iq_form = Blather::Stanza::X.new
 91		iq_form.fields = [
 92			{ var: "payment_method", value: "0" },
 93			{ var: "amount", value: "15" }
 94		]
 95		assert_equal @payment_method, @form.parse(iq_form)[:payment_method]
 96	end
 97
 98	def test_parse_amount_too_low
 99		iq_form = Blather::Stanza::X.new
100		iq_form.fields = [
101			{ var: "payment_method", value: "0" },
102			{ var: "amount", value: "10" }
103		]
104		assert_raises(CreditCardSale::TooLowError) do
105			@form.parse(iq_form)
106		end
107	end
108
109	def test_parse_amount_too_high
110		iq_form = Blather::Stanza::X.new
111		iq_form.fields = [
112			{ var: "payment_method", value: "0" },
113			{ var: "amount", value: "200" }
114		]
115		assert_raises(CreditCardSale::TooHighError) do
116			@form.parse(iq_form)
117		end
118	end
119end