Skip to content

Commit 7727f5d

Browse files
committed
Introduce validation for public email domain for FI responses to update referee details
1 parent a5cedfd commit 7727f5d

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

app/forms/teacher_interface/further_information_request_item_work_history_contact_form.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,30 @@ class FurtherInformationRequestItemWorkHistoryContactForm < BaseForm
1111
validates :contact_name, :contact_job, presence: true
1212
validates :contact_email, presence: true, valid_for_notify: true
1313

14+
validate :contact_email_has_private_email_domain
15+
1416
def update_model
1517
further_information_request_item.update(
1618
contact_name:,
1719
contact_job:,
1820
contact_email:,
1921
)
2022
end
23+
24+
delegate :application_form, to: :further_information_request_item
25+
26+
private
27+
28+
def parsed_contact_email
29+
EmailAddress.new(contact_email)
30+
end
31+
32+
def contact_email_has_private_email_domain
33+
return unless application_form.requires_private_email_for_referee?
34+
35+
if EmailDomain.public?(parsed_contact_email.host_name)
36+
errors.add(:contact_email, :invalid_email_domain)
37+
end
38+
end
2139
end
2240
end

app/views/teacher_interface/further_information_request_items/_inner_form.html.erb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
<% work_history = @further_information_request_item.work_history %>
3636
<p class="govuk-body">Enter the contact details of a suitable referee who can verify your time spent working at <%= work_history.school_name %> from <%= work_history.start_date.to_fs(:month_and_year) %><% if work_history.end_date.present? %> to <%= work_history.end_date.to_fs(:month_and_year) %><% end %>.</p>
3737

38-
<%= f.govuk_text_field :contact_name, label: {text: "Contact’s name"} %>
38+
<%= f.govuk_text_field :contact_name, label: { text: "Contact’s name" } %>
3939

40-
<%= f.govuk_text_field :contact_job, label: {text: "Contact’s job title"} %>
40+
<%= f.govuk_text_field :contact_job, label: { text: "Contact’s job title" } %>
4141

42-
<%= f.govuk_email_field :contact_email, label: {text: "Contact’s email address"} %>
42+
<% if @further_information_request_item.application_form.requires_private_email_for_referee? %>
43+
<%= f.govuk_email_field :contact_email, label: { text: "Contact’s email address" }, hint: { text: "The email address must use the school’s domain, for example, name@school.edu" } %>
44+
<% else %>
45+
<%= f.govuk_email_field :contact_email, label: { text: "Contact’s email address" } %>
46+
<% end %>
4347

4448
<%= govuk_details(summary_text: "Choosing someone to verify this role") do %>
4549

@@ -48,6 +52,10 @@
4852
<p>Make sure you have an up-to-date email address for the person you choose, because the reference they provide will be important to your application.</p>
4953

5054
<p>We’ll ask them to confirm that you worked at the school for the period you’ve told us about.</p>
55+
56+
<% if @further_information_request_item.application_form.requires_private_email_for_referee? %>
57+
<p>Your references need to have an email address that uses the schools domain, which is the part of an email address that shows who provides it. It comes after the @ symbol. For example, in name@school.edu the domain is school.edu.</p>
58+
<% end %>
5159
<% end %>
5260
<% end %>
5361

config/locales/teacher_interface.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ en:
350350
blank: Enter this person’s job title
351351
contact_email:
352352
blank: Enter this person’s email address
353+
invalid_email_domain: Enter an official email address that uses the school’s domain
353354
teacher_interface/name_and_date_of_birth_form:
354355
attributes:
355356
date_of_birth:
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
RSpec.describe TeacherInterface::FurtherInformationRequestItemWorkHistoryContactForm,
6+
type: :model do
7+
subject(:form) do
8+
described_class.new(
9+
further_information_request_item:,
10+
contact_name:,
11+
contact_job:,
12+
contact_email:,
13+
)
14+
end
15+
16+
let(:further_information_request_item) do
17+
create(
18+
:further_information_request_item,
19+
:with_work_history_contact_response,
20+
)
21+
end
22+
23+
describe "validations" do
24+
let(:contact_name) { "" }
25+
let(:contact_job) { "" }
26+
let(:contact_email) { "" }
27+
28+
it { is_expected.to validate_presence_of(:contact_name) }
29+
it { is_expected.to validate_presence_of(:contact_job) }
30+
it { is_expected.to validate_presence_of(:contact_email) }
31+
32+
context "when contact email has public email domain" do
33+
let(:contact_name) { "John Doe" }
34+
let(:contact_job) { "Head Teacher" }
35+
let(:contact_email) { "test@gmail.com" }
36+
37+
it { is_expected.to be_valid }
38+
end
39+
40+
context "when the application form requires private email for referee is enabled" do
41+
before do
42+
further_information_request_item.application_form.update!(
43+
requires_private_email_for_referee: true,
44+
)
45+
end
46+
47+
context "when contact email has public email domain" do
48+
let(:contact_name) { "John Doe" }
49+
let(:contact_job) { "Head Teacher" }
50+
let(:contact_email) { "test@gmail.com" }
51+
52+
it { is_expected.not_to be_valid }
53+
end
54+
end
55+
end
56+
57+
describe "#save" do
58+
subject(:save) { form.save(validate: true) }
59+
60+
let(:contact_name) { "First Last" }
61+
let(:contact_job) { "Job" }
62+
let(:contact_email) { "school@example.com" }
63+
64+
before { expect(save).to be true }
65+
66+
it "saves the work history contact information" do
67+
expect(further_information_request_item.contact_name).to eq("First Last")
68+
expect(further_information_request_item.contact_job).to eq("Job")
69+
expect(further_information_request_item.contact_email).to eq(
70+
"school@example.com",
71+
)
72+
end
73+
end
74+
end

0 commit comments

Comments
 (0)