~samwhited/xmpp

ref: 7fa903e220ac86501595262dd3d89c3b51f9be7d xmpp/muc/room_integration_test.go -rw-r--r-- 5.4 KiB
7fa903e2Sam Whited all: be more strict about stanza namespaces a month 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
197
198
199
200
201
202
203
204
205
206
207
// 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"
	"testing"
	"time"

	"mellium.im/sasl"
	"mellium.im/xmpp"
	"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"
)

const (
	userOne  = "foo@localhost"
	userTwo  = "bar@localhost"
	userPass = "Pass"
)

func TestIntegrationMediatedInvite(t *testing.T) {
	prosodyRun := prosody.Test(context.TODO(), t,
		integration.Log(),
		integration.LogXML(),
		prosody.MUC("muc.localhost"),
		prosody.CreateUser(context.TODO(), userOne, userPass),
		prosody.CreateUser(context.TODO(), userTwo, userPass),
		prosody.ListenC2S(),
	)
	prosodyRun(integrationMediatedInvite)
}

func TestIntegrationSetAffiliation(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{userOne},
			Name:       "Bridge Crew",
			Persistent: true,
			Public:     true,
		}),
		prosody.CreateUser(context.TODO(), userOne, userPass),
		prosody.CreateUser(context.TODO(), userTwo, userPass),
		prosody.ListenC2S(),
	)
	prosodyRun(integrationSetAffiliation)
}

func integrationMediatedInvite(ctx context.Context, t *testing.T, cmd *integration.Cmd) {
	userOneJID := jid.MustParse(userOne)
	userTwoJID := jid.MustParse(userTwo)
	userOneSession, err := cmd.DialClient(ctx, userOneJID, t,
		xmpp.StartTLS(&tls.Config{
			InsecureSkipVerify: true,
		}),
		xmpp.SASL("", userPass, sasl.Plain),
		xmpp.BindResource(),
	)
	if err != nil {
		t.Fatalf("error connecting %s: %v", userOne, err)
	}
	userTwoSession, err := cmd.DialClient(ctx, userTwoJID, t,
		xmpp.StartTLS(&tls.Config{
			InsecureSkipVerify: true,
		}),
		xmpp.SASL("", userPass, sasl.Plain),
		xmpp.BindResource(),
	)
	if err != nil {
		t.Fatalf("error connecting %s: %v", userTwo, err)
	}

	mucClient := &muc.Client{}
	go func() {
		m := mux.New(muc.HandleClient(mucClient))
		err := userOneSession.Serve(m)
		if err != nil {
			t.Logf("error from %s serve: %v", userOne, err)
		}
	}()
	errChan := make(chan error)
	go func(errChan chan<- error) {
		inviteClient := &muc.Client{
			HandleInvite: func(i muc.Invitation) {
				errChan <- nil
			},
		}
		m := mux.New(muc.HandleClient(inviteClient))
		err := userTwoSession.Serve(m)
		if err != nil {
			t.Logf("error from %s serve: %v", userTwo, err)
		}
	}(errChan)

	roomJID := jid.MustParse("bridgecrew@muc.localhost/Picard")
	channel, err := mucClient.Join(ctx, roomJID, userOneSession)
	if err != nil {
		t.Fatalf("error joining MUC: %v", err)
	}

	err = channel.Invite(ctx, "invited!", userTwoSession.LocalAddr())
	if err != nil {
		t.Fatalf("error sending invite: %v", err)
	}
	ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
	defer cancel()
	select {
	case err := <-errChan:
		if err != nil {
			t.Fatalf("error receiving invite: %v", err)
		}
	case <-ctx.Done():
		t.Fatalf("invite not received: %v", ctx.Err())
	}
}

func integrationSetAffiliation(ctx context.Context, t *testing.T, cmd *integration.Cmd) {
	userOneJID := jid.MustParse(userOne)
	userTwoJID := jid.MustParse(userTwo)
	userOneSession, err := cmd.DialClient(ctx, userOneJID, t,
		xmpp.StartTLS(&tls.Config{
			InsecureSkipVerify: true,
		}),
		xmpp.SASL("", userPass, sasl.Plain),
		xmpp.BindResource(),
	)
	if err != nil {
		t.Fatalf("error connecting %s: %v", userOne, err)
	}
	userTwoSession, err := cmd.DialClient(ctx, userTwoJID, t,
		xmpp.StartTLS(&tls.Config{
			InsecureSkipVerify: true,
		}),
		xmpp.SASL("", userPass, sasl.Plain),
		xmpp.BindResource(),
	)
	if err != nil {
		t.Fatalf("error connecting %s: %v", userTwo, err)
	}

	mucClientOne := &muc.Client{}
	go func() {
		m := mux.New(muc.HandleClient(mucClientOne))
		err := userOneSession.Serve(m)
		if err != nil {
			t.Logf("error from %s serve: %v", userOne, err)
		}
	}()

	itemChan := make(chan muc.Item)
	mucClientTwo := &muc.Client{
		HandleUserPresence: func(_ stanza.Presence, i muc.Item) {
			itemChan <- i
		},
	}
	go func(itemChan chan<- muc.Item) {
		m := mux.New(muc.HandleClient(mucClientTwo))
		err := userTwoSession.Serve(m)
		if err != nil {
			t.Logf("error from %s serve: %v", userTwo, err)
		}
	}(itemChan)

	roomJID := jid.MustParse("bridgecrew@muc.localhost/Picard")
	channelOne, err := mucClientOne.Join(ctx, roomJID, userOneSession)
	if err != nil {
		t.Fatalf("error joining MUC as %s: %v", roomJID.Resourcepart(), err)
	}

	roomJIDTwo, err := roomJID.WithResource("CrusherMD")
	if err != nil {
		t.Fatalf("bad resource in test: %v", err)
	}
	_, err = mucClientTwo.Join(ctx, roomJIDTwo, userTwoSession)
	if err != nil {
		t.Fatalf("error joining MUC as %s: %v", roomJIDTwo.Resourcepart(), err)
	}

	err = channelOne.SetAffiliation(ctx, muc.AffiliationMember, userTwoJID, "Crusher", "Permission to speak freely")
	if err != nil {
		t.Fatalf("error setting affiliation: %v", err)
	}
	ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
	defer cancel()
	select {
	case item := <-itemChan:
		if item.Affiliation != muc.AffiliationMember {
			t.Fatalf("wrong affiliation: want=%v, got=%v", muc.AffiliationMember, item.Affiliation)
		}
	case <-ctx.Done():
		t.Fatalf("invite not received: %v", ctx.Err())
	}
}