exercise MMS rewriting for long msgs

Phillip Davis created

basically, add FakeFs and set MMS_{PATH, URL}, then fix things that
those two broke

Change summary

Gemfile                                   |  1 
sgx-bwmsgsv2.rb                           |  2 
test/property/test_invisible_separator.rb |  4 +
test/test_component.rb                    | 28 +++++++-------
test/test_helper.rb                       | 46 ++++++++++++++++++------
5 files changed, 53 insertions(+), 28 deletions(-)

Detailed changes

Gemfile 🔗

@@ -46,4 +46,5 @@ group(:test) do
 	# DSL wrapper around Rantly generators
 	gem "jennifer", github: "phdavis1027/jennifer"
 	gem "regexp-examples", "~> 1.6"
+	gem 'fakefs', require: false
 end

sgx-bwmsgsv2.rb 🔗

@@ -469,7 +469,7 @@ module SGXbwmsgsv2
 		end
 
 		segment_size = body.ascii_only? ? 160 : 70
-		if !murl && ENV["MMS_PATH"] && body.length > segment_size*3
+		if !murl && ENV["MMS_PATH"] && num_dest =~ /^\+?1/ && body.length > segment_size*3
 			file = Multibases.pack(
 				'base58btc',
 				Multihashes.encode(Digest::SHA256.digest(body), "sha2-256")

test/property/test_invisible_separator.rb 🔗

@@ -67,7 +67,9 @@ class InvisibleSeparatorPropertyTest < Minitest::Test
 			# mess when you can.
 			bw_req = stub_request(:post, BW_MESSAGES_URL).with { |req|
 				parsed = ::JSON.parse(req.body)
-				parsed["to"] == dest && parsed["text"] == m.body.to_s
+				parsed["to"] == dest &&
+					(parsed["text"] == m.body.to_s ||
+					 (parsed["text"] == "" && parsed["media"]))
 			}.to_return(
 				status: 201,
 				body: JSON.dump(id: "bw-msg-stub")

test/test_component.rb 🔗

@@ -10,6 +10,7 @@ class ComponentTest < Minitest::Test
 	def setup
 		reset_stanzas!
 		reset_redis!
+		FakeFS.clear!
 	end
 
 	def test_message_unregistered
@@ -28,33 +29,32 @@ class ComponentTest < Minitest::Test
 	em :test_message_unregistered
 
 	def test_message_too_long
+		body = "a" * 4096
+		file = Multibases.pack(
+			'base58btc',
+			Multihashes.encode(Digest::SHA256.digest(body), "sha2-256")
+		).to_s
+		expected_url = "https://mms.test.example.com/#{file}.txt"
+
 		req = stub_request(
 			:post,
 			"https://messaging.bandwidth.com/api/v2/users/account/messages"
 		).with(body: {
 			from: "+15550000000",
 			to: "+15551234567",
-			text: "a"*4096,
+			text: "",
+			media: expected_url,
 			applicationId: nil,
 			tag: " "
-		}).to_return(status: 400, body: JSON.dump(
-			description: "Bad text.",
-			fieldErrors: [{ description: "4096 not allowed" }]
-		))
+		}).to_return(status: 201, body: JSON.dump(id: "bw-mms-123"))
 
-		m = Blather::Stanza::Message.new("+15551234567@component", "a"*4096)
+		m = Blather::Stanza::Message.new("+15551234567@component", body)
 		m.from = "test@example.com"
 		process_stanza(m)
 
 		assert_requested req
-		assert_equal 1, written.length
-
-		stanza = Blather::XMPPNode.parse(written.first.to_xml)
-		assert stanza.error?
-		error = stanza.find_first("error")
-		assert_equal "cancel", error["type"]
-		assert_equal "internal-server-error", xmpp_error_name(error)
-		assert_equal "Bad text. 4096 not allowed", xmpp_error_text(error)
+		assert_empty written
+		assert_equal body, File.read("/mms/#{file}")
 	end
 	em :test_message_too_long
 

test/test_helper.rb 🔗

@@ -9,6 +9,7 @@ end
 
 require "minitest/autorun"
 require "webmock/minitest"
+require "fakefs/safe"
 
 MMS_PROXY = "https://proxy.test.example.com/"
 BW_MESSAGES_URL = "https://messaging.bandwidth.com/api/v2/users/account/messages"
@@ -85,6 +86,17 @@ def xmpp_error_text(error)
 	error.find_first("ns:text", ns: Blather::StanzaError::STANZA_ERR_NS)&.text
 end
 
+def with_mms_env(path: "/mms", url: "https://mms.test.example.com")
+	old_path = ENV["MMS_PATH"]
+	old_url = ENV["MMS_URL"]
+	ENV["MMS_PATH"] = path
+	ENV["MMS_URL"] = url
+	yield
+ensure
+	ENV["MMS_PATH"] = old_path
+	ENV["MMS_URL"] = old_url
+end
+
 
 begin
 	require "pry-byebug"
@@ -241,18 +253,28 @@ module Minitest
 			define_method(m) do
 				$panic = nil
 				e = nil
-				EM.run do
-					Fiber.new {
-						ARGV[0] = "component"
-						ARGV[6] = MMS_PROXY
-						begin
-							send("raw_#{m}")
-						rescue
-							e = $!
-						ensure
-							EM.stop
-						end
-					}.resume
+				FakeFS do
+					FakeFS::FileSystem.clear
+					FileUtils.mkdir_p("/mms")
+					EM.run do
+						Fiber.new {
+							with_mms_env do
+								ARGV[0] = "component"
+								ARGV[6] = MMS_PROXY
+								begin
+									send("raw_#{m}")
+								rescue
+									e = $!
+								ensure
+									EM.stop
+								end
+							end # with_mms_env do
+						}.resume
+					end # EM.run do
+				end # FakeFs do
+				if e
+					LOG.error("Error in test: #{m}", e)
+					raise e
 				end
 				raise e if e
 			end