From a11b37b1105e6332bee855a5061cd300084123e7 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 11 Sep 2017 16:26:26 +0100 Subject: [PATCH] Refactor petition create route Using new_petition_path instead of create_petitions_path more accurately reflects where we're POSTing too. Also expand the routing tests to cover other routes and ensure that we don't accidentally start accepting RESTful routes that modify petitions. --- app/views/petitions/new.html.erb | 2 +- config/routes.rb | 2 +- spec/routing/petitions_spec.rb | 68 ++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/app/views/petitions/new.html.erb b/app/views/petitions/new.html.erb index 0b605916e..6749c9f6d 100644 --- a/app/views/petitions/new.html.erb +++ b/app/views/petitions/new.html.erb @@ -1,3 +1,3 @@ -<%= form_for @stage_manager.stage_object, :url => create_petitions_path do |f| %> +<%= form_for @stage_manager.stage_object, url: new_petition_path do |f| %> <%= render_petition_form @stage_manager, f %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index e91f384df..10a6e5320 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,7 +31,7 @@ collection do get 'check' get 'check_results' - post 'create', path: 'new', as: :create + post 'new', action: 'create', as: nil end member do diff --git a/spec/routing/petitions_spec.rb b/spec/routing/petitions_spec.rb index 150319e8d..cf5854a31 100644 --- a/spec/routing/petitions_spec.rb +++ b/spec/routing/petitions_spec.rb @@ -1,13 +1,73 @@ require 'rails_helper' RSpec.describe "routes for petitions", type: :routes do + it "routes GET /petitions to petitions#index" do + expect(get("/petitions")).to route_to(controller: "petitions", action: "index") + expect(petitions_path).to eq("/petitions") + end + it "routes GET /petitions/new to petitions#new" do - expect({:get => "/petitions/new"}).to route_to({:controller => "petitions", :action => "new"}) - expect(new_petition_path).to eq '/petitions/new' + expect(get("/petitions/new")).to route_to(controller: "petitions", action: "new") + expect(new_petition_path).to eq("/petitions/new") end it "routes POST /petitions/new to petitions#create" do - expect({:post => "/petitions/new"}).to route_to({:controller => "petitions", :action => "create"}) - expect(create_petitions_path).to eq('/petitions/new') + expect(post("/petitions/new")).to route_to(controller: "petitions", action: "create") + expect(new_petition_path).to eq("/petitions/new") + end + + it "doesn't route POST /petitions" do + expect(post("/petitions")).not_to be_routable + end + + it "routes GET /petitions/:id to petitions#show" do + expect(get("/petitions/1")).to route_to(controller: "petitions", action: "show", id: "1") + expect(petition_path("1")).to eq("/petitions/1") + end + + it "doesn't route GET /petitions/:id/edit" do + expect(patch("/petitions/1/edit")).not_to be_routable + end + + it "doesn't route PATCH /petitions/:id" do + expect(patch("/petitions/1")).not_to be_routable + end + + it "doesn't route PUT /petitions/:id" do + expect(put("/petitions/1")).not_to be_routable + end + + it "doesn't route DELETE /petitions/:id" do + expect(delete("/petitions/1")).not_to be_routable + end + + it "routes GET /petitions/check to petitions#check" do + expect(get("/petitions/check")).to route_to(controller: "petitions", action: "check") + expect(check_petitions_path).to eq("/petitions/check") + end + + it "routes GET /petitions/check_results to petitions#check_results" do + expect(get("/petitions/check_results")).to route_to(controller: "petitions", action: "check_results") + expect(check_results_petitions_path).to eq("/petitions/check_results") + end + + it "routes GET /petitions/:id/count to petitions#count" do + expect(get("/petitions/1/count")).to route_to(controller: "petitions", action: "count", id: "1") + expect(count_petition_path("1")).to eq("/petitions/1/count") + end + + it "routes GET /petitions/:id/thank-you to petitions#thank_you" do + expect(get("/petitions/1/thank-you")).to route_to(controller: "petitions", action: "thank_you", id: "1") + expect(thank_you_petition_path("1")).to eq("/petitions/1/thank-you") + end + + it "routes GET /petitions/:id/gathering-support to petitions#gathering_support" do + expect(get("/petitions/1/gathering-support")).to route_to(controller: "petitions", action: "gathering_support", id: "1") + expect(gathering_support_petition_path("1")).to eq("/petitions/1/gathering-support") + end + + it "routes GET /petitions/:id/moderation-info to petitions#moderation_info" do + expect(get("/petitions/1/moderation-info")).to route_to(controller: "petitions", action: "moderation_info", id: "1") + expect(moderation_info_petition_path("1")).to eq("/petitions/1/moderation-info") end end