test_migrate_billing.rb

  1# frozen_string_literal: true
  2
  3require "minitest"
  4
  5require "test_helper"
  6
  7require "paypal_done"
  8require "migrate_billing"
  9
 10Command::COMMAND_MANAGER = Minitest::Mock.new
 11CustomerPlan::DB = Minitest::Mock.new
 12MigrateBilling::BLATHER = Minitest::Mock.new
 13
 14class TestMigrateBilling < Minitest::Test
 15	def setup
 16		@sgx = Minitest::Mock.new
 17	end
 18
 19	def test_write_registered
 20		execute_command do |exe|
 21			Command::COMMAND_MANAGER.expect(
 22				:write,
 23				EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
 24					iq.form.fields = [
 25						{ var: "activation_method", value: "credit_card" },
 26						{ var: "plan_name", value: "test_usd" }
 27					]
 28					iq.from = "test@example.com"
 29				}),
 30				[Matching.new { |iq|
 31					assert_equal :form, iq.form.type
 32					assert iq.form.instructions.include?("legacy PayPal")
 33				}]
 34			)
 35
 36			CustomerPlan::DB.expect(
 37				:exec_defer,
 38				nil,
 39				[
 40					String,
 41					Matching.new do |(customer_id, plan_name, parent_customer_id)|
 42						assert_equal "test", customer_id
 43						assert_equal "test_usd", plan_name
 44						assert_nil parent_customer_id
 45					end
 46				]
 47			)
 48
 49			Command::COMMAND_MANAGER.expect(
 50				:write,
 51				EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
 52					iq.action = :next
 53				}),
 54				[Matching.new { |iq|
 55					assert_equal :executing, iq.status
 56					assert_equal(
 57						"http://creditcard.example.com?&amount=1",
 58						OOB.find_or_create(iq.command).url
 59					)
 60				}]
 61			)
 62
 63			@sgx.expect(
 64				:registered?,
 65				OpenStruct.new(phone: "+15550000")
 66			)
 67
 68			exe.customer_repo.expect(
 69				:find,
 70				EMPromise.resolve(
 71					customer(
 72						plan_name: "test_usd",
 73						sgx: @sgx
 74					).with(balance: 100)
 75				),
 76				["test"]
 77			)
 78			# Apparently the constant assignment from
 79			# `RegistrationTest::InviteCodeTest` pollutes this
 80			# test
 81			Registration::Payment::MaybeBill::BillPlan.expect(
 82				:new,
 83				PaypalDone.new
 84			) { |*| true }
 85
 86			MigrateBilling::BLATHER.expect(
 87				:join,
 88				nil,
 89				[CONFIG[:notify_admin], "sgx-jmp"]
 90			)
 91			MigrateBilling::BLATHER.expect(
 92				:say,
 93				nil,
 94				[
 95					CONFIG[:notify_admin],
 96					"test migrated to USD",
 97					:groupchat
 98				]
 99			)
100
101			cust = customer(plan_name: "test_usd_old_billing", sgx: @sgx)
102			result = MigrateBilling.new(cust).write.catch { |e|
103				e
104			}.sync
105
106			assert_equal(result.stanza.note.text, PaypalDone::MESSAGE)
107			assert_mock Registration::Payment::MaybeBill::BillPlan
108			assert_mock Command::COMMAND_MANAGER
109			assert_mock @sgx
110		end
111	end
112	em :test_write_registered
113end