~singpolyma/sgx-jmp

39685c1c2aafdab9f97846cc1c6154c4ca34af32 — Stephen Paul Weber 1 year, 26 days ago 8cb7c18
Registrations that start on the web will have a tel selected already

So, manage any already-known tel selections and allow registration to use them
right away.  In the future this will fall back to letting the user search for a
tel over XMPP, but that is not implemented here.
2 files changed, 67 insertions(+), 0 deletions(-)

A lib/web_register_manager.rb
A test/test_web_register_manager.rb
A lib/web_register_manager.rb => lib/web_register_manager.rb +35 -0
@@ 0,0 1,35 @@
# frozen_string_literal: true

class WebRegisterManager
	def initialize
		@tel_map = Hash.new { ChooseTel.new }
	end

	def []=(jid, tel)
		@tel_map[jid.to_s] = HaveTel.new(tel)
	end

	def [](jid)
		@tel_map[jid.to_s]
	end

	def choose_tel(iq)
		self[iq.from.stripped].choose_tel(iq)
	end

	class HaveTel
		def initialize(tel)
			@tel = tel
		end

		def choose_tel(iq)
			EMPromise.resolve([iq, @tel])
		end
	end

	class ChooseTel
		def choose_tel(_iq)
			raise "TODO"
		end
	end
end

A test/test_web_register_manager.rb => test/test_web_register_manager.rb +32 -0
@@ 0,0 1,32 @@
# frozen_string_literal: true

require "test_helper"
require "web_register_manager"

class WebRegisterManagerTest < Minitest::Test
	def setup
		@manager = WebRegisterManager.new
	end

	def test_set_get
		assert_kind_of WebRegisterManager::ChooseTel, @manager["jid@example.com"]
		@manager["jid@example.com"] = "+15555550000"
		assert_kind_of WebRegisterManager::HaveTel, @manager["jid@example.com"]
	end

	def test_choose_tel_have_tel
		@manager["jid@example.com"] = "+15555550000"
		iq = Blather::Stanza::Iq.new
		iq.from = "jid@example.com"
		assert_equal [iq, "+15555550000"], @manager.choose_tel(iq).sync
	end
	em :test_choose_tel_have_tel

	def test_choose_tel_not_have_tel
		skip "ChooseTel not implemented yet"
		iq = Blather::Stanza::Iq.new
		iq.from = "jid@example.com"
		@manager.choose_tel(iq).sync
	end
	em :test_choose_tel_not_have_tel
end