From 927105f08c8eb5a00669a0cb989083e21ceb542f Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Tue, 5 Sep 2023 13:31:28 +0100 Subject: [PATCH] Add site creation actions and views We are aiming to archive Transition Config which is currently used to create sites in Transition. This adds the actions and views necessary for a user to add a new transition. --- app/controllers/sites_controller.rb | 44 ++++++++- app/views/organisations/show.html.erb | 2 + app/views/sites/new.html.erb | 96 +++++++++++++++++++ config/breadcrumbs.rb | 5 + config/locales/en.yml | 14 +++ features/site.feature | 13 +++ .../site_interaction_steps.rb | 34 +++++-- spec/controllers/sites_controller_spec.rb | 12 +++ spec/requests/site_creation_spec.rb | 48 ++++++++++ 9 files changed, 259 insertions(+), 9 deletions(-) create mode 100644 app/views/sites/new.html.erb create mode 100644 spec/requests/site_creation_spec.rb diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 67385105a..cd3702c3d 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -1,11 +1,26 @@ class SitesController < ApplicationController - before_action :find_site + before_action :find_site, only: %i[edit update show] + before_action :find_organisation, only: %i[new create] before_action :check_user_is_gds_editor, only: %i[edit update] + def new + @site_form = SiteForm.new(organisation_slug: @organisation.whitehall_slug) + end + + def create + @site_form = SiteForm.new(create_params) + + if (site = @site_form.save) + redirect_to site_path(site), flash: { success: "Transition site created" } + else + render :new + end + end + def edit; end def update - if @site.update(site_params) + if @site.update(update_params) redirect_to site_path(@site), flash: { success: "Transition date updated" } else redirect_to edit_site_path(@site), flash: { alert: "We couldn't save your change" } @@ -24,7 +39,30 @@ def find_site @site = Site.find_by!(abbr: params[:id]) end - def site_params + def find_organisation + @organisation = Organisation.find_by(whitehall_slug: params[:organisation_id]) + end + + def create_params + params.require(:site_form).permit( + :organisation_slug, + :abbr, + :tna_timestamp, + :homepage, + :homepage_title, + :global_type, + :global_new_url, + :global_redirect_append_path, + :homepage_furl, + :hostname, + :query_params, + :special_redirect_strategy, + :aliases, + extra_organisations: [], + ) + end + + def update_params params.require(:site).permit(:launch_date) end diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb index f566e3c51..d3f34db5a 100644 --- a/app/views/organisations/show.html.erb +++ b/app/views/organisations/show.html.erb @@ -10,6 +10,8 @@ <%= render 'in_conjunction_with' %> +<%= link_to "Add a transition site", new_organisation_site_path(@organisation), class: "btn btn-default" %> + <% unless @sites.empty? %>

Sites

diff --git a/app/views/sites/new.html.erb b/app/views/sites/new.html.erb new file mode 100644 index 000000000..12eadde8f --- /dev/null +++ b/app/views/sites/new.html.erb @@ -0,0 +1,96 @@ +<% breadcrumb(:new_site, @organisation) %> + +
+

New transition site

