-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathposts_delete_test.go
More file actions
317 lines (274 loc) · 9.4 KB
/
posts_delete_test.go
File metadata and controls
317 lines (274 loc) · 9.4 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package threads
import (
"context"
"net/http"
"strings"
"testing"
)
func TestDeletePost_Success(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != "DELETE" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"success":true,"deleted_id":"post_1"}`))
}
client := testClient(t, http.HandlerFunc(handler))
deletedID, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if deletedID != "post_1" {
t.Errorf("expected deleted_id %q, got %q", "post_1", deletedID)
}
}
func TestDeletePost_InvalidID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.DeletePost(context.Background(), PostID(""))
if err == nil {
t.Fatal("expected error for empty post ID")
}
}
func TestDeletePost_NotFound(t *testing.T) {
client := testClient(t, jsonHandler(404, `{"error":{"message":"not found","type":"OAuthException","code":100}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.DeletePost(context.Background(), ConvertToPostID("nonexistent"))
if err == nil {
t.Fatal("expected error for 404")
}
if !IsAPIError(err) {
t.Errorf("expected APIError, got %T", err)
}
}
func TestDeletePostWithConfirmation_Success(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"success":true}`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
confirmed := false
_, err := client.DeletePostWithConfirmation(context.Background(), ConvertToPostID("post_1"), func(post *Post) bool {
confirmed = true
return true
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !confirmed {
t.Error("confirmation callback was not called")
}
}
func TestDeletePostWithConfirmation_Cancelled(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","text":"hello"}`))
}
client := testClient(t, http.HandlerFunc(handler))
_, err := client.DeletePostWithConfirmation(context.Background(), ConvertToPostID("post_1"), func(post *Post) bool {
return false // user cancels
})
if err == nil {
t.Fatal("expected error when user cancels deletion")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestDeletePostWithConfirmation_InvalidID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.DeletePostWithConfirmation(context.Background(), PostID(""), func(post *Post) bool {
return true
})
if err == nil {
t.Fatal("expected error for empty post ID")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestDeletePostWithConfirmation_NilCallback(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.DeletePostWithConfirmation(context.Background(), ConvertToPostID("post_1"), nil)
if err == nil {
t.Fatal("expected error for nil callback")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestDeletePostWithConfirmation_GetPostError(t *testing.T) {
client := testClient(t, jsonHandler(404, `{"error":{"message":"not found","type":"OAuthException","code":100}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.DeletePostWithConfirmation(context.Background(), ConvertToPostID("nonexistent"), func(post *Post) bool {
return true
})
if err == nil {
t.Fatal("expected error when post not found")
}
}
func TestDeletePost_Forbidden(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(403)
_, _ = w.Write([]byte(`{"error":{"message":"access denied","type":"OAuthException","code":200}}`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
client.config.RetryConfig.MaxRetries = 0
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err == nil {
t.Fatal("expected error for 403")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestDeletePost_NotAuthenticated(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_ = client.ClearToken()
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err == nil {
t.Fatal("expected error when not authenticated")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestDeletePost_ServerError(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(500)
_, _ = w.Write([]byte(`{"error":{"message":"internal error","type":"OAuthException","code":2}}`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
client.config.RetryConfig.MaxRetries = 0
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err == nil {
t.Fatal("expected error for 500")
}
}
func TestDeletePost_Delete404(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(404)
_, _ = w.Write([]byte(`{"error":{"message":"not found","type":"OAuthException","code":100}}`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
client.config.RetryConfig.MaxRetries = 0
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err == nil {
t.Fatal("expected error for 404 DELETE")
}
// The DeletePost method returns a ValidationError for 404 on the DELETE call
if !strings.Contains(err.Error(), "not found") && !strings.Contains(err.Error(), "Post not found") {
t.Errorf("expected not found error, got: %v", err)
}
}
func TestDeletePost_WithLogger(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"success":true}`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
client.config.Logger = &noopLogger{}
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestDeletePost_MalformedDeleteResponse(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(200)
_, _ = w.Write([]byte(`not json`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
// Should succeed even with malformed response (200 status assumed success)
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestDeletePost_MalformedDeleteResponseWithLogger(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "DELETE" {
w.WriteHeader(200)
_, _ = w.Write([]byte(`not json`))
return
}
// GET requests for post and user info
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"id":"post_1","owner":{"id":"12345"}}`))
}
client := testClient(t, http.HandlerFunc(handler))
client.config.Logger = &noopLogger{}
// Should succeed even with malformed response and logger
_, err := client.DeletePost(context.Background(), ConvertToPostID("post_1"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestValidatePostOwnership_DifferentUser(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
if strings.HasPrefix(r.URL.Path, "/12345") {
// GetMe -> GetUser returns our user
_, _ = w.Write([]byte(`{"id":"12345","username":"me"}`))
} else {
// GetPost returns a post owned by someone else
_, _ = w.Write([]byte(`{"id":"post_1","username":"other_user"}`))
}
}
client := testClient(t, http.HandlerFunc(handler))
err := client.validatePostOwnership(context.Background(), ConvertToPostID("post_1"))
if err == nil {
t.Fatal("expected error for different user ownership")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}