-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbots_api_test.go
127 lines (112 loc) · 2.71 KB
/
bots_api_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
package groupme
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/gorilla/mux"
"github.com/stretchr/testify/suite"
)
type BotsAPISuite struct {
APISuite
botClient *BotClient
}
func (s *BotsAPISuite) SetupSuite() {
s.handler = botsTestRouter()
s.setupSuite()
s.botClient = NewBotClient("bot-id")
s.botClient.apiEndpointBase = "http://" + s.addr
}
func (s *BotsAPISuite) TestBotsCreate() {
bot, err := s.client.CreateBot(context.Background(), &Bot{
Name: "test",
GroupID: "1",
AvatarURL: "url.com",
CallbackURL: "otherURL.com",
DMNotification: true,
})
s.Require().NoError(err)
s.Require().NotZero(bot)
}
func (s *BotsAPISuite) TestBotsPostMessage() {
err := s.botClient.PostBotMessage(context.Background(), "test message", nil)
s.Require().NoError(err)
}
func (s *BotsAPISuite) TestBotsIndex() {
bots, err := s.client.IndexBots(context.Background())
s.Require().NoError(err)
s.Require().NotZero(bots)
for _, bot := range bots {
s.Assert().NotZero(bot)
}
}
func (s *BotsAPISuite) TestBotsDestroy() {
s.Require().NoError(s.client.DestroyBot(context.Background(), "1"))
}
func TestBotsAPISuite(t *testing.T) {
suite.Run(t, new(BotsAPISuite))
}
func botsTestRouter() *mux.Router {
router := mux.NewRouter()
authRouter := router.Queries("token", "").Subrouter()
// Create
authRouter.Path("/bots").
Methods("POST").
Name("CreateBot").
HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(201)
fmt.Fprint(w, `{
"response": {
"bot_id": "1234567890",
"group_id": "1234567890",
"name": "hal9000",
"avatar_url": "https://i.groupme.com/123456789",
"callback_url": "https://example.com/bots/callback",
"dm_notification": false
},
"meta": {
"code": 201,
"errors": []
}
}`)
})
// Post Message
router.Path("/bots/post").
Methods("POST").
Name("PostBotMessage").
HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(201)
})
// Index
authRouter.Path("/bots").
Methods("GET").
Name("IndexBots").
HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
fmt.Fprint(w, `{
"response": [
{
"bot_id": "1234567890",
"group_id": "1234567890",
"name": "hal9000",
"avatar_url": "https://i.groupme.com/123456789",
"callback_url": "https://example.com/bots/callback",
"dm_notification": false
}
],
"meta": {
"code": 200,
"errors": []
}
}`)
})
// Destroy
authRouter.Path("/bots/destroy").
Methods("POST").
Name("DestroyBot").
HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(201)
})
/*// Return test router //*/
return router
}