summaryrefslogtreecommitdiff
path: root/test/controllers/topics/posts/hides_controller_test.rb
blob: 0379984d12a9b947aa669b5163bf8d07ef199909 (plain)
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
# frozen_string_literal: true

require 'test_helper'

class Topics::ForumPosts::HidesControllerTest < ActionController::TestCase
  setup do
    @topic = create(:topic_with_posts)
    @forum = @topic.forum
    @request.cookies['_ses'] = 'c1836832948'
  end

  test 'can be hidden' do
    @post = @topic.posts.last
    sign_in create(:moderator)

    post :create, params: { forum_id: @topic.forum, topic_id: @topic, post_id: @post, deletion_reason: 'Because I feel like it' }

    @post.reload
    assert_response :redirect
    assert_redirected_to short_topic_post_path(@topic.forum, @topic, @post, anchor: "post_#{@post.id}")
    assert @post.hidden_from_users
  end

  test 'should hide topic when hidden if first post' do
    @post = @topic.posts.detect(&:first?)
    sign_in create(:moderator)

    post :create, params: { forum_id: @topic.forum, topic_id: @topic, post_id: @post, deletion_reason: 'Bye bye topic' }

    @topic.reload
    assert @topic.hidden_from_users
  end

  test 'should keep posts hidden with the same metadata when hide requested again.' do
    @post = @topic.posts.last
    @user_a = create(:moderator)
    ForumPostHider.new(@post, user: @user_a, reason: 'Get out.').save

    @user_b = create(:moderator)
    sign_in @user_b

    post :create, params: { forum_id: @topic.forum, topic_id: @topic, post_id: @post, deletion_reason: 'I BELIEVE I SAID GET OUT' }

    assert_response :redirect
    @post.reload
    assert @post.hidden_from_users
    assert_equal @user_a, @post.deleted_by
    assert_equal 'Get out.', @post.deletion_reason
  end

  test 'forbids deletion without reason' do
    @post = @topic.posts.last
    sign_in create(:moderator)
    post :create, params: { forum_id: @topic.forum, topic_id: @topic, post_id: @post, deletion_reason: '' }

    @post.reload
    assert_response :redirect
    assert_not @post.hidden_from_users
  end

  test 'should hide threads with same user when hidden post is the OP' do
    @post = @topic.posts.first
    @luser = create(:moderator)
    sign_in @luser
    post :create, params: { forum_id: @topic.forum, topic_id: @topic, post_id: @post, deletion_reason: 'test op hide' }

    @topic.reload
    assert_response :redirect
    assert @topic.hidden_from_users
    assert_equal 'test op hide', @topic.deletion_reason
    assert_equal @luser, @topic.deleted_by
  end
end