Skip to content

Commit

Permalink
Add host-based routing constraints
Browse files Browse the repository at this point in the history
This allows us to separate routing between the School Placements service and the Track & Pay service.
  • Loading branch information
Nitemaeric committed Dec 14, 2023
1 parent 0fce159 commit c453198
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ POSTGRES_PASSWORD=password
DATABASE_URL=postgres://postgres:$POSTGRES_PASSWORD@localhost:5432
GOVUK_NOTIFY_API_KEY=
SIGN_IN_METHOD=persona

PLACEMENTS_HOST=placements.localhost
CLAIMS_HOST=claims.localhost
2 changes: 2 additions & 0 deletions app/controllers/claims/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Claims::PagesController < ApplicationController
end
2 changes: 2 additions & 0 deletions app/controllers/placements/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Placements::PagesController < ApplicationController
end
8 changes: 8 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
module ApplicationHelper
def current_service
case request.host
when ENV["CLAIMS_HOST"]
:claims
when ENV["PLACEMENTS_HOST"]
:placements
end
end
end
26 changes: 26 additions & 0 deletions app/views/claims/pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">It works! 🎉</h1>

<p class="govuk-body">
Your application is ready - so long as this page rendered without any errors you're good to go.
</p>

<%= govuk_summary_list(
rows: [
{ key: { text: "Service" }, value: { text: "Claims" } },
{ key: { text: "Rails version" }, value: { text: Rails.version } },
{ key: { text: "Ruby version" }, value: { text: RUBY_VERSION } },
{ key: {
text: "GOV.UK Frontend" },
value: {
text: JSON
.parse(File.read(Rails.root.join("package.json")))
.dig("dependencies", "govuk-frontend")
.tr("^", "")
}
}
]
) %>
</div>
</div>
6 changes: 1 addition & 5 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@

<%= govuk_skip_link %>

<%= govuk_header(service_name: "GOV.UK Rails Boilerplate") do |header| %>
<%= header.with_navigation_item(text: "Navigation item 1", href: "#", active: true) %>
<%= header.with_navigation_item(text: "Navigation item 2", href: "#") %>
<%= header.with_navigation_item(text: "Navigation item 3", href: "#") %>
<% end %>
<%= govuk_header(homepage_url: "https://www.gov.uk", service_name: t(".#{current_service}.header.service_name"), service_url: "/") %>

<div class="govuk-width-container">
<main class="govuk-main-wrapper" id="main-content" role="main">
Expand Down
26 changes: 26 additions & 0 deletions app/views/placements/pages/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">It works! 🎉</h1>

<p class="govuk-body">
Your application is ready - so long as this page rendered without any errors you're good to go.
</p>

<%= govuk_summary_list(
rows: [
{ key: { text: "Service" }, value: { text: "Placements" } },
{ key: { text: "Rails version" }, value: { text: Rails.version } },
{ key: { text: "Ruby version" }, value: { text: RUBY_VERSION } },
{ key: {
text: "GOV.UK Frontend" },
value: {
text: JSON
.parse(File.read(Rails.root.join("package.json")))
.dig("dependencies", "govuk-frontend")
.tr("^", "")
}
}
]
) %>
</div>
</div>
6 changes: 6 additions & 0 deletions config/locales/claims/en/layouts/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
layouts:
application:
claims:
header:
service_name: Claim Funding for General Mentors
6 changes: 6 additions & 0 deletions config/locales/placements/en/layouts/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
layouts:
application:
placements:
header:
service_name: Manage School Placements
9 changes: 3 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
Rails.application.routes.draw do
root to: "pages#home"
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"

scope via: :all do
get "/404", to: "errors#not_found"
get "/422", to: "errors#unprocessable_entity"
Expand All @@ -19,4 +13,7 @@
get("/personas", to: "personas#index")
get("/auth/developer/sign-out", to: "sessions#signout")
post("/auth/developer/callback", to: "sessions#callback")

draw :placements
draw :claims
end
3 changes: 3 additions & 0 deletions config/routes/claims.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scope module: :claims, as: :claims, constraints: { host: ENV["CLAIMS_HOST"] } do
root to: "pages#index"
end
3 changes: 3 additions & 0 deletions config/routes/placements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
scope module: :placements, as: :placements, constraints: { host: ENV["PLACEMENTS_HOST"] } do
root to: "pages#index"
end
2 changes: 1 addition & 1 deletion spec/requests/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe "Pages", type: :request do
describe "GET /home" do
it "returns http success" do
xit "returns http success" do
get "/"
expect(response).to have_http_status(:success)
end
Expand Down
57 changes: 57 additions & 0 deletions spec/system/home_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require "rails_helper"

RSpec.feature "Home Page" do
after do
Capybara.app_host = nil
end

scenario "User visits the claims homepage" do
given_i_am_on_the_claims_site
and_i_am_on_the_start_page
i_can_see_the_claims_service_name_in_the_header
and_i_can_see_the_claims_service_value_in_the_page
end

scenario "User visits the placements homepage" do
given_i_am_on_the_placements_site
and_i_am_on_the_start_page
i_can_see_the_placements_service_name_in_the_header
and_i_can_see_the_placements_service_value_in_the_page
end

private

def given_i_am_on_the_claims_site
ENV["CLAIMS_HOST"] = "claims.localhost"
Capybara.app_host = "http://#{ENV["CLAIMS_HOST"]}"
end

def given_i_am_on_the_placements_site
ENV["PLACEMENTS_HOST"] = "placements.localhost"
Capybara.app_host = "http://#{ENV["PLACEMENTS_HOST"]}"
end

def and_i_am_on_the_start_page
visit "/"
end

def i_can_see_the_claims_service_name_in_the_header
within(".govuk-header") do
expect(page).to have_content("Claim Funding for General Mentors")
end
end

def i_can_see_the_placements_service_name_in_the_header
within(".govuk-header") do
expect(page).to have_content("Manage School Placements")
end
end

def and_i_can_see_the_claims_service_value_in_the_page
expect(page).to have_css(".govuk-summary-list__value", text: "Claims")
end

def and_i_can_see_the_placements_service_value_in_the_page
expect(page).to have_css(".govuk-summary-list__value", text: "Placements")
end
end

0 comments on commit c453198

Please sign in to comment.