mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #11487 from pedrocarmona/feature/invitation-email-locale
Send translated email when creating users via super admin interface
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
module Spree
|
||||
module Admin
|
||||
class UsersController < ::Admin::ResourceController
|
||||
helper I18nHelper
|
||||
|
||||
rescue_from Spree::User::DestroyWithOrdersError, with: :user_destroy_with_orders_error
|
||||
|
||||
after_action :sign_in_if_change_own_password, only: :update
|
||||
@@ -127,6 +129,10 @@ module Spree
|
||||
params[:user][:email] != @user.email
|
||||
end
|
||||
|
||||
def build_resource
|
||||
model_class.new(locale: I18n.default_locale)
|
||||
end
|
||||
|
||||
def user_params
|
||||
::PermittedAttributes::User.new(params).call(
|
||||
%i[enterprise_limit show_api_key_view]
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module I18nHelper
|
||||
def locale_options
|
||||
OpenFoodNetwork::I18nConfig.available_locales.map do |locale|
|
||||
[t('language_name', locale:), locale]
|
||||
end
|
||||
end
|
||||
|
||||
def set_locale
|
||||
UserLocaleSetter.new(spree_current_user, params[:locale], cookies).set_locale
|
||||
end
|
||||
|
||||
@@ -16,7 +16,7 @@ module PermittedAttributes
|
||||
|
||||
def permitted_attributes
|
||||
[
|
||||
:email, :password, :password_confirmation, :disabled,
|
||||
:email, :locale, :password, :password_confirmation, :disabled,
|
||||
{ webhook_endpoints_attributes: [:id, :url] },
|
||||
]
|
||||
end
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
= check_box_tag "user[spree_role_ids][]", role.id, @user.spree_roles.include?(role), id: "user_spree_role_#{role.name}"
|
||||
= label_tag role.name
|
||||
= hidden_field_tag "user[spree_role_ids][]", ""
|
||||
= f.field_container :locale do
|
||||
= f.label :locale, t(".locale")
|
||||
= f.select :locale, locale_options, class: "fullwidth"
|
||||
= f.field_container :enterprise_limit do
|
||||
= f.label :enterprise_limit, t(".enterprise_limit")
|
||||
= f.text_field :enterprise_limit, class: "fullwidth"
|
||||
|
||||
@@ -4334,6 +4334,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using
|
||||
enterprise_limit: "Enterprise Limit"
|
||||
confirm_password: "Confirm Password"
|
||||
password: "Password"
|
||||
locale: "Language"
|
||||
email_confirmation:
|
||||
confirmation_pending: "Email confirmation is pending. We've sent a confirmation email to %{address}."
|
||||
variants:
|
||||
|
||||
@@ -83,7 +83,7 @@ describe Spree::UsersController, type: :controller do
|
||||
it 'should create a new user' do
|
||||
post :create,
|
||||
params: { user: { email: 'foobar@example.com', password: 'foobar123',
|
||||
password_confirmation: 'foobar123' } }
|
||||
password_confirmation: 'foobar123', locale: 'es' } }
|
||||
expect(assigns[:user].new_record?).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
@@ -34,15 +34,33 @@ describe Spree::UserMailer do
|
||||
end
|
||||
|
||||
describe "#confirmation_instructions" do
|
||||
it "sends an email" do
|
||||
token = "random"
|
||||
email = Spree::UserMailer.confirmation_instructions(user, token)
|
||||
let(:token) { "random" }
|
||||
subject(:email) { Spree::UserMailer.confirmation_instructions(user, token) }
|
||||
|
||||
expect {
|
||||
it "sends an email" do
|
||||
expect { email.deliver_now }.to change { ActionMailer::Base.deliveries.count }.by(1)
|
||||
end
|
||||
|
||||
context 'when the language is English' do
|
||||
it 'sends an email with the translated subject' do
|
||||
email.deliver_now
|
||||
}.to change {
|
||||
ActionMailer::Base.deliveries.count
|
||||
}.by(1)
|
||||
|
||||
expect(ActionMailer::Base.deliveries.first.subject).to include(
|
||||
"Please confirm your OFN account"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the language is Spanish' do
|
||||
let(:user) { build(:user, locale: 'es') }
|
||||
|
||||
it 'sends an email with the translated subject' do
|
||||
email.deliver_now
|
||||
|
||||
expect(ActionMailer::Base.deliveries.first.subject).to include(
|
||||
"Por favor, confirma tu cuenta de OFN"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -131,21 +131,32 @@ describe "Managing users" do
|
||||
end
|
||||
|
||||
describe "creating a user" do
|
||||
it "shows no confirmation message to start with" do
|
||||
visit spree.new_admin_user_path
|
||||
expect(page).to have_no_text "Email confirmation is pending"
|
||||
end
|
||||
|
||||
it "confirms successful creation" do
|
||||
visit spree.new_admin_user_path
|
||||
|
||||
# shows no confirmation message to start with
|
||||
expect(page).to have_no_text "Email confirmation is pending"
|
||||
|
||||
fill_in "Email", with: "user1@example.org"
|
||||
fill_in "Password", with: "user1Secret"
|
||||
fill_in "Confirm Password", with: "user1Secret"
|
||||
expect do
|
||||
click_button "Create"
|
||||
end.to change { Spree::User.count }.by 1
|
||||
expect(page).to have_text "Created Successfully"
|
||||
expect(page).to have_text "Email confirmation is pending"
|
||||
|
||||
expect(page).to have_select "Language", selected: "English"
|
||||
select "Español", from: "Language"
|
||||
|
||||
perform_enqueued_jobs do
|
||||
expect do
|
||||
click_button "Create"
|
||||
end.to change { Spree::User.count }.by 1
|
||||
expect(page).to have_text "Created Successfully"
|
||||
expect(page).to have_text "Email confirmation is pending"
|
||||
|
||||
expect(Spree::User.last.locale).to eq "es"
|
||||
|
||||
expect(ActionMailer::Base.deliveries.first.subject).to match(
|
||||
"Por favor, confirma tu cuenta de OFN"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user