~singpolyma/sgx-jmp

c89a05ddb0b00faa4f3a220699d9f0ce83b9ee33 — Stephen Paul Weber 2 years ago 8575f64 + c2b0584
Merge branch 'new-blather'

* new-blather:
  Update to latest blather upstream
  notify_customer helper
11 files changed, 45 insertions(+), 129 deletions(-)

M Gemfile
M lib/admin_command.rb
M lib/backend_sgx.rb
M lib/customer.rb
D lib/ibr.rb
M lib/ibr_repo.rb
M sgx_jmp.rb
M test/test_backend_sgx.rb
D test/test_ibr.rb
M test/test_registration.rb
M test/test_web.rb
M Gemfile => Gemfile +1 -1
@@ 4,7 4,7 @@ source "https://rubygems.org"

gem "amazing_print"
gem "bandwidth-sdk", "<= 6.1.0"
gem "blather", git: "https://github.com/singpolyma/blather.git", branch: "ergonomics"
gem "blather", git: "https://github.com/adhearsion/blather", branch: "develop"
gem "braintree"
gem "dhall", ">= 0.5.3.fixed"
gem "em-hiredis"

M lib/admin_command.rb => lib/admin_command.rb +10 -4
@@ 75,13 75,19 @@ class AdminCommand
		BillPlanCommand.for(@target_customer).call
	end

	def action_cancel_account
	def notify_customer(body)
		m = Blather::Stanza::Message.new
		m.from = CONFIG[:notify_from]
		m.body = "Your JMP account has been cancelled."
		@target_customer.stanza_to(m).then {
		m.body = body
		@target_customer.stanza_to(m)
	end

	def action_cancel_account
		notify_customer("Your JMP account has been cancelled.").then {
			EMPromise.all([
				@target_customer.stanza_to(IBR.new(:set).tap(&:remove!)),
				@target_customer.stanza_to(
					Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!)
				),
				@target_customer.deregister!,
				@customer_repo.disconnect_tel(@target_customer)
			])

M lib/backend_sgx.rb => lib/backend_sgx.rb +4 -4
@@ 1,9 1,9 @@
# frozen_string_literal: true

require "blather"
require "value_semantics/monkey_patched"

require_relative "customer_fwd"
require_relative "ibr"
require_relative "not_loaded"

class BackendSgx


@@ 15,11 15,11 @@ class BackendSgx
		fwd Either(CustomerFwd, nil, NotLoaded)
		transcription_enabled Either(Bool(), NotLoaded)
		alternate_transcription_enabled Either(Bool(), NotLoaded)
		registered? Either(IBR, FalseClass, NotLoaded)
		registered? Either(Blather::Stanza::Iq::IBR, FalseClass, NotLoaded)
	end

	def register!(tel)
		ibr = IBR.new(:set, @jid)
		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
		ibr.from = from_jid
		ibr.nick = creds[:account]
		ibr.username = creds[:username]


@@ 29,7 29,7 @@ class BackendSgx
	end

	def deregister!
		ibr = IBR.new(:set, @jid)
		ibr = Blather::Stanza::Iq::IBR.new(:set, @jid)
		ibr.from = from_jid
		ibr.remove!
		IQ_MANAGER.write(ibr)

M lib/customer.rb => lib/customer.rb +0 -1
@@ 10,7 10,6 @@ require_relative "./customer_ogm"
require_relative "./customer_info"
require_relative "./customer_finacials"
require_relative "./backend_sgx"
require_relative "./ibr"
require_relative "./payment_methods"
require_relative "./plan"
require_relative "./proxied_jid"

D lib/ibr.rb => lib/ibr.rb +0 -59
@@ 1,59 0,0 @@
# frozen_string_literal: true

require "blather"

class IBR < Blather::Stanza::Iq::Query
	register :ibr, nil, "jabber:iq:register"

	def registered=(reg)
		query.at_xpath("./ns:registered", ns: self.class.registered_ns)&.remove
		node = Nokogiri::XML::Node.new("registered", document)
		node.default_namespace = self.class.registered_ns
		query << node if reg
	end

	def registered?
		!!query.at_xpath("./ns:registered", ns: self.class.registered_ns)
	end

	def remove!
		query.children.remove
		node = Nokogiri::XML::Node.new("remove", document)
		node.default_namespace = self.class.registered_ns
		query << node
	end

	def remove?
		!!query.at_xpath("./ns:remove", ns: self.class.registered_ns)
	end

	[
		"instructions",
		"username",
		"nick",
		"password",
		"name",
		"first",
		"last",
		"email",
		"address",
		"city",
		"state",
		"zip",
		"phone",
		"url",
		"date"
	].each do |tag|
		define_method("#{tag}=") do |v|
			query.at_xpath("./ns:#{tag}", ns: self.class.registered_ns)&.remove
			node = Nokogiri::XML::Node.new(tag, document)
			node.default_namespace = self.class.registered_ns
			node.content = v
			query << node
		end

		define_method(tag) do
			query.at_xpath("./ns:#{tag}", ns: self.class.registered_ns)&.content
		end
	end
end

M lib/ibr_repo.rb => lib/ibr_repo.rb +2 -3
@@ 1,17 1,16 @@
# frozen_string_literal: true

require "blather"
require "lazy_object"
require "value_semantics/monkey_patched"

require_relative "ibr"

class IBRRepo
	value_semantics do
		iq_manager Anything(), default: LazyObject.new { IQ_MANAGER }
	end

	def registered?(jid, from:)
		ibr = IBR.new(:get, jid)
		ibr = Blather::Stanza::Iq::IBR.new(:get, jid)
		ibr.from = from

		iq_manager.write(ibr).then do |result|

M sgx_jmp.rb => sgx_jmp.rb +1 -2
@@ 250,7 250,6 @@ when_ready do
	Web.run(LOG.child, *WEB_LISTEN)
end

# workqueue_count MUST be 0 or else Blather uses threads!
setup(
	CONFIG[:component][:jid],
	CONFIG[:component][:secret],


@@ 258,7 257,7 @@ setup(
	CONFIG[:server][:port],
	nil,
	nil,
	workqueue_count: 0
	async: true
)

message to: /\Aaccount@/, body: /./ do |m|

M test/test_backend_sgx.rb => test/test_backend_sgx.rb +6 -2
@@ 12,7 12,9 @@ class BackendSgxTest < Minitest::Test
	def test_registered
		IBRRepo::IQ_MANAGER.expect(
			:write,
			EMPromise.resolve(IBR.new.tap { |ibr| ibr.registered = true }),
			EMPromise.resolve(Blather::Stanza::Iq::IBR.new.tap { |ibr|
				ibr.registered = true
			}),
			[Matching.new do |ibr|
				assert_equal :get, ibr.type
				assert_equal "customer_test@component", ibr.from.to_s


@@ 26,7 28,9 @@ class BackendSgxTest < Minitest::Test
	def test_registered_not_registered
		IBRRepo::IQ_MANAGER.expect(
			:write,
			EMPromise.resolve(IBR.new.tap { |ibr| ibr.registered = false }),
			EMPromise.resolve(Blather::Stanza::Iq::IBR.new.tap { |ibr|
				ibr.registered = false
			}),
			[Matching.new do |ibr|
				assert_equal :get, ibr.type
				assert_equal "customer_test@component", ibr.from.to_s

D test/test_ibr.rb => test/test_ibr.rb +0 -38
@@ 1,38 0,0 @@
# frozen_string_literal: true

require "test_helper"
require "ibr"

class IBRTest < Minitest::Test
	property(:registered) { boolean }
	def registered(val)
		ibr = IBR.new
		ibr.registered = val
		assert_equal val, ibr.registered?
	end

	{
		instructions: :string,
		username: :string,
		nick: :string,
		password: :string,
		name: :string,
		first: :string,
		last: :string,
		email: :string,
		address: :string,
		city: :string,
		state: :string,
		zip: :string,
		phone: [:string, :digit],
		url: :string,
		date: ->(*) { Time.at(range(0, 4294967295)).iso8601 }
	}.each do |prop, type|
		property("prop_#{prop}") { call(type) }
		define_method("prop_#{prop}") do |val|
			ibr = IBR.new
			ibr.public_send("#{prop}=", val)
			assert_equal val, ibr.public_send(prop)
		end
	end
end

M test/test_registration.rb => test/test_registration.rb +5 -3
@@ 725,9 725,11 @@ class RegistrationTest < Minitest::Test
			execute_command(blather: blather) do
				@sgx.expect(
					:register!,
					EMPromise.resolve(@sgx.with(registered?: IBR.new.tap do |ibr|
						ibr.phone = "+15555550000"
					end)),
					EMPromise.resolve(@sgx.with(
						registered?: Blather::Stanza::Iq::IBR.new.tap do |ibr|
							ibr.phone = "+15555550000"
						end
					)),
					["+15555550000"]
				)


M test/test_web.rb => test/test_web.rb +16 -12
@@ 62,18 62,22 @@ class WebTest < Minitest::Test
				),
				ibr_repo: FakeIBRRepo.new(
					"sgx" => {
						"customer_customerid@component" => IBR.new.tap do |ibr|
							ibr.phone = "+15551234567"
						end,
						"customer_customerid_low@component" => IBR.new.tap do |ibr|
							ibr.phone = "+15551234567"
						end,
						"customer_customerid_topup@component" => IBR.new.tap do |ibr|
							ibr.phone = "+15551234567"
						end,
						"customer_customerid_limit@component" => IBR.new.tap do |ibr|
							ibr.phone = "+15551234567"
						end
						"customer_customerid@component" =>
							Blather::Stanza::Iq::IBR.new.tap do |ibr|
								ibr.phone = "+15551234567"
							end,
						"customer_customerid_low@component" =>
							Blather::Stanza::Iq::IBR.new.tap do |ibr|
								ibr.phone = "+15551234567"
							end,
						"customer_customerid_topup@component" =>
							Blather::Stanza::Iq::IBR.new.tap do |ibr|
								ibr.phone = "+15551234567"
							end,
						"customer_customerid_limit@component" =>
							Blather::Stanza::Iq::IBR.new.tap do |ibr|
								ibr.phone = "+15551234567"
							end
					}
				)
			)