From a16fd0d068ef810854d365490268bdd381830a27 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 31 Aug 2021 18:54:21 -0500 Subject: [PATCH] Do not direct back to website to pick new number Instead, if number is no longer available keep them in flow and use the number search tool. --- lib/registration.rb | 16 +++++++++------- lib/tel_selections.rb | 4 ++++ test/test_helper.rb | 22 ++++++++++++++++++++++ test/test_registration.rb | 32 ++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/lib/registration.rb b/lib/registration.rb index a4189dc..ffc2f76 100644 --- a/lib/registration.rb +++ b/lib/registration.rb @@ -425,18 +425,20 @@ class Registration def write BandwidthTNOrder.create(@tel).then(&:poll).then( ->(_) { customer_active_tel_purchased }, - lambda do |_| - Command.finish( - "The JMP number #{@tel} is no longer available, " \ - "please visit https://jmp.chat and choose another.", - type: :error - ) - end + ->(_) { number_purchase_error } ) end protected + def number_purchase_error + TEL_SELECTIONS.delete(@customer.jid).then { + TelSelections::ChooseTel.new.choose_tel( + error: "The JMP number #{@tel} is no longer available." + ) + }.then { |tel| Finish.new(@customer, tel).write } + end + def cheogram_sip_addr "sip:#{ERB::Util.url_encode(@customer.jid)}@sip.cheogram.com" end diff --git a/lib/tel_selections.rb b/lib/tel_selections.rb index d297c0a..3c9592c 100644 --- a/lib/tel_selections.rb +++ b/lib/tel_selections.rb @@ -16,6 +16,10 @@ class TelSelections @redis.setex("pending_tel_for-#{jid}", THIRTY_DAYS, tel) end + def delete(jid) + @redis.del("pending_tel_for-#{jid}") + end + def [](jid) @redis.get("pending_tel_for-#{jid}").then do |tel| tel ? HaveTel.new(tel) : ChooseTel.new diff --git a/test/test_helper.rb b/test/test_helper.rb index c546f70..bcd8695 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -34,6 +34,7 @@ rescue LoadError end require "backend_sgx" +require "tel_selections" $VERBOSE = nil Sentry.init @@ -132,6 +133,27 @@ class PromiseMock < Minitest::Mock end end +class FakeTelSelections + def initialize + @selections = {} + end + + def set(jid, tel) + @selections[jid] = EMPromise.resolve(TelSelections::HaveTel.new(tel)) + end + + def delete(jid) + @selections.delete(jid) + EMPromise.resolve("OK") + end + + def [](jid) + @selections.fetch(jid) do + TelSelections::ChooseTel.new + end + end +end + class FakeRedis def initialize(values={}) @values = values diff --git a/test/test_registration.rb b/test/test_registration.rb index d10da55..04c66a0 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -514,6 +514,8 @@ class RegistrationTest < Minitest::Test end class FinishTest < Minitest::Test + Command::COMMAND_MANAGER = Minitest::Mock.new + Registration::Finish::TEL_SELECTIONS = FakeTelSelections.new Registration::Finish::REDIS = Minitest::Mock.new BackendSgx::REDIS = Minitest::Mock.new @@ -628,23 +630,29 @@ class RegistrationTest < Minitest::Test FAILED RESPONSE - blather = Minitest::Mock.new - blather.expect( - :<<, - nil, - [Matching.new do |reply| - assert_equal :completed, reply.status - assert_equal :error, reply.note_type + + Command::COMMAND_MANAGER.expect( + :write, + EMPromise.reject(:test_result), + [Matching.new do |iq| + assert_equal :form, iq.form.type assert_equal( - "The JMP number +15555550000 is no longer available, " \ - "please visit https://jmp.chat and choose another.", - reply.note.content + "The JMP number +15555550000 is no longer available.", + iq.form.instructions ) end] ) - execute_command(blather: blather) { @finish.write } + + assert_equal( + :test_result, + execute_command { @finish.write.catch { |e| e } } + ) + assert_mock Command::COMMAND_MANAGER + assert_instance_of( + TelSelections::ChooseTel, + Registration::Finish::TEL_SELECTIONS["test@example.com"] + ) assert_requested create_order - assert_mock blather end em :test_write_tn_fail end -- 2.45.2