test_snikket.rb

 1# frozen_string_literal: true
 2
 3require "snikket"
 4
 5class TestSnikket < Minitest::Test
 6	NS = "xmpp:snikket.org/hosting/v1"
 7
 8	def test_launch
 9		launch = Snikket::Launch.new(domain: "example.com")
10		assert_equal :set, launch.type
11		assert_equal NS, launch.query.namespace.href
12		assert_equal "launch", launch.query.node_name
13		assert_equal(
14			"example.com",
15			launch.query.at_xpath("./ns:domain", ns: NS).content
16		)
17	end
18
19	def test_launched
20		launched = Blather::XMPPNode.parse(<<~XML)
21			<iq type="result" from="hosting-api.snikket.net" id="123">
22				<launched xmlns="xmpp:snikket.org/hosting/v1">
23					<bootstrap>
24						<token>fZLy6iTh</token>
25					</bootstrap>
26					<instance-id>si-12345</instance-id>
27				</launched>
28			</iq>
29		XML
30		assert_equal :result, launched.type
31		assert_equal NS, launched.query.namespace.href
32		assert_equal "launched", launched.query.node_name
33		assert_equal(
34			"https://example.com/invites_bootstrap?token=fZLy6iTh",
35			launched.bootstrap_uri("example.com")
36		)
37		assert_equal "si-12345", launched.instance_id
38	end
39
40	def test_instance_get
41		instance = Snikket::Instance.new(instance_id: "si-1234")
42		assert_equal :get, instance.type
43		assert_equal NS, instance.query.namespace.href
44		assert_equal "instance", instance.query.node_name
45		assert_equal(
46			"si-1234",
47			instance.query.at_xpath("./ns:instance-id", ns: NS).content
48		)
49	end
50
51	def test_instance_result
52		instance = Blather::XMPPNode.parse(<<~XML)
53			<iq type="result" from="hosting-api.snikket.net" id="000">
54				<instance xmlns="xmpp:snikket.org/hosting/v1">
55					<update-needed/>
56					<instance-id>si-1234</instance-id>
57					<operation>
58						<progress>50</progress>
59						<status>running</status>
60					</operation>
61					<status>up</status>
62				</instance>
63			</iq>
64		XML
65		assert_equal :result, instance.type
66		assert_equal NS, instance.query.namespace.href
67		assert_equal "instance", instance.query.node_name
68		assert_equal "si-1234", instance.instance_id
69		assert instance.update_needed?
70		assert_kind_of Nokogiri::XML::Element, instance.operation
71		assert_equal :up, instance.status
72	end
73end