test_registration.rb

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