From 0dfe909e59d98a444384cb5e4f23e99026f935a2 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 12 May 2021 19:16:44 -0500 Subject: [PATCH] Import newly-purchased numbers to Catapult Since we still use Catapult ("v1") for voice application, we need to import all numbers there and set their applicationId to match. --- .rubocop.yml | 4 +++ config.dhall.sample | 6 ++++ lib/bandwidth_tn_order.rb | 54 ++++++++++++++++++++++++++++- test/data/catapult_import_body.json | 1 + test/test_bandwidth_tn_order.rb | 34 ++++++++++++++++-- test/test_helper.rb | 6 ++++ test/test_registration.rb | 15 ++++++++ 7 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 test/data/catapult_import_body.json diff --git a/.rubocop.yml b/.rubocop.yml index 72686f4..4546d8f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,6 +6,10 @@ Metrics/LineLength: Exclude: - Gemfile +Metrics/ClassLength: + Exclude: + - test/* + Metrics/MethodLength: Exclude: - test/* diff --git a/config.dhall.sample b/config.dhall.sample index ca7516f..c56d6fb 100644 --- a/config.dhall.sample +++ b/config.dhall.sample @@ -13,6 +13,12 @@ username = "dashboard user", password = "dashboard password" }, + catapult = { + user = "", + token = "", + secret = "", + application_id = "" + }, bandwidth_site = "", braintree = { environment = "sandbox", diff --git a/lib/bandwidth_tn_order.rb b/lib/bandwidth_tn_order.rb index 47faf09..970f82c 100644 --- a/lib/bandwidth_tn_order.rb +++ b/lib/bandwidth_tn_order.rb @@ -70,8 +70,60 @@ class BandwidthTNOrder :complete end + def tel + "+1#{@order.completed_numbers[:telephone_number][:full_number]}" + end + def poll - EMPromise.resolve(self) + catapult_import.then do |http| + raise "Catapult import failed" unless http.response_header.status == 201 + + self + end + end + + protected + + # After buying, import to catapult and set v1 voice app + def catapult_import + catapult_request.apost( + head: catapult_headers, + body: { + number: tel, + applicationId: catapult[:application_id], + provider: dashboard_provider + }.to_json + ) + end + + def catapult + CONFIG[:catapult] + end + + def catapult_request + EM::HttpRequest.new( + "https://api.catapult.inetwork.com/v1/users/" \ + "#{catapult[:user]}/phoneNumbers", + tls: { verify_peer: true } + ) + end + + def catapult_headers + { + "Authorization" => [catapult[:token], catapult[:secret]], + "Content-Type" => "application/json" + } + end + + def dashboard_provider + { + providerName: "bandwidth-dashboard", + properties: { + accountId: CONFIG[:creds][:account], + userName: CONFIG[:creds][:username], + password: CONFIG[:creds][:password] + } + } end end diff --git a/test/data/catapult_import_body.json b/test/data/catapult_import_body.json new file mode 100644 index 0000000..bd990b5 --- /dev/null +++ b/test/data/catapult_import_body.json @@ -0,0 +1 @@ +{"number":"+15555550000","applicationId":"catapult_app","provider":{"providerName":"bandwidth-dashboard","properties":{"accountId":"test_bw_account","userName":"test_bw_user","password":"test_bw_password"}}} diff --git a/test/test_bandwidth_tn_order.rb b/test/test_bandwidth_tn_order.rb index 50e8bef..9be753a 100644 --- a/test/test_bandwidth_tn_order.rb +++ b/test/test_bandwidth_tn_order.rb @@ -53,10 +53,23 @@ class BandwidthTNOrderTest < Minitest::Test ).to_return(status: 200, body: <<~RESPONSE) COMPLETE + + + 5555550000 + + RESPONSE - new_order = PromiseMock.new - new_order.expect(:poll, nil) + stub_request( + :post, + "https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers" + ).with( + body: open(__dir__ + "/data/catapult_import_body.json").read.chomp, + headers: { + "Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0", + "Content-Type" => "application/json" + } + ).to_return(status: 200) @order.poll.sync assert_requested req end @@ -65,11 +78,26 @@ class BandwidthTNOrderTest < Minitest::Test class TestComplete < Minitest::Test def setup - @order = BandwidthTNOrder::Complete.new(BandwidthIris::Order.new) + @order = BandwidthTNOrder::Complete.new(BandwidthIris::Order.new( + completed_numbers: { + telephone_number: { full_number: "5555550000" } + } + )) end def test_poll + req = stub_request( + :post, + "https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers" + ).with( + body: open(__dir__ + "/data/catapult_import_body.json").read.chomp, + headers: { + "Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0", + "Content-Type" => "application/json" + } + ).to_return(status: 200) assert_equal @order, @order.poll.sync + assert_requested req end em :test_poll end diff --git a/test/test_helper.rb b/test/test_helper.rb index b9859a3..eeee48a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -44,6 +44,12 @@ CONFIG = { username: "test_bw_user", password: "test_bw_password" }, + catapult: { + user: "catapult_user", + token: "catapult_token", + secret: "catapult_secret", + application_id: "catapult_app" + }, activation_amount: 1, plans: [ { diff --git a/test/test_registration.rb b/test/test_registration.rb index 08c23d6..390b18f 100644 --- a/test/test_registration.rb +++ b/test/test_registration.rb @@ -273,8 +273,23 @@ class RegistrationTest < Minitest::Test ).to_return(status: 201, body: <<~RESPONSE) COMPLETE + + + 5555550000 + + RESPONSE + stub_request( + :post, + "https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers" + ).with( + body: open(__dir__ + "/data/catapult_import_body.json").read.chomp, + headers: { + "Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0", + "Content-Type" => "application/json" + } + ).to_return(status: 200) BACKEND_SGX.expect( :register!, EMPromise.resolve(OpenStruct.new(error?: false)), -- 2.45.2