test_registration.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "customer"
  5require "registration"
  6
  7class RegistrationTest < Minitest::Test
  8	def test_for_registered
  9		sgx = OpenStruct.new(
 10			registered?: OpenStruct.new(phone: "+15555550000")
 11		)
 12		iq = Blather::Stanza::Iq::Command.new
 13		iq.from = "test@example.com"
 14		result = execute_command(iq) do
 15			Registration.for(
 16				customer(sgx: sgx),
 17				Minitest::Mock.new
 18			)
 19		end
 20		assert_kind_of Registration::Registered, result
 21	end
 22	em :test_for_registered
 23
 24	def test_for_activated
 25		web_manager = TelSelections.new(redis: FakeRedis.new)
 26		web_manager.set("test@example.net", "+15555550000")
 27		result = execute_command do
 28			sgx = OpenStruct.new(registered?: false)
 29			Registration.for(
 30				customer(
 31					plan_name: "test_usd",
 32					expires_at: Time.now + 999,
 33					sgx: sgx
 34				),
 35				web_manager
 36			)
 37		end
 38		assert_kind_of Registration::Finish, result
 39	end
 40	em :test_for_activated
 41
 42	def test_for_not_activated_approved
 43		sgx = OpenStruct.new(registered?: false)
 44		web_manager = TelSelections.new(redis: FakeRedis.new)
 45		web_manager.set("test\\40approved.example.com@component", "+15555550000")
 46		iq = Blather::Stanza::Iq::Command.new
 47		iq.from = "test@approved.example.com"
 48		result = execute_command(iq) do
 49			Registration::Activation.for(
 50				customer(
 51					sgx: sgx,
 52					jid: Blather::JID.new("test\\40approved.example.com@component")
 53				),
 54				web_manager
 55			)
 56		end
 57		assert_kind_of Registration::Activation::Allow, result
 58	end
 59	em :test_for_not_activated_approved
 60
 61	def test_for_not_activated_with_customer_id
 62		sgx = OpenStruct.new(registered?: false)
 63		web_manager = TelSelections.new(redis: FakeRedis.new)
 64		web_manager.set("test@example.net", "+15555550000")
 65		iq = Blather::Stanza::Iq::Command.new
 66		iq.from = "test@example.com"
 67		result = execute_command(iq) do
 68			Registration::Activation.for(
 69				customer(sgx: sgx),
 70				web_manager
 71			)
 72		end
 73		assert_kind_of Registration::Activation, result
 74	end
 75	em :test_for_not_activated_with_customer_id
 76
 77	class ActivationTest < Minitest::Test
 78		Command::COMMAND_MANAGER = Minitest::Mock.new
 79		def setup
 80			@activation = Registration::Activation.new("test", "+15555550000")
 81		end
 82
 83		def test_write
 84			stub_request(
 85				:get,
 86				"https://dashboard.bandwidth.com/v1.0/tns/+15555550000"
 87			).to_return(status: 201, body: <<~RESPONSE)
 88				<TelephoneNumberResponse>
 89					<TelephoneNumber>5555550000</TelephoneNumber>
 90				</TelephoneNumberResponse>
 91			RESPONSE
 92			stub_request(
 93				:get,
 94				"https://dashboard.bandwidth.com/v1.0/tns/5555550000/ratecenter"
 95			).to_return(status: 201, body: <<~RESPONSE)
 96				<TelephoneNumberResponse>
 97					<TelephoneNumberDetails>
 98						<State>KE</State>
 99						<RateCenter>FA</RateCenter>
