A platform for community discussion. Free, open, simple.
FIX: Side-effectful `GET /test` endpoint should be POST (#37864)
Sam4b1a1b07b130
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.jsindex c7a766a2cd9..da8fb983ce7 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-llm.js@@ -32,6 +32,7 @@ export default class AiLlm extends RestModel {async testConfig(llmConfig) {return await ajax("/admin/plugins/discourse-ai/ai-llms/test.json", {+ type: "POST",data: { ai_llm: llmConfig ?? this.createProperties() },});}diff --git a/plugins/discourse-ai/config/routes.rb b/plugins/discourse-ai/config/routes.rbindex 2aaab69b6dd..f888ae2c665 100644--- a/plugins/discourse-ai/config/routes.rb+++ b/plugins/discourse-ai/config/routes.rb@@ -135,7 +135,7 @@ Discourse::Application.routes.draw doonly: %i[index new create edit update destroy],path: "ai-llms",controller: "discourse_ai/admin/ai_llms" do- collection { get :test }+ collection { post :test }endresources :ai_llm_quotas,diff --git a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rbindex abcce3fec02..80ea0c6700b 100644--- a/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/admin/ai_llms_controller_spec.rb@@ -463,7 +463,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController doendend- describe "GET #test" do+ describe "POST #test" dolet(:test_attrs) do{name: "llama3",@@ -475,10 +475,17 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController do}end+ it "does not route via GET to prevent CSRF" do+ DiscourseAi::Completions::Llm.with_prepared_responses(["a response"]) do+ get "/admin/plugins/discourse-ai/ai-llms/test.json", params: { ai_llm: test_attrs }+ expect(response.status).to eq(404)+ end+ end+context "when we can contact the model" doit "returns a success true flag" doDiscourseAi::Completions::Llm.with_prepared_responses(["a response"]) do- get "/admin/plugins/discourse-ai/ai-llms/test.json", params: { ai_llm: test_attrs }+ post "/admin/plugins/discourse-ai/ai-llms/test.json", params: { ai_llm: test_attrs }expect(response).to be_successfulexpect(response.parsed_body["success"]).to eq(true)@@ -498,7 +505,7 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController doDiscourseAi::Completions::Endpoints::Base::CompletionFailed.new(error_message.to_json)DiscourseAi::Completions::Llm.with_prepared_responses([error]) do- get "/admin/plugins/discourse-ai/ai-llms/test.json", params: { ai_llm: test_attrs }+ post "/admin/plugins/discourse-ai/ai-llms/test.json", params: { ai_llm: test_attrs }expect(response).to be_successfulexpect(response.parsed_body["success"]).to eq(false)@@ -509,10 +516,10 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController docontext "when config is invalid" doit "returns a success false with the validation error" do- get "/admin/plugins/discourse-ai/ai-llms/test.json",- params: {- ai_llm: test_attrs.except(:max_prompt_tokens),- }+ post "/admin/plugins/discourse-ai/ai-llms/test.json",+ params: {+ ai_llm: test_attrs.except(:max_prompt_tokens),+ }expect(response).to be_successfulexpect(response.parsed_body["success"]).to eq(false)@@ -527,12 +534,12 @@ RSpec.describe DiscourseAi::Admin::AiLlmsController doit "tests the existing model directly instead of using params" doDiscourseAi::Completions::Llm.with_prepared_responses(["a response"]) do- get "/admin/plugins/discourse-ai/ai-llms/test.json",- params: {- ai_llm: {- id: seeded_llm.id,- },- }+ post "/admin/plugins/discourse-ai/ai-llms/test.json",+ params: {+ ai_llm: {+ id: seeded_llm.id,+ },+ }expect(response).to be_successfulexpect(response.parsed_body["success"]).to eq(true)