SavagePeanut: 3 add new admin command to launch snikket instance for existing customer rename test_admin_command to test_admin_cancel_account test for admin launch snikket command 4 files changed, 183 insertions(+), 8 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~singpolyma/dev/patches/50476/mbox | git am -3Learn more about email & git
--- lib/admin_actions/launch_snikket.rb | 22 ++++++++++++++++++++++ lib/admin_command.rb | 23 +++++++++++++++-------- 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 lib/admin_actions/launch_snikket.rb diff --git a/lib/admin_actions/launch_snikket.rb b/lib/admin_actions/launch_snikket.rb new file mode 100644 index 0000000..4edf1fd --- /dev/null +++ b/lib/admin_actions/launch_snikket.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class AdminAction + class LaunchSnikket + def self.call(customer, reply, snikket_repo:, **)
This should be reply: since you pass it as a named argument.
+ reply.call( + FormTemplate.render("snikket_launch") + ).then { |response| + domain = response.form.field("domain").value.to_s + launch_snikket(domain) + }.then { |launched| + instance = ::Snikket::CustomerInstance.for(customer, domain, launched)
Domain will not be available here, since it is a local variable declared in the previous block. Two options to thread it through, either nest this inside: launch_snikket(domain).then { |lanched| ... } or return the domain along with the launch result: EMPromise.all([launch_snikket(domain), domain])
+ snikket_repo.put(instance).then do + reply.call(FormTemplate.render( + "snikket_launched", + instance: instance + )) + end + } + end + end +end diff --git a/lib/admin_command.rb b/lib/admin_command.rb index c35d48a..101030f 100644 --- a/lib/admin_command.rb +++ b/lib/admin_command.rb @@ -8,6 +8,7 @@ require_relative "admin_actions/financial" require_relative "admin_actions/reset_declines" require_relative "admin_actions/set_trust_level" require_relative "admin_actions/number_change" +require_relative "admin_actions/launch_snikket" require_relative "bill_plan_command" require_relative "customer_info_form" require_relative "financial_info" @@ -17,10 +18,11 @@ class AdminCommand def self.for( target_customer, customer_repo, - admin_action_repo=AdminActionRepo.new + admin_action_repo=AdminActionRepo.new, + snikket_repo=Snikket::Repo.new ) if target_customer - new(target_customer, customer_repo, admin_action_repo) + new(target_customer, customer_repo, admin_action_repo, snikket_repo) else NoUser.new(customer_repo, admin_action_repo, notice: "Customer Not Found") end @@ -51,11 +53,13 @@ class AdminCommand def initialize( target_customer, customer_repo, - admin_action_repo=AdminActionRepo.new + admin_action_repo=AdminActionRepo.new, + snikket_repo=Snikket::Repo.new ) @target_customer = target_customer @customer_repo = customer_repo @admin_action_repo = admin_action_repo + @snikket_repo = snikket_repo end def start(command_action=:execute) @@ -98,7 +102,7 @@ class AdminCommand def new_context(q, command_action=:execute) CustomerInfoForm.new(@customer_repo) .parse_something(q).then do |new_customer| - AdminCommand.for(new_customer, @customer_repo, @admin_action_repo) + AdminCommand.for(new_customer, @customer_repo, @admin_action_repo, @snikket_repo) .then { |ac| ac.start(command_action) } end end @@ -142,11 +146,12 @@ class AdminCommand @klass = klass end - def call(customer_id, customer_repo:, **) + def call(customer_id, customer_repo:, snikket_repo:, **) @klass.call( customer_id, reply: method(:reply), - customer_repo: customer_repo + customer_repo: customer_repo, + snikket_repo: snikket_repo ).then { nil } end @@ -180,13 +185,15 @@ class AdminCommand [:set_trust_level, Undoable.new(AdminAction::SetTrustLevel::Command)], [:add_invites, Undoable.new(AdminAction::AddInvites::Command)], [:number_change, Undoable.new(AdminAction::NumberChange::Command)], - [:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)] + [:add_transaction, Undoable.new(AdminAction::AddTransaction::Command)], + [:launch_snikket, Simple.new(AdminAction::LaunchSnikket)] ].each do |action, handler| define_method("action_#{action}") do handler.call( @target_customer, admin_action_repo: @admin_action_repo, - customer_repo: @customer_repo + customer_repo: @customer_repo, + snikket_repo: @snikket_repo ) end end -- 2.43.2
Somebody claiming to be SavagePeanut wrote:
--- test/test_admin_cancel_account.rb | 118 ++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/test_admin_cancel_account.rb diff --git a/test/test_admin_cancel_account.rb b/test/test_admin_cancel_account.rb new file mode 100644 index 0000000..16730db --- /dev/null +++ b/test/test_admin_cancel_account.rb @@ -0,0 +1,118 @@ +# frozen_string_literal: true + +require "test_helper" +require "admin_command" + +BackendSgx::IQ_MANAGER = Minitest::Mock.new +Customer::BLATHER = Minitest::Mock.new +AdminActionRepo::REDIS = Minitest::Mock.new + +class AdminCommandTest < Minitest::Test + def admin_command(tel="+15556667777") + sgx = Minitest::Mock.new(OpenStruct.new( + registered?: OpenStruct.new(phone: tel) + )) + [sgx, AdminCommand.new(customer(sgx: sgx), CustomerRepo.new)] + end + + def test_action_cancel_account + sgx, admin = admin_command + + Customer::BLATHER.expect( + :<<, + EMPromise.resolve(nil), + [ + Matching.new do |m| + assert_equal "Your JMP account has been cancelled.", m.body + assert_equal "test@example.net", m.to.to_s + assert_equal "notify_from@component", m.from.to_s + end + ] + ) + + Customer::BLATHER.expect( + :<<, + EMPromise.resolve(nil), + [ + Matching.new do |iq| + assert iq.remove? + assert_equal "test@example.net", iq.to.to_s + assert_equal "component", iq.from.to_s + end + ] + ) + + sgx.expect(:deregister!, EMPromise.resolve(nil)) + + stub_request( + :post, + "https://dashboard.bandwidth.com/v1.0/accounts//disconnects" + ).with( + body: { + name: "test", + DisconnectTelephoneNumberOrderType: { + TelephoneNumberList: { + TelephoneNumber: "5556667777" + } + } + }.to_xml(indent: 0, root: "DisconnectTelephoneNumberOrder") + ).to_return(status: 200, body: "") + + admin.action_cancel_account.sync + + assert_mock sgx + assert_mock BackendSgx::IQ_MANAGER + assert_mock Customer::BLATHER + end + em :test_action_cancel_account + + def test_action_cancel_account_keep_number + sgx, admin = admin_command("+15566667777") + + Customer::BLATHER.expect( + :<<, + EMPromise.resolve(nil), + [ + Matching.new do |m| + assert_equal "Your JMP account has been cancelled.", m.body + assert_equal "test@example.net", m.to.to_s + assert_equal "notify_from@component", m.from.to_s + end + ] + ) + + Customer::BLATHER.expect( + :<<, + EMPromise.resolve(nil), + [ + Matching.new do |iq| + assert iq.remove? + assert_equal "test@example.net", iq.to.to_s + assert_equal "component", iq.from.to_s + end + ] + ) + + sgx.expect(:deregister!, EMPromise.resolve(nil)) + + stub_request( + :post, + "https://dashboard.bandwidth.com/v1.0/accounts/moveto/moveTns" + ).with( + body: { + CustomerOrderId: "test", + SourceAccountId: "test_bw_account", + SiteId: "movetosite", + SipPeerId: "movetopeer", + TelephoneNumbers: { TelephoneNumber: "5566667777" } + }.to_xml(indent: 0, root: "MoveTnsOrder") + ).to_return(status: 200, body: "") + + admin.action_cancel_account.sync + + assert_mock sgx + assert_mock BackendSgx::IQ_MANAGER + assert_mock Customer::BLATHER + end + em :test_action_cancel_account_keep_number +end -- 2.43.2
--- test/test_admin_launch_snikket.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/test_admin_launch_snikket.rb diff --git a/test/test_admin_launch_snikket.rb b/test/test_admin_launch_snikket.rb new file mode 100644 index 0000000..87224aa --- /dev/null +++ b/test/test_admin_launch_snikket.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require "test_helper" +require "admin_command" + +BackendSgx::IQ_MANAGER = Minitest::Mock.new +Customer::BLATHER = Minitest::Mock.new +AdminActionRepo::REDIS = Minitest::Mock.new + +class AdminCommandTest < Minitest::Test + def admin_command(tel="+15556667777") + sgx = Minitest::Mock.new(OpenStruct.new( + registered?: OpenStruct.new(phone: tel) + )) + [sgx, AdminCommand.new(customer(sgx: sgx), CustomerRepo.new, Snikket::Repo.new)] + end + + def test_action_launch_snikket + sgx, admin = admin_command + + admin.action_launch_snikket.sync
Since you make use of implicit reply functionality in this one, you probably will need to wrap this like so: execute_command { admin.action_launch_snikket }
+ + assert_mock sgx + assert_mock BackendSgx::IQ_MANAGER + assert_mock Customer::BLATHER + end + em :test_action_launch_snikket +end -- 2.43.2