forked from publify/publify
/
feedback_controller_spec.rb
445 lines (370 loc) · 16 KB
/
feedback_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
require 'spec_helper'
describe Admin::FeedbackController do
render_views
shared_examples_for "destroy feedback with feedback from own article" do
it 'should destroy feedback' do
id = feedback_from_own_article.id
lambda do
post 'destroy', :id => id
end.should change(Feedback, :count)
lambda do
Feedback.find(feedback_from_own_article.id)
end.should raise_error(ActiveRecord::RecordNotFound)
end
it 'should redirect to feedback from article' do
post 'destroy', :id => feedback_from_own_article.id
response.should redirect_to(:controller => 'admin/feedback', :action => 'article', :id => feedback_from_own_article.article.id)
end
it 'should not destroy feedback in get request' do
id = feedback_from_own_article.id
lambda do
get 'destroy', :id => id
end.should_not change(Feedback, :count)
lambda do
Feedback.find(feedback_from_own_article.id)
end.should_not raise_error
response.should render_template 'destroy'
end
end
describe 'logged in admin user' do
before :each do
FactoryGirl.create(:blog)
#TODO Delete after removing fixtures
Profile.delete_all
@admin = FactoryGirl.create(:user, :login => 'henri', :profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN))
request.session = { :user => @admin.id }
end
def feedback_from_own_article
@article ||= FactoryGirl.create(:article, :user => @admin)
@comment_own ||= FactoryGirl.create(:comment, :article => @article)
end
def feedback_from_not_own_article
@spam_comment_not_own ||= FactoryGirl.create(:spam_comment)
end
describe 'destroy action' do
it_should_behave_like "destroy feedback with feedback from own article"
it "should destroy feedback from article doesn't own" do
id = feedback_from_not_own_article.id
lambda do
post 'destroy', :id => id
end.should change(Feedback, :count)
lambda do
Feedback.find(feedback_from_not_own_article.id)
end.should raise_error(ActiveRecord::RecordNotFound)
response.should redirect_to(:controller => 'admin/feedback', :action => 'article', :id => feedback_from_not_own_article.article.id)
end
end
describe 'index action' do
before(:each) do
#Remove feedback due to some fixtures
Feedback.delete_all
end
def should_success_with_index(response)
response.should be_success
response.should render_template('index')
end
it 'should success' do
a = FactoryGirl.create(:article)
3.times { FactoryGirl.create(:comment, :article => a) }
get :index
should_success_with_index(response)
assert_equal 3, assigns(:feedback).size
end
it 'should view only unconfirmed feedback' do
c = FactoryGirl.create(:comment, :state => 'presumed_ham')
FactoryGirl.create(:comment)
get :index, :confirmed => 'f'
should_success_with_index(response)
assigns(:feedback).should == [c]
end
it 'should view only spam feedback' do
FactoryGirl.create(:comment)
c = FactoryGirl.create(:spam_comment)
get :index, :published => 'f'
should_success_with_index(response)
assigns(:feedback).should == [c]
end
it 'should view unconfirmed_spam' do
FactoryGirl.create(:comment)
FactoryGirl.create(:spam_comment)
c = FactoryGirl.create(:spam_comment, :state => 'presumed_spam')
get :index, :published => 'f', :confirmed => 'f'
should_success_with_index(response)
assigns(:feedback).should == [c]
end
# TODO: Functionality is counter-intuitive: param presumed_spam is
# set to f(alse), but shows presumed_spam.
it 'should view presumed_spam' do
c = FactoryGirl.create(:comment, :state => :presumed_spam)
FactoryGirl.create(:comment, :state => :presumed_ham)
get :index, :presumed_spam => 'f'
should_success_with_index(response)
assigns(:feedback).should == [c]
end
it 'should view presumed_ham' do
FactoryGirl.create(:comment)
FactoryGirl.create(:comment, :state => :presumed_spam)
c = FactoryGirl.create(:comment, :state => :presumed_ham)
get :index, :presumed_ham => 'f'
should_success_with_index(response)
assigns(:feedback).should == [c]
end
it 'should get page 1 if page params empty' do
get :index, :page => ''
should_success_with_index(response)
end
end
describe 'article action' do
def should_success_with_article_view(response)
response.should be_success
response.should render_template('article')
end
it 'should see all feedback on one article' do
article = FactoryGirl.create(:article)
FactoryGirl.create(:comment, :article => article)
FactoryGirl.create(:comment, :article => article)
get :article, :id => article.id
should_success_with_article_view(response)
assigns(:article).should == article
assigns(:feedback).size.should == 2
end
it 'should see only spam feedback on one article' do
article = FactoryGirl.create(:article)
FactoryGirl.create(:comment, :state => 'spam', :article => article)
get :article, :id => article.id, :spam => 'y'
should_success_with_article_view(response)
assigns(:article).should == article
assigns(:feedback).size.should == 1
end
it 'should see only ham feedback on one article' do
article = FactoryGirl.create(:article)
comment = FactoryGirl.create(:comment, :article => article)
get :article, :id => article.id, :ham => 'y'
should_success_with_article_view(response)
assigns(:article).should == article
assigns(:feedback).size.should == 1
end
it 'should redirect_to index if bad article id' do
lambda{
get :article, :id => 102302
}.should raise_error(ActiveRecord::RecordNotFound)
end
end
describe 'create action' do
def base_comment(options = {})
{"body"=>"a new comment", "author"=>"Me", "url"=>"http://publify.co", "email"=>"dev@publify.co"}.merge(options)
end
describe 'by get access' do
it "should raise ActiveRecordNotFound if article doesn't exist" do
lambda {
get 'create', :article_id => 120431, :comment => base_comment
}.should raise_error(ActiveRecord::RecordNotFound)
end
it 'should not create comment' do
article = FactoryGirl.create(:article)
lambda do
get 'create', :article_id => article.id, :comment => base_comment
response.should redirect_to(:action => 'article', :id => article.id)
end.should_not change(Comment, :count)
end
end
describe 'by post access' do
it "should raise ActiveRecord::RecordNotFound if article doesn't exist" do
lambda {
post 'create', :article_id => 123104, :comment => base_comment
}.should raise_error(ActiveRecord::RecordNotFound)
end
it 'should create comment' do
article = FactoryGirl.create(:article)
lambda do
post 'create', :article_id => article.id, :comment => base_comment
response.should redirect_to(:action => 'article', :id => article.id)
end.should change(Comment, :count)
end
it 'should create comment mark as ham' do
article = FactoryGirl.create(:article)
lambda do
post 'create', :article_id => article.id, :comment => base_comment
response.should redirect_to(:action => 'article', :id => article.id)
end.should change { Comment.count(:conditions => {:state => "ham"}) }
end
end
end
describe 'edit action' do
it 'should render edit form' do
article = FactoryGirl.create(:article)
comment = FactoryGirl.create(:comment, :article => article)
get 'edit', :id => comment.id
assigns(:comment).should == comment
assigns(:article).should == article
response.should be_success
response.should render_template('edit')
end
end
describe 'update action' do
it 'should update comment if post request' do
article = FactoryGirl.create(:article)
comment = FactoryGirl.create(:comment, :article => article)
post 'update', :id => comment.id,
:comment => {:author => 'Bob Foo2',
:url => 'http://fakeurl.com',
:body => 'updated comment'}
response.should redirect_to(:action => 'article', :id => article.id)
comment.reload
comment.body.should == 'updated comment'
end
it 'should not update comment if get request' do
comment = FactoryGirl.create(:comment)
get 'update', :id => comment.id,
:comment => {:author => 'Bob Foo2',
:url => 'http://fakeurl.com',
:body => 'updated comment'}
response.should redirect_to(:action => 'edit', :id => comment.id)
comment.reload
comment.body.should_not == 'updated comment'
end
end
end
describe 'publisher access' do
before :each do
FactoryGirl.create(:blog)
#TODO remove this delete_all after removing all fixture
Profile.delete_all
@publisher = FactoryGirl.create(:user, :profile => FactoryGirl.create(:profile_publisher))
request.session = { :user => @publisher.id }
end
def feedback_from_own_article
@article ||= FactoryGirl.create(:article, :user => @publisher)
@feedback_own_article ||= FactoryGirl.create(:comment, :article => @article)
end
def feedback_from_not_own_article
@article ||= FactoryGirl.create(:article, :user => FactoryGirl.create(:user, :login => 'other_user'))
@feedback_not_own_article ||= FactoryGirl.create(:comment, :article => @article)
end
describe 'destroy action' do
it_should_behave_like "destroy feedback with feedback from own article"
it "should not destroy feedback doesn't own" do
id = feedback_from_not_own_article.id
post 'destroy', :id => id
response.should redirect_to(:controller => 'admin/feedback', :action => 'index')
lambda do
Feedback.find(id)
end.should_not raise_error
end
end
describe 'edit action' do
it 'should not edit comment no own article' do
get 'edit', :id => feedback_from_not_own_article.id
response.should redirect_to(:action => 'index')
end
it 'should edit comment if own article' do
get 'edit', :id => feedback_from_own_article.id
response.should be_success
response.should render_template('edit')
assigns(:comment).should == feedback_from_own_article
assigns(:article).should == feedback_from_own_article.article
end
end
describe 'update action' do
it 'should update comment if own article' do
post 'update', :id => feedback_from_own_article.id,
:comment => {:author => 'Bob Foo2',
:url => 'http://fakeurl.com',
:body => 'updated comment'}
response.should redirect_to(:action => 'article', :id => feedback_from_own_article.article.id)
feedback_from_own_article.reload
feedback_from_own_article.body.should == 'updated comment'
end
it 'should not update comment if not own article' do
post 'update', :id => feedback_from_not_own_article.id,
:comment => {:author => 'Bob Foo2',
:url => 'http://fakeurl.com',
:body => 'updated comment'}
response.should redirect_to(:action => 'index')
feedback_from_not_own_article.reload
feedback_from_not_own_article.body.should_not == 'updated comment'
end
end
describe '#bulkops action' do
it "redirect to index" do
post :bulkops, bulkop_top: 'destroy all spam'
@response.should redirect_to(action: 'index')
end
it "delete all spam" do
Feedback.delete_all
comment = FactoryGirl.create(:comment, state: :spam)
post :bulkops, bulkop_top: 'Delete all spam'
Feedback.count.should == 0
end
it "delete all spam and only confirmed spam" do
Feedback.delete_all
FactoryGirl.create(:comment, state: :presumed_spam)
FactoryGirl.create(:comment, state: :spam)
FactoryGirl.create(:comment, state: :presumed_ham)
FactoryGirl.create(:comment, state: :ham)
post :bulkops, bulkop_top: 'Delete all spam'
Feedback.count.should == 3
end
it "mark presumed spam comments as spam" do
comment = FactoryGirl.create(:comment, state: :presumed_spam)
post :bulkops, bulkop_top: 'Mark Checked Items as Spam', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_spam
end
it "mark confirmed spam comments as spam" do
comment = FactoryGirl.create(:comment, state: :spam)
post :bulkops, bulkop_top: 'Mark Checked Items as Spam', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_spam
end
it "mark presumed ham comments as spam" do
comment = FactoryGirl.create(:comment, state: :presumed_ham)
post :bulkops, bulkop_top: 'Mark Checked Items as Spam', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_spam
end
it "mark ham comments as spam" do
comment = FactoryGirl.create(:comment, state: :ham)
post :bulkops, bulkop_top: 'Mark Checked Items as Spam', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_spam
end
it "mark presumed spam comments as ham" do
comment = FactoryGirl.create(:comment, state: :presumed_spam)
post :bulkops, bulkop_top: 'Mark Checked Items as Ham', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_ham
end
it "mark confirmed spam comments as ham" do
comment = FactoryGirl.create(:comment, state: :spam)
post :bulkops, bulkop_top: 'Mark Checked Items as Ham', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_ham
end
it "mark presumed ham comments as ham" do
comment = FactoryGirl.create(:comment, state: :presumed_ham)
post :bulkops, bulkop_top: 'Mark Checked Items as Ham', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_ham
end
it "mark ham comments as ham" do
comment = FactoryGirl.create(:comment, state: :ham)
post :bulkops, bulkop_top: 'Mark Checked Items as Ham', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_ham
end
it "confirms presumed spam comments as spam" do
comment = FactoryGirl.create(:comment, state: :presumed_spam)
post :bulkops, bulkop_top: 'Confirm Classification of Checked Items', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_spam
end
it "confirms confirmed spam comments as spam" do
comment = FactoryGirl.create(:comment, state: :spam)
post :bulkops, bulkop_top: 'Confirm Classification of Checked Items', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_spam
end
it "confirms presumed ham comments as ham" do
comment = FactoryGirl.create(:comment, state: :presumed_ham)
post :bulkops, bulkop_top: 'Confirm Classification of Checked Items', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_ham
end
it "confirms ham comments as ham" do
comment = FactoryGirl.create(:comment, state: :ham)
post :bulkops, bulkop_top: 'Confirm Classification of Checked Items', feedback_check: {comment.id.to_s => "on"}
Feedback.find(comment.id).should be_ham
end
end
end
end