This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 316
/
topics_controller_test.rb
executable file
·200 lines (163 loc) · 6.79 KB
/
topics_controller_test.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
require 'test_helper'
class TopicsControllerTest < ActionController::TestCase
all_fixtures
def test_should_get_index
get :index, :forum_id => forums(:rails).id
assert_redirected_to forum_path(1)
end
def test_should_get_index_as_xml
content_type 'application/xml'
get :index, :forum_id => forums(:rails).id, :format => 'xml'
assert_response :success
end
def test_should_show_topic_as_rss
content_type 'application/rss+xml'
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
assert_response :success
end
def test_should_show_topic_as_xml
content_type 'application/xml'
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'xml'
assert_response :success
end
def test_should_get_new
login_as :aaron
get :new, :forum_id => forums(:rails).id
assert_response :success
end
def test_sticky_and_locked_protected_from_non_admin
login_as :joe
assert ! users(:joe).admin?
assert ! users(:joe).moderator_of?(forums(:rails))
post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sticky => "1", :locked => "1", :sb_posts_attributes => { "0" => { :body => 'foo' } } }
assert assigns(:topic)
assert ! assigns(:topic).sticky?
assert ! assigns(:topic).locked?
end
def test_sticky_and_locked_allowed_to_moderator
login_as :sam
assert ! users(:sam).admin?
assert users(:sam).moderator_of?(forums(:rails))
post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sticky => "1", :locked => "1", :sb_posts_attributes => { "0" => { :body => 'foo' } } }
assert assigns(:topic)
assert assigns(:topic).sticky?
assert assigns(:topic).locked?
end
def test_should_allow_admin_to_sticky_and_lock
login_as :admin
post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah2', :sticky => "1", :locked => "1", :body => 'foo' }
assert assigns(:topic).sticky?
assert assigns(:topic).locked?
end
uses_transaction :test_should_not_create_topic_without_body
def test_should_not_create_topic_without_body
counts = lambda { [Topic.count, SbPost.count] }
old = counts.call
login_as :aaron
post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sb_posts_attributes => { "0" => { :body => '' } } }
assert assigns(:topic)
assert assigns(:post)
# both of these should be new records if the save fails so that the view can
# render accordingly
assert assigns(:topic).new_record?
assert assigns(:post).new_record?
end
def test_should_create_topic
counts = lambda { [Topic.count, SbPost.count, forums(:rails).topics_count, forums(:rails).sb_posts_count, users(:aaron).sb_posts_count] }
old = counts.call
login_as :aaron
post :create, :forum_id => forums(:rails).id, :topic => { :title => 'blah', :sb_posts_attributes => { "0" => { :body => 'foo' } } }, :tag_list => 'tag1, tag2'
assert assigns(:topic)
assert assigns(:post)
assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
[forums(:rails), users(:aaron)].each(&:reload)
assert_equal old.collect { |n| n + 1}, counts.call
assert_equal ['tag1', 'tag2'], Topic.find(assigns(:topic).id).tag_list
end
def test_should_delete_topic
counts = lambda { [SbPost.count, forums(:rails).topics_count, forums(:rails).sb_posts_count] }
old = counts.call
login_as :admin
delete :destroy, :forum_id => forums(:rails).id, :id => topics(:ponies).id
assert_redirected_to forum_path(forums(:rails))
[forums(:rails), users(:aaron)].each &:reload
assert_equal old.collect { |n| n - 1}, counts.call
end
def test_should_allow_moderator_to_delete_topic
assert_difference Topic, :count, -1 do
login_as :sam
delete :destroy, :forum_id => forums(:rails).id, :id => topics(:pdi).id
end
end
def test_should_update_views_for_show
assert_difference topics(:pdi), :views do
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
assert_response :success
topics(:pdi).reload
end
end
def test_should_not_update_views_for_show_via_rss
assert_difference topics(:pdi), :views, 0 do
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
assert_response :success
topics(:pdi).reload
end
end
def test_should_not_add_viewed_topic_to_session_on_show_rss
login_as :aaron
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :format => 'rss'
assert_response :success
assert session[:topics].blank?
end
def test_should_update_views_for_show_except_topic_author
login_as :aaron
views=topics(:pdi).views
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
assert_response :success
assert_equal views, topics(:pdi).reload.views
end
def test_should_show_topic
get :show, :forum_id => forums(:rails).id, :id => topics(:pdi).id
assert_response :success
assert_equal topics(:pdi), assigns(:topic)
assert_equal [sb_posts(:pdi), sb_posts(:pdi_reply), sb_posts(:pdi_rebuttal)], assigns(:posts)
end
def test_should_show_other_post
get :show, :forum_id => forums(:rails).id, :id => topics(:ponies).id
assert_response :success
assert_equal topics(:ponies), assigns(:topic)
assert_equal [sb_posts(:ponies)], assigns(:posts)
end
def test_should_get_edit
login_as :admin
get :edit, :forum_id => forums(:rails).id, :id => topics(:ponies).id
assert_response :success
end
def test_should_update_own_post
login_as :sam
patch :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { }, :tag_list => 'tagX, tagY'
assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
assert_equal ['tagX', 'tagY'], topics(:ponies).reload.tag_list
end
def test_should_not_update_user_id_of_own_post
login_as :sam
patch :update, :forum_id => forums(:rails).id, :id => topics(:ponies).id, :topic => { :user_id => 32 }
assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
assert_equal users(:sam).id, sb_posts(:ponies).reload.user_id
end
def test_should_not_update_other_post
login_as :sam
patch :update, :forum_id => forums(:comics).id, :id => topics(:galactus).id, :topic => { }
assert_redirected_to login_path
end
def test_should_update_other_post_as_moderator
login_as :sam
patch :update, :forum_id => forums(:rails).id, :id => topics(:pdi).id, :topic => { }
assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
end
def test_should_update_other_post_as_admin
login_as :admin
patch :update, :forum_id => forums(:rails).id, :id => topics(:ponies), :topic => { }
assert_redirected_to forum_topic_path(forums(:rails), assigns(:topic))
end
end