Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

replace legacy spec

ulfertsee096432dfa2
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index e9d42733f80..86c437b8174 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -45,44 +45,6 @@ describe User, type: :model do
status:)
end
- describe 'a user with a long login (<= 256 chars)' do
- let(:login) { 'a' * 256 }
-
- it 'is valid' do
- user.login = login
- expect(user).to be_valid
- end
-
- it 'may be loaded from the database' do
- user.login = login
- expect(user.save).to be_truthy
-
- expect(User.find_by_login(login)).to eq(user)
- expect(User.find_by_unique(login)).to eq(user)
- end
- end
-
- describe 'a user with an invalid login' do
- let(:login) { 'me' }
-
- it 'is invalid' do
- user.login = login
- expect(user).not_to be_valid
- end
- end
-
- describe 'a user with and overly long login (> 256 chars)' do
- it 'is invalid' do
- user.login = 'a' * 257
- expect(user).not_to be_valid
- end
-
- it 'may not be stored in the database' do
- user.login = 'a' * 257
- expect(user.save).to be_falsey
- end
- end
-
describe 'with long but allowed attributes' do
it 'is valid' do
user.firstname = 'a' * 256
@@ -110,31 +72,52 @@ describe User, type: :model do
end
describe '#mail' do
- it 'is stripped' do
- user.mail = ' [email protected] '
- expect(user.mail)
- .to eql '[email protected]'
+ before do
+ user.mail = mail
end
- it 'validates for local mails' do
- user.mail = '[email protected]'
- expect(user).to be_valid
+ context 'with whitespaces' do
+ let(:mail) { ' [email protected] ' }
+
+ it 'is stripped' do
+ expect(user.mail)
+ .to eql '[email protected]'
+ end
end
- it 'invalidates wrong mails' do
- user.mail = 'foobar+abc.def.some-internet'
- expect(user).not_to be_valid
- expect(user.errors[:mail]).to include 'is not a valid email address.'
+ context 'for local mail addresses' do
+ let(:mail) { '[email protected]' }
+
+ it 'is valid' do
+ expect(user).to be_valid
+ end
+ end
+
+ context 'for wrong mail addresses' do
+ let(:mail) { 'foobar+abc.def.some-internet' }
+
+ it 'is invalid' do
+ expect(user).to be_invalid
+ end
+ end
+
+ context 'for an already taken mail addresses (different take)' do
+ let(:mail) { '[email protected]' }
+ let!(:other_user) { create(:user, mail: '[email protected]') }
+
+ it 'is invalid' do
+ expect(user).to be_invalid
+ end
end
end
describe '#login' do
- context 'with whitespace' do
- before do
- user.login = login
- end
+ before do
+ user.login = login
+ end
- context 'simple spaces' do
+ context 'with whitespace' do
+ context 'with simple spaces' do
let(:login) { 'a b c' }
it 'is valid' do
@@ -146,7 +129,7 @@ describe User, type: :model do
end
end
- context 'line breaks' do
+ context 'with line breaks' do
let(:login) { 'ab\nc' }
it 'is invalid' do
@@ -158,7 +141,7 @@ describe User, type: :model do
end
end
- context 'tabs' do
+ context 'with tabs' do
let(:login) { 'ab\tc' }
it 'is invalid' do
@@ -172,10 +155,6 @@ describe User, type: :model do
end
context 'with symbols' do
- before do
- user.login = login
- end
-
%w[+ _ . - @].each do |symbol|
context symbol do
let(:login) { "foo#{symbol}bar" }
@@ -190,7 +169,7 @@ describe User, type: :model do
end
end
- context 'combination thereof' do
+ context 'with combination thereof' do
let(:login) { 'the+boss-is@the_house.' }
it 'is valid' do
@@ -214,6 +193,95 @@ describe User, type: :model do
end
end
end
+
+ context 'with more that 255 chars' do
+ let(:login) { 'a' * 256 }
+
+ it 'is valid' do
+ user.login = login
+ expect(user).to be_valid
+ end
+
+ it 'may be loaded from the database' do
+ user.login = login
+ expect(user.save).to be_truthy
+
+ expect(described_class.find_by_login(login)).to eq(user)
+ expect(described_class.find_by_unique(login)).to eq(user)
+ end
+ end
+
+ context 'with an invalid login' do
+ let(:login) { 'me' }
+
+ it 'is invalid' do
+ user.login = login
+ expect(user).not_to be_valid
+ end
+ end
+
+ context 'with an overly long login (> 256 chars)' do
+ let(:login) { 'a' * 257 }
+
+ it 'is invalid' do
+ expect(user).not_to be_valid
+ end
+
+ it 'may not be stored in the database' do
+ expect(user.save).to be_falsey
+ end
+ end
+
+ context 'with another user having the login in a different case' do
+ let!(:other_user) { create(:user, login: 'NewUser') }
+ let(:login) { 'newuser' }
+
+ it 'is invalid' do
+ expect(user).not_to be_valid
+ end
+ end
+
+ context 'when empty' do
+ let(:login) { '' }
+
+ it 'is invalid' do
+ expect(user).not_to be_valid
+ end
+ end
+ end
+
+ describe '#name' do
+ let(:user) do
+ create(:user,
+ firstname: 'John',
+ lastname: 'Smith',
+ login: 'username',
+ end
+
+ context 'for firstname_lastname', with_settings: { user_format: :firstname_lastname } do
+ it { expect(user.name).to eq "#{user.firstname} #{user.lastname}" }
+ end
+
+ context 'for firstname', with_settings: { user_format: :firstname } do
+ it { expect(user.name).to eq user.firstname }
+ end
+
+ context 'for lastname_firstname', with_settings: { user_format: :lastname_firstname } do
+ it { expect(user.name).to eq "#{user.lastname} #{user.firstname}" }
+ end
+
+ context 'for lastname_n_firstname', with_settings: { user_format: :lastname_n_firstname } do
+ it { expect(user.name).to eq "#{user.lastname}#{user.firstname}" }
+ end
+
+ context 'for lastname_coma_firstname', with_settings: { user_format: :lastname_coma_firstname } do
+ it { expect(user.name).to eq "#{user.lastname}, #{user.firstname}" }
+ end
+
+ context 'for username', with_settings: { user_format: :username } do
+ it { expect(user.name).to eq user.login }
+ end
end
describe '#authentication_provider' do
@@ -439,6 +507,55 @@ describe User, type: :model do
end
end
+ describe '#wants_comments_in_reverse_order?' do
+ let(:user) { create(:user) }
+
+ it 'is false by default' do
+ expect(user)
+ .not_to be_wants_comments_in_reverse_order
+ end
+
+ it 'is false if set to asc' do
+ user.pref.comments_sorting = 'asc'
+
+ expect(user)
+ .not_to be_wants_comments_in_reverse_order
+ end
+
+ it 'is true if set to asc' do
+ user.pref.comments_sorting = 'desc'
+
+ expect(user)
+ .to be_wants_comments_in_reverse_order
+ end
+ end
+
+ describe '#roles_for_project' do
+ let(:project) { create(:project) }
+ let!(:user) do
+ create(:user,
+ member_in_project: project,
+ member_through_role: roles)
+ end
+ let(:roles) { [create(:role), create(:role)] }
+
+ context 'for a project the user has roles in' do
+ it 'returns the roles' do
+ expect(user.roles_for_project(project))
+ .to match_array roles
+ end
+ end
+
+ context 'for a project the user does not have roles in' do
+ let(:other_project) { create(:project) }
+
+ it 'returns an empty set' do
+ expect(user.roles_for_project(other_project))
+ .to be_empty
+ end
+ end
+ end
+
describe '.system' do
context 'no SystemUser exists' do
before do
@@ -516,16 +633,14 @@ describe User, type: :model do
end
describe '.find_by_rss_key' do
- before do
- @rss_key = user.rss_key
- end
+ let(:rss_key) { user.rss_key }
context 'feeds enabled' do
before do
allow(Setting).to receive(:feeds_enabled?).and_return(true)
end
- it { expect(User.find_by_rss_key(@rss_key)).to eq(user) }
+ it { expect(described_class.find_by_rss_key(rss_key)).to eq(user) }
end
context 'feeds disabled' do
@@ -533,11 +648,34 @@ describe User, type: :model do
allow(Setting).to receive(:feeds_enabled?).and_return(false)
end
- it { expect(User.find_by_rss_key(@rss_key)).to be_nil }
+ it { expect(described_class.find_by_rss_key(rss_key)).to be_nil }
end
end
- describe 'scope.newest' do
+ describe '#rss_key' do
+ let(:user) { create(:user) }
+
+ it 'is created on the fly' do
+ expect { user.rss_key }
+ .to change { user.reload.rss_token.nil? }
+ .from(true)
+ .to(false)
+ end
+
+ it 'is persisted' do
+ key = user.rss_key
+
+ expect(user.reload.rss_key)
+ .to eq key
+ end
+
+ it 'has a length of 64' do
+ expect(user.rss_key.length)
+ .to eq 64
+ end
+ end
+
+ describe '.newest' do
let!(:anonymous) { User.anonymous }
let!(:user1) { create(:user) }
let!(:user2) { create(:user) }
@@ -568,7 +706,7 @@ describe User, type: :model do
expect(Setting.mail_suffix_separators).to eq '+'
# Can match either of the first two
- match2 = User.find_by_mail('[email protected]')
+ match2 = described_class.find_by_mail('[email protected]')
expect([user1.id, user2.id]).to include(match2.id)
matches = User.where_mail_with_suffix('[email protected]')
@@ -583,11 +721,11 @@ describe User, type: :model do
it 'finds users matching the suffix' do
expect(Setting.mail_suffix_separators).to eq '+-'
- match1 = User.find_by_mail('[email protected]')
+ match1 = described_class.find_by_mail('[email protected]')
expect(match1).to eq(user3)
# Can match either of the three
- match2 = User.find_by_mail('[email protected]')
+ match2 = described_class.find_by_mail('[email protected]')
expect([user1.id, user2.id, user3.id]).to include(match2.id)
matches = User.where_mail_with_suffix('[email protected]')
@@ -661,6 +799,41 @@ describe User, type: :model do
end
end
+ describe '.find_by_api_key' do
+ let(:status) { described_class.statuses[:active] }
+
+ let!(:user) do
+ create(:user,
+ status:)
+ end
+ let!(:token) do
+ create(:api_token, user:)
+ end
+
+ context 'if the right token is used' do
+ it 'returns the user' do
+ expect(described_class.find_by_api_key(token.plain_value))
+ .to eq user
+ end
+ end
+
+ context 'if it isn\'t the right user' do
+ it 'returns nil' do
+ expect(described_class.find_by_api_key("#{token.value}abc"))
+ .to be_nil
+ end
+ end
+
+ context 'if the right token is used but the user is locked' do
+ let(:status) { described_class.statuses[:locked] }
+
+ it 'returns nil' do
+ expect(described_class.find_by_api_key(token.plain_value))
+ .to be_nil
+ end
+ end
+ end
+
describe '.find_by_mail' do
let(:mail) { '[email protected]' }
let!(:user) { create :user, mail: }
@@ -696,6 +869,18 @@ describe User, type: :model do
end
end
+ describe '.anonymous' do
+ it 'creates an anonymous user on the fly' do
+ expect(described_class.anonymous)
+ .to be_a(AnonymousUser)
+ end
+
+ it 'creates a persisted record' do
+ expect(described_class.anonymous)
+ .to be_persisted
+ end
+ end
+
include_examples 'creates an audit trail on destroy' do
subject { create(:attachment) }
end
diff --git a/spec_legacy/unit/user_spec.rb b/spec_legacy/unit/user_spec.rb
deleted file mode 100644
index 9063522ebd3..00000000000
--- a/spec_legacy/unit/user_spec.rb
+++ /dev/null
@@ -1,169 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-require_relative '../legacy_spec_helper'
-
-describe User, type: :model do
- include MiniTest::Assertions # refute
-
- fixtures :all
-
- before do
- @admin = User.find(1)
- @jsmith = User.find(2)
- @dlopper = User.find(3)
- end
-
- it 'creates' do
- user = User.new(firstname: 'new', lastname: 'user', mail: '[email protected]')
-
- user.login = 'jsmith'
- user.password = 'adminADMIN!'
- user.password_confirmation = 'adminADMIN!'
- # login uniqueness
- assert !user.save
- assert_equal 1, user.errors.count
-
- user.login = 'newuser'
- user.password = 'adminADMIN!'
- user.password_confirmation = 'NOTadminADMIN!'
- # password confirmation
- assert !user.save
- assert_equal 1, user.errors.count
-
- user.password = 'adminADMIN!'
- user.password_confirmation = 'adminADMIN!'
- assert user.save
- end
-
- context 'User.login' do
- it 'is case-insensitive.' do
- u = User.new(firstname: 'new', lastname: 'user', mail: '[email protected]')
- u.login = 'newuser'
- u.password = 'adminADMIN!'
- u.password_confirmation = 'adminADMIN!'
- assert u.save
-
- u = User.new(firstname: 'Similar', lastname: 'User', mail: '[email protected]')
- u.login = 'NewUser'
- u.password = 'adminADMIN!'
- u.password_confirmation = 'adminADMIN!'
- assert !u.save
- assert_includes u.errors[:login], I18n.t('activerecord.errors.messages.taken')
- end
- end
-
- it 'mails uniqueness should not be case sensitive' do
- u = User.new(firstname: 'new', lastname: 'user', mail: '[email protected]')
- u.login = 'newuser1'
- u.password = 'adminADMIN!'
- u.password_confirmation = 'adminADMIN!'
- assert u.save
-
- u = User.new(firstname: 'new', lastname: 'user', mail: '[email protected]')
- u.login = 'newuser2'
- u.password = 'adminADMIN!'
- u.password_confirmation = 'adminADMIN!'
- assert !u.save
- assert_includes u.errors[:mail], I18n.t('activerecord.errors.messages.taken')
- end
-
- it 'validates login presence' do
- @admin.login = ''
- assert_equal 1, @admin.errors.count
- end
-
- it 'names format' do
- assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
- Setting.user_format = :firstname_lastname
- assert_equal 'John Smith', @jsmith.reload.name
- Setting.user_format = :username
- assert_equal 'jsmith', @jsmith.reload.name
- end
-
- it 'returns existing or new anonymous' do
- anon = User.anonymous
- assert !anon.new_record?
- assert_kind_of AnonymousUser, anon
- end
-
- it { is_expected.to have_one :rss_token }
-
- it 'rsses key' do
- assert_nil @jsmith.rss_token
- key = @jsmith.rss_key
- assert_equal 64, key.length
-
- @jsmith.reload
- assert_equal key, @jsmith.rss_key
- end
-
- it { is_expected.to have_one :api_token }
-
- context 'User#find_by_api_key' do
- it 'returns nil if no matching key is found' do
- assert_nil User.find_by_api_key('zzzzzzzzz')
- end
-
- it 'returns nil if the key is found for an inactive user' do
- user = create(:user, status: User.statuses[:locked])
- token = build(:api_token, user:)
- user.api_token = token
- user.save
-
- assert_nil User.find_by_api_key(token.value)
- end
-
- it 'returns the user if the key is found for an active user' do
- user = create(:user, status: User.statuses[:active])
- token = build(:api_token, user:)
- user.api_token = token
- user.save
-
- assert_equal user, User.find_by_api_key(token.plain_value)
- end
- end
-
- it 'roleses for project' do
- # user with a role
- roles = @jsmith.roles_for_project(Project.find(1))
- assert_kind_of Role, roles.first
- assert_equal 'Manager', roles.first.name
-
- # user with no role
- assert_nil @dlopper.roles_for_project(Project.find(2)).detect(&:member?)
- end
-
- it 'commentses sorting preference' do
- assert [email protected]_comments_in_reverse_order?
- @jsmith.pref.comments_sorting = 'asc'
- assert [email protected]_comments_in_reverse_order?
- @jsmith.pref.comments_sorting = 'desc'
- assert @jsmith.wants_comments_in_reverse_order?
- end
-end