|
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 |
| 14 | + end |
| 15 | + |
| 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 |
| 24 | + end |
| 25 | + |
| 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 |
10 | 34 | end |
11 | 35 |
|
12 | | - test 'edit should work for moderators' do |
| 36 | + test 'create should correctly create pinned links' do |
13 | 37 | sign_in users(:moderator) |
14 | | - get :edit, params: { id: pinned_links(:active_with_label).id } |
15 | | - assert_response(:success) |
| 38 | + |
| 39 | + try_create_pinned_link(post: posts(:question_one)) |
| 40 | + |
| 41 | + assert_response(:found) |
| 42 | + assert_redirected_to pinned_links_path |
16 | 43 | assert_not_nil assigns(:link) |
17 | 44 | end |
18 | 45 |
|
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) |
| 46 | + test 'create should correctly handle invalid pinned links' do |
| 47 | + sign_in users(:moderator) |
| 48 | + try_create_pinned_link |
| 49 | + assert_response(:bad_request) |
| 50 | + assert assigns(:link)&.errors&.any? |
23 | 51 | end |
24 | 52 |
|
25 | | - test 'update should work for moderators' do |
| 53 | + test 'update should correctly update pinned links' do |
26 | 54 | sign_in users(:moderator) |
27 | | - post :update, params: { id: pinned_links(:active_with_label).id, pinned_link: { label: 'updated label' } } |
| 55 | + |
| 56 | + try_update_pinned_link(pinned_links(:active_with_label), label: 'updated label') |
| 57 | + |
28 | 58 | assert_response(:found) |
29 | 59 | assert_redirected_to pinned_links_path |
30 | 60 | assert_not_nil assigns(:link) |
31 | 61 | assert_equal 'updated label', assigns(:link).label |
32 | 62 | end |
| 63 | + |
| 64 | + test 'update should correctly handle invlid pinned links' do |
| 65 | + sign_in users(:moderator) |
| 66 | + try_update_pinned_link(pinned_links(:active_with_label), link: nil) |
| 67 | + assert_response(:bad_request) |
| 68 | + assert assigns(:link)&.errors&.any? |
| 69 | + end |
| 70 | + |
| 71 | + private |
| 72 | + |
| 73 | + def try_create_pinned_link(**opts) |
| 74 | + community_id = opts.delete(:community)&.id |
| 75 | + post_id = opts.delete(:post)&.id |
| 76 | + |
| 77 | + post :create, params: { |
| 78 | + pinned_link: { |
| 79 | + community_id: community_id, |
| 80 | + post_id: post_id |
| 81 | + }.merge(opts) |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + def try_edit_pinned_link(link) |
| 86 | + get :edit, params: { id: link.id } |
| 87 | + end |
| 88 | + |
| 89 | + def try_update_pinned_link(link, **opts) |
| 90 | + post :update, params: { |
| 91 | + id: link.id, |
| 92 | + pinned_link: opts |
| 93 | + } |
| 94 | + end |
33 | 95 | end |
0 commit comments