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 NonNanpOobPropertyTest < Minitest::Test
9 BW_MESSAGES_URL =
10 "https://messaging.bandwidth.com/api/v2/users/account/messages"
11
12 def build_oob_message(dest, body, oob_url)
13 m = Blather::Stanza::Message.new("#{dest}@component", body)
14 m.from = "test@example.com/res"
15
16 x = Nokogiri::XML::Node.new("x", m.document)
17 ns = x.add_namespace(nil, "jabber:x:oob")
18 url_node = Nokogiri::XML::Node.new("url", m.document)
19 url_node.namespace = ns
20 url_node.content = oob_url
21 x.add_child(url_node)
22 m.add_child(x)
23 m
24 end
25
26 def test_non_nanp_oob_sends_url_as_text_not_mms
27 property_of {
28 dest = non_nanp_phone
29 body = choose("", sized(range(5, 40)) { string(:alnum) })
30 guard(!body.downcase.match?(BADWORDS))
31 oob_path = sized(range(3, 12)) { string(:alnum) }
32 oob_url = "https://example.com/media/#{oob_path}.jpg"
33 [dest, body, oob_url]
34 }.check { |dest, body, oob_url|
35 reset_stanzas!
36 reset_redis!
37
38 stub_request(:post, BW_MESSAGES_URL).to_return(
39 status: 201, body: JSON.dump(id: "bw-msg-fallback")
40 )
41 bw_req = stub_request(:post, BW_MESSAGES_URL).with(
42 body: hash_including(
43 text: /#{Regexp.escape(oob_url)}/
44 )
45 ).to_return(
46 status: 201, body: JSON.dump(id: "bw-msg-non-nanp")
47 )
48
49 process_stanza(build_oob_message(dest, body, oob_url))
50
51 assert_not_requested :head, oob_url
52 assert_requested bw_req
53 }
54 end
55 em :test_non_nanp_oob_sends_url_as_text_not_mms
56
57 def test_nanp_oob_attempts_mms
58 property_of {
59 dest = nanpa_phone
60 body = sized(range(5, 40)) { string(:alnum) }
61 guard(!body.downcase.match?(BADWORDS))
62 oob_path = sized(range(3, 12)) { string(:alnum) }
63 oob_url = "https://example.com/media/#{oob_path}.jpg"
64 [dest, body, oob_url]
65 }.check { |dest, body, oob_url|
66 reset_stanzas!
67 reset_redis!
68
69 stub_request(:post, BW_MESSAGES_URL).to_return(
70 status: 201, body: JSON.dump(id: "bw-msg-fallback")
71 )
72 bw_req = stub_request(:post, BW_MESSAGES_URL).with(
73 body: hash_including(media: oob_url)
74 ).to_return(
75 status: 201, body: JSON.dump(id: "bw-msg-nanp")
76 )
77 stub_request(:head, oob_url).to_return(
78 status: 200,
79 headers: {
80 "Content-Length" => "500",
81 "Content-Type" => "image/jpeg"
82 }
83 )
84
85 process_stanza(build_oob_message(dest, body, oob_url))
86
87 assert_requested :head, oob_url
88 assert_requested bw_req
89 }
90 end
91 em :test_nanp_oob_attempts_mms
92end