test_to_catapult.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 ToCatapultPropertyTest < Minitest::Test
 9	def test_accepts_201_and_202_with_escaped_tag
10		property_of {
11			dest = nanpa_phone
12			guard(dest != "+15550000000")
13
14			words = array(range(1, 4)) { sized(range(3, 8)) { string(:alnum) } }
15			guard(words.none? { |w| BADWORD_LIST.include?(w.downcase) })
16			body = words.join(" ")
17
18			stanza_id = maybe_http_escapable_string
19			resource = maybe_http_escapable_string
20			status = choose(201, 202)
21
22			[dest, body, stanza_id, resource, status]
23		}.check { |dest, body, stanza_id, resource, status|
24			reset_stanzas!
25			reset_redis!
26			WebMock.reset!
27
28			bw_request_body = nil
29			bw_req = stub_request(:post, BW_MESSAGES_URL).to_return { |request|
30				bw_request_body = JSON.parse(request.body)
31				{
32					status: status,
33					body: JSON.dump(id: "bw-msg-id", time: Time.now.iso8601)
34				}
35			}
36
37			m = Blather::Stanza::Message.new("#{dest}@component", body)
38			m.from = "test@example.com/#{resource}"
39			m.id = stanza_id
40
41			process_stanza(m)
42
43			expected_tag = WEBrick::HTTPUtils.escape(stanza_id) +
44				" " +
45				WEBrick::HTTPUtils.escape(resource)
46			assert_equal expected_tag, bw_request_body["tag"],
47				"Tag should be HTTP-escaped (status=#{status})"
48
49			assert_requested bw_req
50
51			written.each do |response|
52				stanza = Blather::XMPPNode.parse(response.to_xml)
53				refute stanza.error?,
54					"Expected success for status #{status} but got error"
55			end
56		}
57	end
58	em :test_accepts_201_and_202_with_escaped_tag
59end