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		assert_nil launch.query.at_xpath("./ns:test-instance", ns: NS)
 18	end
 19
 20	def test_launch_test_instance
 21		launch = Snikket::Launch.new(
 22			domain: "jmp-test-example.com", test_instance: true
 23		)
 24		refute_nil launch.query.at_xpath("./ns:test-instance", ns: NS)
 25	end
 26
 27	def test_launched
 28		launched = Blather::XMPPNode.parse(<<~XML)
 29			<iq type="result" from="hosting-api.snikket.net" id="123">
 30				<launched xmlns="xmpp:snikket.org/hosting/v1">
 31					<bootstrap>
 32						<token>fZLy6iTh</token>
 33					</bootstrap>
 34					<instance-id>si-12345</instance-id>
 35				</launched>
 36			</iq>
 37		XML
 38		assert_equal :result, launched.type
 39		assert_equal NS, launched.query.namespace.href
 40		assert_equal "launched", launched.query.node_name
 41		assert_equal "si-12345", launched.instance_id
 42	end
 43
 44	def test_instance_get
 45		instance = Snikket::Instance.new(instance_id: "si-1234")
 46		assert_equal :get, instance.type
 47		assert_equal NS, instance.query.namespace.href
 48		assert_equal "instance", instance.query.node_name
 49		assert_equal(
 50			"si-1234",
 51			instance.query.at_xpath("./ns:instance-id", ns: NS).content
 52		)
 53	end
 54
 55	def test_customer_instance_bootstrap_uri
 56		instance = Snikket::CustomerInstance.new(
 57			instance_id: "si-1234",
 58			bootstrap_token: "fZLy6iTh",
 59			customer_id: "test",
 60			domain: "example.com"
 61		)
 62
 63		assert_equal(
 64			"https://example.com/invites_bootstrap?token=fZLy6iTh",
 65			instance.bootstrap_uri
 66		)
 67	end
 68
 69	def test_instance_result
 70		instance = Blather::XMPPNode.parse(<<~XML)
 71			<iq type="result" from="hosting-api.snikket.net" id="000">
 72				<instance xmlns="xmpp:snikket.org/hosting/v1">
 73					<update-needed/>
 74					<instance-id>si-1234</instance-id>
 75					<operation>
 76						<progress>50</progress>
 77						<status>running</status>
 78					</operation>
 79					<status>up</status>
 80				</instance>
 81			</iq>
 82		XML
 83		assert_equal :result, instance.type
 84		assert_equal NS, instance.query.namespace.href
 85		assert_equal "instance", instance.query.node_name
 86		assert_equal "si-1234", instance.instance_id
 87		assert instance.update_needed?
 88		assert_kind_of Nokogiri::XML::Element, instance.operation
 89		assert_equal :up, instance.status
 90	end
 91
 92	def test_invite_repo_fetch_returns_xmpp_alternate_link
 93		stub_request(
 94			:head,
 95			"https://example.com/invites_bootstrap?token=fZLy6iTh"
 96		).to_return(
 97			status: 200,
 98			headers: {
 99				"Link" => [
100					"<https://example.com/invite>; rel=\"alternate\"",
101					"<xmpp:test@example.com?join>; rel=\"alternate\""
102				].join(", ")
103			}
104		)
105
106		invite = Snikket::CustomerInstance::InviteRepo.new(
107			bootstrap_uri: "https://example.com/invites_bootstrap?token=fZLy6iTh"
108		).fetch.sync
109
110		assert_equal "xmpp:test@example.com?join", invite
111	end
112	em :test_invite_repo_fetch_returns_xmpp_alternate_link
113end