~singpolyma/sgx-jmp

48ce76db30c9fbded0eab39c3ab241d88bdea91f — Stephen Paul Weber 2 years ago a0bb2b9
Pass messages to and from the SGX

Rewriting the from/to as appropriate.
6 files changed, 108 insertions(+), 1 deletions(-)

M .rubocop.yml
M lib/backend_sgx.rb
A lib/blather_ext.rb
M lib/customer.rb
M sgx_jmp.rb
M test/test_customer.rb
M .rubocop.yml => .rubocop.yml +4 -0
@@ 47,6 47,10 @@ Style/DoubleNegation:
  EnforcedStyle: allowed_in_returns
  Enabled: false

Style/RegexpLiteral:
  EnforcedStyle: slashes
  AllowInnerSlashes: true

Layout/SpaceAroundEqualsInParameterDefault:
  EnforcedStyle: no_space


M lib/backend_sgx.rb => lib/backend_sgx.rb +11 -1
@@ 26,10 26,20 @@ class BackendSgx
		end
	end

	def stanza(s)
		s.dup.tap do |stanza|
			stanza.to = stanza.to.with(domain: @jid)
			stanza.from = from_jid.with(resource: stanza.from.resource)
		end
	end

protected

	def from_jid
		"customer_#{@customer_id}@#{CONFIG[:component][:jid]}"
		Blather::JID.new(
			"customer_#{@customer_id}",
			CONFIG[:component][:jid]
		)
	end

	def mkibr(type)

A lib/blather_ext.rb => lib/blather_ext.rb +9 -0
@@ 0,0 1,9 @@
# frozen_string_literal: true

module Blather
	class JID
		def with(node: self.node, domain: self.domain, resource: self.resource)
			self.class.new(node, domain, resource)
		end
	end
end

M lib/customer.rb => lib/customer.rb +20 -0
@@ 2,6 2,7 @@

require "forwardable"

require_relative "./blather_ext"
require_relative "./customer_plan"
require_relative "./backend_sgx"
require_relative "./ibr"


@@ 80,5 81,24 @@ class Customer
			.then(PaymentMethods.method(:for_braintree_customer))
	end

	def jid
		@jid ||= REDIS.get("jmp_customer_jid-#{customer_id}").then do |sjid|
			Blather::JID.new(sjid)
		end
	end

	def stanza_to(stanza)
		jid.then do |jid|
			stanza = stanza.dup
			stanza.to = jid.with(resource: stanza.to&.resource)
			stanza.from = stanza.from.with(domain: CONFIG[:component][:jid])
			BLATHER << stanza
		end
	end

	def stanza_from(stanza)
		BLATHER << @sgx.stanza(stanza)
	end

	protected def_delegator :@plan, :expires_at
end

M sgx_jmp.rb => sgx_jmp.rb +12 -0
@@ 113,6 113,18 @@ setup(
	workqueue_count: 0
)

message to: /\Acustomer_/, from: /@#{CONFIG[:sgx]}(\/|\Z)/ do |m|
	Customer.for_customer_id(
		m.to.node.delete_prefix("customer_")
	).then { |customer| customer.stanza_to(m) }.catch(&method(:panic))
end

message do |m|
	Customer.for_jid(m.from.stripped).then { |customer|
		customer.stanza_from(m)
	}.catch(&method(:panic))
end

message :error? do |m|
	puts "MESSAGE ERROR: #{m.inspect}"
end

M test/test_customer.rb => test/test_customer.rb +52 -0
@@ 3,6 3,7 @@
require "test_helper"
require "customer"

Customer::BLATHER = Minitest::Mock.new
Customer::BRAINTREE = Minitest::Mock.new
Customer::REDIS = Minitest::Mock.new
Customer::DB = Minitest::Mock.new


@@ 124,4 125,55 @@ class CustomerTest < Minitest::Test
		CustomerPlan::DB.verify
	end
	em :test_bill_plan_update

	def test_jid
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve("test@example.com"),
			["jmp_customer_jid-test"]
		)
		jid = Customer.new("test").jid.sync
		assert_kind_of Blather::JID, jid
		assert_equal "test@example.com", jid.to_s
		Customer::REDIS.verify
	end
	em :test_jid

	def test_stanza_to
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve("test@example.com"),
			["jmp_customer_jid-test"]
		)
		Customer::BLATHER.expect(
			:<<,
			nil,
			[Matching.new do |stanza|
				assert_equal "+15555550000@component/a", stanza.from.to_s
				assert_equal "test@example.com/b", stanza.to.to_s
			end]
		)
		m = Blather::Stanza::Message.new
		m.from = "+15555550000@sgx/a"
		m.to = "customer_test@component/b"
		Customer.new("test").stanza_to(m).sync
		Customer::BLATHER.verify
	end
	em :test_stanza_to

	def test_stanza_from
		Customer::BLATHER.expect(
			:<<,
			nil,
			[Matching.new do |stanza|
				assert_equal "customer_test@component/a", stanza.from.to_s
				assert_equal "+15555550000@sgx/b", stanza.to.to_s
			end]
		)
		m = Blather::Stanza::Message.new
		m.from = "test@example.com/a"
		m.to = "+15555550000@component/b"
		Customer.new("test").stanza_from(m)
		Customer::BLATHER.verify
	end
end