-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmanage_direct_message_test.go
287 lines (281 loc) · 7.99 KB
/
manage_direct_message_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package gotwtr_test
import (
"context"
_ "embed"
"io"
"net/http"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/sivchari/gotwtr"
)
//go:embed testdata/create_one_to_one_dm.json
var createOneToOneDM []byte
//go:embed testdata/create_new_group_dm.json
var createNewGroupDM []byte
//go:embed testdata/post_dm.json
var postDM []byte
func Test_CreateOneToOneDM(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
client *http.Client
participantID string
body *gotwtr.CreateOneToOneDMBody
}
tests := []struct {
name string
args args
want *gotwtr.CreateOneToOneDMResponse
wantErr bool
}{
{
name: "201 Create a one to one dm",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(strings.NewReader(string(createOneToOneDM))),
}
}),
participantID: "2244994945",
body: &gotwtr.CreateOneToOneDMBody{
Text: "This is a one-to-one Direct Message with an attachment",
Attachments: []gotwtr.DirectMessageAttachment{
{
MediaID: "1455952740635586573",
},
},
},
},
want: &gotwtr.CreateOneToOneDMResponse{
DMConversationID: "1346889436626259968",
DMEventFieldID: "128341038123",
},
wantErr: false,
},
{
name: "403 authentication error",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusForbidden,
Body: io.NopCloser(strings.NewReader(string(forbidden))),
}
}),
participantID: "2244994945",
body: &gotwtr.CreateOneToOneDMBody{
Text: "This is a one-to-one Direct Message with an attachment",
Attachments: []gotwtr.DirectMessageAttachment{
{
MediaID: "1455952740635586573",
},
},
},
},
want: &gotwtr.CreateOneToOneDMResponse{
Errors: []*gotwtr.APIResponseError{
{
Title: "Unsupported Authentication",
Detail: "Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].",
Type: "https://api.twitter.com/2/problems/unsupported-authentication",
Status: 403,
},
},
},
wantErr: true,
},
}
for i, tt := range tests {
i := i
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := gotwtr.New("test-key", gotwtr.WithHTTPClient(tt.args.client))
got, err := c.CreateOneToOneDM(tt.args.ctx, tt.args.participantID, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("client.CreateOneToOneDM() index = %v error = %v, wantErr %v", i, err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("client.CreateNewOneToOneDM() index = %v mismatch (-want +got):\n%s", i, diff)
return
}
})
}
}
func Test_CreateNewGroupDM(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
client *http.Client
conversationID string
body *gotwtr.CreateNewGroupDMBody
}
tests := []struct {
name string
args args
want *gotwtr.CreateNewGroupDMResponse
wantErr bool
}{
{
name: "201 Create New Group",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(strings.NewReader(string(createNewGroupDM))),
}
}),
conversationID: "1346889436626259968",
body: &gotwtr.CreateNewGroupDMBody{
Text: "Adding a Direct Message to a conversation by referencing the conversation ID. This method supports both one-to-one and group conversations.",
},
},
want: &gotwtr.CreateNewGroupDMResponse{
DMConversationID: "1346889436626259968",
DMEventFieldID: "128341038123",
},
wantErr: false,
},
{
name: "403 authentication error",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusForbidden,
Body: io.NopCloser(strings.NewReader(string(forbidden))),
}
}),
conversationID: "1346889436626259968",
body: &gotwtr.CreateNewGroupDMBody{
Text: "Adding a Direct Message to a conversation by referencing the conversation ID. This method supports both one-to-one and group conversations.",
},
},
want: &gotwtr.CreateNewGroupDMResponse{
Errors: []*gotwtr.APIResponseError{
{
Title: "Unsupported Authentication",
Detail: "Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].",
Type: "https://api.twitter.com/2/problems/unsupported-authentication",
Status: 403,
},
},
},
wantErr: true,
},
}
for i, tt := range tests {
i := i
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := gotwtr.New("test-key", gotwtr.WithHTTPClient(tt.args.client))
got, err := c.CreateNewGroupDM(tt.args.ctx, tt.args.conversationID, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("client.CreateNewGroupDM() index = %v error = %v, wantErr %v", i, err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("client.CreateNewGroupDM() index = %v mismatch (-want +got):\n%s", i, diff)
return
}
})
}
}
func Test_PostDM(t *testing.T) {
t.Parallel()
type args struct {
ctx context.Context
client *http.Client
body *gotwtr.PostDMBody
}
tests := []struct {
name string
args args
want *gotwtr.PostDMResponse
wantErr bool
}{
{
name: "201 Post DM",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusCreated,
Body: io.NopCloser(strings.NewReader(string(postDM))),
}
}),
body: &gotwtr.PostDMBody{
ConversationType: "Group",
ParticipantIDs: []string{
"944480690",
"906948460078698496",
},
Message: &gotwtr.DirectMessage{
Text: "Hello to you two, this is a new group conversation",
},
},
},
want: &gotwtr.PostDMResponse{
DMConversationID: "1346889436626259968",
DMEventFieldID: "128341038123",
},
wantErr: false,
},
{
name: "403 authentication error",
args: args{
ctx: context.Background(),
client: mockHTTPClient(func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusForbidden,
Body: io.NopCloser(strings.NewReader(string(forbidden))),
}
}),
body: &gotwtr.PostDMBody{
ConversationType: "Group",
ParticipantIDs: []string{
"944480690",
"906948460078698496",
},
Message: &gotwtr.DirectMessage{
Text: "Hello to you two, this is a new group conversation",
},
},
},
want: &gotwtr.PostDMResponse{
Errors: []*gotwtr.APIResponseError{
{
Title: "Unsupported Authentication",
Detail: "Authenticating with OAuth 2.0 Application-Only is forbidden for this endpoint. Supported authentication types are [OAuth 1.0a User Context, OAuth 2.0 User Context].",
Type: "https://api.twitter.com/2/problems/unsupported-authentication",
Status: 403,
},
},
},
wantErr: true,
},
}
for i, tt := range tests {
i := i
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := gotwtr.New("test-key", gotwtr.WithHTTPClient(tt.args.client))
got, err := c.PostDM(tt.args.ctx, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("client.PostDM() index = %v error = %v, wantErr %v", i, err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("client.PostDM() index = %v mismatch (-want +got):\n%s", i, diff)
return
}
})
}
}