Skip to content

Commit 78ea68c

Browse files
committed
Fix email domain validation for Welsh support email
Users with email addresses not ending in gov.uk were experiencing errors when trying to provide a Welsh translation for their form because their support email was valid for the English version but not the Welsh version. This was happening even though both `support_email` and `support_email_cy` used the `allowed_domains` validator because the Welsh translation input object wasn't exposing the current user to the validator. This commit fixes this so that things work as expected.
1 parent f8380af commit 78ea68c

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

app/controllers/forms/welsh_translation_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def welsh_translation_params
8686
{ selection_options_cy_attributes: %i[id name_cy] },
8787
{ condition_translations_attributes: WelshConditionTranslationInput.attribute_names },
8888
],
89-
).merge(form: current_form)
89+
).merge(current_user:, form: current_form)
9090
end
9191

9292
def delete_welsh_translation_params

app/input_objects/forms/welsh_translation_input.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Forms::WelshTranslationInput < Forms::MarkCompleteInput
22
include TextInputHelper
33
include ActiveModel::Attributes
44

5-
attr_accessor :form, :page_translations
5+
attr_accessor :current_user, :form, :page_translations
66

77
attribute :mark_complete
88

spec/input_objects/forms/welsh_translation_input_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
RSpec.describe Forms::WelshTranslationInput, type: :model do
44
subject(:welsh_translation_input) { described_class.new(new_input_data) }
55

6+
let(:current_user) { build :user }
67
let(:form) { build_form }
78
let(:page) do
89
create :page,
@@ -18,6 +19,7 @@
1819

1920
let(:new_input_data) do
2021
{
22+
current_user:,
2123
form:,
2224
mark_complete:,
2325
name_cy: "New Welsh name",
@@ -194,6 +196,15 @@ def build_empty_welsh_form
194196
expect(welsh_translation_input).not_to be_valid
195197
expect(welsh_translation_input.errors.full_messages_for(:support_email_cy)).to include "Support email cy #{I18n.t('activemodel.errors.models.forms/welsh_translation_input.attributes.support_email_cy.non_government_email')}"
196198
end
199+
200+
context "when the user has an email address with the same domain" do
201+
let(:current_user) { build :user, email: "user@example.com" }
202+
203+
it "is valid" do
204+
expect(welsh_translation_input).to be_valid
205+
expect(welsh_translation_input.errors.full_messages_for(:support_email_cy)).to be_empty
206+
end
207+
end
197208
end
198209
end
199210

spec/requests/forms/welsh_translation_controller_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,33 @@
112112
end
113113
end
114114

115+
context "when 'Yes' is selected and support email does not end in '.gov.uk'" do
116+
let(:domain) { "ogd.ewxample" }
117+
let(:support_email) { Faker::Internet.email(domain:) }
118+
119+
let(:form) { create(:form, :ready_for_routing, welsh_completed: false, support_email:) }
120+
let(:params) { { forms_welsh_translation_input: { form:, mark_complete:, name_cy: "Gwneud cais am drwydded jyglo", privacy_policy_url_cy: "https://juggling.gov.uk/privacy_policy/cy", support_email_cy: support_email, page_translations_attributes: } } }
121+
122+
it "returns a 422, re-renders the page with an error, and does not display a success banner" do
123+
post(welsh_translation_create_path(id), params:)
124+
125+
expect(response).to have_http_status(:unprocessable_content)
126+
expect(response).to render_template(:new)
127+
expect(response.body).to include(I18n.t("activemodel.errors.models.forms/welsh_translation_input.attributes.support_email_cy.non_government_email"))
128+
expect(flash).to be_empty
129+
end
130+
131+
context "and the current user is signed in with an email address at the same domain" do
132+
let(:standard_user) { build :user, :standard, organisation: test_org, email: Faker::Internet.email(domain:) }
133+
134+
it "redirects to the form task list and displays a success banner including text about being marked complete" do
135+
post(welsh_translation_create_path(id), params:)
136+
expect(response).to redirect_to(form_path(id))
137+
expect(flash[:success]).to eq(I18n.t("banner.success.form.welsh_translation_saved_and_completed"))
138+
end
139+
end
140+
end
141+
115142
context "when the user is not authorized" do
116143
let(:current_user) { build :user }
117144

0 commit comments

Comments
 (0)