Refactor IBR get/form to use the IBR helpers

Stephen Paul Weber created

Change summary

sgx-bwmsgsv2.rb        | 43 +++++++++++--------------------------------
test/test_component.rb | 22 ++++++++++++++++++++++
2 files changed, 33 insertions(+), 32 deletions(-)

Detailed changes

sgx-bwmsgsv2.rb 🔗

@@ -590,22 +590,10 @@ module SGXbwmsgsv2
 	end
 
 	def self.registration_form(orig, existing_number=nil)
-		msg = Nokogiri::XML::Node.new 'query', orig.document
-		msg['xmlns'] = 'jabber:iq:register'
-
-		if existing_number
-			msg.add_child(
-				Nokogiri::XML::Node.new(
-					'registered', msg.document
-				)
-			)
-		end
+		orig.registered = !!existing_number
 
 		# TODO: update "User Id" x2 below (to "accountId"?), and others?
-		n1 = Nokogiri::XML::Node.new(
-			'instructions', msg.document
-		)
-		n1.content = "Enter the information from your Account "\
+		orig.instructions = "Enter the information from your Account "\
 			"page as well as the Phone Number\nin your "\
 			"account you want to use (ie. '+12345678901')"\
 			".\nUser Id is nick, API Token is username, "\
@@ -614,18 +602,12 @@ module SGXbwmsgsv2
 			"https://gitlab.com/soprani.ca/sgx-bwmsgsv2 ."\
 			"\nCopyright (C) 2017-2020  Denver Gingerich "\
 			"and others, licensed under AGPLv3+."
-		n2 = Nokogiri::XML::Node.new 'nick', msg.document
-		n3 = Nokogiri::XML::Node.new 'username', msg.document
-		n4 = Nokogiri::XML::Node.new 'password', msg.document
-		n5 = Nokogiri::XML::Node.new 'phone', msg.document
-		n5.content = existing_number.to_s
-		msg.add_child(n1)
-		msg.add_child(n2)
-		msg.add_child(n3)
-		msg.add_child(n4)
-		msg.add_child(n5)
-
-		x = Blather::Stanza::X.new :form, [
+		orig.nick = ""
+		orig.username = ""
+		orig.password = ""
+		orig.phone = existing_number.to_s
+
+		orig.form.fields = [
 			{
 				required: true, type: :"text-single",
 				label: 'User Id', var: 'nick'
@@ -644,20 +626,17 @@ module SGXbwmsgsv2
 				value: existing_number.to_s
 			}
 		]
-		x.title = 'Register for '\
+		orig.form.title = 'Register for '\
 			'Soprani.ca Gateway to XMPP - Bandwidth API V2'
-		x.instructions = "Enter the details from your Account "\
+		orig.form.instructions = "Enter the details from your Account "\
 			"page as well as the Phone Number\nin your "\
 			"account you want to use (ie. '+12345678901')"\
 			".\n\nThe source code for this gateway is at "\
 			"https://gitlab.com/soprani.ca/sgx-bwmsgsv2 ."\
 			"\nCopyright (C) 2017-2020  Denver Gingerich "\
 			"and others, licensed under AGPLv3+."
-		msg.add_child(x)
-
-		orig.add_child(msg)
 
-		return orig
+		orig
 	end
 
 	ibr do |i|

test/test_component.rb 🔗

@@ -326,4 +326,26 @@ class ComponentTest < Minitest::Test
 		assert stanza.result?
 	end
 	em :test_ibr_form
+
+	def test_ibr_get_form_registered
+		iq = Blather::Stanza::Iq::IBR.new(:get, "component")
+		iq.from = "test@example.com"
+		process_stanza(iq)
+
+		assert_equal 1, written.length
+		stanza = Blather::XMPPNode.parse(written.first.to_xml)
+		assert stanza.result?
+		assert stanza.registered?
+		assert_equal(
+			["nick", "username", "password", "phone"],
+			stanza.form.fields.map(&:var)
+		)
+		assert stanza.instructions
+		assert stanza.nick
+		assert stanza.username
+		assert stanza.password
+		assert stanza.phone
+		refute stanza.email
+	end
+	em :test_ibr_get_form_registered
 end