-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands_test.go
125 lines (104 loc) · 3.4 KB
/
commands_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
package main
import (
"testing"
"github.com/alphatroya/redmine-helper-bot/storage"
)
func setupSubTest(t *testing.T) func(t *testing.T) {
setup()
return func(t *testing.T) {
tearDown()
}
}
var botMock *MockBotSender
var redisMock *storage.Mock
var handler *UpdateHandler
func setup() {
botMock = &MockBotSender{}
redisMock = storage.NewStorageMock()
handler = &UpdateHandler{botMock, redisMock}
}
func tearDown() {
botMock = nil
redisMock = nil
handler = nil
}
func TestTokenRequest(t *testing.T) {
const wrongHostArgumentsText = "Неправильное количество аргументов, введите адрес в формате `/host <адрес сервера>`(например, `/host https://google.ru`)"
const wrongTokenArgumentsCountText = "Неправильное количество аргументов, введите токен доступа к АПИ в формате `/token <токен>`"
data := []struct {
command string
message string
chatID int64
expected string
}{
{"token", "", 1, wrongTokenArgumentsCountText},
{"token", "test test", 1, wrongTokenArgumentsCountText},
{"token", "fdsjfdsj", 1, "Токен успешно обновлен"},
{"token", " ", 1, wrongTokenArgumentsCountText},
{"", "qwertyu", 1, "Введена неправильная команда"},
{"host", "", 1, wrongHostArgumentsText},
{"host", " ", 1, wrongHostArgumentsText},
{"host", "test test", 1, wrongHostArgumentsText},
{"host", "test", 1, "parse \"test\": invalid URI for request"},
{"host", "https://www.google.com", 1, "Адрес сервера успешно обновлен"},
}
for _, message := range data {
teardownSubTest := setupSubTest(t)
handler.Handle(message.command, message.message, message.chatID)
if botMock.text != message.expected {
t.Errorf("Wrong response command: %s\narguments: %s\nexpected: %s\nreceived: %s", message.command, message.message, message.expected, botMock.text)
}
teardownSubTest(t)
}
}
func TestStorageTokenData(t *testing.T) {
data := []struct {
command string
chatID int64
}{
{"431", 44},
{"23", 45},
}
for _, message := range data {
teardownSubTest := setupSubTest(t)
defer teardownSubTest(t)
handler.Handle("token", message.command, message.chatID)
tokenValue := redisMock.StorageToken()[message.chatID]
if tokenValue != message.command {
t.Errorf("Wrong token storage logic: %s is not %s", tokenValue, message.command)
}
}
}
func TestMultipleRequestStorageTokenData(t *testing.T) {
teardownSubTest := setupSubTest(t)
defer teardownSubTest(t)
command := "4433"
var chatID int64 = 1
command2 := "4433"
var chatID2 int64 = 2
handler.Handle("token", command, chatID)
handler.Handle("token", command2, chatID2)
tokenValue := redisMock.StorageToken()[chatID2]
if tokenValue != command2 {
t.Errorf("Wrong token storage logic: %s is not %s", tokenValue, command2)
}
}
func TestHandleHostMessageWithCorrectCommand(t *testing.T) {
teardownSubTest := setupSubTest(t)
defer teardownSubTest(t)
data := []struct {
url string
chatID int64
}{
{"https://www.google.com", 44},
{"https://www.tt.com", 45},
{"https://tt.com", 46},
}
for _, message := range data {
handler.Handle("host", message.url, message.chatID)
hostValue := redisMock.StorageHost()[message.chatID]
if hostValue != message.url {
t.Errorf("Wrong saved host value %s is not %s", hostValue, message.url)
}
}
}