/
lark_test.go
117 lines (106 loc) · 2.63 KB
/
lark_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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
type LarkSuite struct {
suite.Suite
lark *larkRequest
client *http.Client
}
func (l *LarkSuite) SetupSuite() {
client := http.Client{}
// InitLark(&larkRequest{client: &client})
l.lark = &larkRequest{client: &client}
l.client = &client
appID = os.Getenv("TEST_APP_ID")
appSecret = os.Getenv("TEST_APP_SECRET")
testChatID = os.Getenv("TEST_CHAT_ID")
}
func (l *LarkSuite) TearDownSuite() {
l.client.CloseIdleConnections()
}
func TestStoreSuite(t *testing.T) {
l := new(LarkSuite)
suite.Run(t, l)
}
func (l *LarkSuite) TestGetAccessToken() {
appDetails := AppDetails{AppID: appID, AppSecret: appSecret}
larkToken, err := l.lark.GetAccessToken(appDetails)
if err != nil {
l.T().Fatal(err)
}
if larkToken.Msg != "ok" {
l.T().Errorf("Incorrect message, wanted %v, got %v", "ok", larkToken.Msg)
}
}
func (l *LarkSuite) TestGetBotGroups() {
var token LarkToken
appDetails := AppDetails{AppID: appID, AppSecret: appSecret}
payload, err := json.Marshal(appDetails)
if err != nil {
l.T().Fatal(err)
}
req, err := http.NewRequest("POST", "https://open.larksuite.com/open-apis/auth/v3/app_access_token/internal", bytes.NewBuffer(payload))
if err != nil {
l.T().Fatal(err)
}
resp, err := l.client.Do(req)
if err != nil {
l.T().Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
l.T().Fatal(err)
}
err = json.Unmarshal(body, &token)
if err != nil {
l.T().Fatal(err)
}
_, err = l.lark.GetBotGroups(token.TenantAccessToken)
if err != nil {
l.T().Fatal(err)
}
}
func (l *LarkSuite) TestSendMessage() {
var token LarkToken
appDetails := AppDetails{AppID: appID, AppSecret: appSecret}
payload, err := json.Marshal(appDetails)
if err != nil {
l.T().Fatal(err)
}
req, err := http.NewRequest("POST", "https://open.larksuite.com/open-apis/auth/v3/app_access_token/internal", bytes.NewBuffer(payload))
if err != nil {
l.T().Fatal(err)
}
resp, err := l.client.Do(req)
if err != nil {
l.T().Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
l.T().Fatal(err)
}
err = json.Unmarshal(body, &token)
if err != nil {
l.T().Fatal(err)
}
var larkMessageRequest = LarkMessageRequest{
ChatID: testChatID,
MsgType: "text",
Content: Content{
Text: "Travis: A commit has just been made on Glark",
},
}
statusCode := l.lark.SendMessage(token.TenantAccessToken, larkMessageRequest)
if statusCode != http.StatusOK {
l.T().Errorf("Incorrect status code, wanted %v, got %v", http.StatusOK, statusCode)
}
}