diff --git a/engine/app/controllers/coplan/application_controller.rb b/engine/app/controllers/coplan/application_controller.rb index 343413e..50f73b3 100644 --- a/engine/app/controllers/coplan/application_controller.rb +++ b/engine/app/controllers/coplan/application_controller.rb @@ -45,11 +45,14 @@ def signed_in? end def authenticate_coplan_user! + if request.get? && agent_request? + render plain: agent_redirect_instructions, content_type: "text/markdown", status: :unauthorized + return + end + @current_coplan_user = CoPlan::Authentication.user_from_request(request) unless @current_coplan_user - if agent_request? - render plain: agent_redirect_instructions, content_type: "text/markdown", status: :unauthorized - elsif CoPlan.configuration.sign_in_path + if CoPlan.configuration.sign_in_path redirect_to CoPlan.configuration.sign_in_path, alert: "Please sign in." else head :unauthorized diff --git a/spec/requests/browse_spec.rb b/spec/requests/browse_spec.rb index 928d4f3..bf3b706 100644 --- a/spec/requests/browse_spec.rb +++ b/spec/requests/browse_spec.rb @@ -63,6 +63,18 @@ def revise(plan, content, actor: author) expect(response).to have_http_status(:ok) expect(versioned.slug).to eq("pricing-v1-2") end + + it "directs an authenticated non-browser client to the API instructions" do + get "/hampton/liveorder/q3/cart-roadmap", headers: { + "Accept" => "text/markdown", + "User-Agent" => "curl/8.11.0" + } + + expect(response).to have_http_status(:unauthorized) + expect(response.media_type).to eq("text/markdown") + expect(response.body).to include("# CoPlan API") + expect(response.body).to include("/agent-instructions") + end end describe "the owner's own library" do