test_component.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require_relative "../sgx-bwmsgsv2"
  5
  6def panic(e)
  7	$panic = e
  8end
  9
 10class ComponentTest < Minitest::Test
 11	def setup
 12		SGXbwmsgsv2.instance_variable_set(:@written, [])
 13
 14		def SGXbwmsgsv2.write_to_stream(s)
 15			@written ||= []
 16			@written << s
 17		end
 18
 19		REDIS.set("catapult_cred-test@example.com", [
 20			'account', 'user', 'password', '+15550000000'
 21		])
 22	end
 23
 24	def written
 25		SGXbwmsgsv2.instance_variable_get(:@written)
 26	end
 27
 28	def xmpp_error_name(error)
 29		error.find_first(
 30			"child::*[name()!='text']",
 31			Blather::StanzaError::STANZA_ERR_NS
 32		).element_name
 33	end
 34
 35	def process_stanza(s)
 36		SGXbwmsgsv2.send(:client).receive_data(s)
 37		raise $panic if $panic
 38	end
 39
 40	def test_message_unregistered
 41		m = Blather::Stanza::Message.new("+15551234567@component", "a"*4096)
 42		m.from = "unknown@example.com"
 43		process_stanza(m)
 44
 45		assert_equal 1, written.length
 46
 47		stanza = Blather::XMPPNode.parse(written.first.to_xml)
 48		assert stanza.error?
 49		error = stanza.find_first("error")
 50		assert_equal "auth", error["type"]
 51		assert_equal "registration-required", xmpp_error_name(error)
 52	end
 53	em :test_message_unregistered
 54
 55	def test_message_too_long
 56		req = stub_request(
 57			:post,
 58			"https://messaging.bandwidth.com/api/v2/users/account/messages"
 59		).with(body: {
 60			from: "+15550000000",
 61			to: "+15551234567",
 62			text: "a"*4096,
 63			applicationId: nil,
 64			tag: " "
 65		}).to_return(status: 400)
 66
 67		m = Blather::Stanza::Message.new("+15551234567@component", "a"*4096)
 68		m.from = "test@example.com"
 69		process_stanza(m)
 70
 71		assert_requested req
 72		assert_equal 1, written.length
 73
 74		stanza = Blather::XMPPNode.parse(written.first.to_xml)
 75		assert stanza.error?
 76		error = stanza.find_first("error")
 77		assert_equal "cancel", error["type"]
 78		assert_equal "internal-server-error", xmpp_error_name(error)
 79	end
 80	em :test_message_too_long
 81
 82	def test_message_to_component_not_group
 83		m = Blather::Stanza::Message.new("component", "a"*4096)
 84		m.from = "test@example.com"
 85		process_stanza(m)
 86
 87		assert_equal 1, written.length
 88
 89		stanza = Blather::XMPPNode.parse(written.first.to_xml)
 90		assert stanza.error?
 91		error = stanza.find_first("error")
 92		assert_equal "cancel", error["type"]
 93		assert_equal "item-not-found", xmpp_error_name(error)
 94	end
 95	em :test_message_to_component_not_group
 96
 97	def test_message_to_invalid_num
 98		m = Blather::Stanza::Message.new("123@component", "a"*4096)
 99		m.from = "test@example.com"