+
+ +<%= form_for @site_form, html: { role: "form" }, url: organisation_sites_path do |form| %> + <%= render "shared/error_messages", error_messages: form.object.errors.full_messages %> + +
+
+ <%= form.hidden_field :organisation_slug, value: @organisation.whitehall_slug %> + +
+ <%= form.label :abbr %> + <%= form.text_field :abbr, class: "form-control" %> +
+ +
+ <%= form.label :tna_timestamp %> + <%= form.text_field :tna_timestamp, class: "form-control" %> +
+ +
+ <%= form.label :homepage %> + <%= form.text_field :homepage, class: "form-control" %> +
+ +
+ <%= form.label :hostname %> + <%= form.text_field :hostname, class: "form-control" %> +
+ +
+ <%= form.label :homepage_title %> + <%= form.text_field :homepage_title, class: "form-control" %> +
+ +
+ <%= form.label :extra_organisations %> + <%= form.collection_select :extra_organisations, + form.object.organisations, + :id, + :title, + {}, + { multiple: true, class: "form-control" } + %> +
+ +
+ <%= form.label :homepage_furl %> + <%= form.text_field :homepage_furl, class: "form-control" %> +
+ +
+ <%= form.label :global_type %> + <%= form.collection_radio_buttons :global_type, + Site::GLOBAL_TYPES, + :to_s, + :humanize + %> +
+ +
+ <%= form.label :global_new_url %> + <%= form.text_field :global_new_url, class: "form-control" %> +
+ +
+ <%= form.label :query_params %> + <%= form.text_field :query_params, class: "form-control" %> +
+ +
+ <%= form.label :global_redirect_append_path %> + <%= form.check_box :global_redirect_append_path %> +
+ +
+ <%= form.label :special_redirect_strategy %> + <%= form.collection_radio_buttons :special_redirect_strategy, + Site::SPECIAL_REDIRECT_STRATEGY_TYPES, + :to_s, + :humanize + %> +
+ +
+ <%= form.label :aliases %> + <%= form.text_area :aliases, class: "form-control" %> +
+ + <%= form.submit 'Save', class: 'add-top-margin btn btn-success' %> +
+
+<% end %> diff --git a/config/breadcrumbs.rb b/config/breadcrumbs.rb index 87824111c..64a987f77 100644 --- a/config/breadcrumbs.rb +++ b/config/breadcrumbs.rb @@ -7,6 +7,11 @@ parent :root end +crumb :new_site do |organisation| + link "New transition site" + parent :organisation, organisation +end + crumb :site do |site| link site.default_host.hostname, site_path(site) parent :organisation, site.organisation diff --git a/config/locales/en.yml b/config/locales/en.yml index fe6ef44e6..4b39be47c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -16,6 +16,20 @@ en: new_url: 'The URL to redirect to' mappings_batch: paths: Old URLs + activemodel: + attributes: + site_form: + abbr: Abbreviated name + host: Host + tna_timestamp: TNA timestamp + homepage: Homepage + homepage_title: Homepage title + extra_organisations: Extra organisations + homepage_furl: Homepage full URL + global_type: Global type + options: Options + global_redirect_append_path: Global redirect append path + special_redirect_strategy: Special redirect strategy mappings: success: all_created: '%{created}%{tagged_with}' diff --git a/features/site.feature b/features/site.feature index 485257052..31220f167 100644 --- a/features/site.feature +++ b/features/site.feature @@ -102,6 +102,19 @@ Scenario: Jumping to a non-existent site And I jump to the site or mapping "http://not-a-site.gov.uk" Then I should see the header "Unknown site" +Scenario: Creating a site + Given I have logged in as a GDS Editor + And there are these organisations without sites: + | whitehall_slug | title | + | ukti | UK Trade & Industry | + | go-science | Government Office for Science | + When I visit the page for the UK Trade & Industry organisation + And I click the link "Add a transition site" + Then I should be on the new transition site page for the UK Trade & Industry organisation + When I fill in the new transition site fields + And I save my changes + Then I should be redirected to the new site + Scenario: Editing a site's transition date as a GDS Editor Given I have logged in as a GDS Editor And the date is 29/11/19 diff --git a/features/step_definitions/site_interaction_steps.rb b/features/step_definitions/site_interaction_steps.rb index 5c3e09bd7..90486d26c 100644 --- a/features/step_definitions/site_interaction_steps.rb +++ b/features/step_definitions/site_interaction_steps.rb @@ -1,11 +1,33 @@ +When(/^I visit the page for the (.*) organisation$/) do |organisation_title| + organisation = Organisation.find_by(title: organisation_title) + visit organisation_path(organisation) +end + When(/^I visit this site page$/) do visit site_path(@site) end -When(/^I edit this site's transition date$/) do - click_link "Edit date" - select("2014", from: "site_launch_date_1i") - select("September", from: "site_launch_date_2i") - select("20", from: "site_launch_date_3i") - click_button "Save" +When(/^I fill in the new transition site fields/) do + fill_in "Abbreviated name", with: "aaib" + fill_in "TNA timestamp", with: "20141104112824" + fill_in "Homepage", with: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch" + fill_in "Hostname", with: "www.aaib.gov.uk" + fill_in "Homepage title", with: "Air accidents investigation branch" + select "Government Office for Science" + fill_in "Homepage full URL", with: "www.gov.uk/aaib" + choose "Redirect" + fill_in "Global new URL", with: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch/about" + fill_in "Query params", with: "file" + check "Global redirect append path" + choose "Via aka" + fill_in "Aliases", with: "aaib.gov.uk,aaib.com" +end + +Then(/^I should be on the new transition site page for the (.*) organisation$/) do |organisation_title| + organisation = Organisation.find_by(title: organisation_title) + i_should_be_on_the_path new_organisation_site_path(organisation) +end + +Then(/^I should be redirected to the new site$/) do + i_should_be_on_the_path site_path(Site.last) end diff --git a/spec/controllers/sites_controller_spec.rb b/spec/controllers/sites_controller_spec.rb index 91731622e..a014f7742 100644 --- a/spec/controllers/sites_controller_spec.rb +++ b/spec/controllers/sites_controller_spec.rb @@ -4,6 +4,18 @@ let(:site) { create :site, abbr: "moj" } let(:gds_bob) { create(:gds_editor, name: "Bob Terwhilliger") } + describe "#new" do + let(:organisation) { create(:organisation) } + + before { login_as gds_bob } + + it "returns a success response" do + get :new, params: { organisation_id: organisation.id } + + expect(response.status).to eql(200) + end + end + describe "#edit" do context "when the user does have permission" do before do diff --git a/spec/requests/site_creation_spec.rb b/spec/requests/site_creation_spec.rb new file mode 100644 index 000000000..97d69ef5d --- /dev/null +++ b/spec/requests/site_creation_spec.rb @@ -0,0 +1,48 @@ +require "rails_helper" + +describe "Site creation", type: :request do + let!(:gds_bob) { create(:gds_editor, name: "Bob Terwhilliger") } + let(:organisation) { create(:organisation, whitehall_slug: "air-accidents-investigation-branch") } + let(:params) { attributes_for :site_form, :with_optional_fields, :with_aliases, organisation_slug: "air-accidents-investigation-branch" } + + it "redirects to the new site path" do + post organisation_sites_path(organisation), params: { create_site: params } + + expect(response).to redirect_to(site_path(Site.last)) + end + + it "creates the new site" do + post organisation_sites_path(organisation), params: { create_site: params } + + attributes = { + abbr: "aaib", + global_new_url: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch/about", + global_redirect_append_path: true, + global_type: "redirect", + homepage: "https://www.gov.uk/government/organisations/air-accidents-investigation-branch", + homepage_furl: "www.gov.uk/aaib", + homepage_title: "Air accidents investigation branch", + query_params: "file", + special_redirect_strategy: "via_aka", + tna_timestamp: Time.strptime("20141104112824", "%Y%m%d%H%M%S"), + } + + expect(Site.last.attributes.with_indifferent_access).to include attributes + expect(Site.last.organisation).to eq organisation + end + + it "creates related hosts and aka hosts" do + post organisation_sites_path(organisation), params: { create_site: params } + + alias_site_1 = Host.where(hostname: "www.aaib.gov.uk") + alias_site_2 = Host.where(hostname: "aaib.gov.uk") + alias_site_3 = Host.where(hostname: "aaib.com") + + expect(alias_site_1).to exist + expect(alias_site_2).to exist + expect(alias_site_3).to exist + expect(Host.where(hostname: "aka.aaib.gov.uk", canonical_host: alias_site_1)).to exist + expect(Host.where(hostname: "aka-aaib.gov.uk", canonical_host: alias_site_2)).to exist + expect(Host.where(hostname: "aka-aaib.com", canonical_host: alias_site_3)).to exist + end +end