-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbots_test.go
More file actions
105 lines (96 loc) · 2.54 KB
/
bots_test.go
File metadata and controls
105 lines (96 loc) · 2.54 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
package maxigo
import (
"context"
"errors"
"net/http"
"testing"
)
func TestGetBot(t *testing.T) {
t.Run("success", func(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("method = %q, want GET", r.Method)
}
if r.URL.Path != "/me" {
t.Errorf("path = %q, want /me", r.URL.Path)
}
writeJSON(t, w, BotInfo{
UserWithPhoto: UserWithPhoto{
User: User{
UserID: 12345,
FirstName: "TestBot",
IsBot: true,
},
},
Commands: []BotCommand{
{Name: "start", Description: strPtr("Start the bot")},
},
})
})
bot, err := c.GetBot(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if bot.UserID != 12345 {
t.Errorf("UserID = %d, want 12345", bot.UserID)
}
if bot.FirstName != "TestBot" {
t.Errorf("FirstName = %q, want %q", bot.FirstName, "TestBot")
}
if !bot.IsBot {
t.Error("IsBot should be true")
}
if len(bot.Commands) != 1 || bot.Commands[0].Name != "start" {
t.Errorf("Commands = %v, want [{start}]", bot.Commands)
}
})
t.Run("unauthorized", func(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
writeError(t, w, http.StatusUnauthorized, `{"code":"verify.token","message":"Invalid access_token"}`)
})
_, err := c.GetBot(context.Background())
if err == nil {
t.Fatal("expected error")
}
var e *Error
if !errors.As(err, &e) {
t.Fatalf("expected *Error, got %T", err)
}
if e.StatusCode != http.StatusUnauthorized {
t.Errorf("StatusCode = %d, want 401", e.StatusCode)
}
})
}
func TestEditBot(t *testing.T) {
t.Run("success", func(t *testing.T) {
c, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPatch {
t.Errorf("method = %q, want PATCH", r.Method)
}
if r.URL.Path != "/me" {
t.Errorf("path = %q, want /me", r.URL.Path)
}
var patch BotPatch
readJSON(t, r, &patch)
if !patch.FirstName.Set || patch.FirstName.Value != "NewName" {
t.Errorf("FirstName = %v, want NewName", patch.FirstName)
}
writeJSON(t, w, BotInfo{
UserWithPhoto: UserWithPhoto{
User: User{
UserID: 12345,
FirstName: "NewName",
IsBot: true,
},
},
})
})
bot, err := c.EditBot(context.Background(), &BotPatch{FirstName: Some("NewName")})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if bot.FirstName != "NewName" {
t.Errorf("FirstName = %q, want %q", bot.FirstName, "NewName")
}
})
}