From a4886672686446be4798eef2489de5c1a533f2dc Mon Sep 17 00:00:00 2001 From: Karl Lingiah Date: Wed, 7 Aug 2024 17:00:59 +0100 Subject: [PATCH] DEVX-8561/DEVX-8603: Verify API and Voice API updates (#312) * Minor updates to Voice and Verify2 implementations --- .../verify2/start_verification_options.rb | 3 +- lib/vonage/voice.rb | 2 + lib/vonage/voice/actions/connect.rb | 1 + .../start_verification_options_test.rb | 30 ++++++++++++++- test/vonage/voice/ncco_test.rb | 37 +++++++++++++++++++ test/vonage/voice_test.rb | 24 ++++++++++++ 6 files changed, 95 insertions(+), 2 deletions(-) diff --git a/lib/vonage/verify2/start_verification_options.rb b/lib/vonage/verify2/start_verification_options.rb index 720b8c42..d1a212fd 100644 --- a/lib/vonage/verify2/start_verification_options.rb +++ b/lib/vonage/verify2/start_verification_options.rb @@ -5,7 +5,7 @@ module Vonage class Verify2::StartVerificationOptions VALID_OPTS = [:locale, :channel_timeout, :client_ref, :code_length, :code, :fraud_check].freeze - MIN_CHANNEL_TIMEOUT, MAX_CHANNEL_TIMEOUT = [60, 900] + MIN_CHANNEL_TIMEOUT, MAX_CHANNEL_TIMEOUT = [15, 900] MIN_CODE_LENGTH, MAX_CODE_LENGTH = [4, 10] @@ -22,6 +22,7 @@ def locale=(locale) end def channel_timeout=(channel_timeout) + raise ArgumentError, "Invalid 'channel_timeout' #{channel_timeout}. Must be an integer" unless channel_timeout.is_a?(Integer) unless channel_timeout.between?(MIN_CHANNEL_TIMEOUT, MAX_CHANNEL_TIMEOUT) raise ArgumentError, "Invalid 'channel_timeout' #{channel_timeout}. Must be between #{MIN_CHANNEL_TIMEOUT} and #{MAX_CHANNEL_TIMEOUT} (inclusive)" end diff --git a/lib/vonage/voice.rb b/lib/vonage/voice.rb index b1924f0c..d1f15ab4 100644 --- a/lib/vonage/voice.rb +++ b/lib/vonage/voice.rb @@ -18,6 +18,8 @@ class Voice < Namespace # # @option params [required, Array] :to # Connect to a Phone (PSTN) number, SIP Endpoint, Websocket, or VBC extension. + # The `to` Hash can contain a number of different properties depending on the `type`. + # See the API reference for specific details. # # @option params [Hash] :from # Connect to a Phone (PSTN) number. Should not be set if **:random_from_number** is **true** diff --git a/lib/vonage/voice/actions/connect.rb b/lib/vonage/voice/actions/connect.rb index cbb718b1..3b2b04fa 100644 --- a/lib/vonage/voice/actions/connect.rb +++ b/lib/vonage/voice/actions/connect.rb @@ -209,6 +209,7 @@ def sip_endpoint(endpoint_attrs) } hash.merge!(headers: endpoint_attrs[:headers]) if endpoint_attrs[:headers] + hash.merge!(standardHeaders: endpoint_attrs[:standardHeaders]) if endpoint_attrs[:standardHeaders] hash end diff --git a/test/vonage/verify2/start_verification_options_test.rb b/test/vonage/verify2/start_verification_options_test.rb index 22ac1cef..692972a6 100644 --- a/test/vonage/verify2/start_verification_options_test.rb +++ b/test/vonage/verify2/start_verification_options_test.rb @@ -27,10 +27,38 @@ def test_channel_timeout_setter_method assert_equal 90, opts.instance_variable_get(:@channel_timeout) end + def test_channel_timeout_setter_method_at_min_value + opts = options + opts.channel_timeout = 15 + + assert_equal 15, opts.instance_variable_get(:@channel_timeout) + end + + def test_channel_timeout_setter_method_at_max_value + opts = options + opts.channel_timeout = 900 + + assert_equal 900, opts.instance_variable_get(:@channel_timeout) + end + + def test_channel_timeout_setter_method_below_min_value + opts = options + assert_raises ArgumentError do + opts.channel_timeout = 14 + end + end + + def test_channel_timeout_setter_method_above_max_value + opts = options + assert_raises ArgumentError do + opts.channel_timeout = 901 + end + end + def test_channel_timeout_setter_method_with_invalid_arg opts = options assert_raises ArgumentError do - opts.channel_timeout = 0 + opts.channel_timeout = '90' end end diff --git a/test/vonage/voice/ncco_test.rb b/test/vonage/voice/ncco_test.rb index 241753ca..8290cc23 100644 --- a/test/vonage/voice/ncco_test.rb +++ b/test/vonage/voice/ncco_test.rb @@ -17,6 +17,43 @@ def test_ncco_with_valid_action_and_optional_parameters assert_equal action, [{:action=>"connect", :endpoint=>[{:type=>"phone", :number=>"12129999999"}], :from=>'12129992222'}] end + def test_ncco_connect_action_sip_endpoint_with_standard_headers + action = ncco.connect( + endpoint: { + type: 'sip', + uri: 'sip:rebekka@sip.mcrussell.com', + headers: { + location: 'New York', + occupation: 'developer' + }, + standardHeaders: { + 'User-to-User' => '56a390f3d2b7310023a2;encoding=hex;purpose=foo;content=bar' + } + } + ) + + action_literal = [ + { + :action=>"connect", + :endpoint=>[ + { + :type=>"sip", + :uri=>"sip:rebekka@sip.mcrussell.com", + :headers=>{ + location: 'New York', + occupation: 'developer' + }, + :standardHeaders => { + 'User-to-User' => '56a390f3d2b7310023a2;encoding=hex;purpose=foo;content=bar' + } + } + ] + } + ] + + assert_equal action_literal, action + end + def test_ncco_with_invalid_action exception = assert_raises { ncco.gotowarp } diff --git a/test/vonage/voice_test.rb b/test/vonage/voice_test.rb index 368a16d1..1cda492a 100644 --- a/test/vonage/voice_test.rb +++ b/test/vonage/voice_test.rb @@ -26,6 +26,30 @@ def test_create_method assert_kind_of Vonage::Response, calls.create(params) end + def test_create_method_with_connect_to_sip_standard_headers + params = { + to: [ + { + type: 'sip', + uri: 'sip:rebekka@sip.example.com', + headers: { + location: 'New York', + occupation: 'developer' + }, + standard_headers: { + 'User-to-User' => '56a390f3d2b7310023a2;encoding=hex;purpose=foo;content=bar' + } + } + ], + from: {type: 'phone', number: '14843335555'}, + answer_url: ['https://example.com/answer'] + } + + stub_request(:post, calls_uri).with(request(body: params)).to_return(response) + + assert_kind_of Vonage::Response, calls.create(params) + end + def test_create_method_raises_error_if_from_set_and_random_from_number_true params = { to: [{type: 'phone', number: '14843331234'}],