This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
81 lines (63 loc) · 1.77 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
package stream_chat
import (
"testing"
"time"
"github.com/pascaldekloe/jwt"
"github.com/stretchr/testify/assert"
)
func initClient(t *testing.T) *Client {
c, err := NewClient(APIKey, []byte(APISecret))
mustNoError(t, err, "new client")
// set hostname to client from env if present
if StreamHost != "" {
c.BaseURL = StreamHost
}
return c
}
func initChannel(t *testing.T, c *Client) *Channel {
_, err := c.UpdateUsers(testUsers...)
mustNoError(t, err, "update users")
members := make([]string, 0, len(testUsers))
for i := range testUsers {
members = append(members, testUsers[i].ID)
}
ch, err := c.CreateChannel("team", "fellowship-of-the-ring", serverUser.ID, map[string]interface{}{
"members": members,
})
mustNoError(t, err, "create channel")
return ch
}
func TestNewClient(t *testing.T) {
c := initClient(t)
assert.Equal(t, c.apiKey, APIKey)
assert.Equal(t, c.apiSecret, []byte(APISecret))
assert.NotEmpty(t, c.authToken)
assert.Equal(t, defaultTimeout, c.HTTP.Timeout)
// assert.Equal(t, defaultBaseURL, c.BaseURL, )
}
func Test_client_CreateToken(t *testing.T) {
c := initClient(t)
var expire = time.Now().Add(time.Hour)
tt := []struct {
name string
expire time.Time
}{
{"token without expire", time.Time{}},
{"token with expire", expire},
}
for _, test := range tt {
test := test
t.Run(test.name, func(t *testing.T) {
token, err := c.CreateToken(testUsers[0].ID, test.expire)
mustNoError(t, err, "create token")
claims, err := jwt.HMACCheck(token, c.apiSecret)
mustNoError(t, err, "jwt check")
var expiresIn *jwt.NumericTime
if !test.expire.IsZero() {
expiresIn = jwt.NewNumericTime(test.expire)
}
assert.Equal(t, expiresIn, claims.Expires)
assert.Equal(t, testUsers[0].ID, claims.Set["user_id"])
})
}
}