test_non_nanp_oob.rb

 1# frozen_string_literal: true
 2
 3require "test_helper"
 4require_relative "../../sgx-bwmsgsv2"
 5require "rantly/minitest_extensions"
 6require_relative "rantly_extensions/data_extensions"
 7
 8class NonNanpOobPropertyTest < Minitest::Test
 9	def build_oob_message(dest, body, oob_url)
10		m = Blather::Stanza::Message.new("#{dest}@component", body)
11		m.from = "test@example.com/res"
12
13		x = Nokogiri::XML::Node.new("x", m.document)
14		ns = x.add_namespace(nil, "jabber:x:oob")
15		url_node = Nokogiri::XML::Node.new("url", m.document)
16		url_node.namespace = ns
17		url_node.content = oob_url
18		x.add_child(url_node)
19		m.add_child(x)
20		m
21	end
22
23	def test_non_nanp_oob_sends_url_as_text_not_mms
24		property_of {
25			dest = non_nanp_phone
26			oob_path = sized(range(3, 12)) { string(:alnum) }
27			oob_url = "https://example.com/media/#{oob_path}.jpg"
28			[dest, message_body, oob_url]
29		}.check { |dest, body, oob_url|
30			reset_stanzas!
31			reset_redis!
32
33			stub_request(:post, BW_MESSAGES_URL).to_return(
34				status: 201, body: JSON.dump(id: "bw-msg-fallback")
35			)
36			bw_req = stub_request(:post, BW_MESSAGES_URL).with(
37				body: hash_including(
38					text: /#{Regexp.escape(oob_url)}/
39				)
40			).to_return(
41				status: 201, body: JSON.dump(id: "bw-msg-non-nanp")
42			)
43
44			process_stanza(build_oob_message(dest, body, oob_url))
45
46			assert_not_requested :head, oob_url
47			assert_requested bw_req
48		}
49	end
50	em :test_non_nanp_oob_sends_url_as_text_not_mms
51
52	def test_nanp_oob_attempts_mms
53		property_of {
54			dest = nanpa_phone
55			oob_path = sized(range(3, 12)) { string(:alnum) }
56			oob_url = "https://example.com/media/#{oob_path}.jpg"
57			[dest, message_body, oob_url]
58		}.check { |dest, body, oob_url|
59			reset_stanzas!
60			reset_redis!
61
62			stub_request(:post, BW_MESSAGES_URL).to_return(
63				status: 201, body: JSON.dump(id: "bw-msg-fallback")
64			)
65			bw_req = stub_request(:post, BW_MESSAGES_URL).with(
66				body: hash_including(media: oob_url)
67			).to_return(
68				status: 201, body: JSON.dump(id: "bw-msg-nanp")
69			)
70			stub_request(:head, oob_url).to_return(
71				status: 200,
72				headers: {
73					"Content-Length" => "500",
74					"Content-Type" => "image/jpeg"
75				}
76			)
77
78			process_stanza(build_oob_message(dest, body, oob_url))
79
80			assert_requested :head, oob_url
81			assert_requested bw_req
82		}
83	end
84	em :test_nanp_oob_attempts_mms
85end