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::CreditCardSale =
450 Minitest::Mock.new
451 Command::COMMAND_MANAGER = Minitest::Mock.new
452
453 def test_write
454 customer = Minitest::Mock.new(
455 customer(plan_name: "test_usd")
456 )
457 Registration::Payment::CreditCard::Activate::CreditCardSale.expect(
458 :create,
459 EMPromise.resolve(nil)
460 ) do |acustomer, amount:, payment_method:|
461 assert_operator customer, :===, acustomer
462 assert_equal CONFIG[:activation_amount], amount
463 assert_equal :test_default_method, payment_method
464 end
465 customer.expect(
466 :bill_plan,
467 nil,
468 note: "Bill +15555550000 for first month"
469 )
470 Registration::Payment::CreditCard::Activate::Finish.expect(
471 :new,
472 OpenStruct.new(write: nil),
473 [customer, "+15555550000"]
474 )
475 execute_command do
476 Registration::Payment::CreditCard::Activate.new(
477 customer,
478 :test_default_method,
479 "+15555550000"
480 ).write
481 end
482 Registration::Payment::CreditCard::Activate::CreditCardSale.verify
483 customer.verify
484 Registration::Payment::CreditCard::Activate::Finish.verify
485 end
486 em :test_write
487
488 def test_write_declines
489 customer = Minitest::Mock.new(
490 customer(plan_name: "test_usd")
491 )
492 iq = Blather::Stanza::Iq::Command.new
493 iq.from = "test@example.com"
494 msg = Registration::Payment::CreditCard::Activate::DECLINE_MESSAGE
495 Command::COMMAND_MANAGER.expect(
496 :write,
497 EMPromise.reject(:test_result),
498 [Matching.new do |reply|
499 assert_equal :error, reply.note_type
500 assert_equal(
501 "#{msg}: http://creditcard.example.com",
502 reply.note.content
503 )
504 end]
505 )
506 result = execute_command do
507 Registration::Payment::CreditCard::Activate::CreditCardSale.expect(
508 :create,
509 EMPromise.reject("declined")
510 ) do |acustomer, amount:, payment_method:|
511 assert_operator customer, :===, acustomer
512 assert_equal CONFIG[:activation_amount], amount
513 assert_equal :test_default_method, payment_method
514 end
515
516 Registration::Payment::CreditCard::Activate.new(
517 customer,
518 :test_default_method,
519 "+15555550000"
520 ).write.catch { |e| e }
521 end
522 assert_equal :test_result, result
523 Registration::Payment::CreditCard::Activate::CreditCardSale.verify
524 end
525 em :test_write_declines
526 end
527
528 class InviteCodeTest < Minitest::Test
529 Registration::Payment::InviteCode::DB =
530 Minitest::Mock.new
531 Registration::Payment::InviteCode::REDIS =
532 Minitest::Mock.new
533 Command::COMMAND_MANAGER = Minitest::Mock.new
534 Registration::Payment::InviteCode::Finish =
535 Minitest::Mock.new
536 def test_write
537 customer = customer(plan_name: "test_usd")
538 Registration::Payment::InviteCode::DB.expect(:transaction, true, [])
539 Registration::Payment::InviteCode::Finish.expect(
540 :new,
541 OpenStruct.new(write: nil),
542 [
543 customer,
544 "+15555550000"
545 ]
546 )
547 execute_command do
548 Registration::Payment::InviteCode::REDIS.expect(
549 :get,
550 EMPromise.resolve(nil),
551 ["jmp_invite_tries-test"]
552 )
553 Command::COMMAND_MANAGER.expect(
554 :write,
555 EMPromise.resolve(
556 Blather::Stanza::Iq::Command.new.tap { |iq|
557 iq.form.fields = [{ var: "code", value: "abc" }]
558 }
559 ),
560 [Matching.new do |reply|
561 assert_equal :form, reply.form.type
562 assert_nil reply.form.instructions
563 end]
564 )
565
566 Registration::Payment::InviteCode.new(
567 customer,
568 "+15555550000"
569 ).write
570 end
571 assert_mock Command::COMMAND_MANAGER
572 assert_mock Registration::Payment::InviteCode::DB
573 assert_mock Registration::Payment::InviteCode::REDIS
574 assert_mock Registration::Payment::InviteCode::Finish
575 end
576 em :test_write
577
578 def test_write_bad_code
579 result = execute_command do
580 customer = customer(plan_name: "test_usd")
581 Registration::Payment::InviteCode::REDIS.expect(
582 :get,
583 EMPromise.resolve(0),
584 ["jmp_invite_tries-test"]
585 )
586 Registration::Payment::InviteCode::DB.expect(:transaction, []) do
587 raise InvitesRepo::Invalid, "wut"
588 end
589 Registration::Payment::InviteCode::REDIS.expect(
590 :incr,
591 EMPromise.resolve(nil),
592 ["jmp_invite_tries-test"]
593 )
594 Registration::Payment::InviteCode::REDIS.expect(
595 :expire,
596 EMPromise.resolve(nil),
597 ["jmp_invite_tries-test", 60 * 60]
598 )
599 Command::COMMAND_MANAGER.expect(
600 :write,
601 EMPromise.resolve(
602 Blather::Stanza::Iq::Command.new.tap { |iq|
603 iq.form.fields = [{ var: "code", value: "abc" }]
604 }
605 ),
606 [Matching.new do |reply|
607 assert_equal :form, reply.form.type
608 assert_nil reply.form.instructions
609 end]
610 )
611 Command::COMMAND_MANAGER.expect(
612 :write,
613 EMPromise.reject(:test_result),
614 [Matching.new do |reply|
615 assert_equal :form, reply.form.type
616 assert_equal "wut", reply.form.instructions
617 end]
618 )
619
620 Registration::Payment::InviteCode.new(
621 customer,
622 "+15555550000"
623 ).write.catch { |e| e }
624 end
625 assert_equal :test_result, result
626 assert_mock Command::COMMAND_MANAGER
627 assert_mock Registration::Payment::InviteCode::DB
628 assert_mock Registration::Payment::InviteCode::REDIS
629 end
630 em :test_write_bad_code
631
632 def test_write_bad_code_over_limit
633 result = execute_command do
634 customer = customer(plan_name: "test_usd")
635 Registration::Payment::InviteCode::REDIS.expect(
636 :get,
637 EMPromise.resolve(11),
638 ["jmp_invite_tries-test"]
639 )
640 Command::COMMAND_MANAGER.expect(
641 :write,
642 EMPromise.resolve(
643 Blather::Stanza::Iq::Command.new.tap { |iq|
644 iq.form.fields = [{ var: "code", value: "abc" }]
645 }
646 ),
647 [Matching.new do |reply|
648 assert_equal :form, reply.form.type
649 assert_nil reply.form.instructions
650 end]
651 )
652 Registration::Payment::InviteCode::REDIS.expect(
653 :incr,
654 EMPromise.resolve(nil),
655 ["jmp_invite_tries-test"]
656 )
657 Registration::Payment::InviteCode::REDIS.expect(
658 :expire,
659 EMPromise.resolve(nil),
660 ["jmp_invite_tries-test", 60 * 60]
661 )
662 Command::COMMAND_MANAGER.expect(
663 :write,
664 EMPromise.reject(:test_result),
665 [Matching.new do |reply|
666 assert_equal :form, reply.form.type
667 assert_equal "Too many wrong attempts", reply.form.instructions
668 end]
669 )
670 Registration::Payment::InviteCode.new(
671 customer,
672 "+15555550000"
673 ).write.catch { |e| e }
674 end
675 assert_equal :test_result, result
676 assert_mock Command::COMMAND_MANAGER
677 assert_mock Registration::Payment::InviteCode::REDIS
678 end
679 em :test_write_bad_code_over_limit
680 end
681 end
682
683 class FinishTest < Minitest::Test
684 Command::COMMAND_MANAGER = Minitest::Mock.new
685 Registration::Finish::TEL_SELECTIONS = FakeTelSelections.new
686 Registration::Finish::REDIS = Minitest::Mock.new
687 Bwmsgsv2Repo::REDIS = Minitest::Mock.new
688
689 def setup
690 @sgx = Minitest::Mock.new(TrivialBackendSgxRepo.new.get("test"))
691 iq = Blather::Stanza::Iq::Command.new
692 iq.from = "test\\40example.com@cheogram.com"
693 @finish = Registration::Finish.new(
694 customer(sgx: @sgx),
695 "+15555550000"
696 )
697 end
698
699 def test_write
700 create_order = stub_request(
701 :post,
702 "https://dashboard.bandwidth.com/v1.0/accounts//orders"
703 ).to_return(status: 201, body: <<~RESPONSE)
704 <OrderResponse>
705 <Order>
706 <id>test_order</id>
707 </Order>
708 </OrderResponse>
709 RESPONSE
710 stub_request(
711 :get,
712 "https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
713 ).to_return(status: 201, body: <<~RESPONSE)
714 <OrderResponse>
715 <OrderStatus>COMPLETE</OrderStatus>
716 <CompletedNumbers>
717 <TelephoneNumber>
718 <FullNumber>5555550000</FullNumber>
719 </TelephoneNumber>
720 </CompletedNumbers>
721 </OrderResponse>
722 RESPONSE
723 stub_request(
724 :post,
725 "https://dashboard.bandwidth.com/v1.0/accounts//sites//sippeers//movetns"
726 )
727 Registration::Finish::REDIS.expect(
728 :del,
729 nil,
730 ["pending_tel_for-test@example.net"]
731 )
732 Bwmsgsv2Repo::REDIS.expect(
733 :set,
734 nil,
735 [
736 "catapult_fwd-+15555550000",
737 "xmpp:test@example.net"
738 ]
739 )
740 Bwmsgsv2Repo::REDIS.expect(
741 :set,
742 nil,
743 ["catapult_fwd_timeout-customer_test@component", 25]
744 )
745 blather = Minitest::Mock.new
746 blather.expect(
747 :<<,
748 nil,
749 [Matching.new do |reply|
750 assert_equal :completed, reply.status
751 assert_equal :info, reply.note_type
752 assert_equal(
753 "Your JMP account has been activated as +15555550000",
754 reply.note.content
755 )
756 end]
757 )
758 execute_command(blather: blather) do
759 @sgx.expect(
760 :register!,
761 EMPromise.resolve(@sgx.with(
762 registered?: Blather::Stanza::Iq::IBR.new.tap do |ibr|
763 ibr.phone = "+15555550000"
764 end
765 )),
766 ["+15555550000"]
767 )
768
769 @finish.write
770 end
771 assert_requested create_order
772 assert_mock @sgx
773 assert_mock Registration::Finish::REDIS
774 assert_mock Bwmsgsv2Repo::REDIS
775 assert_mock blather
776 end
777 em :test_write
778
779 def test_write_tn_fail
780 create_order = stub_request(
781 :post,
782 "https://dashboard.bandwidth.com/v1.0/accounts//orders"
783 ).to_return(status: 201, body: <<~RESPONSE)
784 <OrderResponse>
785 <Order>
786 <id>test_order</id>
787 </Order>
788 </OrderResponse>
789 RESPONSE
790 stub_request(
791 :get,
792 "https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
793 ).to_return(status: 201, body: <<~RESPONSE)
794 <OrderResponse>
795 <OrderStatus>FAILED</OrderStatus>
796 </OrderResponse>
797 RESPONSE
798
799 result = execute_command do
800 Command::COMMAND_MANAGER.expect(
801 :write,
802 EMPromise.reject(:test_result),
803 [Matching.new do |iq|
804 assert_equal :form, iq.form.type
805 assert_equal(
806 "The JMP number +15555550000 is no longer available.",
807 iq.form.instructions
808 )
809 end]
810 )
811
812 @finish.write.catch { |e| e }
813 end
814
815 assert_equal :test_result, result
816 assert_mock Command::COMMAND_MANAGER
817 assert_instance_of(
818 TelSelections::ChooseTel,
819 Registration::Finish::TEL_SELECTIONS["test@example.com"]
820 )
821 assert_requested create_order
822 end
823 em :test_write_tn_fail
824 end
825end