~samwhited/xmpp

ref: 7fa903e220ac86501595262dd3d89c3b51f9be7d xmpp/muc/integration_test.go -rw-r--r-- 4.9 KiB
7fa903e2Sam Whited all: be more strict about stanza namespaces 24 days 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
190
191
192
193
194
195
196
// 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.

//go:build integration
// +build integration

package muc_test

import (
	"context"
	"crypto/tls"
	"errors"
	"testing"

	"mellium.im/sasl"
	"mellium.im/xmpp"
	"mellium.im/xmpp/disco"
	"mellium.im/xmpp/disco/items"
	"mellium.im/xmpp/internal/integration"
	"mellium.im/xmpp/internal/integration/prosody"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/muc"
	"mellium.im/xmpp/mux"
	"mellium.im/xmpp/stanza"
)

func TestIntegrationJoinRoom(t *testing.T) {
	prosodyRun := prosody.Test(context.TODO(), t,
		integration.Log(),
		integration.LogXML(),
		prosody.MUC("muc.localhost"),
		prosody.ListenC2S(),
	)
	prosodyRun(integrationJoinRoom)
}

func integrationJoinRoom(ctx context.Context, t *testing.T, cmd *integration.Cmd) {
	j, pass := cmd.User()
	session, err := cmd.DialClient(ctx, j, t,
		xmpp.StartTLS(&tls.Config{
			InsecureSkipVerify: true,
		}),
		xmpp.SASL("", pass, sasl.Plain),
		xmpp.BindResource(),
	)
	if err != nil {
		t.Fatalf("error connecting: %v", err)
	}
	mucClient := &muc.Client{}
	go func() {
		m := mux.New(muc.HandleClient(mucClient))
		err := session.Serve(m)
		if err != nil {
			t.Logf("error from serve: %v", err)
		}
	}()

	// Fetch rooms and make sure they're empty.
	roomJID := jid.MustParse("bridgecrew@muc.localhost/Picard")
	iter := disco.FetchItems(ctx, items.Item{
		JID: roomJID.Domain(),
	}, session)
	for iter.Next() {
		t.Errorf("did not expect any rooms initially, got: %v", iter.Item())
	}
	if err = iter.Err(); err != nil {
		t.Fatalf("error fetching rooms: %v", err)
	}
	err = iter.Close()
	if err != nil {
		t.Fatalf("error closing initial iter: %v", err)
	}

	channel, err := mucClient.Join(ctx, roomJID, session)
	if err != nil {
		t.Fatalf("error joining MUC: %v", err)
	}

	iter = disco.FetchItems(ctx, items.Item{
		JID: roomJID.Domain(),
	}, session)
	for iter.Next() {
		t.Errorf("did not expect any private rooms, got: %v", iter.Item())
	}
	if err = iter.Err(); err != nil {
		t.Fatalf("error fetching rooms: %v", err)
	}
	err = iter.Close()
	if err != nil {
		t.Fatalf("error closing initial iter: %v", err)
	}

	roomForm, err := muc.GetConfig(ctx, roomJID.Bare(), session)
	if err != nil {
		t.Fatalf("error fetching config: %v", err)
	}
	_, err = roomForm.Set("muc#roomconfig_publicroom", true)
	if err != nil {
		t.Errorf("error making room public: %v", err)
	}

	err = muc.SetConfig(ctx, roomJID.Bare(), roomForm, session)
	if err != nil {
		t.Fatalf("error setting room config: %v", err)
	}

	// Fetch rooms again and make sure the new one was created.
	var list []items.Item
	iter = disco.FetchItems(ctx, items.Item{
		JID: roomJID.Domain(),
	}, session)
	for iter.Next() {
		list = append(list, iter.Item())
	}
	if err = iter.Err(); err != nil {
		t.Fatalf("error fetching rooms: %v", err)
	}
	err = iter.Close()
	if err != nil {
		t.Fatalf("error closing final iter: %v", err)
	}
	if len(list) != 1 || !list[0].JID.Equal(roomJID.Bare()) {
		t.Fatalf("wrong rooms created: want=%v, got=%v", roomJID.Bare(), list)
	}

	err = channel.Leave(ctx, "")
	if err != nil {
		t.Fatalf("error leaving room: %v", err)
	}

	// Fetch rooms and make sure they're empty (room was not persistent and was
	// destroyed when we left, indicating that we did in fact leave correctly).
	iter = disco.FetchItems(ctx, items.Item{
		JID: roomJID.Domain(),
	}, session)
	for iter.Next() {
		t.Errorf("did not expect any rooms after part, got: %v", iter.Item())
	}
	if err = iter.Err(); err != nil {
		t.Fatalf("error fetching rooms: %v", err)
	}
	err = iter.Close()
	if err != nil {
		t.Fatalf("error closing initial iter: %v", err)
	}
}

func TestIntegrationJoinErr(t *testing.T) {
	prosodyRun := prosody.Test(context.TODO(), t,
		integration.Log(),
		integration.LogXML(),
		prosody.MUC("muc.localhost"),
		prosody.Channel("muc.localhost", prosody.ChannelConfig{
			Localpart:  "bridgecrew",
			Admins:     []string{"me@localhost"},
			Name:       "Bridge Crew",
			Persistent: true,
			Public:     true,
			Pass:       "nojoin",
		}),
		prosody.ListenC2S(),
	)
	prosodyRun(integrationJoinErr)
}

func integrationJoinErr(ctx context.Context, t *testing.T, cmd *integration.Cmd) {
	j, pass := cmd.User()
	session, err := cmd.DialClient(ctx, j, t,
		xmpp.StartTLS(&tls.Config{
			InsecureSkipVerify: true,
		}),
		xmpp.SASL("", pass, sasl.Plain),
		xmpp.BindResource(),
	)
	if err != nil {
		t.Fatalf("error connecting: %v", err)
	}
	mucClient := &muc.Client{}
	go func() {
		m := mux.New(muc.HandleClient(mucClient))
		err := session.Serve(m)
		if err != nil {
			t.Logf("error from serve: %v", err)
		}
	}()

	roomJID := jid.MustParse("bridgecrew@muc.localhost/Picard")
	_, err = mucClient.Join(ctx, roomJID, session)
	noAuth := stanza.Error{
		Condition: stanza.NotAuthorized,
	}
	if !errors.Is(err, noAuth) {
		t.Fatalf("wrong error type, want=%T (%[1]v), got=%T (%[2]v)", noAuth, err)
	}
}