Skip to content

Commit

Permalink
Refactor petition create route
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pixeltrix committed Sep 12, 2017
1 parent 099b268 commit a11b37b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/views/petitions/new.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 64 additions & 4 deletions spec/routing/petitions_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a11b37b

Please sign in to comment.