FIX: Update topic route id param (#16166)

This update topic route has never worked. Better late than never. I am
in favor of using non-slug urls when using the api so I do think we
should fix this route.

Just thought I would update the `:id` param to `:topic_id` here in the
routes file instead of updating the controller to handle both params.

Added a spec to test this route.

Also added the same constraint we have on other topic routes to ensure
we only pass in an ID that is a digit.
This commit is contained in:
Blake Erickson 2022-03-11 11:01:08 -07:00 committed by GitHub
parent 4dc5500fa6
commit 02fa04e333
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -767,7 +767,7 @@ Discourse::Application.routes.draw do
# Topics resource
get "t/:id" => "topics#show"
put "t/:id" => "topics#update"
put "t/:topic_id" => "topics#update", constraints: { topic_id: /\d+/ }
delete "t/:id" => "topics#destroy"
put "t/:id/archive-message" => "topics#archive_message"
put "t/:id/move-to-inbox" => "topics#move_to_inbox"

View File

@ -1317,6 +1317,27 @@ RSpec.describe TopicsController do
expect(payload["title"]).to eq('This is a new title for the topic')
end
it 'allows update on short non-slug url' do
put "/t/#{topic.id}.json", params: {
title: 'This is a new title for the topic'
}
topic.reload
expect(topic.title).to eq('This is a new title for the topic')
end
it 'only allows update on digit ids' do
non_digit_id = "asdf"
original_title = topic.title
put "/t/#{non_digit_id}.json", params: {
title: 'This is a new title for the topic'
}
topic.reload
expect(topic.title).to eq(original_title)
expect(response.status).to eq(404)
end
it 'allows a change of then updating the OP' do
topic.update(user: user)
topic.first_post.update(user: user)