-
Notifications
You must be signed in to change notification settings - Fork 34
/
startup_test.go
166 lines (155 loc) · 4.5 KB
/
startup_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
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"github.com/multiplay/go-ts3"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
)
const teamspeakCheckNickname = "SinusBot via GitHub Actions"
type instance struct {
UUID string `json:"uuid"`
}
func TestIsBotRunning(t *testing.T) {
if _, err := getBotID(); err != nil {
t.Fatalf("could not get botId: %v", err)
}
}
func TestConnectToTeamspeak(t *testing.T) {
botId, err := getBotID()
if err != nil {
t.Fatalf("could not get botId: %v", err)
}
pw, err := ioutil.ReadFile(".password")
if err != nil {
t.Fatalf("could not read password file")
}
token, err := login("admin", string(pw), *botId)
if err != nil {
t.Fatalf("could not get token: %v", err)
}
bots, err := getInstances(*token)
if err != nil {
t.Fatalf("could not get instances: %v", err)
}
if err := changeSettings(bots[0].UUID, *token); err != nil {
t.Fatalf("could not change instance settings: %v", err)
}
fmt.Println("Sleeping so that the bot will connect in this time to the server")
time.Sleep(5 * time.Second)
}
func TestIsBotOnTeamspeak(t *testing.T) {
c, err := ts3.NewClient("julia.ts3index.com:10011")
if err != nil {
t.Fatalf("could not create new ts3 client: %v", err)
}
defer c.Close()
if err := c.UsePort(1489); err != nil {
t.Fatalf("could not use port: %v", err)
}
clientList, err := c.Server.ClientList()
if err != nil {
t.Fatalf("could not get clientlist: %v", err)
}
found := false
for _, client := range clientList {
if strings.Contains(client.Nickname, teamspeakCheckNickname) {
found = true
break
}
}
if !found {
t.Fatal("no client found")
}
}
func getInstances(token string) ([]instance, error) {
req, err := http.NewRequest("GET", "http://127.0.0.1:8087/api/v1/bot/instances", nil)
if err != nil {
return nil, errors.Wrap(err, "could not create request")
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "could not do request")
}
var data []instance
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, errors.Wrap(err, "could not decode json")
}
return data, nil
}
func changeSettings(uuid, token string) error {
data, err := json.Marshal(map[string]string{
"instanceId": uuid,
"nick": teamspeakCheckNickname,
"serverHost": "sinusbot.com",
})
req, err := http.NewRequest("POST", "http://127.0.0.1:8087/api/v1/bot/i/"+uuid+"/settings", bytes.NewBuffer(data))
if err != nil {
return errors.Wrap(err, "could not create request")
}
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "could not do request")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("invalid status code received by setting instance settings: %d", resp.StatusCode)
}
req, err = http.NewRequest("POST", "http://127.0.0.1:8087/api/v1/bot/i/"+uuid+"/spawn", nil)
if err != nil {
return errors.Wrap(err, "could not create request")
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err = http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "could not do request")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("invalid status code received by spawning instance settings")
}
return nil
}
func getBotID() (*string, error) {
resp, err := http.Get("http://127.0.0.1:8087/api/v1/botId")
if err != nil {
return nil, errors.Wrap(err, "could not get")
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status is not expected: %d; got: %d", http.StatusOK, resp.StatusCode)
}
var data struct {
DefaultBotID string `json:"defaultBotId"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, errors.Wrap(err, "could not decode data")
}
return &data.DefaultBotID, nil
}
func login(username, password, botId string) (*string, error) {
data, err := json.Marshal(map[string]string{
"username": username,
"password": password,
"botId": botId,
})
if err != nil {
return nil, errors.Wrap(err, "could not marshal json")
}
resp, err := http.Post("http://127.0.0.1:8087/api/v1/bot/login", "application/json", bytes.NewBuffer(data))
if err != nil {
return nil, errors.Wrap(err, "could not post")
}
var res struct {
Token string `json:"token"`
}
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, errors.Wrap(err, "could not decode json")
}
return &res.Token, nil
}