-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbusiness_send_test.go
More file actions
199 lines (151 loc) · 5.7 KB
/
Copy pathbusiness_send_test.go
File metadata and controls
199 lines (151 loc) · 5.7 KB
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
package botapi
import (
"context"
"testing"
"github.com/gotd/td/tg"
)
// multiBusinessMessageUpdates is the response an album sent on behalf of a
// business account returns: each message wrapped in updateBotNewBusinessMessage.
func multiBusinessMessageUpdates(ids ...int) *tg.Updates {
upds := make([]tg.UpdateClass, 0, len(ids))
for _, id := range ids {
upds = append(upds, &tg.UpdateBotNewBusinessMessage{
ConnectionID: "bc1",
Message: &tg.Message{ID: id, Out: true, PeerID: &tg.PeerUser{UserID: 10}},
})
}
return &tg.Updates{Updates: upds, Users: []tg.UserClass{&tg.User{ID: 10, AccessHash: 1}}}
}
// businessSendReply is the response a send on behalf of a business account
// returns: the sent message wrapped in updateBotNewBusinessMessage (not the
// updateNewMessage a normal send returns).
func businessSendReply() *tg.Updates {
return &tg.Updates{
Updates: []tg.UpdateClass{
&tg.UpdateBotNewBusinessMessage{
ConnectionID: "bc1",
Message: &tg.Message{ID: 1, Out: true, Message: "hi", PeerID: &tg.PeerUser{UserID: 10}},
},
},
Users: []tg.UserClass{&tg.User{ID: 10, AccessHash: 1}},
}
}
func TestSendMessageWithBusinessConnection(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, businessSendReply())
b := newMockBot(inv)
m, err := b.SendMessage(context.Background(), userRef(10, 20), "hi", WithBusinessConnection("bc1"))
if err != nil {
t.Fatalf("SendMessage: %v", err)
}
if m.MessageID != 1 {
t.Fatalf("message id = %d", m.MessageID)
}
wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.MessagesSendMessageRequest{}}
inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper)
if wrapper.ConnectionID != "bc1" {
t.Fatalf("connection id = %q", wrapper.ConnectionID)
}
sm, ok := wrapper.Query.(*tg.MessagesSendMessageRequest)
if !ok || sm.Message != "hi" {
t.Fatalf("query = %#v", wrapper.Query)
}
}
func TestSendMessageWithoutBusinessIsDirect(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.MessagesSendMessageRequestTypeID, businessSendReply())
b := newMockBot(inv)
if _, err := b.SendMessage(context.Background(), userRef(10, 20), "hi"); err != nil {
t.Fatalf("SendMessage: %v", err)
}
if inv.called(tg.InvokeWithBusinessConnectionRequestTypeID) {
t.Fatal("a non-business send must not be wrapped")
}
if !inv.called(tg.MessagesSendMessageRequestTypeID) {
t.Fatal("expected a direct messages.sendMessage")
}
}
func TestBusinessContextSendMessage(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, businessSendReply())
b := newMockBot(inv)
if _, err := b.Business("bc3").SendMessage(context.Background(), userRef(10, 20), "hi"); err != nil {
t.Fatalf("SendMessage: %v", err)
}
wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.MessagesSendMessageRequest{}}
inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper)
if wrapper.ConnectionID != "bc3" {
t.Fatalf("connection id = %q", wrapper.ConnectionID)
}
}
func TestContextReplyBusiness(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, businessSendReply())
b := newMockBot(inv)
c := &Context{Context: context.Background(), Bot: b, Update: &Update{
BusinessMessage: &Message{
MessageID: 9,
BusinessConnectionID: "bc4",
Chat: Chat{ID: 10, Type: ChatTypePrivate},
},
}}
if _, err := c.Reply("ok"); err != nil {
t.Fatalf("Reply: %v", err)
}
wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.MessagesSendMessageRequest{}}
inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper)
if wrapper.ConnectionID != "bc4" {
t.Fatalf("connection id = %q", wrapper.ConnectionID)
}
sm, ok := wrapper.Query.(*tg.MessagesSendMessageRequest)
if !ok {
t.Fatalf("query = %#v", wrapper.Query)
}
if _, ok := sm.GetReplyTo(); !ok {
t.Fatal("business reply should set reply_to")
}
}
func TestBusinessReplyUsesEntitiesPeer(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, businessSendReply())
b := newMockBot(inv)
b.OnBusinessMessage(func(c *Context) error {
_, err := c.Reply("hi")
return err
})
// The update delivers the chat's access hash (555) in its entities; the reply
// must address the peer with that hash, not the bot's stored one.
e := tg.Entities{Users: map[int64]*tg.User{10: {ID: 10, AccessHash: 555}}}
msg := &tg.Message{ID: 7, PeerID: &tg.PeerUser{UserID: 10}}
b.dispatchBusinessMessage(context.Background(), e, "bc1", msg, false)
wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.MessagesSendMessageRequest{}}
inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper)
sm, ok := wrapper.Query.(*tg.MessagesSendMessageRequest)
if !ok {
t.Fatalf("query = %#v", wrapper.Query)
}
peer, ok := sm.Peer.(*tg.InputPeerUser)
if !ok || peer.UserID != 10 || peer.AccessHash != 555 {
t.Fatalf("peer = %#v, want user 10 with access hash 555", sm.Peer)
}
}
func TestContextSendBusiness(t *testing.T) {
inv := newMockInvoker()
inv.reply(tg.InvokeWithBusinessConnectionRequestTypeID, businessSendReply())
b := newMockBot(inv)
c := &Context{Context: context.Background(), Bot: b, Update: &Update{
BusinessMessage: &Message{
MessageID: 9,
BusinessConnectionID: "bc5",
Chat: Chat{ID: 10, Type: ChatTypePrivate},
},
}}
if _, err := c.Send("hi"); err != nil {
t.Fatalf("Send: %v", err)
}
wrapper := tg.InvokeWithBusinessConnectionRequest{Query: &tg.MessagesSendMessageRequest{}}
inv.decode(t, tg.InvokeWithBusinessConnectionRequestTypeID, &wrapper)
if wrapper.ConnectionID != "bc5" {
t.Fatalf("connection id = %q", wrapper.ConnectionID)
}
}