Skip to content

Commit

Permalink
Fixes #36891 - Provide a scope for email-notification-eligible users
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka authored and ofedoren committed Feb 21, 2024
1 parent 5a3d323 commit 2233c33
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 2233c33

Please sign in to comment.