Pass messages to and from the SGX

Stephen Paul Weber created

Rewriting the from/to as appropriate.

Change summary

.rubocop.yml          |  4 +++
lib/backend_sgx.rb    | 12 +++++++++
lib/blather_ext.rb    |  9 +++++++
lib/customer.rb       | 20 +++++++++++++++++
sgx_jmp.rb            | 12 ++++++++++
test/test_customer.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 108 insertions(+), 1 deletion(-)

Detailed changes

.rubocop.yml 🔗

@@ -47,6 +47,10 @@ Style/DoubleNegation:
   EnforcedStyle: allowed_in_returns
   Enabled: false
 
+Style/RegexpLiteral:
+  EnforcedStyle: slashes
+  AllowInnerSlashes: true
+
 Layout/SpaceAroundEqualsInParameterDefault:
   EnforcedStyle: no_space
 

lib/backend_sgx.rb 🔗

@@ -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)

lib/blather_ext.rb 🔗

@@ -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

lib/customer.rb 🔗

@@ -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

sgx_jmp.rb 🔗

@@ -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

test/test_customer.rb 🔗

@@ -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