summaryrefslogtreecommitdiff
path: root/test/controllers/topics_controller_test.rb
blob: fc5c62d6731ed02b71a15ad2d151b4e688f36b72 (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
# frozen_string_literal: true

require 'test_helper'

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

  test 'should get show' do
    get :show, params: { id: @topic, forum_id: @forum }
    assert_response :success
    assert_equal @topic, assigns(:topic)
    assert_equal @forum, assigns(:forum)
  end

  test 'should not get new when signed out' do
    get :new, params: { forum_id: @forum }
    assert_response :redirect
  end

  # Disabled due to forum posts being disabled for normal users.
  # test 'should get new when signed in' do
  #   sign_in create(:user)
  #   get :new, params: { forum_id: @forum }
  #   assert_response :success
  # end

  test 'should not create topic when signed out' do
    assert_no_difference('Topic.count') do
      post :create, params: { forum_id: @forum, topic: {
        title:            'This is a created topic',
        posts_attributes: { '0' => { body: 'This is the content of the first post' } },
        anonymous:        false
      } }

      assert_response :redirect
    end
  end

  # Disabled due to forum posts being disabled for normal users.
  # test 'should create topic when signed in' do
  #   @user = create(:user)
  #   sign_in @user
  #
  #   assert_difference('Topic.count') do
  #     post :create, params: { forum_id: @forum, topic: {
  #       title:            'This is a created topic',
  #       posts_attributes: { '0' => { body: 'This is the content of the first post' } },
  #       anonymous:        false
  #     } }
  #
  #     assert_response :redirect
  #
  #     @topic = assigns(:topic)
  #     assert_not_nil @topic
  #     @post = @topic.posts.first
  #     assert_not_nil @post
  #     assert_equal 'This is the content of the first post', @post.body
  #     assert_equal 0, @post.topic_position
  #     assert_equal @user, @post.user
  #   end
  # end
end