From 2c04bc443b1616e9dbb8ca4f0ca2ce1f76ae5440 Mon Sep 17 00:00:00 2001 From: Karl Lingiah Date: Wed, 19 Jun 2024 10:40:07 +0100 Subject: [PATCH] Verify v2 Validation Updates (#309) * Adding validation to `from` and `brand` parameters for sms and whatsapp channels --- .github/workflows/ci.yml | 2 +- CHANGES.md | 4 ++ lib/vonage/verify2.rb | 3 ++ lib/vonage/verify2/channels/sms.rb | 10 +++++ lib/vonage/verify2/channels/whats_app.rb | 1 + lib/vonage/version.rb | 2 +- test/vonage/test.rb | 2 +- .../verify2/channels/silent_auth_test.rb | 4 +- test/vonage/verify2/channels/sms_test.rb | 38 +++++++++++++++++-- test/vonage/verify2/channels/voice_test.rb | 4 +- .../channels/whats_app_interactive_test.rb | 4 +- .../vonage/verify2/channels/whats_app_test.rb | 20 ++++++++-- test/vonage/verify2_test.rb | 24 ++++++++++++ 13 files changed, 102 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f7859441..cbd4d798 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] - ruby: [2.5, 2.6, 2.7, 3.0, 3.1] + ruby: [3.0, 3.1, 3.2, 3.3] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 diff --git a/CHANGES.md b/CHANGES.md index 0b349f3d..27453b20 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +# 7.25.0 + +* Validation updates to Verify v2 SMS and WhatsApp channels. [#309](https://github.com/Vonage/vonage-ruby-sdk/pull/309) + # 7.24.0 * Updating Video API functionality with methods for Live Captions, Audio Connector, Experience Composer, and a `publisheronly` cleint token role. [#307](https://github.com/Vonage/vonage-ruby-sdk/pull/307) diff --git a/lib/vonage/verify2.rb b/lib/vonage/verify2.rb index d4311986..9027b7d7 100644 --- a/lib/vonage/verify2.rb +++ b/lib/vonage/verify2.rb @@ -17,6 +17,7 @@ class Verify2 < Namespace # ) # # @param [required, String] :brand The brand that is sending the verification request + # - Must be between 1 and 16 characters in length # # @param [required, Array] :workflow An array of hashes for channels in the workflow # @@ -32,6 +33,8 @@ class Verify2 < Namespace # @see https://developer.vonage.com/en/api/verify.v2#newRequest # def start_verification(brand:, workflow:, **opts) + raise ArgumentError, ':brand must be a String' unless brand.is_a?(String) + raise ArgumentError, "Invalid 'brand' value #{brand}. Length must be between 1 and 16 characters." unless brand.length.between?(1, 16) raise ArgumentError, ':workflow must be an Array' unless workflow.is_a?(Array) raise ArgumentError, ':workflow must not be empty' if workflow.empty? diff --git a/lib/vonage/verify2/channels/sms.rb b/lib/vonage/verify2/channels/sms.rb index f7cb1a79..220abce6 100644 --- a/lib/vonage/verify2/channels/sms.rb +++ b/lib/vonage/verify2/channels/sms.rb @@ -20,6 +20,7 @@ def to=(to) end def from=(from) + validate_from(from) @from = from end @@ -49,5 +50,14 @@ def to_h private attr_writer :channel + + def validate_from(from) + if from.match?(/\D/) + raise ArgumentError, "Invalid alpha-numeric 'from' value #{from}. Length must be between 3 and 11 characters." unless from.length.between?(3, 11) + else + raise ArgumentError, "Invalid numeric 'from' value #{from}. Length must be between 11 and 15 characters." unless from.length.between?(11, 15) + raise ArgumentError, "Invalid 'from' value #{from}. Expected to be in E.164 format" unless Phonelib.parse(from).valid? + end + end end end diff --git a/lib/vonage/verify2/channels/whats_app.rb b/lib/vonage/verify2/channels/whats_app.rb index 4dcbbd7b..ad333f1f 100644 --- a/lib/vonage/verify2/channels/whats_app.rb +++ b/lib/vonage/verify2/channels/whats_app.rb @@ -19,6 +19,7 @@ def to=(to) end def from=(from) + raise ArgumentError, "Invalid 'from' value #{from}. Length must be between 11 and 15 characters." unless from.length.between?(11, 15) raise ArgumentError, "Invalid 'from' value #{from}. Expected to be in E.164 format" unless Phonelib.parse(from.to_i).valid? @from = from end diff --git a/lib/vonage/version.rb b/lib/vonage/version.rb index 4281258d..a44d3131 100644 --- a/lib/vonage/version.rb +++ b/lib/vonage/version.rb @@ -1,5 +1,5 @@ # typed: strong module Vonage - VERSION = '7.24.0' + VERSION = '7.25.0' end diff --git a/test/vonage/test.rb b/test/vonage/test.rb index bee82215..95032d12 100644 --- a/test/vonage/test.rb +++ b/test/vonage/test.rb @@ -408,7 +408,7 @@ def e164_compliant_number '447000000000' end - def invalid_number + def non_e164_compliant_number 'abcdefg' end diff --git a/test/vonage/verify2/channels/silent_auth_test.rb b/test/vonage/verify2/channels/silent_auth_test.rb index 40e17784..877a8505 100644 --- a/test/vonage/verify2/channels/silent_auth_test.rb +++ b/test/vonage/verify2/channels/silent_auth_test.rb @@ -21,9 +21,9 @@ def test_to_setter_method assert_equal new_number, sa_workflow.instance_variable_get(:@to) end - def test_to_setter_method_with_invalid_number + def test_to_setter_method_with_non_e164_compliant_number assert_raises ArgumentError do - silent_auth_channel.to = invalid_number + silent_auth_channel.to = non_e164_compliant_number end end diff --git a/test/vonage/verify2/channels/sms_test.rb b/test/vonage/verify2/channels/sms_test.rb index 51159ac0..1fed0a54 100644 --- a/test/vonage/verify2/channels/sms_test.rb +++ b/test/vonage/verify2/channels/sms_test.rb @@ -29,9 +29,9 @@ def test_to_setter_method assert_equal new_number, channel.instance_variable_get(:@to) end - def test_to_setter_method_with_invalid_number + def test_to_setter_method_with_non_e164_compliant_number assert_raises ArgumentError do - sms_channel.to = invalid_number + sms_channel.to = non_e164_compliant_number end end @@ -39,7 +39,7 @@ def test_from_getter_method assert_nil sms_channel.from end - def test_from_setter_method + def test_from_setter_method_with_valid_numeric channel = sms_channel new_number = '447000000002' channel.from = new_number @@ -47,6 +47,38 @@ def test_from_setter_method assert_equal new_number, channel.instance_variable_get(:@from) end + def test_from_setter_method_with_valid_alphanumeric + channel = sms_channel + new_number = 'abc123' + channel.from = new_number + + assert_equal new_number, channel.instance_variable_get(:@from) + end + + def test_from_setter_method_with_numeric_too_short + assert_raises ArgumentError do + sms_channel.from = '4470000002' + end + end + + def test_from_setter_method_with_numeric_too_long + assert_raises ArgumentError do + sms_channel.from = '4470000000000002' + end + end + + def test_from_setter_method_with_alphanumeric_too_short + assert_raises ArgumentError do + sms_channel.from = 'ab' + end + end + + def test_from_setter_method_with_alphanumeric_too_long + assert_raises ArgumentError do + sms_channel.from = 'abcdefghijkl' + end + end + def test_entity_id_getter_method assert_nil sms_channel.entity_id end diff --git a/test/vonage/verify2/channels/voice_test.rb b/test/vonage/verify2/channels/voice_test.rb index ecbe5e1e..00643229 100644 --- a/test/vonage/verify2/channels/voice_test.rb +++ b/test/vonage/verify2/channels/voice_test.rb @@ -25,9 +25,9 @@ def test_to_setter_method assert_equal new_number, channel.instance_variable_get(:@to) end - def test_to_setter_method_with_invalid_number + def test_to_setter_method_with_non_e164_compliant_number assert_raises ArgumentError do - voice_channel.to = invalid_number + voice_channel.to = non_e164_compliant_number end end diff --git a/test/vonage/verify2/channels/whats_app_interactive_test.rb b/test/vonage/verify2/channels/whats_app_interactive_test.rb index 44377ddb..ef11de9b 100644 --- a/test/vonage/verify2/channels/whats_app_interactive_test.rb +++ b/test/vonage/verify2/channels/whats_app_interactive_test.rb @@ -25,9 +25,9 @@ def test_to_setter_method assert_equal new_number, channel.instance_variable_get(:@to) end - def test_to_setter_method_with_invalid_number + def test_to_setter_method_with_non_e164_compliant_number assert_raises ArgumentError do - whatsapp_interactive_channel.to = invalid_number + whatsapp_interactive_channel.to = non_e164_compliant_number end end diff --git a/test/vonage/verify2/channels/whats_app_test.rb b/test/vonage/verify2/channels/whats_app_test.rb index b4d8b38d..5c4d074c 100644 --- a/test/vonage/verify2/channels/whats_app_test.rb +++ b/test/vonage/verify2/channels/whats_app_test.rb @@ -29,9 +29,9 @@ def test_to_setter_method assert_equal new_number, channel.instance_variable_get(:@to) end - def test_to_setter_method_with_invalid_number + def test_to_setter_method_with_non_e164_compliant_number assert_raises ArgumentError do - whatsapp_channel.to = invalid_number + whatsapp_channel.to = non_e164_compliant_number end end @@ -47,9 +47,21 @@ def test_from_setter_method assert_equal new_number, channel.instance_variable_get(:@from) end - def test_from_setter_method_with_invalid_number + def test_from_setter_method_with_non_e164_compliant_number assert_raises ArgumentError do - whatsapp_channel.from = invalid_number + whatsapp_channel.from = non_e164_compliant_number + end + end + + def test_from_setter_method_with_number_too_short + assert_raises ArgumentError do + whatsapp_channel.from = '4470000002' + end + end + + def test_from_setter_method_with_number_too_long + assert_raises ArgumentError do + whatsapp_channel.from = '4470000000000002' end end diff --git a/test/vonage/verify2_test.rb b/test/vonage/verify2_test.rb index e51bd600..f2aa08e3 100644 --- a/test/vonage/verify2_test.rb +++ b/test/vonage/verify2_test.rb @@ -77,6 +77,30 @@ def test_start_verification_method_without_brand end end + def test_start_verification_method_with_invalid_brand_type + workflow = [{channel: 'sms', to: to_number}] + + assert_raises ArgumentError do + verify2.start_verification(brand: 123, workflow: workflow) + end + end + + def test_start_verification_method_with_brand_too_short + workflow = [{channel: 'sms', to: to_number}] + + assert_raises ArgumentError do + verify2.start_verification(brand: '', workflow: workflow) + end + end + + def test_start_verification_method_with_brand_too_long + workflow = [{channel: 'sms', to: to_number}] + + assert_raises ArgumentError do + verify2.start_verification(brand: 'abcdefghijklmnopq', workflow: workflow) + end + end + def test_start_verification_method_without_workflow assert_raises ArgumentError do verify2.start_verification(brand: brand)