Skip to content

Commit

Permalink
Add change application email page
Browse files Browse the repository at this point in the history
This adds a page which allows assessors to change the email address
associated with an application/teacher. To implement this I've
refactored the existing change name page to use a different path.
  • Loading branch information
thomasleese committed Mar 28, 2024
1 parent 1b5c888 commit 29c8373
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 22 deletions.
33 changes: 28 additions & 5 deletions app/controllers/assessor_interface/application_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,37 @@ def timeline
.order(created_at: :desc)
end

def edit
def edit_email
@form = ApplicationFormEmailForm.new
end

def update_email
@form =
ApplicationFormEmailForm.new(
email_form_params.merge(application_form:, user: current_staff),
)

if @form.save
redirect_to [:assessor_interface, application_form]
else
render :edit_email, status: :unprocessable_entity
end
end

def edit_name
@form = ApplicationFormNameForm.new
end

def update
def update_name
@form =
ApplicationFormNameForm.new(
form_params.merge(application_form:, user: current_staff),
name_form_params.merge(application_form:, user: current_staff),
)

if @form.save
redirect_to [:assessor_interface, application_form]
else
render :edit, status: :unprocessable_entity
render :edit_name, status: :unprocessable_entity
end
end

Expand Down Expand Up @@ -99,7 +116,13 @@ def application_form
@application_form ||= show_view_object.application_form
end

def form_params
def email_form_params
params.require(:assessor_interface_application_form_email_form).permit(
:email,
)
end

def name_form_params
params.require(:assessor_interface_application_form_name_form).permit(
:given_names,
:family_name,
Expand Down
15 changes: 13 additions & 2 deletions app/helpers/application_form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ def application_form_summary_rows(
if AssessorInterface::ApplicationFormPolicy.new(
current_staff,
application_form,
).edit?
).edit_name?
{
visually_hidden_text: I18n.t("application_form.summary.name"),
href: [:edit, :assessor_interface, application_form],
href: [:name, :assessor_interface, application_form],
}
end,
].compact,
Expand Down Expand Up @@ -76,6 +76,17 @@ def application_form_summary_rows(
end
),
},
actions: [
if AssessorInterface::ApplicationFormPolicy.new(
current_staff,
application_form,
).edit_email?
{
visually_hidden_text: I18n.t("application_form.summary.email"),
href: [:email, :assessor_interface, application_form],
}
end,
].compact,
},
{
key: {
Expand Down
10 changes: 9 additions & 1 deletion app/policies/assessor_interface/application_form_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ def show?
alias_method :timeline?, :show?
alias_method :show_pdf?, :show?

def update?
def update_email?
user.change_email_permission
end

alias_method :edit_email?, :update_email?

def update_name?
user.change_name_permission
end

alias_method :edit_name?, :update_name?

def destroy?
user.withdraw_permission
end
Expand Down
26 changes: 26 additions & 0 deletions app/views/assessor_interface/application_forms/edit_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% title = "Change email for ‘#{application_form_full_name(@application_form)}’" %>

<% content_for :page_title, title_with_error_prefix(title, error: @form.errors.any?) %>
<% content_for :back_link_url, back_history_path(default: assessor_interface_application_form_path(@application_form)) %>

<%= form_with model: @form, url: [:email, :assessor_interface, @application_form] do |f| %>
<%= f.govuk_error_summary %>

<h1 class="govuk-heading-xl"><%= title %></h1>

<h2 class="govuk-heading-m">Current email</h2>

<%= govuk_summary_list(actions: false) do |summary_list|
summary_list.with_row do |row|
row.with_key { "Email" }
row.with_value { @application_form.teacher.email }
end
end %>

<h2 class="govuk-heading-m">Change email</h2>
<p class="govuk-body">Use this form to change the email address.</p>

<%= f.govuk_text_field :email %>

<%= render "shared/assessor_interface/continue_cancel_button", f: %>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<% title = "Change details for ‘#{application_form_full_name(@application_form)}’" %>
<% title = "Change name for ‘#{application_form_full_name(@application_form)}’" %>

<% content_for :page_title, title_with_error_prefix(title, error: @form.errors.any?) %>
<% content_for :back_link_url, back_history_path(default: assessor_interface_application_form_path(@application_form)) %>

<%= form_with model: @form, url: [:assessor_interface, @application_form], method: :put do |f| %>
<%= form_with model: @form, url: [:name, :assessor_interface, @application_form] do |f| %>
<%= f.govuk_error_summary %>

<h1 class="govuk-heading-xl"><%= title %></h1>
Expand Down
8 changes: 7 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@

resources :application_forms,
path: "/applications",
except: :new,
only: %i[index show destroy],
param: :reference do
collection do
post "filters/apply", to: "application_forms#apply_filters"
get "filters/clear", to: "application_forms#clear_filters"
end

member do
get "email", to: "application_forms#edit_email"
post "email", to: "application_forms#update_email"

get "name", to: "application_forms#edit_name"
post "name", to: "application_forms#update_name"

get "status"
get "timeline"
get "withdraw"
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/staff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
assess_permission { true }
end

trait :with_change_email_permission do
change_email_permission { true }
end

trait :with_change_name_permission do
change_name_permission { true }
end
Expand Down
28 changes: 27 additions & 1 deletion spec/helpers/application_form_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
value: {
text: application_form.teacher.email,
},
actions: [],
},
{ key: { text: "Created on" }, value: { text: " 1 January 2020" } },
{
Expand Down Expand Up @@ -129,6 +130,31 @@
)
end

context "user has change email permission" do
let(:current_staff) { create(:staff, :with_change_email_permission) }

it "has an action to change the name" do
email_row = summary_rows.find { |row| row[:key][:text] == "Email" }

expect(email_row).to eq(
{
key: {
text: "Email",
},
value: {
text: application_form.teacher.email,
},
actions: [
{
visually_hidden_text: "Email",
href: [:email, :assessor_interface, application_form],
},
],
},
)
end
end

context "user has change name permission" do
let(:current_staff) { create(:staff, :with_change_name_permission) }

Expand All @@ -146,7 +172,7 @@
actions: [
{
visually_hidden_text: "Name",
href: [:edit, :assessor_interface, application_form],
href: [:name, :assessor_interface, application_form],
},
],
},
Expand Down
20 changes: 20 additions & 0 deletions spec/policies/assessor_interface/application_form_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@

