Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions config/site.defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""

Expand Down
28 changes: 28 additions & 0 deletions spec/controllers/api/v8/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Loading