From 424a02ffbafc8cefede6fa391b6e1839e3d4c716 Mon Sep 17 00:00:00 2001 From: Henrik Nygren Date: Wed, 18 Nov 2026 21:07:20 +0200 Subject: [PATCH 1/2] Make email settings configurable --- app/mailers/user_mailer.rb | 2 ++ config/site.defaults.yml | 3 ++ .../api/v8/users_controller_spec.rb | 28 +++++++++++++++++++ 3 files changed, 33 insertions(+) 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..51a6a7e90 100644 --- a/spec/controllers/api/v8/users_controller_spec.rb +++ b/spec/controllers/api/v8/users_controller_spec.rb @@ -135,6 +135,24 @@ expect(json['success']).to eq(false) expect(json['errors']).to eq('username' => ['has already been taken']) end + + before { allow_any_instance_of(User).to receive(:post_new_user_to_courses_mooc_fi) } + + 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) From 2501461ce7809fb8e94d31462b16fd95d460608e Mon Sep 17 00:00:00 2001 From: Henrik Nygren Date: Wed, 18 Nov 2026 21:34:10 +0200 Subject: [PATCH 2/2] Move the spec setup above the examples --- spec/controllers/api/v8/users_controller_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/controllers/api/v8/users_controller_spec.rb b/spec/controllers/api/v8/users_controller_spec.rb index 51a6a7e90..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) @@ -136,8 +138,6 @@ expect(json['errors']).to eq('username' => ['has already been taken']) end - before { allow_any_instance_of(User).to receive(:post_new_user_to_courses_mooc_fi) } - it 'sends a confirmation mail by default' do expect do post :create, params: { user: { email: 'new@example.com', password: 'secret123', password_confirmation: 'secret123' } }