Skip to content

Commit

Permalink
Implement logging in as support user for placements
Browse files Browse the repository at this point in the history
  • Loading branch information
elceebee authored and ollietreend committed Dec 21, 2023
1 parent ac8e5c7 commit 9684ac0
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 106 deletions.
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ApplicationController < ActionController::Base
include ApplicationHelper
include RoutesHelper

default_form_builder(GOVUKDesignSystemFormBuilder::FormBuilder)

Expand All @@ -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
6 changes: 1 addition & 5 deletions app/controllers/personas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions app/controllers/placements/support/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 0 additions & 9 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions app/helpers/routes_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions app/models/dfe_sign_in_user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

class DfESignInUser
attr_reader :email, :dfe_sign_in_uid
attr_accessor :first_name, :last_name, :service
Expand Down
4 changes: 3 additions & 1 deletion app/models/school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions app/views/placements/support/organisations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl"><%= t("organisations") %></h1>

<% if @schools.any? %>
<h2 class="govuk-heading-l"><%= t("schools") %></h2>
<ul class="govuk-list">
<% @schools.each do |school| %>
<li><%= school.name %></li>
<% end %>
</ul>
<% end %>

<% if @providers.any? %>
<h2 class="govuk-heading-l"><%= t("providers") %></h2>
<ul class="govuk-list">
<% @providers.each do |provider| %>
<li><%= provider.provider_code %></li>
<% end %>
</ul>
<% end %>
</div>
</div>
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
en:
organisations: Organisations
schools: Schools
providers: Providers
account:
index:
email_address: Email address
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 5 additions & 0 deletions config/routes/placements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/factories/schools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
FactoryBot.define do
factory :school do
sequence(:urn) { _1 }
association :gias_school

trait :claims do
claims { true }
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 15 additions & 17 deletions spec/features/personas/sign_in_as_a_claims_user_persona_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

require "rails_helper"

feature "Sign In as a Claims User Persona" do
Expand All @@ -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
Expand All @@ -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

Expand Down
25 changes: 0 additions & 25 deletions spec/features/personas/sign_in_as_a_persona_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

require "rails_helper"

feature "Sign In as a Placements User Persona" do
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions spec/models/dfe_sign_in_user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

require "rails_helper"

describe DfESignInUser do
Expand Down
6 changes: 6 additions & 0 deletions spec/models/gias_school_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion spec/models/school_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 0 additions & 33 deletions spec/requests/personas_spec.rb

This file was deleted.

0 comments on commit 9684ac0

Please sign in to comment.