summaryrefslogtreecommitdiff
path: root/test/controllers/topics/posts_controller_test.rb
blob: 04d522a8e80151d6498ab3000c7cef993edff04d (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
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
# frozen_string_literal: true

require 'test_helper'

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

  test 'should post to unlocked, undeleted topic when logged out' do
    assert_difference('Post.count') do
      post :create, params: { forum_id: @forum, topic_id: @topic, post: { body: 'Post body' } }
      assert_response :redirect
      @post = assigns(:post)
      assert_not_nil @post
      assert_redirected_to short_topic_post_path(@forum, @topic, @post, anchor: "post_#{@post.id}")
      assert_equal 7, @post.topic_position # 6 posts already existed, see factory
    end
  end

  test 'should post to unlocked, undeleted topic when signed in' do
    @user = create(:user)
    sign_in @user

    assert_difference('Post.count') do
      post :create, params: { forum_id: @forum, topic_id: @topic, post: { body: 'Post body' } }
      assert_response :redirect
      @post = assigns(:post)
      assert_not_nil @post
      assert_redirected_to short_topic_post_path(@forum, @topic, @post, anchor: "post_#{@post.id}")
      assert_equal 7, @post.topic_position # 6 posts already existed, see factory
      assert_equal @user, @post.user
    end
  end

  test 'cannot create posts on a locked topic' do
    @topic = create(:locked_topic_with_posts)
    @forum = @topic.forum

    assert_no_difference('Post.count') do
      post :create, params: { forum_id: @forum, topic_id: @topic, post: { body: 'Post body' } }
      assert_response :redirect
      assert_redirected_to root_url
    end
  end

  test 'cannot create posts on a deleted topic' do
    @topic = create(:deleted_topic_with_posts)
    @forum = @topic.forum

    assert_no_difference('Post.count') do
      post :create, params: { forum_id: @forum, topic_id: @topic, post: { body: 'Post body' } }
      assert_response :redirect
      assert_redirected_to root_url
    end
  end

  test 'should get edit' do
    @post = create(:post_as_user)
    @topic = @post.topic
    sign_in @post.user

    get :edit, params: { forum_id: @topic.forum, topic_id: @topic, id: @post }
    assert_response :success
  end

  test 'can update posts' do
    @post = create(:post_as_user)
    @topic = @post.topic
    @forum = @topic.forum
    sign_in @post.user

    put :update, params: { forum_id: @forum, topic_id: @topic, id: @post, post: { body: 'Updated' } }
    @post.reload
    assert_response :redirect
    assert_redirected_to short_topic_post_path(@forum, @topic, @post, anchor: "post_#{@post.id}")
    assert_equal 'Updated', @post.body
  end

  test 'cannot update posts in a locked topic' do
    @topic = create(:locked_topic_with_posts)
    @post = @topic.posts.last
    @user = @post.user
    sign_in @user

    put :update, params: { forum_id: @forum, topic_id: @topic, id: @post, post: { body: 'Updated' } }
    @post.reload
    assert_response :redirect
    assert_redirected_to root_url
  end

  test 'cannot update posts in a deleted topic' do
    @topic = create(:deleted_topic_with_posts)
    @post = @topic.posts.last
    @user = @post.user
    sign_in @user

    put :update, params: { forum_id: @forum, topic_id: @topic, id: @post, post: { body: 'Updated' } }
    @post.reload
    assert_response :redirect
    assert_redirected_to root_url
  end
end