|
3 | 3 | class PinnedLinksControllerTest < ActionController::TestCase |
4 | 4 | include Devise::Test::ControllerHelpers |
5 | 5 |
|
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 |
10 | 14 | end |
11 | 15 |
|
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 |
17 | 24 | end |
18 | 25 |
|
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 |
23 | 34 | end |
24 | 35 |
|
25 | | - test 'update should work for moderators' do |
| 36 | + test 'create should correctly create pinned links' do |
26 | 37 | 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 | + |
28 | 51 | assert_response(:found) |
29 | 52 | assert_redirected_to pinned_links_path |
30 | 53 | assert_not_nil assigns(:link) |
31 | 54 | assert_equal 'updated label', assigns(:link).label |
32 | 55 | 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 |
33 | 81 | end |
0 commit comments