-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
194 lines (170 loc) · 4.74 KB
/
main_test.go
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
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"testing"
"github.com/fsantiag/mussum/challenge"
"github.com/fsantiag/mussum/language"
"github.com/fsantiag/mussum/mocks"
botapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func TestSendChallengeToUser(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockBotAPI := mocks.NewMockBotIface(mockCtrl)
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stdout)
}()
c := challenge.Generate()
l := language.GetDefault()
userName := "someUser"
msg1 := botapi.NewMessage(20, fmt.Sprintf(l.Welcome(), userName))
msg2 := botapi.NewMessage(20, fmt.Sprintf(l.Challenge(), c.A, c.Operation, c.B))
mockBotAPI.EXPECT().Send(msg1).Return(botapi.Message{}, nil).Times(1)
mockBotAPI.EXPECT().Send(msg2).Return(botapi.Message{}, nil).Times(1)
sendChallengeToUser(userName, 20, l, mockBotAPI, c)
}
func TestUserPassesTheChallenge(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockBotAPI := mocks.NewMockBotIface(mockCtrl)
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stdout)
}()
c := challenge.Generate()
u := botapi.Update{
Message: &botapi.Message{
Chat: &botapi.Chat{
ID: 1,
},
Text: strconv.Itoa(c.Result),
From: &botapi.User{
ID: 1,
},
MessageID: 10,
},
}
m := map[int]challenge.SumChallenge{
1: c,
}
l := language.GetDefault()
msg := botapi.NewMessage(u.Message.Chat.ID, l.Correct())
msg.ReplyToMessageID = u.Message.MessageID
mockBotAPI.EXPECT().Send(msg).Return(botapi.Message{}, nil).Times(1)
verifyUserAnswer(u, m, l, mockBotAPI)
assert.Contains(t, buf.String(), "[1] User successfully solved the challange")
_, ok := m[u.Message.From.ID]
assert.False(t, ok)
}
func TestUserFailsTheChallenge(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockBotAPI := mocks.NewMockBotIface(mockCtrl)
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stdout)
}()
c := challenge.Generate()
u := botapi.Update{
Message: &botapi.Message{
Chat: &botapi.Chat{
ID: 1,
},
Text: "wrong answer",
From: &botapi.User{
ID: 1,
},
MessageID: 10,
},
}
m := map[int]challenge.SumChallenge{
1: c,
}
l := language.GetDefault()
msg := botapi.NewMessage(u.Message.Chat.ID, l.Wrong())
msg.ReplyToMessageID = u.Message.MessageID
mockBotAPI.EXPECT().Send(msg).Return(botapi.Message{}, nil).Times(1)
verifyUserAnswer(u, m, l, mockBotAPI)
assert.Contains(t, buf.String(), "[1] Wrong answer for challenge")
_, ok := m[u.Message.From.ID]
assert.True(t, ok)
}
// func TestIgnorePrivateMessagesWhenNoChallengeIsActiveForUser(t *testing.T) {
// mockCtrl := gomock.NewController(t)
// defer mockCtrl.Finish()
// mockBotAPI := mocks.NewMockBotIface(mockCtrl)
// var buf bytes.Buffer
// log.SetOutput(&buf)
// defer func() {
// log.SetOutput(os.Stdout)
// }()
// m := map[int]challenge.SumChallenge{}
// l := language.Pt{}
// u := botapi.Update{
// Message: &botapi.Message{
// Chat: &botapi.Chat{
// Type: "private",
// },
// From: &botapi.User{
// ID: 10,
// },
// },
// }
// updates := make(chan botapi.Update, 1)
// updates <- u
// quit := make(chan bool, 1)
// timeout := make(chan message, 1)
// quit <- true
// mockBotAPI.EXPECT().Send(gomock.Any()).Return(botapi.Message{}, nil).Times(0)
// startBot(mockBotAPI, l, m, updates, quit, timeout)
// }
func TestKickUser(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockBotAPI := mocks.NewMockBotIface(mockCtrl)
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stdout)
}()
kickCfg := botapi.KickChatMemberConfig{
ChatMemberConfig: botapi.ChatMemberConfig{
UserID: 555,
ChatID: 2,
},
UntilDate: 400, //forever
}
mockBotAPI.EXPECT().KickChatMember(kickCfg).Return(botapi.APIResponse{}, nil).Times(1)
kickUser(message{userID: 555, chatID: 2}, mockBotAPI)
assert.Contains(t, buf.String(), "[555] User kicked from the channel")
}
func TestKickUserFails(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockBotAPI := mocks.NewMockBotIface(mockCtrl)
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stdout)
}()
kickCfg := botapi.KickChatMemberConfig{
ChatMemberConfig: botapi.ChatMemberConfig{
UserID: 1,
ChatID: 2,
},
UntilDate: 400, //forever
}
mockBotAPI.EXPECT().KickChatMember(kickCfg).Return(botapi.APIResponse{}, fmt.Errorf("something went wrong")).Times(1)
kickUser(message{userID: 1, chatID: 2}, mockBotAPI)
assert.Contains(t, buf.String(), "[1] Unable to kick user from group: something went wrong")
}