tests for change_number

Phillip Davis created

Change summary

test/test_admin_command.rb | 76 +++++++++++++++++++++++++++------------
1 file changed, 52 insertions(+), 24 deletions(-)

Detailed changes

test/test_admin_command.rb 🔗

@@ -2,6 +2,7 @@
 
 require "test_helper"
 require "admin_command"
+require "admin_action"
 
 BackendSgx::IQ_MANAGER = Minitest::Mock.new
 AdminAction::LaunchSnikket::IQ_MANAGER = Minitest::Mock.new
@@ -9,11 +10,22 @@ Customer::BLATHER = Minitest::Mock.new
 AdminActionRepo::REDIS = Minitest::Mock.new
 TrivialBackendSgxRepo::REDIS = Minitest::Mock.new
 BandwidthTnRepo::DB = Minitest::Mock.new
+AdminAction::NumberChange::REDIS = Minitest::Mock.new
 
 class AdminCommandTest < Minitest::Test
-	def admin_command(tel="+15556667777")
+	def assert_undoable_form(iq, note_type=nil, note_text=nil)
+		assert_equal :form, iq.form.type
+		[:execute, :next, :complete].each do |action|
+			assert iq.allowed_actions.include?(action)
+		end
+		assert_equal iq.note_type, note_type if note_type
+
+		assert iq.note_text = note_text if note_text
+	end
+
+	def admin_command(tel="+15556667777", registered: OpenStruct.new(phone: tel))
 		sgx = Minitest::Mock.new(OpenStruct.new(
-			registered?: OpenStruct.new(phone: tel)
+			registered?: registered
 		))
 		[
 			sgx,
@@ -88,9 +100,9 @@ class AdminCommandTest < Minitest::Test
 				:write,
 				EMPromise.resolve(q_form),
 				[Matching.new do |iq|
-					 assert_equal :form, iq.form.type
-					 assert iq.form.field("q")
-				 end]
+					assert_equal :form, iq.form.type
+					assert iq.form.field("q")
+				end]
 			)
 
 			AdminCommand.for(nil, customer_repo).start.catch { |e| e }
@@ -137,36 +149,36 @@ class AdminCommandTest < Minitest::Test
 				:write,
 				EMPromise.resolve(domain_form),
 				[Matching.new do |iq|
-					 assert_equal :form, iq.form.type
-					 assert iq.form.field("domain")
-				 end]
+					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]
+					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]
+					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 }
@@ -338,4 +350,20 @@ class AdminCommandTest < Minitest::Test
 		assert_requested bandwidth_req
 	end
 	em :test_action_cancel_account_keep_number
+
+	def test_change_num_for_unregistered_customer
+		_sgx, admin = admin_command(registered: false)
+
+		error =
+			assert_raises("number_change should have raised") {
+				admin.action_number_change.sync
+			}
+
+		assert_equal "Customer not registered", error.message
+
+		assert_mock BackendSgx::IQ_MANAGER
+		assert_mock Customer::BLATHER
+		assert_mock BandwidthTnRepo::DB
+	end
+	em :test_change_num_for_unregistered_customer
 end