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			body = choose("", sized(range(5, 40)) { string(:alnum) })
27			guard(!body.downcase.match?(BADWORDS))
28			oob_path = sized(range(3, 12)) { string(:alnum) }
29			oob_url = "https://example.com/media/#{oob_path}.jpg"
30			[dest, body, oob_url]
31		}.check { |dest, body, oob_url|
32			reset_stanzas!
33			reset_redis!
34
35			stub_request(:post, BW_MESSAGES_URL).to_return(
36				status: 201, body: JSON.dump(id: "bw-msg-fallback")
37			)
38			bw_req = stub_request(:post, BW_MESSAGES_URL).with(
39				body: hash_including(
40					text: /#{Regexp.escape(oob_url)}/
41				)
42			).to_return(
43				status: 201, body: JSON.dump(id: "bw-msg-non-nanp")
44			)
45
46			process_stanza(build_oob_message(dest, body, oob_url))
47
48			assert_not_requested :head, oob_url
49			assert_requested bw_req
50		}
51	end
52	em :test_non_nanp_oob_sends_url_as_text_not_mms
53
54	def test_nanp_oob_attempts_mms
55		property_of {
56			dest = nanpa_phone
57			body = sized(range(5, 40)) { string(:alnum) }
58			guard(!body.downcase.match?(BADWORDS))
59			oob_path = sized(range(3, 12)) { string(:alnum) }
60			oob_url = "https://example.com/media/#{oob_path}.jpg"
61			[dest, body, oob_url]
62		}.check { |dest, body, oob_url|
63			reset_stanzas!
64			reset_redis!
65
66			stub_request(:post, BW_MESSAGES_URL).to_return(
67				status: 201, body: JSON.dump(id: "bw-msg-fallback")
68			)
69			bw_req = stub_request(:post, BW_MESSAGES_URL).with(
70				body: hash_including(media: oob_url)
71			).to_return(
72				status: 201, body: JSON.dump(id: "bw-msg-nanp")
73			)
74			stub_request(:head, oob_url).to_return(
75				status: 200,
76				headers: {
77					"Content-Length" => "500",
78					"Content-Type" => "image/jpeg"
79				}
80			)
81
82			process_stanza(build_oob_message(dest, body, oob_url))
83
84			assert_requested :head, oob_url
85			assert_requested bw_req
86		}
87	end
88	em :test_nanp_oob_attempts_mms
89end