OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec_legacy/unit/user_spec.rb

384 lines
12 KiB

#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc 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
specify 'object_daddy creation' do
FactoryBot.create(:user, firstname: 'Testing connection')
FactoryBot.create(:user, firstname: 'Testing connection')
assert_equal 2, User.where(firstname: 'Testing connection').count
end
it 'should truth' do
assert_kind_of User, @jsmith
end
it 'should mail should be stripped' do
u = User.new
u.mail = ' foo@bar.com '
assert_equal 'foo@bar.com', u.mail
end
it 'should create' do
user = User.new(firstname: 'new', lastname: 'user', mail: 'newuser@somenet.foo')
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#before_create' do
it 'should set the mail_notification to the default Setting' do
@user1 = FactoryBot.create(:user, mail_notification: nil)
assert_equal 'only_my_events', @user1.mail_notification
Setting.default_notification_option = 'all'
@user2 = FactoryBot.create(:user)
assert_equal 'all', @user2.mail_notification
end
end
context 'User.login' do
it 'should be case-insensitive.' do
u = User.new(firstname: 'new', lastname: 'user', mail: 'newuser@somenet.foo')
u.login = 'newuser'
u.password = 'adminADMIN!'
u.password_confirmation = 'adminADMIN!'
assert u.save
u = User.new(firstname: 'Similar', lastname: 'User', mail: 'similaruser@somenet.foo')
u.login = 'NewUser'
u.password = 'adminADMIN!'
u.password_confirmation = 'adminADMIN!'
assert !u.save
assert_includes u.errors[:login], I18n.translate('activerecord.errors.messages.taken')
end
end
it 'should mail uniqueness should not be case sensitive' do
u = User.new(firstname: 'new', lastname: 'user', mail: 'newuser@somenet.foo')
u.login = 'newuser1'
u.password = 'adminADMIN!'
u.password_confirmation = 'adminADMIN!'
assert u.save
u = User.new(firstname: 'new', lastname: 'user', mail: 'newUser@Somenet.foo')
u.login = 'newuser2'
u.password = 'adminADMIN!'
u.password_confirmation = 'adminADMIN!'
assert !u.save
assert_includes u.errors[:mail], I18n.translate('activerecord.errors.messages.taken')
end
it 'should update' do
assert_equal 'admin', @admin.login
@admin.login = 'john'
assert @admin.save, @admin.errors.full_messages.join('; ')
@admin.reload
assert_equal 'john', @admin.login
end
it 'should destroy' do
User.find(2).destroy
assert_nil User.find_by(id: 2)
assert Member.where(user_id: 2).empty?
end
it 'should validate login presence' do
@admin.login = ''
assert !@admin.save
assert_equal 1, @admin.errors.count
end
it 'should validate mail notification inclusion' do
u = User.new
u.mail_notification = 'foo'
u.save
refute_empty u.errors[:mail_notification]
end
context 'User#try_to_login' do
it 'should fall-back to case-insensitive if user login is not found as-typed.' do
user = User.try_to_login('AdMin', 'adminADMIN!')
assert_kind_of User, user
assert_equal 'admin', user.login
end
it 'should select the exact matching user first' do
case_sensitive_user = FactoryBot.create(:user, login: 'changed', password: 'adminADMIN!', password_confirmation: 'adminADMIN!')
# bypass validations to make it appear like existing data
case_sensitive_user.update_attribute(:login, 'ADMIN')
user = User.try_to_login('ADMIN', 'adminADMIN!')
assert_kind_of User, user
assert_equal 'ADMIN', user.login
end
end
it 'should password' do
user = User.try_to_login('admin', 'adminADMIN!')
assert_kind_of User, user
assert_equal 'admin', user.login
user.password = 'newpassPASS!'
assert user.save
user = User.try_to_login('admin', 'newpassPASS!')
assert_kind_of User, user
assert_equal 'admin', user.login
end
it 'should name 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 'should lock' do
user = User.try_to_login('jsmith', 'jsmith')
assert_equal @jsmith, user
@jsmith.status = User.statuses[:locked]
assert @jsmith.save
user = User.try_to_login('jsmith', 'jsmith')
assert_equal nil, user
end
context '.try_to_login' do
context 'with good credentials' do
it 'should return the user' do
user = User.try_to_login('admin', 'adminADMIN!')
assert_kind_of User, user
assert_equal 'admin', user.login
end
end
context 'with wrong credentials' do
it 'should return nil' do
assert_nil User.try_to_login('admin', 'foo')
end
end
end
if ldap_configured?
context '#try_to_login using LDAP' do
context 'with failed connection to the LDAP server' do
it 'should return nil' do
@auth_source = LdapAuthSource.find(1)
allow_any_instance_of(AuthSource).to receive(:initialize_ldap_con).and_raise(Net::LDAP::Error, 'Cannot connect')
assert_equal nil, User.try_to_login('edavis', 'wrong')
end
end
context 'with an unsuccessful authentication' do
it 'should return nil' do
assert_equal nil, User.try_to_login('edavis', 'wrong')
end
end
context 'on the fly registration' do
before do
@auth_source = LdapAuthSource.find(1)
end
context 'with a successful authentication' do
it "should create a new user account if it doesn't exist" do
assert_difference('User.count') do
user = User.try_to_login('edavis', '123456')
assert !user.admin?
end
end
it 'should retrieve existing user' do
user = User.try_to_login('edavis', '123456')
user.admin = true
user.save!
assert_no_difference('User.count') do
user = User.try_to_login('edavis', '123456')
assert user.admin?
end
end
end
end
end
else
puts 'Skipping LDAP tests.'
end
it 'should return existing or new anonymous' do
anon = User.anonymous
assert !anon.new_record?
assert_kind_of AnonymousUser, anon
end
Convert specs to RSpec 3.2.0 syntax with Transpec This conversion is done by Transpec 3.1.0 with the following command: transpec -f -c "bundle exec rspec -Itest" test/functional/account_controller_test.rb test/functional/activities_controller_test.rb test/functional/admin_controller_test.rb test/functional/application_controller_test.rb test/functional/attachments_controller_test.rb test/functional/boards_controller_test.rb test/functional/custom_fields_controller_test.rb test/functional/enumerations_controller_test.rb test/functional/groups_controller_test.rb test/functional/help_controller_test.rb test/functional/journals_controller_test.rb test/functional/mail_handler_controller_test.rb test/functional/messages_controller_test.rb test/functional/my_controller_test.rb test/functional/project_enumerations_controller_test.rb test/functional/projects_controller_test.rb test/functional/repositories_controller_test.rb test/functional/repositories_filesystem_controller_test.rb test/functional/repositories_git_controller_test.rb test/functional/repositories_subversion_controller_test.rb test/functional/roles_controller_test.rb test/functional/search_controller_test.rb test/functional/settings_controller_test.rb test/functional/sys_controller_test.rb test/functional/time_entries/reports_controller_test.rb test/functional/timelog_controller_test.rb test/functional/types_controller_test.rb test/functional/user_mailer_test.rb test/functional/users_controller_test.rb test/functional/watchers_controller_test.rb test/functional/welcome_controller_test.rb test/functional/wiki_controller_test.rb test/functional/wikis_controller_test.rb test/functional/workflows_controller_test.rb test/integration/api_test/disabled_rest_api_test.rb test/integration/api_test/http_accept_auth_test.rb test/integration/api_test/http_basic_login_test.rb test/integration/api_test/http_basic_login_with_api_token_test.rb test/integration/api_test/token_authentication_test.rb test/integration/application_test.rb test/integration/layout_test.rb test/integration/lib/redmine/menu_manager_test.rb test/integration/lib/redmine/themes_test.rb test/integration/routing_test.rb test/unit/activity_test.rb test/unit/attachment_test.rb test/unit/board_test.rb test/unit/category_test.rb test/unit/changeset_test.rb test/unit/comment_test.rb test/unit/custom_field_test.rb test/unit/custom_field_user_format_test.rb test/unit/custom_value_test.rb test/unit/default_data_test.rb test/unit/enabled_module_test.rb test/unit/enumeration_test.rb test/unit/group_test.rb test/unit/helpers/application_helper_test.rb test/unit/helpers/custom_fields_helper_test.rb test/unit/helpers/sort_helper_test.rb test/unit/helpers/timelog_helper_test.rb test/unit/issue_nested_set_test.rb test/unit/issue_priority_test.rb test/unit/journal_observer_test.rb test/unit/journal_test.rb test/unit/ldap_auth_source_test.rb test/unit/lib/open_project/database_test.rb test/unit/lib/redmine/access_control_test.rb test/unit/lib/redmine/ciphering_test.rb test/unit/lib/redmine/codeset_util_test.rb test/unit/lib/redmine/helpers/calendar_test.rb test/unit/lib/redmine/hook_test.rb test/unit/lib/redmine/i18n_test.rb test/unit/lib/redmine/menu_manager/mapper_test.rb test/unit/lib/redmine/menu_manager/menu_helper_test.rb test/unit/lib/redmine/menu_manager/menu_item_test.rb test/unit/lib/redmine/menu_manager_test.rb test/unit/lib/redmine/mime_type_test.rb test/unit/lib/redmine/notifiable_test.rb test/unit/lib/redmine/plugin_test.rb test/unit/lib/redmine/safe_attributes_test.rb test/unit/lib/redmine/scm/adapters/filesystem_adapter_test.rb test/unit/lib/redmine/scm/adapters/git_adapter_test.rb test/unit/lib/redmine/scm/adapters/subversion_adapter_test.rb test/unit/lib/redmine/unified_diff_test.rb test/unit/lib/redmine/wiki_formatting/macros_test.rb test/unit/lib/redmine/wiki_formatting/null_formatter_test.rb test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb test/unit/lib/redmine/wiki_formatting_test.rb test/unit/lib/redmine_test.rb test/unit/mail_handler_test.rb test/unit/member_test.rb test/unit/message_test.rb test/unit/principal_test.rb test/unit/project_nested_set_test.rb test/unit/project_test.rb test/unit/query_test.rb test/unit/relation_test.rb test/unit/repository_filesystem_test.rb test/unit/repository_git_test.rb test/unit/repository_subversion_test.rb test/unit/repository_test.rb test/unit/role_test.rb test/unit/search_test.rb test/unit/status_test.rb test/unit/time_entry_activity_test.rb test/unit/time_entry_test.rb test/unit/token_test.rb test/unit/type_test.rb test/unit/user_preference_test.rb test/unit/user_test.rb test/unit/version_test.rb test/unit/watcher_test.rb test/unit/wiki_content_test.rb test/unit/wiki_page_test.rb test/unit/wiki_redirect_test.rb test/unit/wiki_test.rb * 180 conversions from: it { should ... } to: it { is_expected.to ... } * 12 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 4 conversions from: Klass.any_instance.should_receive(:message) to: expect_any_instance_of(Klass).to receive(:message) * 4 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 2 conversions from: Klass.any_instance.stub(:message) to: allow_any_instance_of(Klass).to receive(:message) For more details: https://github.com/yujinakayama/transpec#supported-conversions
10 years ago
it { is_expected.to have_one :rss_token }
it 'should rss 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
Convert specs to RSpec 3.2.0 syntax with Transpec This conversion is done by Transpec 3.1.0 with the following command: transpec -f -c "bundle exec rspec -Itest" test/functional/account_controller_test.rb test/functional/activities_controller_test.rb test/functional/admin_controller_test.rb test/functional/application_controller_test.rb test/functional/attachments_controller_test.rb test/functional/boards_controller_test.rb test/functional/custom_fields_controller_test.rb test/functional/enumerations_controller_test.rb test/functional/groups_controller_test.rb test/functional/help_controller_test.rb test/functional/journals_controller_test.rb test/functional/mail_handler_controller_test.rb test/functional/messages_controller_test.rb test/functional/my_controller_test.rb test/functional/project_enumerations_controller_test.rb test/functional/projects_controller_test.rb test/functional/repositories_controller_test.rb test/functional/repositories_filesystem_controller_test.rb test/functional/repositories_git_controller_test.rb test/functional/repositories_subversion_controller_test.rb test/functional/roles_controller_test.rb test/functional/search_controller_test.rb test/functional/settings_controller_test.rb test/functional/sys_controller_test.rb test/functional/time_entries/reports_controller_test.rb test/functional/timelog_controller_test.rb test/functional/types_controller_test.rb test/functional/user_mailer_test.rb test/functional/users_controller_test.rb test/functional/watchers_controller_test.rb test/functional/welcome_controller_test.rb test/functional/wiki_controller_test.rb test/functional/wikis_controller_test.rb test/functional/workflows_controller_test.rb test/integration/api_test/disabled_rest_api_test.rb test/integration/api_test/http_accept_auth_test.rb test/integration/api_test/http_basic_login_test.rb test/integration/api_test/http_basic_login_with_api_token_test.rb test/integration/api_test/token_authentication_test.rb test/integration/application_test.rb test/integration/layout_test.rb test/integration/lib/redmine/menu_manager_test.rb test/integration/lib/redmine/themes_test.rb test/integration/routing_test.rb test/unit/activity_test.rb test/unit/attachment_test.rb test/unit/board_test.rb test/unit/category_test.rb test/unit/changeset_test.rb test/unit/comment_test.rb test/unit/custom_field_test.rb test/unit/custom_field_user_format_test.rb test/unit/custom_value_test.rb test/unit/default_data_test.rb test/unit/enabled_module_test.rb test/unit/enumeration_test.rb test/unit/group_test.rb test/unit/helpers/application_helper_test.rb test/unit/helpers/custom_fields_helper_test.rb test/unit/helpers/sort_helper_test.rb test/unit/helpers/timelog_helper_test.rb test/unit/issue_nested_set_test.rb test/unit/issue_priority_test.rb test/unit/journal_observer_test.rb test/unit/journal_test.rb test/unit/ldap_auth_source_test.rb test/unit/lib/open_project/database_test.rb test/unit/lib/redmine/access_control_test.rb test/unit/lib/redmine/ciphering_test.rb test/unit/lib/redmine/codeset_util_test.rb test/unit/lib/redmine/helpers/calendar_test.rb test/unit/lib/redmine/hook_test.rb test/unit/lib/redmine/i18n_test.rb test/unit/lib/redmine/menu_manager/mapper_test.rb test/unit/lib/redmine/menu_manager/menu_helper_test.rb test/unit/lib/redmine/menu_manager/menu_item_test.rb test/unit/lib/redmine/menu_manager_test.rb test/unit/lib/redmine/mime_type_test.rb test/unit/lib/redmine/notifiable_test.rb test/unit/lib/redmine/plugin_test.rb test/unit/lib/redmine/safe_attributes_test.rb test/unit/lib/redmine/scm/adapters/filesystem_adapter_test.rb test/unit/lib/redmine/scm/adapters/git_adapter_test.rb test/unit/lib/redmine/scm/adapters/subversion_adapter_test.rb test/unit/lib/redmine/unified_diff_test.rb test/unit/lib/redmine/wiki_formatting/macros_test.rb test/unit/lib/redmine/wiki_formatting/null_formatter_test.rb test/unit/lib/redmine/wiki_formatting/textile_formatter_test.rb test/unit/lib/redmine/wiki_formatting_test.rb test/unit/lib/redmine_test.rb test/unit/mail_handler_test.rb test/unit/member_test.rb test/unit/message_test.rb test/unit/principal_test.rb test/unit/project_nested_set_test.rb test/unit/project_test.rb test/unit/query_test.rb test/unit/relation_test.rb test/unit/repository_filesystem_test.rb test/unit/repository_git_test.rb test/unit/repository_subversion_test.rb test/unit/repository_test.rb test/unit/role_test.rb test/unit/search_test.rb test/unit/status_test.rb test/unit/time_entry_activity_test.rb test/unit/time_entry_test.rb test/unit/token_test.rb test/unit/type_test.rb test/unit/user_preference_test.rb test/unit/user_test.rb test/unit/version_test.rb test/unit/watcher_test.rb test/unit/wiki_content_test.rb test/unit/wiki_page_test.rb test/unit/wiki_redirect_test.rb test/unit/wiki_test.rb * 180 conversions from: it { should ... } to: it { is_expected.to ... } * 12 conversions from: obj.stub(:message) to: allow(obj).to receive(:message) * 4 conversions from: Klass.any_instance.should_receive(:message) to: expect_any_instance_of(Klass).to receive(:message) * 4 conversions from: obj.should_receive(:message) to: expect(obj).to receive(:message) * 2 conversions from: Klass.any_instance.stub(:message) to: allow_any_instance_of(Klass).to receive(:message) For more details: https://github.com/yujinakayama/transpec#supported-conversions
10 years ago
it { is_expected.to have_one :api_token }
context 'User#find_by_api_key' do
it 'should return nil if no matching key is found' do
assert_nil User.find_by_api_key('zzzzzzzzz')
end
it 'should return nil if the key is found for an inactive user' do
user = FactoryBot.create(:user, status: User.statuses[:locked])
token = FactoryBot.build(:api_token, user: user)
user.api_token = token
user.save
assert_nil User.find_by_api_key(token.value)
end
it 'should return the user if the key is found for an active user' do
user = FactoryBot.create(:user, status: User.statuses[:active])
token = FactoryBot.build(:api_token, user: user)
user.api_token = token
user.save
assert_equal user, User.find_by_api_key(token.plain_value)
end
end
it 'should roles 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 'should projects by role for user with role' do
user = User.find(2)
assert_kind_of Hash, user.projects_by_role
assert_equal 2, user.projects_by_role.size
assert_equal [1, 5], user.projects_by_role[Role.find(1)].map(&:id).sort
assert_equal [2], user.projects_by_role[Role.find(2)].map(&:id).sort
end
it 'should projects by role for user with no role' do
user = FactoryBot.create(:user)
assert_equal({}, user.projects_by_role)
end
it 'should projects by role for anonymous' do
assert_equal({}, User.anonymous.projects_by_role)
end
it 'should valid notification options' do
# without memberships
assert_equal 5, User.find(7).valid_notification_options.size
# with memberships
assert_equal 6, User.find(2).valid_notification_options.size
end
it 'should valid notification options class method' do
assert_equal 5, User.valid_notification_options.size
assert_equal 5, User.valid_notification_options(User.find(7)).size
assert_equal 6, User.valid_notification_options(User.find(2)).size
end
it 'should mail notification all' do
@jsmith.mail_notification = 'all'
@jsmith.notified_project_ids = []
@jsmith.save
@jsmith.reload
assert @jsmith.projects.first.recipients.include?(@jsmith)
end
it 'should mail notification selected' do
@jsmith.mail_notification = 'selected'
@jsmith.notified_project_ids = [1]
@jsmith.save
@jsmith.reload
assert Project.find(1).recipients.include?(@jsmith)
end
it 'should mail notification only my events' do
@jsmith.mail_notification = 'only_my_events'
@jsmith.notified_project_ids = []
@jsmith.save
@jsmith.reload
assert !@jsmith.projects.first.recipients.include?(@jsmith)
end
it 'should comments sorting preference' do
assert !@jsmith.wants_comments_in_reverse_order?
@jsmith.pref.comments_sorting = 'asc'
assert !@jsmith.wants_comments_in_reverse_order?
@jsmith.pref.comments_sorting = 'desc'
assert @jsmith.wants_comments_in_reverse_order?
end
it 'should find by mail should be case insensitive' do
u = User.find_by_mail('JSmith@somenet.foo')
refute_nil u
assert_equal 'jsmith@somenet.foo', u.mail
end
end