diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 6ef47a2d0..0c0ca7123 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -7,6 +7,8 @@ class UserMailer < ActionMailer::Base extend T::Sig sig { params(user: User, origin: T.nilable(String), language: T.nilable(String)).void } def email_confirmation(user, origin = nil, language = nil) + return unless SiteSetting.value('send_account_confirmation_email') + # For the Elements course, emails often bounce because the course does not mention mooc.fi handles the technical implementation of the course. # To protect our delivery rates we temporaily disable sending this email. return if origin && origin.downcase.include?('elements') diff --git a/config/site.defaults.yml b/config/site.defaults.yml index 3546a1e31..4f75584a0 100644 --- a/config/site.defaults.yml +++ b/config/site.defaults.yml @@ -78,6 +78,9 @@ emails: from: noreply@example.com email_code_reviews_by_default: false +# Send a confirmation email when an account is created or its email address changes. +send_account_confirmation_email: true + # Used for email notifications intended for site admins administrative_email: "" diff --git a/spec/controllers/api/v8/users_controller_spec.rb b/spec/controllers/api/v8/users_controller_spec.rb index 7ee44eda7..d0112da26 100644 --- a/spec/controllers/api/v8/users_controller_spec.rb +++ b/spec/controllers/api/v8/users_controller_spec.rb @@ -126,6 +126,8 @@ describe 'POST create' do let(:token) { nil } + before { allow_any_instance_of(User).to receive(:post_new_user_to_courses_mooc_fi) } + it 'reports login errors under username' do allow(SecureRandom).to receive(:uuid).and_return(other_user.login) @@ -135,6 +137,22 @@ expect(json['success']).to eq(false) expect(json['errors']).to eq('username' => ['has already been taken']) end + + it 'sends a confirmation mail by default' do + expect do + post :create, params: { user: { email: 'new@example.com', password: 'secret123', password_confirmation: 'secret123' } } + end.to change { ActionMailer::Base.deliveries.size }.by(1) + end + + it 'sends no mail when account confirmation emails are off' do + allow(SiteSetting).to receive(:value).and_call_original + allow(SiteSetting).to receive(:value).with('send_account_confirmation_email').and_return(false) + + expect do + post :create, params: { user: { email: 'new@example.com', password: 'secret123', password_confirmation: 'secret123' } } + end.not_to(change { ActionMailer::Base.deliveries.size }) + expect(response.parsed_body['success']).to eq(true) + end end describe 'PUT update password' do @@ -211,6 +229,16 @@ def do_update(old_password) expect(user.reload.email).to eq('changed@example.com') end + it 'sends no mail when the email changes and account confirmation emails are off' do + allow(SiteSetting).to receive(:value).and_call_original + allow(SiteSetting).to receive(:value).with('send_account_confirmation_email').and_return(false) + + expect do + put :update, params: { id: user.id, user: { email: 'changed@example.com' } }, as: :json + end.not_to(change { ActionMailer::Base.deliveries.size }) + expect(user.reload.email_verified).to be(false) + end + it 'changes the password through courses.mooc.fi' do expect_any_instance_of(User).to receive(:update_password_via_courses_mooc_fi).with('oldpassword', 'newpassword').and_return(true)