Skip to content

Commit 5a8b321

Browse files
committed
improved coverage for the pinned links controller actions
1 parent d1abae9 commit 5a8b321

1 file changed

Lines changed: 63 additions & 15 deletions

File tree

test/controllers/pinned_links_controller_test.rb

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,79 @@
33
class PinnedLinksControllerTest < ActionController::TestCase
44
include Devise::Test::ControllerHelpers
55

6-
test 'edit should require moderator' do
7-
sign_in users(:standard_user)
8-
get :edit, params: { id: pinned_links(:active_with_label).id }
9-
assert_response(:not_found)
6+
test 'only mods or higher should be able to create pinned links' do
7+
post = posts(:question_one)
8+
9+
users.each do |user|
10+
sign_in user
11+
try_create_pinned_link(post: post)
12+
assert_response(user.at_least_moderator? ? :found : :not_found)
13+
end
1014
end
1115

12-
test 'edit should work for moderators' do
13-
sign_in users(:moderator)
14-
get :edit, params: { id: pinned_links(:active_with_label).id }
15-
assert_response(:success)
16-
assert_not_nil assigns(:link)
16+
test 'only mods or higher should be able to edit pinned links' do
17+
link = pinned_links(:active_with_label)
18+
19+
users.each do |user|
20+
sign_in user
21+
try_edit_pinned_link(link)
22+
assert_response(user.at_least_moderator? ? :success : :not_found)
23+
end
1724
end
1825

19-
test 'update should require moderator' do
20-
sign_in users(:standard_user)
21-
post :update, params: { id: pinned_links(:active_with_label).id, pinned_link: { label: 'updated label' } }
22-
assert_response(:not_found)
26+
test 'only mods or higher should be able to update pinned links' do
27+
link = pinned_links(:active_with_label)
28+
29+
users.each do |user|
30+
sign_in user
31+
try_update_pinned_link(link, label: 'updated label')
32+
assert_response(user.at_least_moderator? ? :found : :not_found)
33+
end
2334
end
2435

25-
test 'update should work for moderators' do
36+
test 'create should correctly create pinned links' do
2637
sign_in users(:moderator)
27-
post :update, params: { id: pinned_links(:active_with_label).id, pinned_link: { label: 'updated label' } }
38+
39+
try_create_pinned_link(post: posts(:question_one))
40+
41+
assert_response(:found)
42+
assert_redirected_to pinned_links_path
43+
assert_not_nil assigns(:link)
44+
end
45+
46+
test 'update should correctly update pinned links' do
47+
sign_in users(:moderator)
48+
49+
try_update_pinned_link(pinned_links(:active_with_label), label: 'updated label')
50+
2851
assert_response(:found)
2952
assert_redirected_to pinned_links_path
3053
assert_not_nil assigns(:link)
3154
assert_equal 'updated label', assigns(:link).label
3255
end
56+
57+
private
58+
59+
def try_create_pinned_link(**opts)
60+
community_id = opts.delete(:community)&.id
61+
post_id = opts.delete(:post)&.id
62+
63+
post :create, params: {
64+
pinned_link: {
65+
community_id: community_id,
66+
post_id: post_id
67+
}.merge(opts)
68+
}
69+
end
70+
71+
def try_edit_pinned_link(link)
72+
get :edit, params: { id: link.id }
73+
end
74+
75+
def try_update_pinned_link(link, **opts)
76+
post :update, params: {
77+
id: link.id,
78+
pinned_link: opts
79+
}
80+
end
3381
end

0 commit comments

Comments
 (0)