Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed May 9, 2024
1 parent 91c788a commit f51cd75
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 32 deletions.
5 changes: 4 additions & 1 deletion components/app/app/call_controllers/call_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ def register_event_handlers
def build_call_properties
return metadata[:call_properties] if metadata[:call_properties].present?

call_serializer = CallSerializer.new(call)

response = call_platform_client.create_call(
to: call.variables.fetch("variable_sip_h_x_somleng_callee_identity"),
from: call.variables.fetch("variable_sip_h_x_somleng_caller_identity"),
external_id: call.id,
external_id: call_serializer.id,
host: call_serializer.host,
source_ip: call.variables["variable_sip_h_x_src_ip"] || call.variables["variable_sip_via_host"],
client_identifier: call.variables["variable_sip_h_x_somleng_client_identifier"],
variables: {
Expand Down
24 changes: 24 additions & 0 deletions components/app/app/serializers/call_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "socket"

class CallSerializer
attr_reader :object

def initialize(object)
@object = object
end

def id
object.id
end

def host
Socket.ip_address_list.find { |interface| interface.ipv4_private? }.ip_address
end

def to_h
{
id:,
host:
}
end
end
2 changes: 1 addition & 1 deletion components/app/app/web/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class API < Application
post "/calls" do
call_params = JSON.parse(request.body.read)
resource = OutboundCall.new(call_params).initiate
json(id: resource.id)
json(CallSerializer.new(resource).to_h)
end

delete "/calls/:id" do
Expand Down
2 changes: 1 addition & 1 deletion components/app/app/workflows/execute_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ExecuteConnect < ExecuteTwiMLVerb
def initialize(verb, **options)
super
@redis_connection = options.fetch(:redis_connection) { -> { AppSettings.redis } }
@event_handler = options.fetch(:event_handler) { ConnectEventHandler.new }
@event_handler = options.fetch(:event_handler) { ConnectEventHandler.new(call_platform_client:) }
@call_update_event_handler = options.fetch(:call_update_event_handler) { CallUpdateEventHandler.new }
end

Expand Down
16 changes: 13 additions & 3 deletions components/app/spec/call_controllers/inbound_call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
)
expect(controller).to have_received(:say)
expect(controller).to have_received(:play_audio)
expect(WebMock).to(have_requested(:post, "http://api.lvh.me:3000/services/inbound_phone_calls").with { |request|
request_body = JSON.parse(request.body)
expect(request_body).to include(
"from" => "0715100960",
"external_id" => call.id,
"host" => be_present
)
})
end

it "handles inbound calls from client gateway", :vcr, cassette: :inbound_call do
Expand All @@ -43,8 +51,10 @@
controller.run

expect(WebMock).to(have_requested(:post, "http://api.lvh.me:3000/services/inbound_phone_calls").with { |request|
request_body = JSON.parse(request.body)
request_body.fetch("from") == "0715100960" && request_body.fetch("client_identifier") == "user1"
})
request_body = JSON.parse(request.body)
expect(request_body).to include(
"client_identifier" => "user1"
)
})
end
end
3 changes: 3 additions & 0 deletions components/app/spec/web/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module Web

expect(last_response.status).to eq(200)
expect(json_response["id"]).to eq(call_id)
expect(IPAddr.new(json_response["host"])).to have_attributes(
private?: true
)
end
end

Expand Down
3 changes: 2 additions & 1 deletion components/testing/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ fi
if [ "$1" = "services" ]; then
WS_SERVER_PORT="${WS_SERVER_PORT:="3001"}"
FILE_SERVER_PORT="${FILE_SERVER_PORT:="8000"}"
FILE_SERVER_LOG_FILE="${FILE_SERVER_LOG_FILE:="http-server.log"}"

nohup node ./support/ws_server/test_server.js --port "$WS_SERVER_PORT" > test-server.log &
nohup python3 -m http.server $FILE_SERVER_PORT > http-server.log &
nohup python3 -u -m http.server $FILE_SERVER_PORT 2> $FILE_SERVER_LOG_FILE &
tail -f /dev/null
fi

Expand Down
3 changes: 3 additions & 0 deletions components/testing/support/say.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Response>
<Say>Hello World from Test Server</Say>
</Response>
37 changes: 12 additions & 25 deletions components/testing/tests/public_gateway/modify_call_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ current_dir=$(dirname "$(readlink -f "$0")")
source $current_dir/support/test_helpers.sh
source $current_dir/../support/test_helpers.sh

log_file=$(find . -type f -iname "uas_*_messages.log")
cat /dev/null > $log_file

uas="$(hostname -i)"
media_server="$(dig +short freeswitch)"
call_sid="$(cat /proc/sys/kernel/random/uuid)"

output=$(curl -s -XPOST -u "adhearsion:password" http://switch-app:8080/calls \
output=$(curl -s -XPOST -u "adhearsion:password" http://switch-app:$SWITCH_PORT/calls \
-H 'Content-Type: application/json; charset=utf-8' \
--data-binary @- << EOF
{
"to": "+85512334667",
"from": "2442",
"voice_url": "https://demo.twilio.com/docs/voice.xml",
"voice_method": "POST",
"twiml": "<Response><Say>Hello how are you today? How is the weather?</Say></Response>",
"sid": "sample-call-sid",
"voice_url": null,
"voice_method": null,
"twiml": "<Response><Connect><Stream url=\"ws://$uas:$WS_SERVER_PORT\" /></Connect></Response>",
"sid": "$call_sid",
"account_sid": "sample-account-sid",
"account_auth_token": "sample-auth-token",
"direction": "outbound-api",
Expand All @@ -43,33 +40,23 @@ output=$(curl -s -XPOST -u "adhearsion:password" http://switch-app:8080/calls \
EOF
)

echo $output

call_id=$(echo $output | jq -r ".id")

echo $call_id
host=$(echo $output | jq -r ".host")

sleep 5

output=$(curl -s -XPATCH -u "adhearsion:password" http://switch-app:8080/calls/$call_id \
curl -s -XPATCH -u "adhearsion:password" http://$host:$SWITCH_PORT/calls/$call_id \
-H 'Content-Type: application/json; charset=utf-8' \
--data-binary @- << EOF
{
"voice_url": "https://demo.twilio.com/welcome/",
"voice_url": "http://$uas:$FILE_SERVER_PORT/support/say.xml",
"voice_method": "GET"
}
EOF
)

echo $output

sleep 10

if ! assert_in_file $log_file "c=IN IP4 $media_server"; then
exit 1
fi
sleep 5

# Checks that FreeSWITCH sets an empty rport
if ! assert_in_file $log_file "rport;"; then
# Checks the call SID is included in the request to get the new TwiML
if ! assert_in_file $FILE_SERVER_LOG_FILE "$call_sid"; then
exit 1
fi
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ services:
FIFO_DIR: /opensips/fifo
WS_SERVER_PORT: 3001
FILE_SERVER_PORT: 8000
SWITCH_PORT: 8080
FILE_SERVER_LOG_FILE: http-server.log
UAS: true
extra_hosts:
- "host.docker.internal:host-gateway"
Expand Down

0 comments on commit f51cd75

Please sign in to comment.