100					</TelephoneNumberDetails>
101				</TelephoneNumberResponse>
102			RESPONSE
103			Command::COMMAND_MANAGER.expect(
104				:write,
105				EMPromise.reject(:test_result),
106				[Matching.new do |iq|
107					assert_equal :form, iq.form.type
108					assert_equal(
109						"You've selected +15555550000 (FA, KE) as your JMP number.",
110						iq.form.instructions.lines.first.chomp
111					)
112				end]
113			)
114			assert_equal(
115				:test_result,
116				execute_command { @activation.write.catch { |e| e } }
117			)
118			assert_mock Command::COMMAND_MANAGER
119		end
120		em :test_write
121	end
122
123	class AllowTest < Minitest::Test
124		Command::COMMAND_MANAGER = Minitest::Mock.new
125		Registration::Activation::Allow::DB = Minitest::Mock.new
126
127		def test_write_credit_to_nil
128			cust = Minitest::Mock.new(customer("test"))
129			allow = Registration::Activation::Allow.new(cust, "+15555550000", nil)
130
131			stub_request(
132				:get,
133				"https://dashboard.bandwidth.com/v1.0/tns/+15555550000"
134			).to_return(status: 201, body: <<~RESPONSE)
135				<TelephoneNumberResponse>
136					<TelephoneNumber>5555550000</TelephoneNumber>
137				</TelephoneNumberResponse>
138			RESPONSE
139			stub_request(
140				:get,
141				"https://dashboard.bandwidth.com/v1.0/tns/5555550000/ratecenter"
142			).to_return(status: 201, body: <<~RESPONSE)
143				<TelephoneNumberResponse>
144					<TelephoneNumberDetails>
145						<State>KE</State>
146						<RateCenter>FA</RateCenter>
147					</TelephoneNumberDetails>
148				</TelephoneNumberResponse>
149			RESPONSE
150			Command::COMMAND_MANAGER.expect(
151				:write,
152				EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
153					iq.form.fields = [{ var: "plan_name", value: "test_usd" }]
154				}),
155				[Matching.new do |iq|
156					assert_equal :form, iq.form.type
157					assert_equal(
158						"You've selected +15555550000 (FA, KE) as your JMP number.",
159						iq.form.instructions.lines.first.chomp
160					)
161					assert_equal 1, iq.form.fields.length
162				end]
163			)
164			Registration::Activation::Allow::DB.expect(
165				:transaction,
166				EMPromise.reject(:test_result)
167			) do |&blk|
168				blk.call
169				true
170			end
171			cust.expect(:with_plan, cust, ["test_usd"])
172			cust.expect(:activate_plan_starting_now, nil)
173			assert_equal(
174				:test_result,
175				execute_command { allow.write.catch { |e| e } }
176			)
177			assert_mock Command::COMMAND_MANAGER
178		end
179		em :test_write_credit_to_nil
180
181		def test_write_credit_to_refercust
182			cust = Minitest::Mock.new(customer("test"))
183			allow = Registration::Activation::Allow.new(
184				cust, "+15555550000", "refercust"
185			)
186
187			stub_request(
188				:get,
189				"https://dashboard.bandwidth.com/v1.0/tns/+15555550000"
190			).to_return(status: 201, body: <<~RESPONSE)
191				<TelephoneNumberResponse>
192					<TelephoneNumber>5555550000</TelephoneNumber>
193				</TelephoneNumberResponse>
194			RESPONSE
195			stub_request(
196				:get,
197				"https://dashboard.bandwidth.com/v1.0/tns/5555550000/ratecenter"
198			).to_return(status: 201, body: <<~RESPONSE)
199				<TelephoneNumberResponse>
200					<TelephoneNumberDetails>
201						<State>KE</State>
202						<RateCenter>FA</RateCenter>
203					</TelephoneNumberDetails>
204				</TelephoneNumberResponse>
205			RESPONSE
206			Command::COMMAND_MANAGER.expect(
207				:write,
208				EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
209					iq.form.fields = [{ var: "plan_name", value: "test_usd" }]
210				}),
211				[Matching.new do |iq|
212					assert_equal :form, iq.form.type
213					assert_equal(
214						"You've selected +15555550000 (FA, KE) as your JMP number.",
215						iq.form.instructions.lines.first.chomp
216					)
217					assert_equal 1, iq.form.fields.length
218				end]
219			)
220			Registration::Activation::Allow::DB.expect(
221				:transaction,
222				EMPromise.reject(:test_result)
223			) do |&blk|
224				blk.call
225				true
226			end
227			Registration::Activation::Allow::DB.expect(
228				:exec,
229				nil,
230				[String, ["refercust", "test"]]
231			)
232			cust.expect(:with_plan, cust, ["test_usd"])
233			cust.expect(:activate_plan_starting_now, nil)
234			assert_equal(
235				:test_result,
236				execute_command { allow.write.catch { |e| e } }
237			)
238			assert_mock Command::COMMAND_MANAGER
239		end
240		em :test_write_credit_to_refercust
241	end
242
243	class PaymentTest < Minitest::Test
244		CustomerFinancials::BRAINTREE = Minitest::Mock.new
245
246		def test_for_bitcoin
247			cust = Minitest::Mock.new(customer)
248			cust.expect(:with_plan, cust, ["test_usd"])
249			cust.expect(:save_plan!, nil)
250			iq = Blather::Stanza::Iq::Command.new
251			iq.form.fields = [
252				{ var: "activation_method", value: "bitcoin" },
253				{ var: "plan_name", value: "test_usd" }
254			]
255			result = Registration::Payment.for(iq, cust, "+15555550000")
256			assert_kind_of Registration::Payment::Bitcoin, result
257			assert_mock cust
258		end
259
260		def test_for_credit_card
261			cust = Minitest::Mock.new(customer)
262			cust.expect(:with_plan, cust, ["test_usd"])
263			cust.expect(:save_plan!, nil)
264			braintree_customer = Minitest::Mock.new
265			CustomerFinancials::BRAINTREE.expect(
266				:customer,
267				braintree_customer
268			)
269			CustomerFinancials::REDIS.expect(:smembers, [], ["block_credit_cards"])
270			braintree_customer.expect(
271				:find,
272				EMPromise.resolve(OpenStruct.new(payment_methods: [])),
273				["test"]
274			)
275			iq = Blather::Stanza::Iq::Command.new
276			iq.from = "test@example.com"
277			iq.form.fields = [
278				{ var: "activation_method", value: "credit_card" },
279				{ var: "plan_name", value: "test_usd" }
280			]
281			result = Registration::Payment.for(
282				iq,
283				cust,
284				"+15555550000"
285			).sync
286			assert_kind_of Registration::Payment::CreditCard, result
287			assert_mock cust
288		end
289		em :test_for_credit_card
290
291		def test_for_code
292			cust = Minitest::Mock.new(customer)
293			cust.expect(:with_plan, cust, ["test_usd"])
294			cust.expect(:save_plan!, nil)
295			iq = Blather::Stanza::Iq::Command.new
296			iq.form.fields = [
297				{ var: "activation_method", value: "code" },
298				{ var: "plan_name", value: "test_usd" }
299			]
300			result = Registration::Payment.for(
301				iq,
302				cust,
303				"+15555550000"
304			)
305			assert_kind_of Registration::Payment::InviteCode, result
306			assert_mock cust
307		end
308
309		class BitcoinTest < Minitest::Test
310			Registration::Payment::Bitcoin::BTC_SELL_PRICES = Minitest::Mock.new
311			CustomerFinancials::REDIS = Minitest::Mock.new
312
313			def setup
314				@customer = Minitest::Mock.new(
315					customer(plan_name: "test_usd")
316				)
317				@customer.expect(
318					:add_btc_address,
319					EMPromise.resolve("testaddr")
320				)
321				@bitcoin = Registration::Payment::Bitcoin.new(
322					@customer,
323					"+15555550000"
324				)
325			end
326
327			def test_write
328				CustomerFinancials::REDIS.expect(
329					:smembers,
330					EMPromise.resolve([]),
331					["jmp_customer_btc_addresses-test"]
332				)
333				reply_text = <<~NOTE
334					Activate your account by sending at least 1.000000 BTC to
335					testaddr
336
337					You will receive a notification when your payment is complete.
338				NOTE
339				blather = Minitest::Mock.new
340				Command::COMMAND_MANAGER.expect(
341					:write,
342					EMPromise.reject(SessionManager::Timeout.new),
343					[Matching.new do |reply|
344						assert_equal :canceled, reply.status
345						assert_equal :info, reply.note_type
346						assert_equal reply_text, reply.note.content
347						true
348					end]
349				)
350				Registration::Payment::Bitcoin::BTC_SELL_PRICES.expect(
351					:usd,
352					EMPromise.resolve(BigDecimal(1))
353				)
354				@bitcoin.stub(:save, EMPromise.resolve(nil)) do
355					execute_command(blather: blather) do
356						@bitcoin.write
357					end
358				end
359				assert_mock blather
360			end
361			em :test_write
362		end
363
364		class CreditCardTest < Minitest::Test
365			def setup
366				@credit_card = Registration::Payment::CreditCard.new(
367					customer,
368					"+15555550000"
369				)
370			end
371
372			def test_for
373				customer = Minitest::Mock.new(customer)
374				customer.expect(
375					:payment_methods,
376					EMPromise.resolve(OpenStruct.new(default_payment_method: :test))
377				)
378				assert_kind_of(
379					Registration::Payment::CreditCard::Activate,
380					Registration::Payment::CreditCard.for(
381						customer,
382						"+15555550000"
383					).sync
384				)
385			end
386			em :test_for
387
388			def test_write
389				result = execute_command do
390					Command::COMMAND_MANAGER.expect(
391						:write,
392						EMPromise.reject(:test_result),
393						[Matching.new do |reply|
394							assert_equal [:execute, :next, :prev], reply.allowed_actions
395							assert_equal(
396								"Add credit card, save, then next here to continue: " \
397								"http://creditcard.example.com",
398								reply.note.content
399							)
400						end]
401					)
402
403					@credit_card.write.catch { |e| e }
404				end
405
406				assert_equal :test_result, result
407			end
408			em :test_write
409		end
410
411		class MailTest < Minitest::Test
412			def setup
413				@mail = Registration::Payment::Mail.new(
414					customer(plan_name: "test_cad"),
415					"+15555550000"
416				)
417			end
418
419			def test_write
420				result = execute_command do
421					Command::COMMAND_MANAGER.expect(
422						:write,
423						EMPromise.reject(:test_result),
424						[Matching.new do |reply|
425							assert_equal [:execute, :prev], reply.allowed_actions
426							refute reply.form.instructions.empty?
427							assert_equal(
428								"A Mailing Address",
429								reply.form.field("adr").value
430							)
431							assert_equal(
432								"interac@example.com",
433								reply.form.field("interac_email").value
434							)
435						end]
436					)
437
438					@mail.write.catch { |e| e }
439				end
440
441				assert_equal :test_result, result
442			end
443			em :test_write
444		end
445
446		class ActivateTest < Minitest::Test
447			Registration::Payment::CreditCard::Activate::Finish =
448				Minitest::Mock.new
449			Registration::Payment::CreditCard::Activate::Transaction =
450				Minitest::Mock.new
451			Command::COMMAND_MANAGER = Minitest::Mock.new
452
453			def test_write
454				transaction = PromiseMock.new
455				transaction.expect(
456					:insert,
457					EMPromise.resolve(nil)
458				)
459				customer = Minitest::Mock.new(
460					customer(plan_name: "test_usd")
461				)
462				Registration::Payment::CreditCard::Activate::Transaction.expect(
463					:sale,
464					transaction
465				) do |acustomer, amount:, payment_method:|
466					assert_operator customer, :===, acustomer
467					assert_equal CONFIG[:activation_amount], amount
468					assert_equal :test_default_method, payment_method
469				end
470				customer.expect(
471					:bill_plan,
472					nil,
473					note: "Bill +15555550000 for first month"
474				)
475				Registration::Payment::CreditCard::Activate::Finish.expect(
476					:new,
477					OpenStruct.new(write: nil),
478					[customer, "+15555550000"]
479				)
480				execute_command do
481					Registration::Payment::CreditCard::Activate.new(
482						customer,
483						:test_default_method,
484						"+15555550000"
485					).write
486				end
487				Registration::Payment::CreditCard::Activate::Transaction.verify
488				transaction.verify
489				customer.verify
490				Registration::Payment::CreditCard::Activate::Finish.verify
491			end
492			em :test_write
493
494			def test_write_declines
495				customer = Minitest::Mock.new(
496					customer(plan_name: "test_usd")
497				)
498				iq = Blather::Stanza::Iq::Command.new
499				iq.from = "test@example.com"
500				msg = Registration::Payment::CreditCard::Activate::DECLINE_MESSAGE
501				Command::COMMAND_MANAGER.expect(
502					:write,
503					EMPromise.reject(:test_result),
504					[Matching.new do |reply|
505						assert_equal :error, reply.note_type
506						assert_equal(
507							"#{msg}: http://creditcard.example.com",
508							reply.note.content
509						)
510					end]
511				)
512				result = execute_command do
513					Registration::Payment::CreditCard::Activate::Transaction.expect(
514						:sale,
515						EMPromise.reject("declined")
516					) do |acustomer, amount:, payment_method:|
517						assert_operator customer, :===, acustomer
518						assert_equal CONFIG[:activation_amount], amount
519						assert_equal :test_default_method, payment_method
520					end
521
522					Registration::Payment::CreditCard::Activate.new(
523						customer,
524						:test_default_method,
525						"+15555550000"
526					).write.catch { |e| e }
527				end
528				assert_equal :test_result, result
529				Registration::Payment::CreditCard::Activate::Transaction.verify
530			end
531			em :test_write_declines
532		end
533
534		class InviteCodeTest < Minitest::Test
535			Registration::Payment::InviteCode::DB =
536				Minitest::Mock.new
537			Registration::Payment::InviteCode::REDIS =
538				Minitest::Mock.new
539			Command::COMMAND_MANAGER = Minitest::Mock.new
540			Registration::Payment::InviteCode::Finish =
541				Minitest::Mock.new
542			def test_write
543				customer = customer(plan_name: "test_usd")
544				Registration::Payment::InviteCode::DB.expect(:transaction, true, [])
545				Registration::Payment::InviteCode::Finish.expect(
546					:new,
547					OpenStruct.new(write: nil),
548					[
549						customer,
550						"+15555550000"
551					]
552				)
553				execute_command do
554					Registration::Payment::InviteCode::REDIS.expect(
555						:get,
556						EMPromise.resolve(nil),
557						["jmp_invite_tries-test"]
558					)
559					Command::COMMAND_MANAGER.expect(
560						:write,
561						EMPromise.resolve(
562							Blather::Stanza::Iq::Command.new.tap { |iq|
563								iq.form.fields = [{ var: "code", value: "abc" }]
564							}
565						),
566						[Matching.new do |reply|
567							assert_equal :form, reply.form.type
568							assert_nil reply.form.instructions
569						end]
570					)
571
572					Registration::Payment::InviteCode.new(
573						customer,
574						"+15555550000"
575					).write
576				end
577				assert_mock Command::COMMAND_MANAGER
578				assert_mock Registration::Payment::InviteCode::DB
579				assert_mock Registration::Payment::InviteCode::REDIS
580				assert_mock Registration::Payment::InviteCode::Finish
581			end
582			em :test_write
583
584			def test_write_bad_code
585				result = execute_command do
586					customer = customer(plan_name: "test_usd")
587					Registration::Payment::InviteCode::REDIS.expect(
588						:get,
589						EMPromise.resolve(0),
590						["jmp_invite_tries-test"]
591					)
592					Registration::Payment::InviteCode::DB.expect(:transaction, []) do
593						raise InvitesRepo::Invalid, "wut"
594					end
595					Registration::Payment::InviteCode::REDIS.expect(
596						:incr,
597						EMPromise.resolve(nil),
598						["jmp_invite_tries-test"]
599					)
600					Registration::Payment::InviteCode::REDIS.expect(
601						:expire,
602						EMPromise.resolve(nil),
603						["jmp_invite_tries-test", 60 * 60]
604					)
605					Command::COMMAND_MANAGER.expect(
606						:write,
607						EMPromise.resolve(
608							Blather::Stanza::Iq::Command.new.tap { |iq|
609								iq.form.fields = [{ var: "code", value: "abc" }]
610							}
611						),
612						[Matching.new do |reply|
613							assert_equal :form, reply.form.type
614							assert_nil reply.form.instructions
615						end]
616					)
617					Command::COMMAND_MANAGER.expect(
618						:write,
619						EMPromise.reject(:test_result),
620						[Matching.new do |reply|
621							assert_equal :form, reply.form.type
622							assert_equal "wut", reply.form.instructions
623						end]
624					)
625
626					Registration::Payment::InviteCode.new(
627						customer,
628						"+15555550000"
629					).write.catch { |e| e }
630				end
631				assert_equal :test_result, result
632				assert_mock Command::COMMAND_MANAGER
633				assert_mock Registration::Payment::InviteCode::DB
634				assert_mock Registration::Payment::InviteCode::REDIS
635			end
636			em :test_write_bad_code
637
638			def test_write_bad_code_over_limit
639				result = execute_command do
640					customer = customer(plan_name: "test_usd")
641					Registration::Payment::InviteCode::REDIS.expect(
642						:get,
643						EMPromise.resolve(11),
644						["jmp_invite_tries-test"]
645					)
646					Command::COMMAND_MANAGER.expect(
647						:write,
648						EMPromise.resolve(
649							Blather::Stanza::Iq::Command.new.tap { |iq|
650								iq.form.fields = [{ var: "code", value: "abc" }]
651							}
652						),
653						[Matching.new do |reply|
654							assert_equal :form, reply.form.type
655							assert_nil reply.form.instructions
656						end]
657					)
658					Registration::Payment::InviteCode::REDIS.expect(
659						:incr,
660						EMPromise.resolve(nil),
661						["jmp_invite_tries-test"]
662					)
663					Registration::Payment::InviteCode::REDIS.expect(
664						:expire,
665						EMPromise.resolve(nil),
666						["jmp_invite_tries-test", 60 * 60]
667					)
668					Command::COMMAND_MANAGER.expect(
669						:write,
670						EMPromise.reject(:test_result),
671						[Matching.new do |reply|
672							assert_equal :form, reply.form.type
673							assert_equal "Too many wrong attempts", reply.form.instructions
674						end]
675					)
676					Registration::Payment::InviteCode.new(
677						customer,
678						"+15555550000"
679					).write.catch { |e| e }
680				end
681				assert_equal :test_result, result
682				assert_mock Command::COMMAND_MANAGER
683				assert_mock Registration::Payment::InviteCode::REDIS
684			end
685			em :test_write_bad_code_over_limit
686		end
687	end
688
689	class FinishTest < Minitest::Test
690		Command::COMMAND_MANAGER = Minitest::Mock.new
691		Registration::Finish::TEL_SELECTIONS = FakeTelSelections.new
692		Registration::Finish::REDIS = Minitest::Mock.new
693		Bwmsgsv2Repo::REDIS = Minitest::Mock.new
694
695		def setup
696			@sgx = Minitest::Mock.new(TrivialBackendSgxRepo.new.get("test"))
697			iq = Blather::Stanza::Iq::Command.new
698			iq.from = "test\\40example.com@cheogram.com"
699			@finish = Registration::Finish.new(
700				customer(sgx: @sgx),
701				"+15555550000"
702			)
703		end
704
705		def test_write
706			create_order = stub_request(
707				:post,
708				"https://dashboard.bandwidth.com/v1.0/accounts//orders"
709			).to_return(status: 201, body: <<~RESPONSE)
710				<OrderResponse>
711					<Order>
712						<id>test_order</id>
713					</Order>
714				</OrderResponse>
715			RESPONSE
716			stub_request(
717				:get,
718				"https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
719			).to_return(status: 201, body: <<~RESPONSE)
720				<OrderResponse>
721					<OrderStatus>COMPLETE</OrderStatus>
722					<CompletedNumbers>
723						<TelephoneNumber>
724							<FullNumber>5555550000</FullNumber>
725						</TelephoneNumber>
726					</CompletedNumbers>
727				</OrderResponse>
728			RESPONSE
729			stub_request(
730				:post,
731				"https://dashboard.bandwidth.com/v1.0/accounts//sites//sippeers//movetns"
732			)
733			Registration::Finish::REDIS.expect(
734				:del,
735				nil,
736				["pending_tel_for-test@example.net"]
737			)
738			Bwmsgsv2Repo::REDIS.expect(
739				:set,
740				nil,
741				[
742					"catapult_fwd-+15555550000",
743					"xmpp:test@example.net"
744				]
745			)
746			Bwmsgsv2Repo::REDIS.expect(
747				:set,
748				nil,
749				["catapult_fwd_timeout-customer_test@component", 25]
750			)
751			blather = Minitest::Mock.new
752			blather.expect(
753				:<<,
754				nil,
755				[Matching.new do |reply|
756					assert_equal :completed, reply.status
757					assert_equal :info, reply.note_type
758					assert_equal(
759						"Your JMP account has been activated as +15555550000",
760						reply.note.content
761					)
762				end]
763			)
764			execute_command(blather: blather) do
765				@sgx.expect(
766					:register!,
767					EMPromise.resolve(@sgx.with(
768						registered?: Blather::Stanza::Iq::IBR.new.tap do |ibr|
769							ibr.phone = "+15555550000"
770						end
771					)),
772					["+15555550000"]
773				)
774
775				@finish.write
776			end
777			assert_requested create_order
778			assert_mock @sgx
779			assert_mock Registration::Finish::REDIS
780			assert_mock Bwmsgsv2Repo::REDIS
781			assert_mock blather
782		end
783		em :test_write
784
785		def test_write_tn_fail
786			create_order = stub_request(
787				:post,
788				"https://dashboard.bandwidth.com/v1.0/accounts//orders"
789			).to_return(status: 201, body: <<~RESPONSE)
790				<OrderResponse>
791					<Order>
792						<id>test_order</id>
793					</Order>
794				</OrderResponse>
795			RESPONSE
796			stub_request(
797				:get,
798				"https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
799			).to_return(status: 201, body: <<~RESPONSE)
800				<OrderResponse>
801					<OrderStatus>FAILED</OrderStatus>
802				</OrderResponse>
803			RESPONSE
804
805			result = execute_command do
806				Command::COMMAND_MANAGER.expect(
807					:write,
808					EMPromise.reject(:test_result),
809					[Matching.new do |iq|
810						assert_equal :form, iq.form.type
811						assert_equal(
812							"The JMP number +15555550000 is no longer available.",
813							iq.form.instructions
814						)
815					end]
816				)
817
818				@finish.write.catch { |e| e }
819			end
820
821			assert_equal :test_result, result
822			assert_mock Command::COMMAND_MANAGER
823			assert_instance_of(
824				TelSelections::ChooseTel,
825				Registration::Finish::TEL_SELECTIONS["test@example.com"]
826			)
827			assert_requested create_order
828		end
829		em :test_write_tn_fail
830	end
831end