-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathban_test.go
151 lines (116 loc) · 4.02 KB
/
ban_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
package stream_chat
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestShadowBanUser(t *testing.T) {
c := initClient(t)
userA := randomUser(t, c)
userB := randomUser(t, c)
userC := randomUser(t, c)
ctx := context.Background()
ch := initChannel(t, c, userA.ID, userB.ID, userC.ID)
resp, err := c.CreateChannel(ctx, ch.Type, ch.ID, userA.ID, nil)
require.NoError(t, err)
ch = resp.Channel
// shadow ban userB globally
_, err = c.ShadowBan(ctx, userB.ID, userA.ID)
require.NoError(t, err)
// shadow ban userC on channel
_, err = ch.ShadowBan(ctx, userC.ID, userA.ID)
require.NoError(t, err)
msg := &Message{Text: "test message"}
messageResp, err := ch.SendMessage(ctx, msg, userB.ID)
require.NoError(t, err)
msg = messageResp.Message
require.False(t, msg.Shadowed)
messageResp, err = c.GetMessage(ctx, msg.ID)
require.NoError(t, err)
require.True(t, messageResp.Message.Shadowed)
msg = &Message{Text: "test message"}
messageResp, err = ch.SendMessage(ctx, msg, userC.ID)
require.NoError(t, err)
msg = messageResp.Message
require.False(t, msg.Shadowed)
messageResp, err = c.GetMessage(ctx, msg.ID)
require.NoError(t, err)
require.True(t, messageResp.Message.Shadowed)
_, err = c.UnBanUser(ctx, userB.ID)
require.NoError(t, err)
msg = &Message{Text: "test message"}
messageResp, err = ch.SendMessage(ctx, msg, userB.ID)
require.NoError(t, err)
msg = messageResp.Message
require.False(t, msg.Shadowed)
messageResp, err = c.GetMessage(ctx, msg.ID)
require.NoError(t, err)
require.False(t, messageResp.Message.Shadowed)
_, err = ch.UnBanUser(ctx, userC.ID)
require.NoError(t, err)
msg = &Message{Text: "test message"}
messageResp, err = ch.SendMessage(ctx, msg, userC.ID)
require.NoError(t, err)
msg = messageResp.Message
require.False(t, msg.Shadowed)
messageResp, err = c.GetMessage(ctx, msg.ID)
require.NoError(t, err)
require.False(t, messageResp.Message.Shadowed)
}
func TestBanUnbanUser(t *testing.T) {
c := initClient(t)
target := randomUser(t, c)
user := randomUser(t, c)
ctx := context.Background()
_, err := c.BanUser(ctx, target.ID, user.ID, BanWithReason("spammer"), BanWithExpiration(60))
require.NoError(t, err)
resp, err := c.QueryBannedUsers(ctx, &QueryBannedUsersOptions{
QueryOption: &QueryOption{Filter: map[string]interface{}{
"user_id": map[string]string{"$eq": target.ID},
}},
})
require.NoError(t, err)
require.Equal(t, "spammer", resp.Bans[0].Reason)
require.NotZero(t, resp.Bans[0].Expires)
_, err = c.UnBanUser(ctx, target.ID)
require.NoError(t, err)
resp, err = c.QueryBannedUsers(ctx, &QueryBannedUsersOptions{
QueryOption: &QueryOption{Filter: map[string]interface{}{
"user_id": map[string]string{"$eq": target.ID},
}},
})
require.NoError(t, err)
require.Empty(t, resp.Bans)
}
func TestChannelBanUnban(t *testing.T) {
c := initClient(t)
target := randomUser(t, c)
user := randomUser(t, c)
ch := initChannel(t, c, user.ID, target.ID)
ctx := context.Background()
_, err := ch.BanUser(ctx, target.ID, user.ID, BanWithReason("spammer"), BanWithExpiration(60))
require.NoError(t, err)
_, err = ch.UnBanUser(ctx, target.ID)
require.NoError(t, err)
resp, err := c.QueryBannedUsers(ctx, &QueryBannedUsersOptions{
QueryOption: &QueryOption{Filter: map[string]interface{}{
"channel_cid": map[string]string{"$eq": ch.CID},
}},
})
require.NoError(t, err)
require.Empty(t, resp.Bans)
}
func ExampleClient_BanUser() {
client, _ := NewClient("XXXX", "XXXX")
ctx := context.Background()
// ban a user for 60 minutes from all channel
_, _ = client.BanUser(ctx, "eviluser", "modUser", BanWithExpiration(60), BanWithReason("Banned for one hour"))
// ban a user from the livestream:fortnite channel
channel := client.Channel("livestream", "fortnite")
_, _ = channel.BanUser(ctx, "eviluser", "modUser", BanWithReason("Profanity is not allowed here"))
// remove ban from channel
channel = client.Channel("livestream", "fortnite")
_, _ = channel.UnBanUser(ctx, "eviluser")
// remove global ban
_, _ = client.UnBanUser(ctx, "eviluser")
}