forked from diaspora/diaspora
/
post_interactions_controller_spec.rb
450 lines (409 loc) · 11.7 KB
/
post_interactions_controller_spec.rb
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# frozen_sTring_literal: true
require_relative "api_spec_helper"
describe Api::V1::PostInteractionsController do
let(:auth) {
FactoryGirl.create(
:auth_with_default_scopes,
scopes: %w[openid public:read public:modify private:read private:modify interactions]
)
}
let(:auth_public_only) {
FactoryGirl.create(
:auth_with_default_scopes,
scopes: %w[openid public:read public:modify interactions]
)
}
let(:auth_minimum_scopes) {
FactoryGirl.create(:auth_with_default_scopes)
}
let!(:access_token) { auth.create_access_token.to_s }
let!(:access_token_public_only) { auth_public_only.create_access_token.to_s }
let!(:access_token_minimum_scopes) { auth_minimum_scopes.create_access_token.to_s }
let(:invalid_token) { SecureRandom.hex(9) }
before do
@status = alice.post(
:status_message,
text: "hello @{#{bob.diaspora_handle}} and @{#{eve.diaspora_handle}}from Alice!",
public: true,
to: "all"
)
alice_shared_aspect = alice.aspects.create(name: "shared aspect")
alice.share_with(auth_public_only.user.person, alice_shared_aspect)
alice.share_with(auth.user.person, alice_shared_aspect)
alice.share_with(auth_minimum_scopes.user.person, alice_shared_aspect)
@shared_post = alice.post(:status_message, text: "to aspect only", public: false, to: alice_shared_aspect.id)
end
describe "#subscribe" do
context "succeeds" do
it "with proper guid and access token" do
participation_count = @status.participations.count
post(
api_v1_post_subscribe_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(204)
expect(@status.participations.count).to eq(participation_count + 1)
end
end
context "fails" do
it "when duplicate" do
post(
api_v1_post_subscribe_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(204)
post(
api_v1_post_subscribe_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(409)
end
it "with improper guid" do
post(
api_v1_post_subscribe_path("999_999_999"),
params: {
access_token: access_token
}
)
confirm_api_error(response, 404, "Post with provided guid could not be found")
end
it "with insufficient token" do
post(
api_v1_post_subscribe_path(@status.guid),
params: {
access_token: access_token_minimum_scopes
}
)
expect(response.status).to eq(403)
end
it "on private post without private token" do
post(
api_v1_post_subscribe_path(@shared_post.guid),
params: {
access_token: access_token_public_only
}
)
expect(response.status).to eq(404)
end
it "with invalid token" do
post(
api_v1_post_subscribe_path(@status.guid),
params: {
access_token: invalid_token
}
)
expect(response.status).to eq(401)
end
end
end
describe "#hide" do
context "succeeds" do
it "with proper guid and access token" do
hidden_count = auth.user.hidden_shareables.count
post(
api_v1_post_hide_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(204)
expect(auth.user.reload.hidden_shareables.count).to eq(hidden_count + 1)
end
end
context "fails" do
it "with improper guid" do
post(
api_v1_post_hide_path("999_999_999"),
params: {
access_token: access_token
}
)
confirm_api_error(response, 404, "Post with provided guid could not be found")
end
it "with insufficient token" do
post(
api_v1_post_hide_path(@status.guid),
params: {
access_token: access_token_minimum_scopes
}
)
expect(response.status).to eq(403)
end
it "on private post without private token" do
post(
api_v1_post_hide_path(@shared_post.guid),
params: {
access_token: access_token_public_only
}
)
expect(response.status).to eq(404)
end
it "with invalid token" do
post(
api_v1_post_hide_path(@status.guid),
params: {
access_token: invalid_token
}
)
expect(response.status).to eq(401)
end
end
end
describe "#mute" do
before do
post(
api_v1_post_subscribe_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(204)
end
context "succeeds" do
it "with proper guid and access token" do
count = @status.participations.count
post(
api_v1_post_mute_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(204)
expect(@status.participations.count).to eq(count - 1)
end
end
context "fails" do
it "with improper guid" do
post(
api_v1_post_mute_path("999_999_999"),
params: {
access_token: access_token
}
)
confirm_api_error(response, 404, "Post with provided guid could not be found")
end
it "when not subscribed already" do
post(
api_v1_post_mute_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(204)
post(
api_v1_post_mute_path(@status.guid),
params: {
access_token: access_token
}
)
expect(response.status).to eq(410)
end
it "with insufficient token" do
post(
api_v1_post_mute_path(@status.guid),
params: {
access_token: access_token_minimum_scopes
}
)
expect(response.status).to eq(403)
end
it "on private post without private token" do
post(
api_v1_post_mute_path(@shared_post.guid),
params: {
access_token: access_token_public_only
}
)
expect(response.status).to eq(404)
end
it "with invalid token" do
post(
api_v1_post_mute_path(@status.guid),
params: {
access_token: invalid_token
}
)
expect(response.status).to eq(401)
end
end
end
describe "#report" do
context "succeeds" do
it "with proper guid and access token" do
report_count = @status.reports.count
post(
api_v1_post_report_path(@status.guid),
params: {
reason: "My reason",
access_token: access_token
}
)
expect(response.status).to eq(204)
expect(@status.reports.count).to eq(report_count + 1)
end
end
context "fails" do
it "with improper guid" do
post(
api_v1_post_report_path("999_999_999"),
params: {
reason: "My reason",
access_token: access_token
}
)
confirm_api_error(response, 404, "Post with provided guid could not be found")
end
it "when already reported" do
post(
api_v1_post_report_path(@status.guid),
params: {
reason: "My reason",
access_token: access_token
}
)
expect(response.status).to eq(204)
post(
api_v1_post_report_path(@status.guid),
params: {
reason: "My reason",
access_token: access_token
}
)
confirm_api_error(response, 409, "Failed to create report on this post")
end
it "when missing reason" do
post(
api_v1_post_report_path(@status.guid),
params: {
access_token: access_token
}
)
confirm_api_error(response, 422, "Failed to create report on this post")
end
it "with insufficient token" do
post(
api_v1_post_report_path(@status.guid),
params: {
reason: "My reason",
access_token: access_token_minimum_scopes
}
)
expect(response.status).to eq(403)
end
it "on private post without private token" do
post(
api_v1_post_report_path(@shared_post.guid),
params: {
reason: "My reason",
access_token: access_token_public_only
}
)
expect(response.status).to eq(404)
end
it "with invalid token" do
post(
api_v1_post_report_path(@status.guid),
params: {
reason: "My reason",
access_token: invalid_token
}
)
expect(response.status).to eq(401)
end
end
end
describe "#vote" do
before do
base_params = {status_message: {text: "myText"}, public: true}
poll_params = {poll_question: "something?", poll_answers: %w[yes no maybe]}
merged_params = base_params.merge(poll_params)
@poll_post = StatusMessageCreationService.new(alice).create(merged_params)
@poll_answer = @poll_post.poll.poll_answers.first
end
it "succeeds" do
post(
api_v1_post_vote_path(@poll_post.guid),
params: {
poll_answer: @poll_answer.id,
access_token: access_token
}
)
expect(response.status).to eq(204)
expect(@poll_answer.reload.vote_count).to eq(1)
end
it "fails to vote twice" do
post(
api_v1_post_vote_path(@poll_post.guid),
params: {
poll_answer: @poll_answer.id,
access_token: access_token
}
)
expect(response.status).to eq(204)
post(
api_v1_post_vote_path(@poll_post.guid),
params: {
poll_answer: @poll_answer.id,
access_token: access_token
}
)
confirm_api_error(response, 422, "Cant vote on this post")
end
it "fails with bad answer id" do
post(
api_v1_post_vote_path(@poll_post.guid),
params: {
poll_answer: -1,
access_token: access_token
}
)
confirm_api_error(response, 422, "Cant vote on this post")
end
it "fails with bad post id" do
post(
api_v1_post_vote_path("999_999_999"),
params: {
poll_answer: @poll_answer.id,
access_token: access_token
}
)
confirm_api_error(response, 404, "Post with provided guid could not be found")
end
it "with insufficient token" do
post(
api_v1_post_vote_path(@poll_post.guid),
params: {
poll_answer: @poll_answer.id,
access_token: access_token_minimum_scopes
}
)
expect(response.status).to eq(403)
end
it "on private post without private token" do
post(
api_v1_post_vote_path(@shared_post.guid),
params: {
poll_answer: @poll_answer.id,
access_token: access_token_public_only
}
)
expect(response.status).to eq(404)
end
it "with invalid token" do
post(
api_v1_post_vote_path(@poll_post.guid),
params: {
poll_answer: @poll_answer.id,
access_token: invalid_token
}
)
expect(response.status).to eq(401)
end
end
end