-
Notifications
You must be signed in to change notification settings - Fork 31
/
client_test.go
97 lines (82 loc) · 2.25 KB
/
client_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
package stream_chat
import (
"context"
"net/http"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func initClient(t *testing.T) *Client {
t.Helper()
c, err := NewClientFromEnvVars()
require.NoError(t, err, "new client")
return c
}
func initChannel(t *testing.T, c *Client, membersID ...string) *Channel {
t.Helper()
owner := randomUser(t, c)
ctx := context.Background()
resp, err := c.CreateChannelWithMembers(ctx, "team", randomString(12), owner.ID, membersID...)
require.NoError(t, err, "create channel")
t.Cleanup(func() {
_, _ = c.DeleteChannels(ctx, []string{resp.Channel.CID}, true)
})
return resp.Channel
}
func TestClient_SwapHttpClient(t *testing.T) {
c := initClient(t)
ctx := context.Background()
tr := http.DefaultTransport.(*http.Transport).Clone() //nolint:forcetypeassert
proxyURL, _ := url.Parse("http://getstream.io")
tr.Proxy = http.ProxyURL(proxyURL)
cl := &http.Client{Transport: tr}
c.SetClient(cl)
_, err := c.GetAppSettings(ctx)
require.Error(t, err)
cl = &http.Client{}
c.SetClient(cl)
_, err = c.GetAppSettings(ctx)
require.NoError(t, err)
}
func TestClient_CreateToken(t *testing.T) {
type args struct {
userID string
expire time.Time
iat time.Time
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
"simple without expiration and iat",
args{"tommaso", time.Time{}, time.Time{}},
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoidG9tbWFzbyJ9.v-x-jt3ZnBXXbQ0GoWloIZtVnat2IE74U1a4Yuxd63M",
false,
},
{
"simple with expiration and iat",
args{"tommaso", time.Unix(1566941272, 123121), time.Unix(1566941272, 123121)},
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NjY5NDEyNzIsImlhdCI6MTU2Njk0MTI3MiwidXNlcl9pZCI6InRvbW1hc28ifQ.3HY2O_7o5ZjZ-6KCXLzyPpHZOlNEDy6_m3iNb5DKAMY",
false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient("key", "secret")
require.NoError(t, err)
got, err := c.CreateToken(tt.args.userID, tt.args.expire, tt.args.iat)
if (err != nil) != tt.wantErr {
t.Errorf("createToken() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("createToken() got = %v, want %v", got, tt.want)
}
})
}
}