Skip to content

Commit e8b5f5f

Browse files
committed
trying to update a pinned link to an invalid state should be a 400 status response
1 parent 2733c15 commit e8b5f5f

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

app/controllers/pinned_links_controller.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@ def edit; end
4949

5050
def update
5151
before = @link.attributes_print
52-
@link.update pinned_link_params
53-
after = @link.attributes_print
54-
AuditLog.moderator_audit(event_type: 'pinned_link_update', related: @link, user: current_user,
55-
comment: "from <<PinnedLink #{before}>>\nto <<PinnedLink #{after}>>")
5652

57-
flash[:success] = 'The pinned link has been updated. Due to caching, it may take some time until it is shown.'
58-
redirect_to pinned_links_path
53+
if @link.update(pinned_link_params)
54+
after = @link.attributes_print
55+
56+
AuditLog.moderator_audit(event_type: 'pinned_link_update',
57+
related: @link,
58+
user: current_user,
59+
comment: "from <<PinnedLink #{before}>>\nto <<PinnedLink #{after}>>")
60+
61+
flash[:success] = 'The pinned link has been updated. Due to caching, it may take some time until it is shown.'
62+
redirect_to pinned_links_path
63+
else
64+
render 'pinned_links/edit', status: :bad_request
65+
end
5966
end
6067

6168
private

test/controllers/pinned_links_controller_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class PinnedLinksControllerTest < ActionController::TestCase
6161
assert_equal 'updated label', assigns(:link).label
6262
end
6363

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+
6471
private
6572

6673
def try_create_pinned_link(**opts)

0 commit comments

Comments
 (0)