~singpolyma/sgx-jmp

358e8664769bdeef7c225e347d309c139ec51a69 — Stephen Paul Weber 8 months ago 94b7b99 + c49b2c6
Merge branch 'finish-btc'

* finish-btc:
  No more legacy session for BTC
  Do not lose WebRegisterManager on retart
M lib/registration.rb => lib/registration.rb +9 -13
@@ 17,7 17,7 @@ class Registration
			if registered
				Registered.new(registered.phone)
			else
				web_register_manager[customer.jid].choose_tel.then do |tel|
				web_register_manager[customer.jid].then(&:choose_tel).then do |tel|
					Activation.for(customer, tel)
				end
			end


@@ 157,6 157,8 @@ class Registration
		class Bitcoin
			Payment.kinds[:bitcoin] = method(:new)

			THIRTY_DAYS = 60 * 60 * 24 * 30

			def initialize(customer, tel)
				@customer = customer
				@customer_id = customer.customer_id


@@ 165,20 167,13 @@ class Registration

			attr_reader :customer_id, :tel

			def legacy_session_save
				sid = SecureRandom.hex
				REDIS.mset(
					"reg-sid_for-#{customer_id}", sid,
					"reg-session_tel-#{sid}", tel
				)
			end

			def save
				EMPromise.all([
					legacy_session_save,
					REDIS.mset(
						"pending_tel_for-#{customer_id}", tel,
						"pending_plan_for-#{customer_id}", @customer.plan_name
					REDIS.setex("pending_tel_for-#{@customer.jid}", tel, THIRTY_DAYS),
					REDIS.setex(
						"pending_plan_for-#{customer_id}",
						@customer.plan_name,
						THIRTY_DAYS
					)
				])
			end


@@ 458,6 453,7 @@ class Registration
		def customer_active_tel_purchased
			@customer.register!(@tel).catch(&method(:raise_setup_error)).then {
				EMPromise.all([
					REDIS.del("pending_tel_for-#{@customer.jid}"),
					REDIS.set("catapult_fwd-#{@tel}", cheogram_sip_addr),
					@customer.fwd_timeout = 25 # ~5 seconds / ring, 5 rings
				])

M lib/web_register_manager.rb => lib/web_register_manager.rb +9 -5
@@ 1,16 1,20 @@
# frozen_string_literal: true

class WebRegisterManager
	def initialize
		@tel_map = Hash.new { ChooseTel.new }
	THIRTY_DAYS = 60 * 60 * 24 * 30

	def initialize(redis: REDIS)
		@redis = redis
	end

	def []=(jid, tel)
		@tel_map[jid.to_s] = HaveTel.new(tel)
	def set(jid, tel)
		@redis.setex("pending_tel_for-#{jid}", tel, THIRTY_DAYS)
	end

	def [](jid)
		@tel_map[jid.to_s]
		@redis.get("pending_tel_for-#{jid}").then do |tel|
			tel ? HaveTel.new(tel) : ChooseTel.new
		end
	end

	class HaveTel

M sgx_jmp.rb => sgx_jmp.rb +2 -2
@@ 547,8 547,8 @@ command :execute?, node: "web-register", sessionid: nil do |iq|
				cmd.form.fields = [var: "to", value: jid]
				cmd.form.type = "submit"
			}).then { |result|
				final_jid = result.form.field("from")&.value.to_s.strip
				web_register_manager[final_jid] = tel
				web_register_manager.set(result.form.field("from")&.value.to_s.strip, tel)
			}.then {
				BLATHER << iq.reply.tap { |reply| reply.status = :completed }
			}.catch { |e| panic(e, sentry_hub) }
		end

M test/test_helper.rb => test/test_helper.rb +10 -1
@@ 133,10 133,19 @@ class PromiseMock < Minitest::Mock
end

class FakeRedis
	def initialize(values)
	def initialize(values={})
		@values = values
	end

	def set(key, value)
		@values[key] = value
		EMPromise.resolve("OK")
	end

	def setex(key, value, _expiry)
		set(key, value)
	end

	def get(key)
		EMPromise.resolve(@values[key])
	end

M test/test_registration.rb => test/test_registration.rb +9 -4
@@ 35,8 35,8 @@ class RegistrationTest < Minitest::Test
	em :test_for_registered

	def test_for_activated
		web_manager = WebRegisterManager.new
		web_manager["test@example.net"] = "+15555550000"
		web_manager = WebRegisterManager.new(redis: FakeRedis.new)
		web_manager.set("test@example.net", "+15555550000")
		result = execute_command do
			sgx = OpenStruct.new(registered?: EMPromise.resolve(nil))
			Registration.for(


@@ 54,8 54,8 @@ class RegistrationTest < Minitest::Test

	def test_for_not_activated_with_customer_id
		sgx = OpenStruct.new(registered?: EMPromise.resolve(nil))
		web_manager = WebRegisterManager.new
		web_manager["test@example.net"] = "+15555550000"
		web_manager = WebRegisterManager.new(redis: FakeRedis.new)
		web_manager.set("test@example.net", "+15555550000")
		iq = Blather::Stanza::Iq::Command.new
		iq.from = "test@example.com"
		result = execute_command(iq) do


@@ 569,6 569,11 @@ class RegistrationTest < Minitest::Test
					"sip:test%40example.net@sip.cheogram.com"
				]
			)
			Registration::Finish::REDIS.expect(
				:del,
				nil,
				["pending_tel_for-test@example.net"]
			)
			BackendSgx::REDIS.expect(
				:set,
				nil,

M test/test_web_register_manager.rb => test/test_web_register_manager.rb +7 -6
@@ 5,19 5,20 @@ require "web_register_manager"

class WebRegisterManagerTest < Minitest::Test
	def setup
		@manager = WebRegisterManager.new
		@manager = WebRegisterManager.new(redis: FakeRedis.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"]
		assert_kind_of WebRegisterManager::ChooseTel, @manager["jid@example.com"].sync
		@manager.set("jid@example.com", "+15555550000").sync
		assert_kind_of WebRegisterManager::HaveTel, @manager["jid@example.com"].sync
	end
	em :test_set_get

	def test_choose_tel_have_tel
		jid = "jid@example.com"
		@manager[jid] = "+15555550000"
		assert_equal "+15555550000", @manager[jid].choose_tel.sync
		@manager.set(jid, "+15555550000").sync
		assert_equal "+15555550000", @manager[jid].then(&:choose_tel).sync
	end
	em :test_choose_tel_have_tel
end