From ac4268f8746a578dca050158b885a8c6fef72e7b Mon Sep 17 00:00:00 2001 From: Lori Bailey <44073106+elceebee@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:20:46 +0000 Subject: [PATCH] Implement logging in as support user for placements --- app/controllers/application_controller.rb | 19 +++++++++++ app/controllers/personas_controller.rb | 6 +--- .../support/application_controller.rb | 11 +++++++ .../support/organisations_controller.rb | 8 +++++ app/controllers/sessions_controller.rb | 2 +- app/helpers/application_helper.rb | 9 ----- app/helpers/routes_helper.rb | 15 +++++++++ app/models/dfe_sign_in_user.rb | 2 -- app/models/school.rb | 4 ++- .../support/organisations/index.html.erb | 23 +++++++++++++ config/locales/en.yml | 3 ++ config/routes.rb | 2 +- config/routes/placements.rb | 5 +++ spec/factories/schools.rb | 1 + spec/factories/users.rb | 6 ++++ .../sign_in_as_a_claims_user_persona_spec.rb | 32 +++++++++--------- .../personas/sign_in_as_a_persona_spec.rb | 25 -------------- ...gn_in_as_a_placements_user_persona_spec.rb | 15 ++++----- spec/models/dfe_sign_in_user_spec.rb | 2 -- spec/models/gias_school_spec.rb | 6 ++++ spec/models/school_spec.rb | 2 +- spec/requests/personas_spec.rb | 33 ------------------- 22 files changed, 125 insertions(+), 106 deletions(-) create mode 100644 app/controllers/placements/support/application_controller.rb create mode 100644 app/controllers/placements/support/organisations_controller.rb create mode 100644 app/helpers/routes_helper.rb create mode 100644 app/views/placements/support/organisations/index.html.erb delete mode 100644 spec/features/personas/sign_in_as_a_persona_spec.rb delete mode 100644 spec/requests/personas_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 139a2d82d..8a3339471 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,6 @@ class ApplicationController < ActionController::Base include ApplicationHelper + include RoutesHelper default_form_builder(GOVUKDesignSystemFormBuilder::FormBuilder) @@ -18,4 +19,22 @@ def sign_in_user def current_user @current_user ||= sign_in_user&.user end + + def after_sign_in_path + return support_root_path if current_user.support_user? + + root_path + end + + def after_sign_out_path + sign_in_path + end + + def authenticate_user! + return if current_user + + session[:requested_path] = request.fullpath + + redirect_to sign_in_path + end end diff --git a/app/controllers/personas_controller.rb b/app/controllers/personas_controller.rb index 26d2d0873..b85ff0868 100644 --- a/app/controllers/personas_controller.rb +++ b/app/controllers/personas_controller.rb @@ -2,10 +2,6 @@ class PersonasController < ApplicationController def index - if current_service.present? - @personas = Persona.public_send(current_service).decorate - else - redirect_to :not_found - end + @personas = Persona.public_send(current_service).decorate end end diff --git a/app/controllers/placements/support/application_controller.rb b/app/controllers/placements/support/application_controller.rb new file mode 100644 index 000000000..db5cd87b3 --- /dev/null +++ b/app/controllers/placements/support/application_controller.rb @@ -0,0 +1,11 @@ +class Placements::Support::ApplicationController < ApplicationController + before_action :authenticate_user!, :authorize_user! + + private + + def authorize_user! + return if current_user.support_user? + + redirect_to placements_root_path, alert: "You cannot perform this action" + end +end diff --git a/app/controllers/placements/support/organisations_controller.rb b/app/controllers/placements/support/organisations_controller.rb new file mode 100644 index 000000000..79c556056 --- /dev/null +++ b/app/controllers/placements/support/organisations_controller.rb @@ -0,0 +1,8 @@ +class Placements::Support::OrganisationsController < Placements::Support::ApplicationController + def index + @schools = + Placements::School.includes(:gias_school).order("gias_schools.name") + # TODO: when we have more from the provider API.... + @providers = Provider.all + end +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 6ed5f9d35..9a2884fc7 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -7,7 +7,7 @@ def callback if current_user # DfESignInUsers::Update.call(user: current_user, sign_in_user: sign_in_user) - redirect_to(root_path) + redirect_to after_sign_in_path else # session.delete(:requested_path) DfESignInUser.end_session!(session) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e1e82abc8..0b096a66e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,13 +7,4 @@ def current_service :placements end end - - def root_path - case current_service - when :claims - claims_root_path - when :placements - placements_root_path - end - end end diff --git a/app/helpers/routes_helper.rb b/app/helpers/routes_helper.rb new file mode 100644 index 000000000..f916d1486 --- /dev/null +++ b/app/helpers/routes_helper.rb @@ -0,0 +1,15 @@ +module RoutesHelper + def root_path + { + claims: claims_root_path, + placements: placements_root_path + }.fetch current_service + end + + def support_root_path + { + claims: root_path, # TODO: claims support path in another PR + placements: placements_support_root_path + }.fetch current_service + end +end diff --git a/app/models/dfe_sign_in_user.rb b/app/models/dfe_sign_in_user.rb index 12bca9084..4ae7efd6b 100644 --- a/app/models/dfe_sign_in_user.rb +++ b/app/models/dfe_sign_in_user.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - class DfESignInUser attr_reader :email, :dfe_sign_in_uid attr_accessor :first_name, :last_name, :service diff --git a/app/models/school.rb b/app/models/school.rb index ba55941bb..903d22acf 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -16,10 +16,12 @@ # index_schools_on_urn (urn) UNIQUE # class School < ApplicationRecord - has_one :gias_school, foreign_key: :urn, primary_key: :urn + belongs_to :gias_school, foreign_key: :urn, primary_key: :urn validates :urn, presence: true validates :urn, uniqueness: { case_sensitive: false } + delegate :name, to: :gias_school + scope :placements, -> { where placements: true } scope :claims, -> { where claims: true } end diff --git a/app/views/placements/support/organisations/index.html.erb b/app/views/placements/support/organisations/index.html.erb new file mode 100644 index 000000000..5f08645bf --- /dev/null +++ b/app/views/placements/support/organisations/index.html.erb @@ -0,0 +1,23 @@ +
+
+

