test_snikket.rb

  1# frozen_string_literal: true
  2
  3require "snikket"
  4require "openssl"
  5
  6class TestSnikket < Minitest::Test
  7	NS = "xmpp:snikket.org/hosting/v1"
  8
  9	def test_launch
 10		launch = Snikket::Launch.new(domain: "example.com")
 11		assert_equal :set, launch.type
 12		assert_equal NS, launch.query.namespace.href
 13		assert_equal "launch", launch.query.node_name
 14		assert_equal(
 15			"example.com",
 16			launch.query.at_xpath("./ns:domain", ns: NS).content
 17		)
 18		assert_nil launch.query.at_xpath("./ns:test-instance", ns: NS)
 19	end
 20
 21	def test_launch_test_instance
 22		launch = Snikket::Launch.new(
 23			domain: "jmp-test-example.com", test_instance: true
 24		)
 25		refute_nil launch.query.at_xpath("./ns:test-instance", ns: NS)
 26	end
 27
 28	def test_launched
 29		launched = Blather::XMPPNode.parse(<<~XML)
 30			<iq type="result" from="hosting-api.snikket.net" id="123">
 31				<launched xmlns="xmpp:snikket.org/hosting/v1">
 32					<bootstrap>
 33						<token>fZLy6iTh</token>
 34					</bootstrap>
 35					<instance-id>si-12345</instance-id>
 36				</launched>
 37			</iq>
 38		XML
 39		assert_equal :result, launched.type
 40		assert_equal NS, launched.query.namespace.href
 41		assert_equal "launched", launched.query.node_name
 42		assert_equal "si-12345", launched.instance_id
 43	end
 44
 45	def test_instance_get
 46		instance = Snikket::Instance.new(instance_id: "si-1234")
 47		assert_equal :get, instance.type
 48		assert_equal NS, instance.query.namespace.href
 49		assert_equal "instance", instance.query.node_name
 50		assert_equal(
 51			"si-1234",
 52			instance.query.at_xpath("./ns:instance-id", ns: NS).content
 53		)
 54	end
 55
 56	def test_customer_instance_bootstrap_uri
 57		instance = Snikket::CustomerInstance.new(
 58			instance_id: "si-1234",
 59			bootstrap_token: "fZLy6iTh",
 60			customer_id: "test",
 61			domain: "example.com"
 62		)
 63
 64		assert_equal(
 65			"https://example.com/invites_bootstrap?token=fZLy6iTh",
 66			instance.bootstrap_uri
 67		)
 68	end
 69
 70	def test_fetch_invite_returns_nil_when_http_head_raises_ssl_error
 71		instance = Snikket::CustomerInstance.new(
 72			instance_id: "si-1234",
 73			bootstrap_token: "fZLy6iTh",
 74			customer_id: "test",
 75			domain: "example.com"
 76		)
 77
 78		request = Minitest::Mock.new
 79		request.expect(:ahead, nil) do |redirects:|
 80			assert_equal 5, redirects
 81			raise OpenSSL::SSL::SSLError, "certificate verify failed"
 82		end
 83
 84		EM::HttpRequest.stub(:new, request) do
 85			assert_nil instance.fetch_invite.sync
 86		end
 87
 88		request.verify
 89	end
 90	em :test_fetch_invite_returns_nil_when_http_head_raises_ssl_error
 91
 92	def test_instance_result
 93		instance = Blather::XMPPNode.parse(<<~XML)
 94			<iq type="result" from="hosting-api.snikket.net" id="000">
 95				<instance xmlns="xmpp:snikket.org/hosting/v1">
 96					<update-needed/>
 97					<instance-id>si-1234</instance-id>
 98					<operation>
 99						<progress>50</progress>
100						<status>running</status>
101					</operation>
102					<status>up</status>
103				</instance>
104			</iq>
105		XML
106		assert_equal :result, instance.type
107		assert_equal NS, instance.query.namespace.href
108		assert_equal "instance", instance.query.node_name
109		assert_equal "si-1234", instance.instance_id
110		assert instance.update_needed?
111		assert_kind_of Nokogiri::XML::Element, instance.operation
112		assert_equal :up, instance.status
113	end
114
115	def test_invite_repo_fetch_returns_xmpp_alternate_link
116		stub_request(
117			:head,
118			"https://example.com/invites_bootstrap?token=fZLy6iTh"
119		).to_return(
120			status: 200,
121			headers: {
122				"Link" => [
123					"<https://example.com/invite>; rel=\"alternate\"",
124					"<xmpp:test@example.com?join>; rel=\"alternate\""
125				].join(", ")
126			}
127		)
128
129		invite = Snikket::CustomerInstance::InviteRepo.new(
130			bootstrap_uri: "https://example.com/invites_bootstrap?token=fZLy6iTh"
131		).fetch.sync
132
133		assert_equal "xmpp:test@example.com?join", invite
134	end
135	em :test_invite_repo_fetch_returns_xmpp_alternate_link
136end