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