<%= t("organisations") %>

+ + <% if @schools.any? %> +

<%= t("schools") %>

+ + <% end %> + + <% if @providers.any? %> +

<%= t("providers") %>

+ + <% end %> +
+
diff --git a/config/locales/en.yml b/config/locales/en.yml index e64e4f518..f2a772fd9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,4 +1,7 @@ en: + organisations: Organisations + schools: Schools + providers: Providers account: index: email_address: Email address diff --git a/config/routes.rb b/config/routes.rb index 26015a122..bcfdf0e9e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do scope via: :all do - get "/404", to: "errors#not_found", as: :not_found + get "/404", to: "errors#not_found" get "/422", to: "errors#unprocessable_entity" get "/429", to: "errors#too_many_requests" get "/500", to: "errors#internal_server_error" diff --git a/config/routes/placements.rb b/config/routes/placements.rb index d80da4947..5ac869471 100644 --- a/config/routes/placements.rb +++ b/config/routes/placements.rb @@ -4,4 +4,9 @@ host: ENV["PLACEMENTS_HOST"] } do root to: "pages#index" + + namespace :support do + root to: redirect("/support/organisations") + resources :organisations, only: :index + end end diff --git a/spec/factories/schools.rb b/spec/factories/schools.rb index 617fc638f..17b18b653 100644 --- a/spec/factories/schools.rb +++ b/spec/factories/schools.rb @@ -18,6 +18,7 @@ FactoryBot.define do factory :school do sequence(:urn) { _1 } + association :gias_school trait :claims do claims { true } diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 4ab5b0e27..78750f417 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -34,4 +34,10 @@ factory :placements_user, class: "Placements::User", parent: :user do service { "placements" } end + + factory :placements_support_user, + class: "Placements::SupportUser", + parent: :user do + service { "placements" } + end end diff --git a/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb b/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb index 331008c3d..926cbf3cc 100644 --- a/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb +++ b/spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require "rails_helper" feature "Sign In as a Claims User Persona" do @@ -10,36 +8,36 @@ end scenario "I sign in as persona Anne" do - given_there_is_an_existing_persona_for("Anne") - when_i_visit_the_personas_page - then_i_see_the_persona_for("Anne") + given_there_is_an_existing_claims_persona_for("Anne") + when_i_visit_the_claims_personas_page + then_i_see_the_claims_persona_for("Anne") when_i_click_sign_in_as("Anne") and_i_visit_my_account_page then_i_see_persona_details_for_anne end scenario "I sign in as persona Patricia" do - given_there_is_an_existing_persona_for("Patricia") - when_i_visit_the_personas_page - then_i_see_the_persona_for("Patricia") + given_there_is_an_existing_claims_persona_for("Patricia") + when_i_visit_the_claims_personas_page + then_i_see_the_claims_persona_for("Patricia") when_i_click_sign_in_as("Patricia") and_i_visit_my_account_page then_i_see_persona_details_for_patricia end scenario "I sign in as persona Mary" do - given_there_is_an_existing_persona_for("Mary") - when_i_visit_the_personas_page - then_i_see_the_persona_for("Mary") + given_there_is_an_existing_claims_persona_for("Mary") + when_i_visit_the_claims_personas_page + then_i_see_the_claims_persona_for("Mary") when_i_click_sign_in_as("Mary") and_i_visit_my_account_page then_i_see_persona_details_for_mary end scenario "I sign in as persona colin" do - given_there_is_an_existing_persona_for("Colin") - when_i_visit_the_personas_page - then_i_see_the_persona_for("Colin") + given_there_is_an_existing_claims_persona_for("Colin") + when_i_visit_the_claims_personas_page + then_i_see_the_claims_persona_for("Colin") when_i_click_sign_in_as("Colin") and_i_visit_my_account_page then_i_see_persona_details_for_colin @@ -48,15 +46,15 @@ private -def given_there_is_an_existing_persona_for(persona_name) +def given_there_is_an_existing_claims_persona_for(persona_name) create(:persona, persona_name.downcase.to_sym, service: "claims") end -def when_i_visit_the_personas_page +def when_i_visit_the_claims_personas_page visit personas_path end -def then_i_see_the_persona_for(persona_name) +def then_i_see_the_claims_persona_for(persona_name) expect(page).to have_content(persona_name) end diff --git a/spec/features/personas/sign_in_as_a_persona_spec.rb b/spec/features/personas/sign_in_as_a_persona_spec.rb deleted file mode 100644 index 7ff72e3e2..000000000 --- a/spec/features/personas/sign_in_as_a_persona_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -feature "Attempting to sign in without a service specified" do - scenario "I sign in as persona Anne" do - given_there_is_an_existing_persona - when_i_visit_the_personas_page - then_i_see_a_404_error_page - end - - private - - def given_there_is_an_existing_persona - create(:persona) - end - - def when_i_visit_the_personas_page - visit personas_path - end - - def then_i_see_a_404_error_page - expect(page).to have_content "Page not found" - end -end diff --git a/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb b/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb index e4f2e614a..0606f6cc9 100644 --- a/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb +++ b/spec/features/personas/sign_in_as_a_placements_user_persona_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require "rails_helper" feature "Sign In as a Placements User Persona" do @@ -60,9 +58,9 @@ def when_i_visit_the_personas_page end def and_there_are_placement_organisations - create(:gias_school, name: "Placement School") - create(:school, :placements) - create(:provider, id: 123_456_789) + gias_school = create(:gias_school, name: "Placement School") + create(:school, :placements, gias_school:) + create(:provider, provider_code: "PROVIDER_CODE") end def then_i_see_the_persona_for(persona_name) @@ -78,10 +76,9 @@ def and_i_visit_my_account_page end def then_i_see_a_list_of_organisations - expect(path).to eq dashboard_path - expect(page).to have_content("Placements School") - # We won't have a name or data for the providers until after the Provider API integration is done - expect(page).to have_content("123456789") + expect(current_path).to eq placements_support_organisations_path + expect(page).to have_content("Placement School") + expect(page).to have_content("PROVIDER_CODE") end def then_i_see_persona_details_for_anne diff --git a/spec/models/dfe_sign_in_user_spec.rb b/spec/models/dfe_sign_in_user_spec.rb index 722211c9d..536e62db2 100644 --- a/spec/models/dfe_sign_in_user_spec.rb +++ b/spec/models/dfe_sign_in_user_spec.rb @@ -1,5 +1,3 @@ -# frozen_string_literal: true - require "rails_helper" describe DfESignInUser do diff --git a/spec/models/gias_school_spec.rb b/spec/models/gias_school_spec.rb index eef08d0e1..5806edaee 100644 --- a/spec/models/gias_school_spec.rb +++ b/spec/models/gias_school_spec.rb @@ -25,6 +25,12 @@ RSpec.describe GiasSchool, type: :model do subject { create(:gias_school) } + describe "associations" do + it do + should have_one(:school).with_foreign_key(:urn).with_primary_key(:urn) + end + end + describe "validations" do it { is_expected.to validate_presence_of(:urn) } it { is_expected.to validate_uniqueness_of(:urn).case_insensitive } diff --git a/spec/models/school_spec.rb b/spec/models/school_spec.rb index bb162eaea..5fe6ff44c 100644 --- a/spec/models/school_spec.rb +++ b/spec/models/school_spec.rb @@ -20,7 +20,7 @@ RSpec.describe School, type: :model do context "associations" do it do - should have_one(:gias_school).with_foreign_key(:urn).with_primary_key( + should belong_to(:gias_school).with_foreign_key(:urn).with_primary_key( :urn ) end diff --git a/spec/requests/personas_spec.rb b/spec/requests/personas_spec.rb deleted file mode 100644 index 9670309c5..000000000 --- a/spec/requests/personas_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require "rails_helper" - -RSpec.describe "Personas", type: :request do - context "placements" do - describe "GET /personas" do - around do |example| - host! ENV["PLACEMENTS_HOST"] - example.run - host! nil - end - - it "returns http success" do - get personas_path - expect(response).to have_http_status(:success) - end - end - end - - context "claims" do - describe "GET /personas" do - around do |example| - host! ENV["CLAIMS_HOST"] - example.run - host! nil - end - - it "returns http success" do - get personas_path - expect(response).to have_http_status(:success) - end - end - end -end