test_registration.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "registration"
  5
  6class RegistrationTest < Minitest::Test
  7	Customer::IQ_MANAGER = Minitest::Mock.new
  8
  9	def test_for_activated
 10		skip "Registration#for activated not implemented yet"
 11		iq = Blather::Stanza::Iq::Command.new
 12		Registration.for(iq, Customer.new("test"), Minitest::Mock.new).sync
 13	end
 14	em :test_for_activated
 15
 16	def test_for_not_activated_with_customer_id
 17		Customer::IQ_MANAGER.expect(
 18			:write,
 19			EMPromise.resolve(nil),
 20			[Blather::Stanza::Iq]
 21		)
 22		web_manager = WebRegisterManager.new
 23		web_manager["test@example.com"] = "+15555550000"
 24		iq = Blather::Stanza::Iq::Command.new
 25		iq.from = "test@example.com"
 26		result = Registration.for(
 27			iq,
 28			Customer.new("test"),
 29			web_manager
 30		).sync
 31		assert_kind_of Registration::Activation, result
 32	end
 33	em :test_for_not_activated_with_customer_id
 34
 35	def test_for_not_activated_without_customer_id
 36		skip "customer_id creation not implemented yet"
 37		iq = Blather::Stanza::Iq::Command.new
 38		Registration.for(iq, nil, Minitest::Mock.new).sync
 39	end
 40	em :test_for_not_activated_without_customer_id
 41
 42	class ActivationTest < Minitest::Test
 43		Registration::Activation::COMMAND_MANAGER = Minitest::Mock.new
 44		def setup
 45			iq = Blather::Stanza::Iq::Command.new
 46			@activation = Registration::Activation.new(iq, "test", "+15555550000")
 47		end
 48
 49		def test_write
 50			result = Minitest::Mock.new
 51			result.expect(:then, result)
 52			result.expect(:then, EMPromise.resolve(:test_result))
 53			Registration::Activation::COMMAND_MANAGER.expect(
 54				:write,
 55				result,
 56				[Blather::Stanza::Iq::Command]
 57			)
 58			assert_equal :test_result, @activation.write.sync
 59		end
 60		em :test_write
 61	end
 62
 63	class PaymentTest < Minitest::Test
 64		Customer::BRAINTREE = Minitest::Mock.new
 65		Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new
 66
 67		def test_for_bitcoin
 68			Registration::Payment::Bitcoin::ELECTRUM.expect(:createnewaddress, "addr")
 69			iq = Blather::Stanza::Iq::Command.new
 70			iq.form.fields = [
 71				{ var: "activation_method", value: "bitcoin" },
 72				{ var: "plan_name", value: "test_usd" }
 73			]
 74			result = Registration::Payment.for(
 75				iq,
 76				Customer.new("test"),
 77				"+15555550000"
 78			)
 79			assert_kind_of Registration::Payment::Bitcoin, result
 80		end
 81
 82		def test_for_credit_card
 83			braintree_customer = Minitest::Mock.new
 84			Customer::BRAINTREE.expect(
 85				:customer,
 86				braintree_customer
 87			)
 88			braintree_customer.expect(
 89				:find,
 90				EMPromise.resolve(OpenStruct.new(payment_methods: [])),
 91				["test"]
 92			)
 93			iq = Blather::Stanza::Iq::Command.new
 94			iq.from = "test@example.com"
 95			iq.form.fields = [
 96				{ var: "activation_method", value: "credit_card" },
 97				{ var: "plan_name", value: "test_usd" }
 98			]
 99			result = Registration::Payment.for(
100				iq,
101				Customer.new("test"),
102				"+15555550000"
103			).sync
104			assert_kind_of Registration::Payment::CreditCard, result
105		end
106		em :test_for_credit_card
107
108		def test_for_code
109			skip "Code not implemented yet"
110			iq = Blather::Stanza::Iq::Command.new
111			iq.form.fields = [
112				{ var: "activation_method", value: "code" },
113				{ var: "plan_name", value: "test_usd" }
114			]
115			result = Registration::Payment.for(iq, "test", "+15555550000")
116			assert_kind_of Registration::Payment::Code, result
117		end
118
119		class BitcoinTest < Minitest::Test
120			Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new
121			Registration::Payment::Bitcoin::REDIS = Minitest::Mock.new
122			Registration::Payment::Bitcoin::BTC_SELL_PRICES = Minitest::Mock.new
123			Registration::Payment::Bitcoin::BLATHER = Minitest::Mock.new
124
125			def setup
126				Registration::Payment::Bitcoin::ELECTRUM.expect(
127					:createnewaddress,
128					EMPromise.resolve("testaddr")
129				)
130				iq = Blather::Stanza::Iq::Command.new
131				iq.form.fields = [
132					{ var: "plan_name", value: "test_usd" }
133				]
134				@bitcoin = Registration::Payment::Bitcoin.new(
135					iq,
136					Customer.new("test"),
137					"+15555550000"
138				)
139			end
140
141			def test_write
142				reply_text = <<~NOTE
143					Activate your account by sending at least 1.000000 BTC to
144					testaddr
145
146					You will receive a notification when your payment is complete.
147				NOTE
148				Registration::Payment::Bitcoin::BLATHER.expect(
149					:<<,
150					nil,
151					[Matching.new do |reply|
152						assert_equal :completed, reply.status
153						assert_equal :info, reply.note_type
154						assert_equal reply_text, reply.note.content
155						true
156					end]
157				)
158				Registration::Payment::Bitcoin::BTC_SELL_PRICES.expect(
159					:usd,
160					EMPromise.resolve(BigDecimal.new(1))
161				)
162				@bitcoin.stub(:save, EMPromise.resolve(nil)) do
163					@bitcoin.write.sync
164				end
165				Registration::Payment::Bitcoin::BLATHER.verify
166			end
167			em :test_write
168		end
169
170		class CreditCardTest < Minitest::Test
171			def setup
172				iq = Blather::Stanza::Iq::Command.new
173				iq.from = "test@example.com"
174				@credit_card = Registration::Payment::CreditCard.new(
175					iq,
176					Customer.new("test"),
177					"+15555550000"
178				)
179			end
180
181			def test_reply
182				assert_equal [:execute, :next], @credit_card.reply.allowed_actions
183				assert_equal(
184					"Add credit card, then return here and choose next: " \
185					"http://creditcard.example.com",
186					@credit_card.reply.note.content
187				)
188			end
189		end
190	end
191end