-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchat_polls_integration_test.go
More file actions
149 lines (132 loc) · 4.23 KB
/
chat_polls_integration_test.go
File metadata and controls
149 lines (132 loc) · 4.23 KB
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
package getstream_test
import (
"context"
"strings"
"testing"
. "github.com/GetStream/getstream-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestChatPollsIntegration(t *testing.T) {
t.Parallel()
skipIfShort(t)
client := initClient(t)
ctx := context.Background()
userIDs := createTestUsers(t, client, 2)
userID := userIDs[0]
voterID := userIDs[1]
t.Run("CreateGetUpdateDeletePoll", func(t *testing.T) {
// Create a poll
createResp, err := client.CreatePoll(ctx, &CreatePollRequest{
Name: "Favorite color?",
Description: PtrTo("Pick your favorite color"),
EnforceUniqueVote: PtrTo(true),
UserID: PtrTo(userID),
Options: []PollOptionInput{
{Text: PtrTo("Red")},
{Text: PtrTo("Blue")},
{Text: PtrTo("Green")},
},
})
require.NoError(t, err)
pollID := createResp.Data.Poll.ID
assert.NotEmpty(t, pollID)
assert.Equal(t, "Favorite color?", createResp.Data.Poll.Name)
assert.True(t, createResp.Data.Poll.EnforceUniqueVote)
assert.Len(t, createResp.Data.Poll.Options, 3)
t.Cleanup(func() {
_, _ = client.DeletePoll(context.Background(), pollID, &DeletePollRequest{UserID: PtrTo(userID)})
})
// Get the poll
getResp, err := client.GetPoll(ctx, pollID, &GetPollRequest{})
require.NoError(t, err)
assert.Equal(t, pollID, getResp.Data.Poll.ID)
assert.Equal(t, "Favorite color?", getResp.Data.Poll.Name)
// Update the poll
updateResp, err := client.UpdatePoll(ctx, &UpdatePollRequest{
ID: pollID,
Name: "Updated: Favorite color?",
Description: PtrTo("Updated description"),
UserID: PtrTo(userID),
})
require.NoError(t, err)
assert.Equal(t, "Updated: Favorite color?", updateResp.Data.Poll.Name)
// Delete the poll
_, err = client.DeletePoll(ctx, pollID, &DeletePollRequest{UserID: PtrTo(userID)})
require.NoError(t, err)
})
t.Run("QueryPolls", func(t *testing.T) {
// Create a poll to query
createResp, err := client.CreatePoll(ctx, &CreatePollRequest{
Name: "Query test poll " + randomString(8),
UserID: PtrTo(userID),
Options: []PollOptionInput{
{Text: PtrTo("Option A")},
{Text: PtrTo("Option B")},
},
})
require.NoError(t, err)
pollID := createResp.Data.Poll.ID
t.Cleanup(func() {
_, _ = client.DeletePoll(context.Background(), pollID, &DeletePollRequest{UserID: PtrTo(userID)})
})
// Query polls (server-side auth requires user_id)
qResp, err := client.QueryPolls(ctx, &QueryPollsRequest{
UserID: PtrTo(userID),
Filter: map[string]any{
"id": pollID,
},
})
require.NoError(t, err)
require.NotEmpty(t, qResp.Data.Polls)
assert.Equal(t, pollID, qResp.Data.Polls[0].ID)
})
t.Run("CastPollVote", func(t *testing.T) {
// Create a poll
createResp, err := client.CreatePoll(ctx, &CreatePollRequest{
Name: "Vote test poll",
EnforceUniqueVote: PtrTo(true),
UserID: PtrTo(userID),
Options: []PollOptionInput{
{Text: PtrTo("Yes")},
{Text: PtrTo("No")},
},
})
require.NoError(t, err)
pollID := createResp.Data.Poll.ID
optionID := createResp.Data.Poll.Options[0].ID
t.Cleanup(func() {
_, _ = client.DeletePoll(context.Background(), pollID, &DeletePollRequest{UserID: PtrTo(userID)})
})
// Send a message with the poll attached
ch, _ := createTestChannelWithMembers(t, client, userID, []string{userID, voterID})
sendResp, err := ch.SendMessage(ctx, &SendMessageRequest{
Message: MessageRequest{
Text: PtrTo("Please vote!"),
UserID: PtrTo(userID),
PollID: PtrTo(pollID),
},
})
if err != nil {
if strings.Contains(err.Error(), "polls not enabled") {
t.Skip("Polls not enabled for this channel")
}
}
require.NoError(t, err)
msgID := sendResp.Data.Message.ID
// Cast a vote
voteResp, err := client.Chat().CastPollVote(ctx, msgID, pollID, &CastPollVoteRequest{
UserID: PtrTo(voterID),
Vote: &VoteData{
OptionID: PtrTo(optionID),
},
})
require.NoError(t, err)
require.NotNil(t, voteResp.Data.Vote)
assert.Equal(t, optionID, voteResp.Data.Vote.OptionID)
// Verify poll has votes by getting it
getResp, err := client.GetPoll(ctx, pollID, &GetPollRequest{})
require.NoError(t, err)
assert.Equal(t, 1, getResp.Data.Poll.VoteCount)
})
}