-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathslackdump_test.go
259 lines (235 loc) · 6.1 KB
/
slackdump_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package slackdump
import (
"context"
"log"
"log/slog"
"math"
"net/http"
"os"
"testing"
"testing/fstest"
"time"
"github.com/rusq/fsadapter"
"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/auth"
"github.com/rusq/slackdump/v3/internal/edge"
"github.com/rusq/slackdump/v3/internal/mocks/mock_auth"
"github.com/rusq/slackdump/v3/internal/network"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func Test_newLimiter(t *testing.T) {
t.Parallel()
type args struct {
t network.Tier
burst uint
boost int
}
tests := []struct {
name string
args args
wantDelay time.Duration
}{
{
"Tier test",
args{
network.Tier3,
1,
0,
},
time.Duration(math.Round(60.0/float64(network.Tier3)*1000.0)) * time.Millisecond, // 6/5 sec
},
{
"burst 2",
args{
network.Tier3,
2,
0,
},
1 * time.Millisecond,
},
{
"boost 70",
args{
network.Tier3,
1,
70,
},
time.Duration(math.Round(60.0/float64(network.Tier3+70)*1000.0)) * time.Millisecond, // 500 msec
},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := network.NewLimiter(tt.args.t, tt.args.burst, tt.args.boost)
assert.NoError(t, got.Wait(context.Background())) // prime
start := time.Now()
err := got.Wait(context.Background())
stop := time.Now()
assert.NoError(t, err)
assert.WithinDurationf(t, start.Add(tt.wantDelay), stop, 10*time.Millisecond, "delayed for: %s, expected: %s", stop.Sub(start), tt.wantDelay)
})
}
}
func ExampleNew_tokenAndCookie() {
provider, err := auth.NewValueAuth("xoxc-...", "xoxd-...")
if err != nil {
log.Print(err)
return
}
fsa := openTempFS()
defer fsa.Close()
sd, err := New(context.Background(), provider)
if err != nil {
log.Print(err)
return
}
_ = sd
}
func ExampleNew_cookieFile() {
provider, err := auth.NewCookieFileAuth("xoxc-...", "cookies.txt")
if err != nil {
log.Print(err)
return
}
fsa := openTempFS()
defer fsa.Close()
sd, err := New(context.Background(), provider)
if err != nil {
log.Print(err)
return
}
_ = sd
}
func ExampleNew_browserAuth() {
provider, err := auth.NewPlaywrightAuth(context.Background())
if err != nil {
log.Print(err)
return
}
fsa := openTempFS()
defer fsa.Close()
sd, err := New(context.Background(), provider)
if err != nil {
log.Print(err)
return
}
_ = sd
}
func openTempFS() fsadapter.FSCloser {
dir, err := os.MkdirTemp("", "slackdump")
if err != nil {
panic(err)
}
fsc, err := fsadapter.New(dir)
if err != nil {
panic(err)
}
return fsc
}
func TestSession_initWorkspaceInfo(t *testing.T) {
ctx := context.Background()
t.Run("ok", func(t *testing.T) {
ctrl := gomock.NewController(t)
mc := NewmockClienter(ctrl)
mc.EXPECT().AuthTestContext(gomock.Any()).Return(&slack.AuthTestResponse{
TeamID: "TEST",
}, nil)
s := Session{
client: nil, // it should use the provided client
}
err := s.initWorkspaceInfo(ctx, mc)
assert.NoError(t, err, "unexpected initialisation error")
})
t.Run("error", func(t *testing.T) {
ctrl := gomock.NewController(t)
mc := NewmockClienter(ctrl)
mc.EXPECT().AuthTestContext(gomock.Any()).Return(nil, assert.AnError)
s := Session{
client: nil, // it should use the provided client
}
err := s.initWorkspaceInfo(ctx, mc)
assert.Error(t, err, "expected error")
})
}
func TestSession_initClient(t *testing.T) {
// fakeSlackAPI contains fake endpoints for the slack API.
fakeSlackAPI := fstest.MapFS{
"api/auth.test": &fstest.MapFile{
Data: []byte(`{"ok":true,"url":"https:\/\/test.slack.com\/","team":"TEST","user":"test","team_id":"T123456","user_id":"U123456"}`),
Mode: 0644,
},
}
fakeEnterpriseSlackAPI := fstest.MapFS{
"api/auth.test": &fstest.MapFile{
Data: []byte(`{"ok":true,"url":"https:\/\/test.slack.com\/","team":"TEST","user":"test","team_id":"T123456","user_id":"U123456","enterprise_id":"E123456"}`),
},
}
expectAuthTestFn := func(mc *mockClienter, enterpriseID string) {
mc.EXPECT().AuthTestContext(gomock.Any()).Return(&slack.AuthTestResponse{
TeamID: "TEST",
EnterpriseID: enterpriseID,
}, nil)
}
t.Run("pre-initialised client", func(t *testing.T) {
ctrl := gomock.NewController(t)
mc := NewmockClienter(ctrl)
expectAuthTestFn(mc, "") // not an anterprise instance
s := Session{
client: mc,
}
err := s.initClient(context.Background(), nil, false)
assert.NoError(t, err, "unexpected error")
assert.IsType(t, &mockClienter{}, s.client)
})
t.Run("standard client", func(t *testing.T) {
// http client will return the file from the fakeAPIFS.
cl := http.Client{
Transport: http.NewFileTransportFS(fakeSlackAPI),
}
ctrl := gomock.NewController(t)
mprov := mock_auth.NewMockProvider(ctrl)
mprov.EXPECT().SlackToken().Return("xoxb-...")
mprov.EXPECT().HTTPClient().Return(&cl, nil)
s := Session{
client: nil,
log: slog.Default(),
}
err := s.initClient(context.Background(), mprov, false)
assert.NoError(t, err, "unexpected error")
assert.IsType(t, &slack.Client{}, s.client)
})
t.Run("enterprise client", func(t *testing.T) {
cl := http.Client{
Transport: http.NewFileTransportFS(fakeEnterpriseSlackAPI),
}
ctrl := gomock.NewController(t)
mprov := mock_auth.NewMockProvider(ctrl)
mprov.EXPECT().SlackToken().Return("xoxb-...").Times(2)
mprov.EXPECT().HTTPClient().Return(&cl, nil).Times(2)
s := Session{
client: nil,
log: slog.Default(),
}
err := s.initClient(context.Background(), mprov, false)
assert.NoError(t, err, "unexpected error")
assert.IsType(t, &edge.Wrapper{}, s.client)
})
t.Run("forced enterprise client", func(t *testing.T) {
cl := http.Client{
Transport: http.NewFileTransportFS(fakeSlackAPI),
}
ctrl := gomock.NewController(t)
mprov := mock_auth.NewMockProvider(ctrl)
mprov.EXPECT().SlackToken().Return("xoxb-...").Times(2)
mprov.EXPECT().HTTPClient().Return(&cl, nil).Times(2)
s := Session{
client: nil,
log: slog.Default(),
}
err := s.initClient(context.Background(), mprov, true)
assert.NoError(t, err, "unexpected error")
assert.IsType(t, &edge.Wrapper{}, s.client)
})
}