describe "#edit?" do
subject(:edit?) { policy.edit? }
it_behaves_like "a policy method without permission"
end

describe "#update_email?" do
subject(:update?) { policy.update_email? }
it_behaves_like "a policy method requiring the change email permission"
end

describe "#edit_email?" do
subject(:edit?) { policy.edit_email? }
it_behaves_like "a policy method requiring the change email permission"
end

describe "#update_name?" do
subject(:update?) { policy.update_name? }
it_behaves_like "a policy method requiring the change name permission"
end

describe "#edit_name?" do
subject(:edit?) { policy.edit_name? }
it_behaves_like "a policy method requiring the change name permission"
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module PageObjects
module AssessorInterface
class EditApplicationEmail < SitePrism::Page
set_url "/assessor/applications/{reference}/email"

section :form, "form" do
element :email_field,
"#assessor-interface-application-form-email-form-email-field"
element :submit_button, ".govuk-button"
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module PageObjects
module AssessorInterface
class EditApplication < SitePrism::Page
set_url "/assessor/applications/{reference}/edit"
class EditApplicationName < SitePrism::Page
set_url "/assessor/applications/{reference}/name"

section :form, "form" do
element :given_names_field,
Expand Down
11 changes: 8 additions & 3 deletions spec/support/page_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,14 @@ def assessor_edit_age_range_subjects_assessment_recommendation_award_page
PageObjects::AssessorInterface::EditAgeRangeSubjectsAssessmentRecommendationAward.new
end

def assessor_edit_application_page
@assessor_edit_application_page ||=
PageObjects::AssessorInterface::EditApplication.new
def assessor_edit_application_email_page
@assessor_edit_application_email_page ||=
PageObjects::AssessorInterface::EditApplicationEmail.new
end

def assessor_edit_application_name_page
@assessor_edit_application_name_page ||=
PageObjects::AssessorInterface::EditApplicationName.new
end

def assessor_edit_work_history_page
Expand Down
12 changes: 12 additions & 0 deletions spec/support/shared_examples/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@
end
end

RSpec.shared_examples "a policy method requiring the change email permission" do
context "without permission" do
let(:user) { create(:staff) }
it { is_expected.to be false }
end

context "with permission" do
let(:user) { create(:staff, :with_change_email_permission) }
it { is_expected.to be true }
end
end

RSpec.shared_examples "a policy method requiring the change name permission" do
context "without permission" do
let(:user) { create(:staff) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Assessor change application form email", type: :system do
before { given_there_is_an_application_form }

it "checks manage applications permission" do
given_i_am_authorized_as_a_user(assessor)

when_i_visit_the(:assessor_edit_application_email_page, reference:)
then_i_see_the_forbidden_page
end

it "allows changing application form name" do
given_i_am_authorized_as_a_user(manager)

when_i_visit_the(:assessor_application_page, reference:)
then_i_see_the(:assessor_application_page)

when_i_click_on_change_email
then_i_see_the(:assessor_edit_application_email_page, reference:)

when_i_fill_in_the_email
then_i_see_the(:assessor_application_page, reference:)
end

def given_there_is_an_application_form
application_form
end

def when_i_click_on_change_email
assessor_application_page
.summary_list
.find_row(key: "Email")
.actions
.link
.click
end

def when_i_fill_in_the_email
assessor_edit_application_email_page.form.email_field.fill_in with:
"[email protected]"
assessor_edit_application_email_page.form.submit_button.click
end

def application_form
@application_form ||=
create(
:application_form,
:submitted,
:with_personal_information,
:with_assessment,
)
end

delegate :reference, to: :application_form

def assessor
create(:staff, :confirmed, :with_assess_permission)
end

def manager
create(:staff, :confirmed, :with_change_email_permission)
end
end
Loading

0 comments on commit 29c8373

Please sign in to comment.