100		process_stanza(m)
101
102		assert_equal 1, written.length
103
104		stanza = Blather::XMPPNode.parse(written.first.to_xml)
105		assert stanza.error?
106		error = stanza.find_first("error")
107		assert_equal "cancel", error["type"]
108		assert_equal "item-not-found", xmpp_error_name(error)
109	end
110	em :test_message_to_invalid_num
111
112	def test_message_to_anonymous
113		m = Blather::Stanza::Message.new(
114			"1;phone-context=anonymous.phone-context.soprani.ca@component",
115			"a"*4096
116		)
117		m.from = "test@example.com"
118		process_stanza(m)
119
120		assert_equal 1, written.length
121
122		stanza = Blather::XMPPNode.parse(written.first.to_xml)
123		assert stanza.error?
124		error = stanza.find_first("error")
125		assert_equal "cancel", error["type"]
126		assert_equal "gone", xmpp_error_name(error)
127	end
128	em :test_message_to_anonymous
129
130	def test_blank_message
131		m = Blather::Stanza::Message.new("+15551234567@component", " ")
132		m.from = "test@example.com"
133		process_stanza(m)
134
135		assert_equal 1, written.length
136
137		stanza = Blather::XMPPNode.parse(written.first.to_xml)
138		assert stanza.error?
139		error = stanza.find_first("error")
140		assert_equal "modify", error["type"]
141		assert_equal "policy-violation", xmpp_error_name(error)
142	end
143	em :test_blank_message
144
145	def test_ibr_bad_tel
146		iq = Blather::Stanza::Iq::IBR.new(:set, "component")
147		iq.from = "newuser@example.com"
148		iq.phone = "5551234567"
149		process_stanza(iq)
150
151		assert_equal 1, written.length
152
153		stanza = Blather::XMPPNode.parse(written.first.to_xml)
154		assert stanza.error?
155		error = stanza.find_first("error")
156		assert_equal "cancel", error["type"]
157		assert_equal "item-not-found", xmpp_error_name(error)
158	end
159	em :test_ibr_bad_tel
160
161	def test_ibr_bad_creds
162		stub_request(
163			:get,
164			"https://messaging.bandwidth.com/api/v2/users/acct/media"
165		).with(basic_auth: ["user", "pw"]).to_return(status: 401)
166
167		iq = Blather::Stanza::Iq::IBR.new(:set, "component")
168		iq.from = "newuser@example.com"
169		iq.phone = "+15551234567"
170		iq.nick = "acct"
171		iq.username = "user"
172		iq.password = "pw"
173		process_stanza(iq)
174
175		assert_equal 1, written.length
176
177		stanza = Blather::XMPPNode.parse(written.first.to_xml)
178		assert stanza.error?
179		error = stanza.find_first("error")
180		assert_equal "auth", error["type"]
181		assert_equal "not-authorized", xmpp_error_name(error)
182	end
183	em :test_ibr_bad_creds
184
185	def test_ibr_number_not_found
186		stub_request(
187			:get,
188			"https://messaging.bandwidth.com/api/v2/users/acct/media"
189		).with(basic_auth: ["user", "pw"]).to_return(status: 404)
190
191		iq = Blather::Stanza::Iq::IBR.new(:set, "component")
192		iq.from = "newuser@example.com"
193		iq.phone = "+15551234567"
194		iq.nick = "acct"
195		iq.username = "user"
196		iq.password = "pw"
197		process_stanza(iq)
198
199		assert_equal 1, written.length
200
201		stanza = Blather::XMPPNode.parse(written.first.to_xml)
202		assert stanza.error?
203		error = stanza.find_first("error")
204		assert_equal "cancel", error["type"]
205		assert_equal "item-not-found", xmpp_error_name(error)
206	end
207	em :test_ibr_number_not_found
208
209	def test_ibr_other_error
210		stub_request(
211			:get,
212			"https://messaging.bandwidth.com/api/v2/users/acct/media"
213		).with(basic_auth: ["user", "pw"]).to_return(status: 400)
214
215		iq = Blather::Stanza::Iq::IBR.new(:set, "component")
216		iq.from = "newuser@example.com"
217		iq.phone = "+15551234567"
218		iq.nick = "acct"
219		iq.username = "user"
220		iq.password = "pw"
221		process_stanza(iq)
222
223		assert_equal 1, written.length
224
225		stanza = Blather::XMPPNode.parse(written.first.to_xml)
226		assert stanza.error?
227		error = stanza.find_first("error")
228		assert_equal "modify", error["type"]
229		assert_equal "not-acceptable", xmpp_error_name(error)
230	end
231	em :test_ibr_other_error
232
233	def test_ibr_conflict
234		stub_request(
235			:get,
236			"https://messaging.bandwidth.com/api/v2/users/acct/media"
237		).with(basic_auth: ["user", "pw"]).to_return(status: 200, body: "[]")
238
239		iq = Blather::Stanza::Iq::IBR.new(:set, "component")
240		iq.from = "test@example.com"
241		iq.phone = "+15550000000"
242		iq.nick = "acct"
243		iq.username = "user"
244		iq.password = "pw"
245		process_stanza(iq)
246
247		assert_equal 1, written.length
248
249		stanza = Blather::XMPPNode.parse(written.first.to_xml)
250		assert stanza.error?
251		error = stanza.find_first("error")
252		assert_equal "cancel", error["type"]
253		assert_equal "conflict", xmpp_error_name(error)
254	end
255	em :test_ibr_conflict
256end