-
Notifications
You must be signed in to change notification settings - Fork 3
/
available-methods_test.go
119 lines (86 loc) · 2.4 KB
/
available-methods_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
package telegram_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/nasermirzaei89/telegram"
)
const (
testToken = "someToken"
invalidToken = "invalidToken"
)
func TestGetMe(t *testing.T) {
ctx := context.Background()
response := []byte(`{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"Test Bot","username":"TestBot"}}`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := fmt.Sprintf("/bot%s/getMe", testToken)
if r.URL.String() != u {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"ok":false,"error_code":401,"description":"Unauthorized"}`))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
}))
defer server.Close()
// success
bot := telegram.New(testToken, telegram.SetBaseURL(server.URL))
res, err := bot.GetMe(ctx)
if err != nil {
t.Errorf("error on get me: %s", err.Error())
return
}
if !res.IsOK() {
t.Error("result should be ok but is not")
return
}
if res.GetErrorCode() != 0 {
t.Errorf("result error code should be zero but is %d", res.GetErrorCode())
return
}
usr := res.GetUser()
if usr == nil {
t.Error("result user should not be nil but is")
return
}
if usr.ID != 1 {
t.Errorf("result user id should be 1 but is %d", usr.ID)
}
if !usr.IsBot {
t.Errorf("result user should be bot but is not")
}
expectedFirstName := "Test Bot"
if usr.FirstName != expectedFirstName {
t.Errorf("result user first name should be '%s' but is '%s'", expectedFirstName, usr.FirstName)
}
if usr.LastName != nil {
t.Errorf("result user should not have last name but has")
}
if usr.Username == nil {
t.Errorf("result user username should not be nil but is")
return
}
if expectedUsername := "TestBot"; *usr.Username != expectedUsername {
t.Errorf("result user username should be '%s' but is '%s'", expectedUsername, *usr.Username)
}
// fail
bot = telegram.New(invalidToken, telegram.SetBaseURL(server.URL))
res, err = bot.GetMe(ctx)
if err != nil {
t.Errorf("error on get me: %s", err.Error())
return
}
if res.IsOK() {
t.Error("result should not be ok but is")
return
}
if res.GetUser() != nil {
t.Error("result user should be nil but is not")
}
if res.GetErrorCode() != http.StatusUnauthorized {
t.Errorf("result error code should be %d but is %d", http.StatusUnauthorized, res.GetErrorCode())
return
}
}