1# frozen_string_literal: true
2
3require "rack/test"
4require "test_helper"
5require_relative "../web"
6
7Customer::BLATHER = Minitest::Mock.new
8
9class WebTest < Minitest::Test
10 include Rack::Test::Methods
11
12 def app
13 Web.opts[:customer_repo] = CustomerRepo.new(
14 redis: FakeRedis.new(
15 "jmp_customer_jid-customerid" => "customer@example.com",
16 "catapult_jid-+15551234567" => "customer_customerid@component"
17 ),
18 db: FakeDB.new,
19 sgx_repo: Bwmsgsv2Repo.new(
20 redis: FakeRedis.new,
21 ibr_repo: FakeIBRRepo.new(
22 "sgx" => {
23 "customer_customerid@component" => IBR.new.tap do |ibr|
24 ibr.phone = "+15551234567"
25 end
26 }
27 )
28 )
29 )
30 Web.app
31 end
32
33 def test_outbound_forwards
34 post(
35 "/outbound/calls",
36 { from: "customerid", to: "+15557654321" }.to_json,
37 { "CONTENT_TYPE" => "application/json" }
38 )
39
40 assert last_response.ok?
41 assert_equal(
42 "<?xml version=\"1.0\" encoding=\"utf-8\" ?><Response>" \
43 "<Forward from=\"+15551234567\" to=\"+15557654321\" />" \
44 "</Response>",
45 last_response.body
46 )
47 end
48 em :test_outbound_forwards
49
50 def test_voicemail
51 Customer::BLATHER.expect(
52 :<<,
53 nil,
54 [Matching.new do |stanza|
55 assert_equal "+15557654321@component", stanza.from.to_s
56 assert_equal "customer@example.com", stanza.to.to_s
57 assert_equal "https://jmp.chat/media", OOB.find_or_create(stanza).url
58 end]
59 )
60
61 post(
62 "/inbound/calls/CALLID/voicemail/audio",
63 {
64 "startTime" => "2021-01-01T00:00:00Z",
65 "endTime" => "2021-01-01T00:00:06Z",
66 "mediaUrl" => "https://voice.bandwidth.com/api/v2/accounts/1/media",
67 "to" => "+15551234567",
68 "from" => "+15557654321"
69 }.to_json,
70 { "CONTENT_TYPE" => "application/json" }
71 )
72
73 assert last_response.ok?
74 assert_mock Customer::BLATHER
75 end
76 em :test_voicemail
77
78 def test_anonymous_voicemail
79 Customer::BLATHER.expect(
80 :<<,
81 nil,
82 [Matching.new do |stanza|
83 assert_equal(
84 "16;phone-context=anonymous.phone-context.soprani.ca@component",
85 stanza.from.to_s
86 )
87 assert_equal "customer@example.com", stanza.to.to_s
88 assert_equal "https://jmp.chat/media", OOB.find_or_create(stanza).url
89 end]
90 )
91
92 post(
93 "/inbound/calls/CALLID/voicemail/audio",
94 {
95 "startTime" => "2021-01-01T00:00:00Z",
96 "endTime" => "2021-01-01T00:00:06Z",
97 "mediaUrl" => "https://voice.bandwidth.com/api/v2/accounts/1/media",
98 "to" => "+15551234567",
99 "from" => "Anonymous"
100 }.to_json,
101 { "CONTENT_TYPE" => "application/json" }
102 )
103
104 assert last_response.ok?
105 assert_mock Customer::BLATHER
106 end
107 em :test_anonymous_voicemail
108
109 def test_voicemail_short
110 post(
111 "/inbound/calls/CALLID/voicemail/audio",
112 {
113 "startTime" => "2021-01-01T00:00:00Z",
114 "endTime" => "2021-01-01T00:00:05Z"
115 }.to_json,
116 { "CONTENT_TYPE" => "application/json" }
117 )
118
119 assert last_response.ok?
120 assert_mock Customer::BLATHER
121 end
122 em :test_voicemail_short
123end