diff --git a/app/controllers/constituencies_controller.rb b/app/controllers/constituencies_controller.rb index 0779c9b19..f93644128 100644 --- a/app/controllers/constituencies_controller.rb +++ b/app/controllers/constituencies_controller.rb @@ -1,11 +1,23 @@ class ConstituenciesController < ApplicationController before_action :set_cors_headers, only: [:index], if: :json_request? + before_action :fetch_parliament, only: [:index] def index - @constituencies = Constituency.current.by_ons_code + @constituencies = @parliament.constituencies.by_ons_code respond_to do |format| format.json end end + + private + + def fetch_parliament + case params[:period] + when Parliament::PERIOD_FORMAT + @parliament = Parliament.archived.find_by!(period: params[:period]) + else + @parliament = Parliament.instance + end + end end diff --git a/app/controllers/petitions_controller.rb b/app/controllers/petitions_controller.rb index c2d9b13f9..d673e53c4 100644 --- a/app/controllers/petitions_controller.rb +++ b/app/controllers/petitions_controller.rb @@ -102,7 +102,9 @@ def redirect_to_home_page_if_dissolved end def redirect_to_home_page_unless_opened - redirect_to home_url unless Parliament.opened? + unless json_request? + redirect_to home_url unless Parliament.opened? + end end def request_format diff --git a/app/models/parliament.rb b/app/models/parliament.rb index 64eca0987..6ccdeed30 100644 --- a/app/models/parliament.rb +++ b/app/models/parliament.rb @@ -4,6 +4,7 @@ class Parliament < ActiveRecord::Base include ActiveSupport::NumberHelper CUTOFF_DATE = Date.civil(2015, 5, 7) + PERIOD_FORMAT = /\A\d{4}-\d{4}\z/ has_many :petitions, inverse_of: :parliament, class_name: "Archived::Petition" has_many :parliament_constituencies diff --git a/config/routes.rb b/config/routes.rb index b85e928f8..c1b4cd4ee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,11 +2,15 @@ constraints Site.constraints_for_public do controller 'constituencies' do get '/constituencies', action: 'index', as: :constituencies + + constraints period: /\d{4}-\d{4}|current/ do + get '/parliaments/:period/constituencies', action: 'index', as: nil + end end controller 'parliaments' do get '/parliaments', action: 'index', as: :parliaments - get '/parliaments/:period', action: 'show' + get '/parliaments/:period', action: 'show', constraints: { period: /\d{4}-\d{4}/ } end controller 'topics' do diff --git a/spec/controllers/constituencies_controller_spec.rb b/spec/controllers/constituencies_controller_spec.rb index fcf923fba..1f1ab6e57 100644 --- a/spec/controllers/constituencies_controller_spec.rb +++ b/spec/controllers/constituencies_controller_spec.rb @@ -30,4 +30,36 @@ expect(response.headers["Access-Control-Allow-Headers"]).to eq("Origin, X-Requested-With, Content-Type, Accept") end end + + describe "GET /parliaments/:period/constituencies.json" do + before do + FactoryBot.create(:parliament, :conservatives_2019) + + get :index, format: "json", params: { period: "2019-2024" } + end + + it "responds with 200 OK" do + expect(response.status).to eq(200) + end + + it "assigns the @constituencies instance variable" do + expect(assigns[:constituencies]).not_to be_nil + end + + it "renders the constituencies/index template" do + expect(response).to render_template("constituencies/index") + end + + it "sets the Access-Control-Allow-Origin header to '*'" do + expect(response.headers["Access-Control-Allow-Origin"]).to eq("*") + end + + it "sets the Access-Control-Allow-Methods header to 'GET'" do + expect(response.headers["Access-Control-Allow-Methods"]).to eq("GET") + end + + it "sets the Access-Control-Allow-Headers header to 'Origin, X-Requested-With, Content-Type, Accept'" do + expect(response.headers["Access-Control-Allow-Headers"]).to eq("Origin, X-Requested-With, Content-Type, Accept") + end + end end diff --git a/spec/controllers/petitions_controller_spec.rb b/spec/controllers/petitions_controller_spec.rb index 43a3690e3..12977fcae 100644 --- a/spec/controllers/petitions_controller_spec.rb +++ b/spec/controllers/petitions_controller_spec.rb @@ -501,6 +501,11 @@ get :index expect(response).to redirect_to("https://petition.parliament.uk/") end + + it "doesn't redirect JSON requests" do + get :index, as: :json + expect(response).to be_successful + end end end diff --git a/spec/factories.rb b/spec/factories.rb index 3da3ecbdd..d9b477763 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -927,6 +927,19 @@ trait :archived do archived_at { 1.month.ago } end + + trait :conservatives_2019 do + government { "Conservatives" } + state_opening_at { "2019-12-19 11:30:00".in_time_zone } + opening_at { "2020-03-03 10:55:00".in_time_zone } + archived_at { "2024-10-21 09:00:00".in_time_zone } + + dissolution_heading { "Parliament is dissolving" } + dissolution_message { "This means all petitions will close in 2 weeks" } + dissolved_heading { "Parliament is dissolved" } + dissolved_message { "All petitions are now closed" } + dissolution_at { "2024-05-26 20:40:00".in_time_zone } + end end factory :tag do