diff --git a/.rubocop.yml b/.rubocop.yml index b3d81c8b9e0e82064618fa8955ee81880159e7d3..996f457d11c89c9a7c255fc9273332c6a5f01837 100644 --- a/.rubocop.yml +++ b/.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 diff --git a/lib/backend_sgx.rb b/lib/backend_sgx.rb index 2414afb650492397f0497a78c38ddf70e2a391f0..85e1b82b8a722e4c0668ce9c81c1a99dc075f1c5 100644 --- a/lib/backend_sgx.rb +++ b/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) diff --git a/lib/blather_ext.rb b/lib/blather_ext.rb new file mode 100644 index 0000000000000000000000000000000000000000..eb85cf55185873997d6fefb383d009986e4c265f --- /dev/null +++ b/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 diff --git a/lib/customer.rb b/lib/customer.rb index e0f4dcdc91fd9a4bd5b81477e1259d1414a0e24f..409f0f3460990e2bb0538e6ca1e75ba48af5933f 100644 --- a/lib/customer.rb +++ b/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 diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 810153e33742a6c220d3dbf649ec72671cb520e6..7668757c7a8a3d57490613b5c9a8fd42c3939e5b 100644 --- a/sgx_jmp.rb +++ b/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 diff --git a/test/test_customer.rb b/test/test_customer.rb index 400c85de6b3a7eb3546e397bc6c9ca19907cc083..bbe812c1ffcee1cabeb16a8175ef68dfe0defa51 100644 --- a/test/test_customer.rb +++ b/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