-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoverage_errpaths_test.go
More file actions
103 lines (85 loc) · 3.46 KB
/
Copy pathcoverage_errpaths_test.go
File metadata and controls
103 lines (85 loc) · 3.46 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
package botapi
import (
"context"
"testing"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
// testTgErr is a canned Telegram error used to drive asAPIError branches.
func testTgErr() *tgerr.Error {
return &tgerr.Error{Code: 400, Type: "TEST_ERROR", Message: "TEST_ERROR"}
}
// botFailing returns a Bot whose given RPC fails, leaving the default
// resolution handlers intact.
func botFailing(id uint32) *Bot {
inv := newMockInvoker()
inv.fail(id, testTgErr())
return newMockBot(inv)
}
// TestChatAdminMethodErrors covers the RPC-error branch of the direct-RPC chat
// administration methods.
func TestChatAdminMethodErrors(t *testing.T) {
ctx := context.Background()
ch := tdlibChannel(123)
checks := []struct {
name string
call func(b *Bot) error
rpc uint32
}{
{"pin", func(b *Bot) error { return b.PinChatMessage(ctx, userRef(10, 20), 5) }, tg.MessagesUpdatePinnedMessageRequestTypeID},
{"unpin", func(b *Bot) error { return b.UnpinChatMessage(ctx, userRef(10, 20), 5) }, tg.MessagesUpdatePinnedMessageRequestTypeID},
{"unpin-all", func(b *Bot) error { return b.UnpinAllChatMessages(ctx, userRef(10, 20)) }, tg.MessagesUnpinAllMessagesRequestTypeID},
{"permissions", func(b *Bot) error { return b.SetChatPermissions(ctx, userRef(10, 20), ChatPermissions{}) }, tg.MessagesEditChatDefaultBannedRightsRequestTypeID},
{"set-sticker-set", func(b *Bot) error { return b.SetChatStickerSet(ctx, ch, "pack") }, tg.ChannelsSetStickersRequestTypeID},
{"del-sticker-set", func(b *Bot) error { return b.DeleteChatStickerSet(ctx, ch) }, tg.ChannelsSetStickersRequestTypeID},
}
for _, c := range checks {
t.Run(c.name, func(t *testing.T) {
if err := c.call(botFailing(c.rpc)); err == nil {
t.Fatal("expected error")
}
})
}
}
// TestChatMemberQueryErrors covers the RPC-error branch of the participant
// query methods.
func TestChatMemberQueryErrors(t *testing.T) {
ctx := context.Background()
ch := tdlibChannel(123)
if _, err := botFailing(tg.ChannelsGetParticipantRequestTypeID).GetChatMember(ctx, ch, 10); err == nil {
t.Fatal("GetChatMember: expected error")
}
if _, err := botFailing(tg.ChannelsGetParticipantsRequestTypeID).GetChatMemberCount(ctx, ch); err == nil {
t.Fatal("GetChatMemberCount: expected error")
}
if _, err := botFailing(tg.ChannelsGetParticipantsRequestTypeID).GetChatAdministrators(ctx, ch); err == nil {
t.Fatal("GetChatAdministrators: expected error")
}
}
// TestChatAdminRejectsPrivate covers errNotInPrivateChat: the shared-admin
// methods are not available on a private (user) chat.
func TestChatAdminRejectsPrivate(t *testing.T) {
ctx := context.Background()
b := newMockBot(newMockInvoker())
if err := b.SetChatTitle(ctx, userRef(10, 20), "t"); err == nil {
t.Fatal("SetChatTitle on private chat should fail")
}
if err := b.SetChatDescription(ctx, userRef(10, 20), "d"); err == nil {
t.Fatal("SetChatDescription on private chat should fail")
}
if err := b.LeaveChat(ctx, userRef(10, 20)); err == nil {
t.Fatal("LeaveChat on private chat should fail")
}
}
// TestResolveChannelRejectsUser covers the resolveChannel error path: a numeric
// user id is not a channel.
func TestResolveChannelRejectsUser(t *testing.T) {
ctx := context.Background()
b := newMockBot(newMockInvoker())
if _, err := b.GetChatMemberCount(ctx, ID(10)); err == nil {
t.Fatal("GetChatMemberCount on a user id should fail")
}
if err := b.SetChatStickerSet(ctx, ID(10), "p"); err == nil {
t.Fatal("SetChatStickerSet on a user id should fail")
}
}