~singpolyma/dev

add new admin command to launch snikket instance for existing customer v1 SUPERSEDED

SavagePeanut: 2
 add new admin command to launch snikket instance for existing customer
 test for admin launch snikket command

 4 files changed, 106 insertions(+), 10 deletions(-)
Not sure what to do about this.

1) Error:

AdminCommandTest#test_action_launch_snikket:
NoMethodError: undefined method `reply' for nil:NilClass
     /home/peanut/jmp/sgx-jmp/lib/command.rb:19:in `reply'
     /home/peanut/jmp/sgx-jmp/lib/admin_command.rb:159:in `reply'
/home/peanut/jmp/sgx-jmp/lib/admin_actions/launch_snikket.rb:18:in `call'
/home/peanut/jmp/sgx-jmp/lib/admin_actions/launch_snikket.rb:18:in `block (2 levels) in call'
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~singpolyma/dev/patches/50486/mbox | git am -3
Learn more about email & git

[PATCH 1/2] add new admin command to launch snikket instance for existing customer Export this patch

---
 forms/admin_menu.rb                 |  3 ++-
 lib/admin_actions/launch_snikket.rb | 26 ++++++++++++++++++++++++++
 lib/admin_command.rb                | 23 +++++++++++++++--------
 3 files changed, 43 insertions(+), 9 deletions(-)
 create mode 100644 lib/admin_actions/launch_snikket.rb

diff --git a/forms/admin_menu.rb b/forms/admin_menu.rb
index 2d46719..01e1738 100644
--- a/forms/admin_menu.rb
+++ b/forms/admin_menu.rb
@@ -24,6 +24,7 @@ field(
		{ value: "set_trust_level", label: "Set Trust Level" },
		{ value: "add_invites", label: "Add Referral Codes" },
		{ value: "number_change", label: "Number Change" },
		{ value: "add_transaction", label: "Add Transaction" }
		{ value: "add_transaction", label: "Add Transaction" },
		{ value: "launch_snikket", label: "Launch Snikket" }
	]
)
diff --git a/lib/admin_actions/launch_snikket.rb b/lib/admin_actions/launch_snikket.rb
new file mode 100644
index 0000000..98f9661
--- /dev/null
+++ b/lib/admin_actions/launch_snikket.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class AdminAction
	class LaunchSnikket
		def self.call(customer_id, reply:, snikket_repo:, **)
			reply.call(
				FormTemplate.render("snikket_launch")
			).then { |response|
				domain = response.form.field("domain").value.to_s
				IQ_MANAGER.write(Snikket::Launch.new(
					nil, CONFIG[:snikket_hosting_api],
					domain: domain
				)).then do |launched|
					Snikket::CustomerInstance.for(customer_id, domain, launched)
				end
			}.then { |instance|
				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.44.0

[PATCH 2/2] test for admin launch snikket command Export this patch

---
 test/test_admin_command.rb | 64 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/test/test_admin_command.rb b/test/test_admin_command.rb
index 16730db..7bb9211 100644
--- a/test/test_admin_command.rb
+++ b/test/test_admin_command.rb
@@ -4,17 +4,79 @@ require "test_helper"
require "admin_command"

BackendSgx::IQ_MANAGER = Minitest::Mock.new
AdminAction::LaunchSnikket::IQ_MANAGER = Minitest::Mock.new
Customer::BLATHER = Minitest::Mock.new
AdminActionRepo::REDIS = Minitest::Mock.new
DB = FakeDB.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)]
		[sgx, AdminCommand.new(customer(sgx: sgx), CustomerRepo.new, Snikket::Repo.new)]
	end

	def test_action_launch_snikket
		sgx, admin = admin_command
		domain_form = Blather::Stanza::Iq::Command.new
		domain_form.form.fields = [
			{ var: "domain", value: "test.snikket.chat" }
		]

		launched = Snikket::Launched.new
		launched << Niceogiri::XML::Node.new(
			:launched, launched.document, "xmpp:snikket.org/hosting/v1"
		).tap { |inner|
			inner << Niceogiri::XML::Node.new(
				:'instance-id', launched.document, "xmpp:snikket.org/hosting/v1"
			).tap { |id|
				id.content = "si-1234"
			}
			inner << Niceogiri::XML::Node.new(
				:bootstrap, launched.document, "xmpp:snikket.org/hosting/v1"
			).tap { |bootstrap|
				bootstrap << Niceogiri::XML::Node.new(
					:token, launched.document, "xmpp:snikket.org/hosting/v1"
				).tap { |token|
					token.content = "TOKEN"
				}
			}
		}

		Command::COMMAND_MANAGER.expect(
			:write,
			EMPromise.resolve(domain_form),
			[Matching.new do |iq|
				assert_equal :form, iq.form.type
				assert iq.form.field("domain")
			end]
		)

		AdminAction::LaunchSnikket::IQ_MANAGER.expect(
			:write,
			EMPromise.resolve(launched),
			[Matching.new do |iq|
				assert_equal :set, iq.type
				assert_equal CONFIG[:snikket_hosting_api], iq.to.to_s
				assert_equal(
					"test.snikket.chat",
					iq.xpath(
						"./ns:launch/ns:domain",
						ns: "xmpp:snikket.org/hosting/v1"
					).text
				)
			end]
		)

		execute_command { admin.action_launch_snikket }

		assert_mock sgx
		assert_mock AdminAction::LaunchSnikket::IQ_MANAGER
		assert_mock Customer::BLATHER
	end
	em :test_action_launch_snikket

	def test_action_cancel_account
		sgx, admin = admin_command

-- 
2.44.0
Not sure what to do about this.

1) Error:

AdminCommandTest#test_action_launch_snikket:
NoMethodError: undefined method `reply' for nil:NilClass
     /home/peanut/jmp/sgx-jmp/lib/command.rb:19:in `reply'
     /home/peanut/jmp/sgx-jmp/lib/admin_command.rb:159:in `reply'
/home/peanut/jmp/sgx-jmp/lib/admin_actions/launch_snikket.rb:18:in `call'
/home/peanut/jmp/sgx-jmp/lib/admin_actions/launch_snikket.rb:18:in `block (2 levels) in call'