~singpolyma/sgx-jmp

8715e74fd43eb6236717bc2a36f8ce833623f898 — Osakpolor Obaseki 30 days ago 78c1b39
Remove expired payment methods from customer list of payment methods [with tests]
2 files changed, 53 insertions(+), 1 deletions(-)

M lib/payment_methods.rb
M test/test_payment_methods.rb
M lib/payment_methods.rb => lib/payment_methods.rb +2 -1
@@ 3,7 3,8 @@
class PaymentMethods
	def self.for(braintree_customer, badcards)
		methods = braintree_customer.payment_methods.reject { |m|
			badcards.include?(m.unique_number_identifier)
			badcards.include?(m.unique_number_identifier) ||
				(m.respond_to?(:expired?) && m.expired?)
		}
		if methods.empty?
			Empty.new

M test/test_payment_methods.rb => test/test_payment_methods.rb +51 -0
@@ 30,6 30,57 @@ class PaymentMethodsTest < Minitest::Test
		assert methods.empty?
	end

	def test_for_expired_cards
		braintree_customer = Minitest::Mock.new
		braintree_customer.expect(:payment_methods, [
			OpenStruct.new(
				card_type: "Test",
				last_4: "1234",
				unique_number_identifier: "abc",
				expired?: true
			)
		])

		methods = PaymentMethods.for(braintree_customer, [])
		assert_kind_of PaymentMethods, methods
		assert_equal 0, methods.to_a.length
		assert methods.empty?
	end

	def test_for_badcards_and_expired_cards
		braintree_customer = Minitest::Mock.new
		braintree_customer.expect(:payment_methods, [
			OpenStruct.new(
				card_type: "Test",
				last_4: "1234",
				unique_number_identifier: "abc",
				expired?: true
			)
		])

		methods = PaymentMethods.for(braintree_customer, ["abc"])
		assert_kind_of PaymentMethods, methods
		assert_equal 0, methods.to_a.length
		assert methods.empty?
	end

	def test_for_non_expired_cards
		braintree_customer = Minitest::Mock.new
		braintree_customer.expect(:payment_methods, [
			OpenStruct.new(
				card_type: "Test",
				last_4: "1234",
				unique_number_identifier: "abc",
				expired?: false
			)
		])

		methods = PaymentMethods.for(braintree_customer, [])
		assert_kind_of PaymentMethods, methods
		assert_equal 1, methods.to_a.length
		refute methods.empty?
	end

	def test_for_no_methods
		braintree_customer = Minitest::Mock.new
		braintree_customer.expect(:payment_methods, [])