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