M lib/registration.rb => lib/registration.rb +9 -7
@@ 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
M lib/tel_selections.rb => lib/tel_selections.rb +4 -0
@@ 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
M test/test_helper.rb => test/test_helper.rb +22 -0
@@ 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
M test/test_registration.rb => test/test_registration.rb +20 -12
@@ 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
<OrderStatus>FAILED</OrderStatus>
</OrderResponse>
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