test_invisible_separator.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 InvisibleSeparatorPropertyTest < Minitest::Test
 9	BW_MESSAGES_URL =
10		"https://messaging.bandwidth.com/api/v2/users/account/messages"
11
12	def setup
13		reset_stanzas!
14		reset_redis!
15	end
16
17	def test_message_containing_invisible_separator_is_rejected
18		property_of {
19			words = array(range(1, 6)) { sized(range(3, 10)) { string(:alnum) } }
20			guard(words.none? { |w| BADWORD_LIST.include?(w.downcase) })
21			chars = words.join(" ").chars
22			insertions = range(1, 3)
23			insertions.times { chars.insert(range(0, chars.length), "\u2063") }
24			body = chars.join
25
26			dest = nanpa_phone
27			[body, dest]
28		}.check { |body, dest|
29			reset_stanzas!
30			reset_redis!
31
32			stub_request(:post, BW_MESSAGES_URL).to_return(
33				status: 201,
34				body: JSON.dump(id: "bw-msg-stub")
35			)
36
37			m = Blather::Stanza::Message.new("#{dest}@component", body)
38			m.from = "test@example.com"
39			process_stanza(m)
40
41			assert_equal 1, written.length,
42			             "Expected exactly one error stanza for body: #{body.inspect}"
43
44			stanza = Blather::XMPPNode.parse(written.first.to_xml)
45			assert stanza.error?,
46			       "Expected error stanza for body: #{body.inspect}"
47
48			error = stanza.find_first("error")
49			assert_equal "wait", error["type"],
50			             "Expected error type 'wait' for body: #{body.inspect}"
51			assert_equal "recipient-unavailable", xmpp_error_name(error),
52			             "Expected 'recipient-unavailable' for body: #{body.inspect}"
53		}
54	end
55	em :test_message_containing_invisible_separator_is_rejected
56
57	def test_message_without_invisible_separator_is_not_rejected_as_unavailable
58		property_of {
59			words = array(range(1, 6)) { sized(range(3, 10)) { string(:alnum) } }
60			guard(words.none? { |w| BADWORD_LIST.include?(w.downcase) })
61			body = words.join(" ")
62
63			dest = nanpa_phone
64			[body, dest]
65		}.check { |body, dest|
66			reset_stanzas!
67			reset_redis!
68
69			stub_request(:post, BW_MESSAGES_URL).to_return(
70				status: 201,
71				body: JSON.dump(id: "bw-msg-stub")
72			)
73
74			m = Blather::Stanza::Message.new("#{dest}@component", body)
75			m.from = "test@example.com"
76			process_stanza(m)
77
78			written.each do |response|
79				stanza = Blather::XMPPNode.parse(response.to_xml)
80				next unless stanza.error?
81
82				error = stanza.find_first("error")
83				msg = "Clean message rejected as " \
84					"recipient-unavailable: #{body.inspect}"
85				refute_equal "recipient-unavailable",
86				             xmpp_error_name(error), msg
87			end
88		}
89	end
90	em :test_message_without_invisible_separator_is_not_rejected_as_unavailable
91end