~samwhited/xmpp

xmpp/muc/muc_test.go -rw-r--r-- 5.3 KiB
e9b0a2deSam Whited docs: do a quick editing pass over the docs a day ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package muc_test

import (
	"context"
	"encoding/xml"
	"errors"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/internal/xmpptest"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/muc"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

func TestJoinPartMuc(t *testing.T) {
	j := jid.MustParse("room@example.net/me")
	h := &muc.Client{}
	m := mux.New(ns.Client, muc.HandleClient(h))
	s := xmpptest.NewClientServer(
		xmpptest.ClientHandler(m),
		xmpptest.ServerHandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
			// Send back a self presence, indicating that the join is complete.
			p, err := stanza.NewPresence(*start)
			if err != nil {
				return err
			}
			p.To, p.From = p.From, p.To
			_, err = xmlstream.Copy(t, p.Wrap(xmlstream.Wrap(
				nil,
				xml.StartElement{Name: xml.Name{Space: muc.NSUser, Local: "x"}},
			)))
			return err
		}),
	)

	channel, err := h.Join(context.Background(), j, s.Client)
	if err != nil {
		t.Fatalf("error joining: %v", err)
	}

	if !channel.Me().Equal(j) {
		t.Errorf("wrong JID: want=%v, got=%v", j, channel.Me())
	}
	if !channel.Addr().Equal(j.Bare()) {
		t.Errorf("wrong JID: want=%v, got=%v", j.Bare(), channel.Addr())
	}

	err = channel.Leave(context.Background(), "")
	if err != nil {
		t.Fatalf("error leaving: %v", err)
	}
	if channel.Joined() {
		t.Errorf("expected channel to be unjoined")
	}
}

func TestJoinError(t *testing.T) {
	j := jid.MustParse("room@example.net/me")
	h := &muc.Client{}
	m := mux.New(ns.Client, muc.HandleClient(h))
	s := xmpptest.NewClientServer(
		xmpptest.ClientHandler(m),
		xmpptest.ServerHandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
			// Send back an error indicating that we couldn't join.
			p, err := stanza.NewPresence(*start)
			if err != nil {
				return err
			}
			p.Type = stanza.ErrorPresence
			p.To, p.From = p.From, p.To
			se := stanza.Error{
				By:        p.To.Bare(),
				Type:      stanza.Modify,
				Condition: stanza.NotAcceptable,
			}
			_, err = xmlstream.Copy(t, p.Wrap(xmlstream.MultiReader(
				xmlstream.Wrap(
					nil,
					xml.StartElement{Name: xml.Name{Space: muc.NS, Local: "x"}},
				),
				se.TokenReader(),
			)))
			return err
		}),
	)

	_, err := h.Join(context.Background(), j, s.Client)
	if !errors.Is(err, stanza.Error{}) {
		t.Fatalf("expected a stanza error but got: %v", err)
	}
}

func TestPartError(t *testing.T) {
	j := jid.MustParse("room@example.net/me")
	h := &muc.Client{}
	m := mux.New(ns.Client, muc.HandleClient(h))
	errHotelCalifornia := stanza.Error{
		Type:      stanza.Auth,
		Condition: stanza.NotAllowed,
	}
	s := xmpptest.NewClientServer(
		xmpptest.ClientHandler(m),
		xmpptest.ServerHandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
			// Send back a self presence, indicating that the join is complete.
			p, err := stanza.NewPresence(*start)
			if err != nil {
				return err
			}
			p.To, p.From = p.From, p.To
			switch p.Type {
			case "":
				_, err = xmlstream.Copy(t, p.Wrap(xmlstream.Wrap(
					nil,
					xml.StartElement{Name: xml.Name{Space: muc.NSUser, Local: "x"}},
				)))
			case stanza.UnavailablePresence:
				p.Type = stanza.ErrorPresence
				_, err = xmlstream.Copy(t, p.Wrap(xmlstream.MultiReader(
					xmlstream.Wrap(
						nil,
						xml.StartElement{Name: xml.Name{Space: muc.NS, Local: "x"}},
					),
					errHotelCalifornia.TokenReader(),
				)))
			}
			return err
		}),
	)

	channel, err := h.Join(context.Background(), j, s.Client)
	if err != nil {
		t.Fatalf("error joining: %v", err)
	}

	err = channel.Leave(context.Background(), "")
	if !errors.Is(err, errHotelCalifornia) {
		t.Errorf("wrong error leaving: %v", err)
	}
	if channel.Joined() {
		t.Errorf("expected channel to be unjoined")
	}
}

func TestJoinCancel(t *testing.T) {
	j := jid.MustParse("room@example.net/me")
	s := xmpptest.NewClientServer()
	h := &muc.Client{}

	ctx, cancel := context.WithCancel(context.Background())
	cancel()
	_, err := h.Join(ctx, j, s.Client)
	if !errors.Is(err, context.Canceled) {
		t.Fatalf("wrong error: want=%v, got=%v", context.Canceled, err)
	}
}

func TestGetForm(t *testing.T) {
	j := jid.MustParse("room@example.net/me")
	const iqID = "1234"
	s := xmpptest.NewClientServer(
		xmpptest.ServerHandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error {
			reply := `<iq type='result' id='` + iqID + `' to='me@localhost/cHKubP5q' from='` + j.Bare().String() + `'><query xmlns='http://jabber.org/protocol/muc#owner'><x type='form' xmlns='jabber:x:data'><title>Configuration</title><instructions>Complete and submit this form to configure the room.</instructions><field var='FORM_TYPE' type='hidden'><value>http://jabber.org/protocol/muc#roomconfig</value></field></x></query></iq>`
			d := xml.NewDecoder(strings.NewReader(reply))
			_, err := xmlstream.Copy(t, d)
			return err
		}),
	)

	formData, err := muc.GetConfigIQ(context.Background(), stanza.IQ{
		ID: iqID,
		To: j.Bare(),
	}, s.Client)
	if err != nil {
		t.Fatalf("error fetching form: %v", err)
	}

	const expected = "Configuration"
	if title := formData.Title(); title != expected {
		t.Errorf("wrong title, form decode failed: want=%q, got=%q", expected, title)
	}
}