Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #36891 - Provide a scope for email-notification-eligible users #9892

Merged
merged 1 commit into from
Feb 21, 2024
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/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def self.name_format
end
}

scope :with_enabled_email, -> { where(disabled: [nil, false], mail_enabled: true).where.not(mail: ['', nil]) }

dirty_has_many_associations :roles

attr_exportable :firstname, :lastname, :mail, :description, :fullname, :name => ->(user) { user.login }, :ssh_authorized_keys => ->(user) { user.ssh_keys.map(&:to_export_hash) }
Expand Down
21 changes: 21 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ def setup
assert_valid u
end

test ".with_enabled_email gives only the right users" do
# Mail enabled
u1 = FactoryBot.create(:user, :mail => '[email protected]', :mail_enabled => nil)
u2 = FactoryBot.create(:user, :mail => '[email protected]', :mail_enabled => '')
assert User.with_enabled_email.where(login: [u1, u2].map(&:login)).empty?

# Mail missing
u1 = FactoryBot.create(:user, :mail => '', :mail_enabled => true)
u2 = FactoryBot.create(:user, :mail => nil, :mail_enabled => true)
assert User.with_enabled_email.where(login: [u1, u2].map(&:login)).empty?

# Disabled
u1 = FactoryBot.create(:user, :mail => '[email protected]', :mail_enabled => true, :disabled => false)
u2 = FactoryBot.create(:user, :mail => '[email protected]', :mail_enabled => true, :disabled => nil)
u3 = FactoryBot.create(:user, :mail => '[email protected]', :mail_enabled => true, :disabled => true)
actual = User.with_enabled_email.where(login: [u1, u2, u3].map(&:login))
assert_includes actual, u1
assert_includes actual, u2
assert_equal actual.count, 2
end

test 'login should also be unique across usergroups' do
Usergroup.expects(:where).with(:name => 'foo').returns(['fakeusergroup'])
u = FactoryBot.build_stubbed(:user, :auth_source => auth_sources(:one),
Expand Down