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_instance_result
56 instance = Blather::XMPPNode.parse(<<~XML)
57 <iq type="result" from="hosting-api.snikket.net" id="000">
58 <instance xmlns="xmpp:snikket.org/hosting/v1">
59 <update-needed/>
60 <instance-id>si-1234</instance-id>
61 <operation>
62 <progress>50</progress>
63 <status>running</status>
64 </operation>
65 <status>up</status>
66 </instance>
67 </iq>
68 XML
69 assert_equal :result, instance.type
70 assert_equal NS, instance.query.namespace.href
71 assert_equal "instance", instance.query.node_name
72 assert_equal "si-1234", instance.instance_id
73 assert instance.update_needed?
74 assert_kind_of Nokogiri::XML::Element, instance.operation
75 assert_equal :up, instance.status
76 end
77
78 def test_bootstrap_uri
79 instance = Snikket::CustomerInstance.new(
80 instance_id: "si-1234",
81 bootstrap_token: "fZLy6iTh",
82 customer_id: "test",
83 domain: "example.com"
84 )
85
86 assert_equal(
87 "https://example.com/invites_bootstrap?token=fZLy6iTh",
88 instance.bootstrap_uri
89 )
90 end
91end