-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhelpers.go
100 lines (84 loc) · 2.23 KB
/
helpers.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
package test
import (
"bytes"
"context"
"math/rand"
"os"
"testing"
"time"
stream "github.com/GetStream/stream-chat-go/v5"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/GetStream/stream-cli/pkg/config"
)
func prepareViperConfig() {
viper.Set("default", "default_app")
viper.Set("apps", []config.App{
{
Name: "default_app",
AccessKey: os.Getenv("STREAM_KEY"),
AccessSecretKey: os.Getenv("STREAM_SECRET"),
ChatURL: config.DefaultChatEdgeURL,
},
})
}
func GetRootCmdWithSubCommands(c ...*cobra.Command) *cobra.Command {
prepareViperConfig()
rootCmd := &cobra.Command{}
rootCmd.PersistentFlags().String("app", "", "app name")
rootCmd.AddCommand(c...)
rootCmd.SetIn(&bytes.Buffer{})
rootCmd.SetOut(&bytes.Buffer{})
rootCmd.SetErr(&bytes.Buffer{})
return rootCmd
}
func InitClient() *stream.Client {
c, err := stream.NewClientFromEnvVars()
if err != nil {
panic(err)
}
return c
}
func InitChannel(t *testing.T) string {
name := RandomString(10)
c := InitClient()
_, _ = c.CreateChannel(context.Background(), "messaging", name, "userid", nil)
return name
}
func DeleteChannel(id string) {
c := InitClient()
_, _ = c.DeleteChannels(context.Background(), []string{"messaging:" + id}, true)
}
func CreateUser() string {
c := InitClient()
id := RandomString(10)
_, _ = c.UpsertUser(context.Background(), &stream.User{ID: id})
return id
}
func DeleteUser(id string) {
c := InitClient()
_, _ = c.DeleteUser(context.Background(),
id,
stream.DeleteUserWithHardDelete(),
stream.DeleteUserWithDeleteConversations())
}
func CreateMessage(channelID, userID string) string {
return CreateMessageWithText(channelID, userID, RandomString(10))
}
func CreateMessageWithText(channelID, userID, text string) string {
c := InitClient()
msg, _ := c.Channel("messaging", channelID).SendMessage(context.Background(), &stream.Message{Text: text}, userID)
return msg.Message.ID
}
func DeleteMessage(id string) {
c := InitClient()
_, _ = c.HardDeleteMessage(context.Background(), id)
}
func RandomString(n int) string {
rand.Seed(time.Now().UnixNano())
bytes := make([]byte, n)
for i := 0; i < n; i++ {
bytes[i] = byte(65 + rand.Intn(25)) // A=65 and Z = 65+25
}
return string(bytes)
}