forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lark_test.go
161 lines (143 loc) · 3.95 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
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
package lark
import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
)
// This appID & appSecret is for test use
var (
testAppID string
testAppSecret string
testUserEmail string
testUserOpenID string
testUserID string
testUserUnionID string
testGroupChatID string
testWebhookV1 string
testWebhookV2 string
testWebhookV2Signed string
)
func newTestBot() *Bot {
testMode := os.Getenv("GO_LARK_TEST_MODE")
if testMode == "" {
testMode = "testing"
}
if testMode == "local" {
err := godotenv.Load(".env")
if err != nil {
panic(err)
}
}
testAppID = os.Getenv("LARK_APP_ID")
testAppSecret = os.Getenv("LARK_APP_SECRET")
testUserEmail = os.Getenv("LARK_USER_EMAIL")
testUserID = os.Getenv("LARK_USER_ID")
testUserUnionID = os.Getenv("LARK_UNION_ID")
testUserOpenID = os.Getenv("LARK_OPEN_ID")
testGroupChatID = os.Getenv("LARK_CHAT_ID")
testWebhookV1 = os.Getenv("LARK_WEBHOOK_V1")
testWebhookV2 = os.Getenv("LARK_WEBHOOK_V2")
testWebhookV2Signed = os.Getenv("LARK_WEBHOOK_V2_SIGNED")
if len(testAppID) == 0 ||
len(testAppSecret) == 0 ||
len(testUserEmail) == 0 ||
len(testUserID) == 0 ||
len(testUserUnionID) == 0 ||
len(testUserOpenID) == 0 ||
len(testGroupChatID) == 0 {
panic("insufficient test environment")
}
return NewChatBot(testAppID, testAppSecret)
}
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}
func performRequest(r http.HandlerFunc, method, path string, body interface{}) *httptest.ResponseRecorder {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req := httptest.NewRequest(method, path, buf)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
// for general API test suites
var bot *Bot
func init() {
bot = newTestBot()
_, _ = bot.GetTenantAccessTokenInternal(true)
}
func TestBotProperties(t *testing.T) {
chatBot := newTestBot()
assert.NotEmpty(t, chatBot.appID)
assert.NotEmpty(t, chatBot.appSecret)
assert.Empty(t, chatBot.webhook)
assert.Equal(t, DomainFeishu, chatBot.domain)
assert.Equal(t, ChatBot, chatBot.botType)
assert.NotNil(t, chatBot.client)
assert.NotNil(t, chatBot.logger)
notifyBot := NewNotificationBot(testWebhookV1)
assert.Empty(t, notifyBot.appID)
assert.Empty(t, notifyBot.appSecret)
assert.NotEmpty(t, notifyBot.webhook)
assert.Empty(t, notifyBot.domain)
assert.Equal(t, NotificationBot, notifyBot.botType)
assert.NotNil(t, notifyBot.client)
assert.NotNil(t, notifyBot.logger)
}
func TestRequiredType(t *testing.T) {
bot := newTestBot()
assert.True(t, bot.requireType(ChatBot))
assert.False(t, bot.requireType(NotificationBot))
}
func TestSetDomain(t *testing.T) {
bot := newTestBot()
assert.Equal(t, DomainFeishu, bot.domain)
assert.Equal(t, DomainFeishu, bot.Domain())
bot.SetDomain("https://test.test")
assert.Equal(t, "https://test.test", bot.domain)
assert.Equal(t, "https://test.test", bot.Domain())
}
func TestBotGetters(t *testing.T) {
bot := newTestBot()
assert.Equal(t, testAppID, bot.AppID())
assert.Equal(t, ChatBot, bot.BotType())
assert.Equal(t, "", bot.AccessToken())
assert.Equal(t, "", bot.TenantAccessToken())
}
func TestSetClient(t *testing.T) {
bot := &Bot{}
assert.Nil(t, bot.client)
bot.SetClient(&http.Client{})
assert.NotNil(t, bot.client)
}
type customHTTPWrapper struct {
client *http.Client
}
func (c customHTTPWrapper) Do(ctx context.Context, method, url string, header http.Header, body io.Reader) (io.ReadCloser, error) {
return nil, nil
}
func TestCustomClient(t *testing.T) {
bot := &Bot{}
assert.Nil(t, bot.customClient)
var c customHTTPWrapper
bot.SetCustomClient(c)
assert.NotNil(t, bot.customClient)
}
func TestUpdateWebhook(t *testing.T) {
bot := NewNotificationBot("abc")
assert.Equal(t, "abc", bot.webhook)
bot.SetWebhook("def")
assert.Equal(t, "def", bot.webhook)
}