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		WebMock.reset!
16	end
17
18	BW_MESSAGES_URL =
19		"https://messaging.bandwidth.com/api/v2/users/account/messages"
20
21	def test_message_containing_invisible_separator_is_rejected
22		property_of {
23			words = array(range(1, 6)) { sized(range(3, 10)) { string(:alnum) } }
24			guard(words.none? { |w| BADWORD_LIST.include?(w.downcase) })
25			chars = words.join(" ").chars
26			insertions = range(1, 3)
27			insertions.times { chars.insert(range(0, chars.length), "\u2063") }
28			body = chars.join
29
30			dest = nanpa_phone
31			[body, dest]
32		}.check { |body, dest|
33			reset_stanzas!
34			reset_redis!
35			WebMock.reset!
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			WebMock.reset!
69
70			bw_req = stub_request(:post, BW_MESSAGES_URL).with(
71				body: hash_including(
72					to: dest,
73					text: body
74				)
75			).to_return(
76				status: 201,
77				body: JSON.dump(id: "bw-msg-stub")
78			)
79
80			m = Blather::Stanza::Message.new("#{dest}@component", body)
81			m.from = "test@example.com"
82			process_stanza(m)
83
84			assert_empty written,
85			             "Expected no error stanzas for body: #{body.inspect}"
86			assert_requested bw_req
87		}
88	end
89	em :test_message_without_invisible_separator_is_not_rejected_as_unavailable
90end