From 784e896fae15ec190e705aff78bb01572ba88862 Mon Sep 17 00:00:00 2001 From: SavagePeanut Date: Wed, 27 Mar 2024 16:22:43 -0500 Subject: [PATCH] test for admin launch snikket command --- lib/admin_command.rb | 8 +++- test/test_admin_command.rb | 81 +++++++++++++++++++++++++++++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/lib/admin_command.rb b/lib/admin_command.rb index 101030f1a1ef2b4905c1cab5e2c6d457e7ccb9f3..5c1a64efc22ad3c8fafbf1b9a993a10f3917a0c2 100644 --- a/lib/admin_command.rb +++ b/lib/admin_command.rb @@ -102,8 +102,12 @@ 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, @snikket_repo) - .then { |ac| ac.start(command_action) } + AdminCommand.for( + new_customer, + @customer_repo, + @admin_action_repo, + @snikket_repo + ).then { |ac| ac.start(command_action) } end end diff --git a/test/test_admin_command.rb b/test/test_admin_command.rb index 16730db91555b238dc15ec0a899b55e239bc695c..3bdefc5452c2637ce8ef474f3184d8dc8bcbbb75 100644 --- a/test/test_admin_command.rb +++ b/test/test_admin_command.rb @@ -4,17 +4,96 @@ 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" + } + } + } + + result = execute_command { + 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] + ) + Command::COMMAND_MANAGER.expect( + :write, + EMPromise.reject(:test_result), + [Matching.new do |iq| + assert :result, iq.type + assert( + "https://test.snikket.chat/invites_bootstrap?token=TOKEN", + iq.form.field("bootstrap-uri").value + ) + 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] + ) + + admin.action_launch_snikket.catch { |e| e } + } + + assert_equal :test_result, result + 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